NodeMCU Config: Difference between revisions
Content added Content deleted
Line 170: | Line 170: | ||
<syntaxhighlight lang="python"> |
<syntaxhighlight lang="python"> |
||
# This file is executed on every boot (including wake-boot from deepsleep) |
# This file is executed on every boot (including wake-boot from deepsleep) |
||
import esp |
|||
import uos |
import uos |
||
import machine |
import machine |
||
Line 178: | Line 178: | ||
import ntptime |
import ntptime |
||
from machine import RTC |
from machine import RTC |
||
import micropython |
|||
#esp.osdebug(None) |
#esp.osdebug(None) |
||
Line 186: | Line 187: | ||
print("Freq: ", machine.freq()) |
print("Freq: ", machine.freq()) |
||
print("ESP Flash size: ", esp.flash_size()) |
|||
print("ESP Free Mem: ", esp.freemem()) |
|||
#print("GC Mem: ", gc.mem_free()) |
|||
print("Micropython Info: ") |
|||
micropython.mem_info() |
|||
# Network Config |
# Network Config |
||
Line 199: | Line 203: | ||
while not sta_if.isconnected(): |
while not sta_if.isconnected(): |
||
pass |
pass |
||
print(' |
print('STA Config: ', sta_if.ifconfig()) |
||
# Turn off Wifi Access Point |
|||
ap = network.WLAN(network.AP_IF) |
|||
ap.active(False) |
|||
print('AP Config: ', ap.ifconfig()) |
|||
# synchronize with ntp need to be connected to wifi |
# synchronize with ntp need to be connected to wifi |
||
⚫ | |||
try: |
try: |
||
⚫ | |||
ntptime.settime() # set the rtc datetime from the remote server |
ntptime.settime() # set the rtc datetime from the remote server |
||
print("Time: ", rtc.datetime()) # get the date and time in UTC |
print("Time: ", rtc.datetime()) # get the date and time in UTC |
||
except OSError: |
except OSError: |
||
print("Time: Unable to get data from Server") |
print("Time: Unable to get data from Server") |
||
# Deep Sleep |
|||
def deep_sleep(delta): |
|||
rtc.irq(trigger=rtc.ALARM0, wake=machine.DEEPSLEEP) |
|||
if machine.reset_cause() == machine.DEEPSLEEP_RESET: |
|||
print('woke from a deep sleep') |
|||
rtc.alarm(rtc.ALARM0, delta*1000*60) |
|||
machine.deepsleep() |
|||
</syntaxhighlight> |
</syntaxhighlight> |
||
Revision as of 18:25, 21 March 2020
Micropython
- Documentation:
http://docs.micropython.org/en/latest/esp8266/quickref.html
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
Exiting from picocom:
Cntrl + A Cntrl + X
- 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()
Information
- 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
- Diagnostics
import port_diag
Networking
AP Mode
- 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(False)
Station Mode
Connecting to WIFI AP:
sta_if = network.WLAN(network.STA_IF) sta_if.active(True) sta_if.connect('<your ESSID>', '<your password>')
To check if the connection is established:
sta_if.isconnected()
Once established you can check the IP address:
sta_if.ifconfig()
You can then disable the access-point interface if you no longer need it:
ap_if.active(False)
Disconnecting from Station:
sta_if = network.WLAN(network.STA_IF) sta_if.active(False)
Here is a function you can run (or put in your boot.py file) to automatically connect to your WiFi network:
def do_connect():
import network
sta_if = network.WLAN(network.STA_IF)
if not sta_if.isconnected():
print('connecting to network...')
sta_if.active(True)
sta_if.connect('<essid>', '<password>')
while not sta_if.isconnected():
pass
print('network config:', sta_if.ifconfig())
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()
boot.py File
# This file is executed on every boot (including wake-boot from deepsleep)
import esp
import uos
import machine
import gc
import webrepl
import network
import ntptime
from machine import RTC
import micropython
#esp.osdebug(None)
#uos.dupterm(None, 1) # disable REPL on UART(0)
webrepl.start()
gc.collect()
print("Freq: ", machine.freq())
print("ESP Flash size: ", esp.flash_size())
print("ESP Free Mem: ", esp.freemem())
#print("GC Mem: ", gc.mem_free())
print("Micropython Info: ")
micropython.mem_info()
# Network Config
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True) # allow auto reconnect
if not sta_if.isconnected():
print('Connecting to WiFi...')
sta_if.active(True)
sta_if.connect('TP-Link_6A31', 'Kirat#291011')
while not sta_if.isconnected():
pass
print('STA Config: ', sta_if.ifconfig())
# Turn off Wifi Access Point
ap = network.WLAN(network.AP_IF)
ap.active(False)
print('AP Config: ', ap.ifconfig())
# synchronize with ntp need to be connected to wifi
rtc = RTC()
try:
ntptime.settime() # set the rtc datetime from the remote server
print("Time: ", rtc.datetime()) # get the date and time in UTC
except OSError:
print("Time: Unable to get data from Server")
# Deep Sleep
def deep_sleep(delta):
rtc.irq(trigger=rtc.ALARM0, wake=machine.DEEPSLEEP)
if machine.reset_cause() == machine.DEEPSLEEP_RESET:
print('woke from a deep sleep')
rtc.alarm(rtc.ALARM0, delta*1000*60)
machine.deepsleep()
- References
{{#widget:DISQUS
|id=networkm
|uniqid=NodeMCU Config
|url=https://aman.awiki.org/wiki/NodeMCU_Config
}}