Skip to content

Instantly share code, notes, and snippets.

@kamikat
Created December 22, 2016 06:32
Show Gist options
  • Save kamikat/105cfa1df6d7366ffe4ffbf26c16bd7c to your computer and use it in GitHub Desktop.
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.
# 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))
@kamikat
Copy link
Author

kamikat commented Dec 22, 2016

Installation

Upload prelude.py. You may use WebREPL or through Serial REPL (on OS X):

echo 'print repr(open("prelude.py","r").read())' | python | pbcopy

copies prelude.py to clipboard, and in serial REPL (screen /dev/tty.SLAB_USBtoUART 115200 for example)

f = open('prelude.py','w')
f.write(<PASTE>)
f.close()

Then, edit main.py (upload from WebREPL or through serial REPL the way above)

from prelude import *

Reset the machine from REPL (or a hardware reset):

import machine
machine.reset()

Connect to REPL after reset and you can use these functions.

TIPS

  • Remember to call ifinit() before any if call.
  • You may use tee('main.py', "...") to write main.py and any other files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment