Skip to content

Instantly share code, notes, and snippets.

@ryanwoodsmall
Last active February 8, 2021 19:53
Show Gist options
  • Save ryanwoodsmall/1ac1b21a8c270bb306308b9ae26627a7 to your computer and use it in GitHub Desktop.
Save ryanwoodsmall/1ac1b21a8c270bb306308b9ae26627a7 to your computer and use it in GitHub Desktop.
esp8266 esp32 micropython main.py wifi setup
#!/usr/bin/env python
#
# erase the esp8266 flash
# sudo $(which esptool.py) --port /dev/ttyUSB0 erase_flash
# write the micropython firmware
# sudo $(which esptool.py) --port /dev/ttyUSB0 --baud 460800 write_flash --flash_size=detect -fm dio 0 /tmp/esp8266-20190125-v1.10.bin
# save this file to esp8266_wifi.py
# add your wireless network/password to the wifi.connect() line
# run:
# echo "f=open('/main.py','w')"
# grep -v '^#' esp8266_wifi.py | sed 's/^/f.write("/g;s/$/\\n")/g'
# echo "f.close()"
# connect to the usb serial console with
# microcom -s 115200 /dev/ttyUSB0
# put the esp8266 micropython repl into "raw" mode with CTRL-E
# copy the output of the echo/grep/sed/echo stuff and finish with CTRL-D
# reset the esp8266 with
# import machine
# machine.reset()
# verify you're connected with
# wifi.isconnected()
# wifi.ifconfig()
#
# to split/join last mac in a dumb way:
# import ure
# mac = ubinascii.hexlify(mac,':').decode()
# mac = ure.compile(':').split(mac)[3:6]
# mac = ':'.join(ure.compile(':').split(mac)[3:6])
#
# substitute ap name/pass
# put esp8266/esp32 into paste mode with CTRL-E
# paste
# CTRL-D
# reset the board
#
f=open('/main.py','w')
f.write("""
import gc
import network
import ubinascii
import sys
mac = network.WLAN().config('mac')
mac = ubinascii.hexlify(mac).decode()
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
wifi.config(dhcp_hostname=sys.platform + '_' + mac)
wifi.scan()
wifi.connect('YourAPName', 'YourAPPassword')
wifi.isconnected()
wifi.ifconfig()
ap = network.WLAN(network.AP_IF)
ap.active(False)
del(mac)
del(ap)
gc.collect()
""")
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment