NodeMCU Config: Difference between revisions
Content added Content deleted
(→Basics) |
|||
Line 108: | Line 108: | ||
# put the device to sleep |
# put the device to sleep |
||
machine.deepsleep() |
machine.deepsleep() |
||
</syntaxhighlight> |
|||
== Code Snipets == |
|||
===Playing with GPIO=== |
|||
<syntaxhighlight lang="python"> |
|||
import machine |
|||
import time |
|||
import urandom |
|||
pin = machine.Pin(2, machine.Pin.OUT) |
|||
def toggle(p): |
|||
p.value(not p.value()) |
|||
while True: |
|||
time.sleep_ms(urandom.getrandbits(8)) |
|||
toggle(pin) |
|||
</syntaxhighlight> |
|||
===Fading an LED=== |
|||
<syntaxhighlight lang="python"> |
|||
import time, math |
|||
import machine |
|||
led = machine.PWM(machine.Pin(2), freq=1000) |
|||
def pulse(l, t): |
|||
for i in range(20): |
|||
l.duty(int(math.sin(i / 10 * math.pi) * 500 + 500)) |
|||
time.sleep_ms(t) |
|||
while True: |
|||
pulse(led, 20) |
|||
</syntaxhighlight> |
|||
=== Control a hobby servo === |
|||
Hobby servo motors can be controlled using PWM. |
|||
They require a frequency of 50Hz and a duty between about 40 and 115, with 77 being the center value. |
|||
*Manual Movements |
|||
<syntaxhighlight lang="python"> |
|||
servo = machine.PWM(machine.Pin(12), freq=50) |
|||
servo.duty(40) |
|||
servo.duty(115) |
|||
servo.duty(77) |
|||
</syntaxhighlight> |
|||
*Random movements: |
|||
<syntaxhighlight lang="python"> |
|||
servo = machine.PWM(machine.Pin(12), freq=50) |
|||
while True: |
|||
servo.duty(urandom.getrandbits(8)) |
|||
time.sleep(1) |
|||
</syntaxhighlight> |
|||
=== One Wire DS18B20 Temp Sensor === |
|||
<syntaxhighlight lang="python"> |
|||
import time |
|||
import machine |
|||
import onewire, ds18x20 |
|||
# the device is on GPIO12 |
|||
dat = machine.Pin(12) |
|||
# create the onewire object |
|||
ds = ds18x20.DS18X20(onewire.OneWire(dat)) |
|||
# scan for devices on the bus |
|||
roms = ds.scan() |
|||
print('found devices:', roms) |
|||
# loop 10 times and print all temperatures |
|||
for i in range(10): |
|||
print('temperatures:', end=' ') |
|||
ds.convert_temp() |
|||
time.sleep_ms(750) |
|||
for rom in roms: |
|||
print(ds.read_temp(rom), end=' ') |
|||
print() |
|||
</syntaxhighlight> |
</syntaxhighlight> |
||
Revision as of 13:06, 26 January 2018
Micropython
Installation
- Flashing Micropython
Download firmware file from micropython.org
sudo pip install esptool esptool.py --port /dev/ttyUSB0 erase_flash esptool.py --port /dev/ttyUSB0 --baud 460800 write_flash --flash_size=detect 0 ~/Desktop/esp8266-20171101-v1.9.3.bin
- Check the firmware integrity:
import esp esp.check_fw()
Connecting
- Via Terminal:
sudo apt install picocom picocom /dev/ttyUSB0 -b115200
- Via REPL(web):
import webrepl_setup
- If you disabled automatic start-up on boot, you may run configured daemon on demand using:
import webrepl webrepl.start()
- Upload data using Serial Port
sudo pip install adafruit-ampy ampy --port /dev/ttyUSB0 put ~/Desktop/main.py
File System
- Checking filesystem:
import os os.listdir()
- Create directories:
os.mkdir('dir')
- Remove Files:
os.remove('data.txt')
- Reading data:
f = open('data.txt') f.read() f.close()
- Writing Data to files:
f = open('data.txt', 'w') f.write('some data') f.close()
- Checking Machine Frequency & Overclocking:
import machine machine.freq() # get the current frequency of the CPU machine.freq(160000000) # set the CPU frequency to 160 MHz
- Check AP Name:
import network ap = network.WLAN(network.AP_IF) print(ap.config('essid'))
- Change AP name and password:
import network ap = network.WLAN(network.AP_IF) ap.active(True) ap.config(essid='MyESP8266', authmode=network.AUTH_WPA_WPA2_PSK, password='mypassword') print(ap.config('essid'))
- Turning off AP:
ap = network.WLAN(network.AP_IF) ap.active(True)
Boot Process
As a final step of boot procedure, main.py is executed from filesystem This file is a hook to start up a user application each time on boot (instead of going to REPL). For small test applications, you may name them directly as main.py but instead it’s recommended to keep your application(s) in separate files, and have just the following in main.py:
import my_app my_app.main()
Deep-sleep mode
- This shut down the ESP8266 and all its peripherals & also WiFi.
- But not including the real-time-clock, which is used to wake the chip.
- This drastically reduces current consumption and is a good way to make devices that can run for a while on a battery.
- You Must connect GPIO16 to the reset pin.
import machine
# configure RTC.ALARM0 to be able to wake the device
rtc = machine.RTC()
rtc.irq(trigger=rtc.ALARM0, wake=machine.DEEPSLEEP)
# check if the device woke from a deep sleep
if machine.reset_cause() == machine.DEEPSLEEP_RESET:
print('woke from a deep sleep')
# set RTC.ALARM0 to fire after 10 seconds (waking the device)
rtc.alarm(rtc.ALARM0, 10000)
# put the device to sleep
machine.deepsleep()
- References
{{#widget:DISQUS
|id=networkm
|uniqid=NodeMCU Config
|url=https://aman.awiki.org/wiki/NodeMCU_Config
}}