Created
December 22, 2016 06:32
-
-
Save kamikat/105cfa1df6d7366ffe4ffbf26c16bd7c to your computer and use it in GitHub Desktop.
A prelude script to easy access filesystem & network functionalities for MicroPython on ESP8266.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# utility | |
logfile = None | |
def debug(*objects): | |
print(*objects) | |
if logfile: | |
print(*objects, file=logfile, flush=True) | |
def format_hex(bs, delim=''): | |
return delim.join(map(lambda x: ('0' + hex(x)[2:])[-2:], bs)) | |
def sha1(s): | |
import uhashlib as hashlib | |
hasher = hashlib.sha1() | |
hasher.update(s) | |
return format_hex(hasher.digest()) | |
def sha256(s): | |
import uhashlib as hashlib | |
hasher = hashlib.sha256() | |
hasher.update(s) | |
return format_hex(hasher.digest()) | |
# filesystem | |
def cat(path): | |
f = open(path, 'r'); | |
print(f.read()); | |
f.close() | |
def ls(*objects): | |
import os | |
return os.listdir(*objects) | |
def rm(*objects): | |
import os | |
for filename in objects: | |
try: | |
os.remove(filename) | |
except: | |
debug('failed to remove', filename) | |
def tee(path, data): | |
f = open(path,'wb') | |
l = f.write(data) | |
f.close() | |
print(data) | |
return l | |
def cd(*objects): | |
import os | |
return os.chdir(*objects) | |
def pwd(): | |
import os | |
return os.getcwd() | |
def mkdir(path): | |
import os | |
os.mkdir(path) | |
# network | |
ap_if = None | |
sta_if = None | |
def ifinit(): | |
import network as net | |
global ap_if, sta_if | |
ap_if = net.WLAN(net.AP_IF) | |
sta_if = net.WLAN(net.STA_IF) | |
ap_if.active(False) | |
sta_if.active(True) | |
def ifconnect(essid, passwd, timeout=5000): | |
if not sta_if.isconnected(): | |
import utime | |
debug('connecting to network...') | |
sta_if.active(True) | |
sta_if.connect(essid, passwd) | |
guard = utime.ticks_ms() | |
while not sta_if.isconnected(): | |
if utime.ticks_ms() - guard > timeout: | |
debug('unable to connect', essid, 'in', timeout, 'ms') | |
return False | |
debug('network config:', sta_if.ifconfig()) | |
return True | |
def ifscan(): | |
debug('scanning...') | |
for ap in sta_if.scan(): | |
debug('BSSID:', format_hex(ap[1], ':'), 'Channel:', ap[2], '\tRSSI:', str(ap[3]) + 'dBm', '\tAuthMode:', ap[4],'\tSSID:', ap[0].decode()) | |
def ifconfig(*objects): | |
debug(sta_if.ifconfig(*objects)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Installation
Upload
prelude.py
. You may use WebREPL or through Serial REPL (on OS X):copies
prelude.py
to clipboard, and in serial REPL (screen /dev/tty.SLAB_USBtoUART 115200
for example)Then, edit
main.py
(upload from WebREPL or through serial REPL the way above)Reset the machine from REPL (or a hardware reset):
Connect to REPL after reset and you can use these functions.
TIPS
ifinit()
before any if call.tee('main.py', "...")
to writemain.py
and any other files.