alankrantas / esp8266-micropython-cookbook

Simple and useful MicroPython examples on ESP8266/ESP32/Pico W

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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.

Most of the code works for ESp8266, ESP32 and Raspberry Pi Pico W.

See ESP8266 Pinout Reference for the actual GPIO number for each pins. All ESP8266s (for example, NodeMCU and WeMos D1/D1 mini as well as their clones) are functionally identical. Most of them use either CP2012 or CH340 USB chip which requires driver on Windows.

Hello World (Blinky)

Controlling the on-board LED on pin D4 (GPIO 2) or an external LED:

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()))

The onboaard LED in most ESP8266s are reversed so it will light up at low voltage instead.

Pull-Up Button

Reading a button connecting to pin D1 (GPIO 5) and GND, no resistor required:

from machine import Pin, Signal
import utime

btn = Signal(Pin(5, Pin.IN, Pin.PULL_UP), invert=True)

while True:
    print(f'Button pressed: {'yes' if btn.value() else 'no'}')
    utime.sleep_ms(100)

MicroPython now supports f-string but the examples below still use format for backward compatibility.


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 will return a HTML webpage to your 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.

File: Simple_WebServer_AP.py

This is the AP mode version, which will start its own WiFi access point (works without externam WiFi). Connect the AP (default named "ESP8266" with password 12345678, can be changed in the code) from your smartphone and open http://192.168.4.1 in your phone browser.

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, basically a wrapper class with rainbow/rotate methods, slice assignment and brightness control.

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.

About

Simple and useful MicroPython examples on ESP8266/ESP32/Pico W

License:MIT License


Languages

Language:Python 100.0%