Skip to content

Instantly share code, notes, and snippets.

@B45i
Last active March 12, 2020 02:43
Show Gist options
  • Save B45i/b74dca8382bb22fb1f2dbf5fd9e4f097 to your computer and use it in GitHub Desktop.
Save B45i/b74dca8382bb22fb1f2dbf5fd9e4f097 to your computer and use it in GitHub Desktop.

pip install esptool

esptool.py --port /dev/ttyUSB0 erase_flash esptool.py --port /dev/ttyUSB0 --baud 460800 write_flash --flash_size=detect 0 esp8266-20190125-v1.10.bin

esptool.py --chip esp32 --port /dev/ttyUSB0 erase_flash esptool.py --chip esp32 --port /dev/ttyUSB0 --baud 460800 write_flash -z 0x1000 esp32-20190125-v1.10.bin

import esp
esp.check_fw()
import esp
esp.osdebug(None)

picocom /dev/ttyUSB0 -b115200

import machine
pin = machine.Pin(2, machine.Pin.OUT)
pin.on()
pin.off()
import time
while True:
	p.value(not p.value())
	time.sleep_ms(500)
  • Pressing ctrl-E will enter a special paste mod
  • Ctrl-A on a blank line will enter raw REPL mode. This is like a permanent paste mode, except that characters are not echoed back.
  • Ctrl-B on a blank like goes to normal REPL mode.
  • Ctrl-C cancels any input, or interrupts the currently running code.
  • Ctrl-D on a blank line will do a soft reset.
  • Note that ctrl-A and ctrl-D do not work with WebREPL.
f = open('data.txt', 'w')
f.write('some data')
f.close()

f = open('data.txt')
f.read()
'some data'
f.close()
import os
os.listdir()
os.mkdir('dir')
os.remove('data.txt')
import network
sta_if = network.WLAN(network.STA_IF)
ap_if = network.WLAN(network.AP_IF)

sta_if.active()
False
ap_if.active()
True

ap_if.ifconfig()

def connect_to_wifi(ssid, password):
    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(ssid, password)
        while not sta_if.isconnected():
            pass
    print('Connected to %s' % ssid)
    print('network config:', sta_if.ifconfig())
import machine
pins = [machine.Pin(i, machine.Pin.IN) for i in (0, 2, 4, 5, 12, 13, 14, 15)]

html = """<!DOCTYPE html>
<html>
    <head> <title>ESP8266 Pins</title> </head>
    <body> <h1>ESP8266 Pins</h1>
        <table border="1"> <tr><th>Pin</th><th>Value</th></tr> %s </table>
    </body>
</html>
"""

import socket
addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]

s = socket.socket()
s.bind(addr)
s.listen(1)

print('listening on', addr)

while True:
    cl, addr = s.accept()
    print('client connected from', addr)
    cl_file = cl.makefile('rwb', 0)
    while True:
        line = cl_file.readline()
        if not line or line == b'\r\n':
            break
    rows = ['<tr><td>%s</td><td>%d</td></tr>' % (str(p), p.value()) for p in pins]
    response = html % '\n'.join(rows)
    cl.send(response)
    cl.close()
pin.value(0)
pin.value(1)

Or:

pin.off()
pin.on()
import machine, neopixel
np = neopixel.NeoPixel(machine.Pin(4), 8)
np[0] = (255, 0, 0) # set to red, full brightness
np[1] = (0, 128, 0) # set to green, half brightness
np[2] = (0, 0, 64)  # set to blue, quarter brightness
np.write()
import time
def demo(np):
    n = np.n

    # cycle
    for i in range(4 * n):
        for j in range(n):
            np[j] = (0, 0, 0)
        np[i % n] = (255, 255, 255)
        np.write()
        time.sleep_ms(25)

    # bounce
    for i in range(4 * n):
        for j in range(n):
            np[j] = (0, 0, 128)
        if (i // n) % 2 == 0:
            np[i % n] = (0, 0, 0)
        else:
            np[n - 1 - (i % n)] = (0, 0, 0)
        np.write()
        time.sleep_ms(60)

    # fade in/out
    for i in range(0, 4 * 256, 8):
        for j in range(n):
            if (i // 256) % 2 == 0:
                val = i & 0xff
            else:
                val = 255 - (i & 0xff)
            np[j] = (val, 0, 0)
        np.write()

    # clear
    for i in range(n):
        np[i] = (0, 0, 0)
    np.write()

demo(np)
import network
import urequests
import time

URL = 'http://newsapi.org/v2/top-headlines?sources=the-verge&apiKey=374fb9f276c24901a6c74c82f7bc4d68' 

def connect_to_wifi(ssid, password):
    sta_if = network.WLAN(network.STA_IF)
    if not sta_if.isconnected():
        print('connecting to network...')
        sta_if.active(True)
        sta_if.connect(ssid, password)
        while not sta_if.isconnected():
            pass
    print('Connected to %s' % ssid)
    print('network config:', sta_if.ifconfig())
    

def get_news():
  print('Refreshing.....')
  result = urequests.get(URL)
  if(result.status_code == 200):
    for x in result.json()['articles']:
      print(x['title'])
  time.sleep_ms(1000)

connect_to_wifi('EvilCorp', 'helloworld')

while True:
  get_news()
import machine, ssd1306
i2c = machine.I2C(scl=machine.Pin(4), sda=machine.Pin(5))
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
oled.fill(0) 
oled.text('MicroPython on', 0, 0)
oled.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment