/esp8266-micropython-cookbook

Some simple and useful projects for MicroPython on ESP8266/ESP32

Primary LanguagePythonGNU General Public License v3.0GPL-3.0

ESP8266 MicroPython Cookbook

Micropython-logo svg

I publish ESP8266/ESP32 MicroPyton projects from time to time on Hackster.io. Here I have some smaller project/scripts I wrote forMicroPython, in which I tried to make the code as simple as possible and only use built-in modules.

-- Alan Wang

Hello World (Blinky)

from machine import Pin
import utime

led = Pin(2, Pin.OUT)

while True:
    led.value(not led.value())
    utime.sleep_ms(500)

or

from machine import Pin, Timer

led = Pin(2, Pin.OUT)

timer = Timer(-1)
timer.init(mode=Timer.PERIODIC, period=500,
           callback=lambda _: led.value(not led.value()))

Simple Timer-Based Simple Web Clock on SSD1306

File: SimpleWebClockWithTimer.py

A simple web clock that update system RTC time every 15 minutes via NTP server. It uses two machine.timers instead of a while loop, reducing the code down to less than 40 actual lines. Change the SSID and PW to your own WiFi AP.

The SimpleWebClockWithTimerUsingWebAPI.py version works the same but use World Time API to query time instead and update it via the machine.RTC module. Since the API can detect your timezone, you don't need to set it in this version.

Display DHT11 Sensor Readings on SSD1306

File: DHT11_Sensor_SSD1306.py

A simple example of displaying DHT11's temperature and humidity readings on SSD1306 OLED as well as printing them in REPL. A very basic weather station.

Change dht.DHT11 to dht.DHT22 if you are using a DHT22 sensor.

Simple Web Server

File: Simple_WebServer.py

A simple web server in STA mode (connect to your WiFi and you can access a webpage from a web browser). You'll have to connect the ESP8266 on your computer to read the actual IP it get. This example allows you to turn the onboard LED on or off.

Web JSON Query Template

File: WebJSONQuery_Template.py

A template for querying a API and get its JSON response. The JSON response would be a dictionary object, in which you can extract any data you need.

Note: if you get a SSL error (like "TLS buffer overflow" and/or "ssl_handshake_status: -xxx"), either your WiFi is unstable or the API is not fully supported by MicroPython.

Deep Sleep/Cloud Data Update

File: DeepSleep_Cloud_Update.py

This use deep sleep to make the board wake up every 30 seconds and upload readings of a DHT11 via IFTTT's Webhook service (in my case the data would be uploaded to a Google Drive spreadsheet).

The script must be uploaded onto the board in order to make deep sleep work. Connect D0 (GPIO 16) and RST before you powering it up. Afterwards the board's REPL may not be responsive and you'll have to re-flash the firmware.

WS2812 NeoPixel Rainbow/Rotation Effect

File: WS2812_NeoPixelRainbow.py

Based on Adafruit's NeoPixel example code, here I extended the original NeoPixel class to add some convenient methods.

Conway's Game of Life on SSD1306

File: ConwayGameOfLife_SSD1306.py

Run the simulation of Conway's Game of Life on the SSD1306 OLED display. The rules and the size of cell matrix can both be adjusted. Here I use a single-dimension list to simplify the code.