{"id":19601629,"url":"https://github.com/alankrantas/esp8266-micropython-cookbook","last_synced_at":"2025-04-27T17:32:01.663Z","repository":{"id":113012342,"uuid":"248904959","full_name":"alankrantas/esp8266-micropython-cookbook","owner":"alankrantas","description":"Simple and useful MicroPython examples on ESP8266/ESP32/Pico W","archived":false,"fork":false,"pushed_at":"2023-10-12T07:44:30.000Z","size":109,"stargazers_count":13,"open_issues_count":0,"forks_count":8,"subscribers_count":5,"default_branch":"master","last_synced_at":"2023-10-13T00:04:55.710Z","etag":null,"topics":["esp32","esp8266","iot","micropython","micropython-esp32","micropython-esp8266","raspberrypipicow"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/alankrantas.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2020-03-21T04:15:09.000Z","updated_at":"2023-07-11T19:13:45.000Z","dependencies_parsed_at":"2023-10-12T17:21:16.754Z","dependency_job_id":null,"html_url":"https://github.com/alankrantas/esp8266-micropython-cookbook","commit_stats":null,"previous_names":[],"tags_count":0,"template":null,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alankrantas%2Fesp8266-micropython-cookbook","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alankrantas%2Fesp8266-micropython-cookbook/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alankrantas%2Fesp8266-micropython-cookbook/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alankrantas%2Fesp8266-micropython-cookbook/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alankrantas","download_url":"https://codeload.github.com/alankrantas/esp8266-micropython-cookbook/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224076433,"owners_count":17251756,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["esp32","esp8266","iot","micropython","micropython-esp32","micropython-esp8266","raspberrypipicow"],"created_at":"2024-11-11T09:19:15.098Z","updated_at":"2024-11-11T09:19:16.425Z","avatar_url":"https://github.com/alankrantas.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ESP8266 MicroPython Cookbook\n\n![Micropython-logo svg](https://user-images.githubusercontent.com/44191076/79063718-e5975580-7cd5-11ea-90a2-6f350adfb0cd.png)\n\nI publish ESP8266/ESP32 MicroPyton projects from time to time on [Hackster.io](https://www.hackster.io/alankrantas). 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.\n\nMost of the code works for ESp8266, ESP32 and Raspberry Pi Pico W.\n\nSee [ESP8266 Pinout Reference](https://randomnerdtutorials.com/esp8266-pinout-reference-gpios/) 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.\n\n## Hello World (Blinky)\n\nControlling the on-board LED on pin D4 (GPIO 2) or an external LED:\n\n```python\nfrom machine import Pin\nimport utime\n\nled = Pin(2, Pin.OUT)\n\nwhile True:\n    led.value(not led.value())\n    utime.sleep_ms(500)\n```\n\nor\n\n```python\nfrom machine import Pin, Timer\n\nled = Pin(2, Pin.OUT)\n\ntimer = Timer(-1)\ntimer.init(mode=Timer.PERIODIC, period=500,\n           callback=lambda _: led.value(not led.value()))\n```\n\n\u003e The onboaard LED in most ESP8266s are reversed so it will light up at low voltage instead.\n\n## Pull-Up Button\n\nReading a button connecting to pin D1 (GPIO 5) and GND, no resistor required:\n\n```python\nfrom machine import Pin, Signal\nimport utime\n\nbtn = Signal(Pin(5, Pin.IN, Pin.PULL_UP), invert=True)\n\nwhile True:\n    print(f'Button pressed: {'yes' if btn.value() else 'no'}')\n    utime.sleep_ms(100)\n```\n\n\u003e MicroPython now supports f-string but the examples below still use ```format``` for backward compatibility. \n\n---\n\n## Simple Timer-Based Simple Web Clock on SSD1306\n\n\u003ci\u003eFile: [SimpleWebClockWithTimer.py](https://github.com/alankrantas/esp8266-micropython-cookbook/blob/master/SimpleWebClockWithTimer.py)\u003c/i\u003e\n\nA simple web clock that update system RTC time every 15 minutes via NTP server. It uses two \u003cb\u003emachine.timer\u003c/b\u003es 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.\n\nThe \u003ci\u003e[SimpleWebClockWithTimerUsingWebAPI.py](https://github.com/alankrantas/esp8266-micropython-cookbook/blob/master/SimpleWebClockWithTimerUsingWebAPI.py)\u003c/i\u003e version works the same but use [World Time API](http://worldtimeapi.org/) 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.\n\n## Display DHT11 Sensor Readings on SSD1306\n\n\u003ci\u003eFile: [DHT11_Sensor_SSD1306.py](https://github.com/alankrantas/esp8266-micropython-cookbook/blob/master/DHT11_Sensor_SSD1306.py)\u003c/i\u003e\n\nA 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.\n\nChange dht.DHT11 to dht.DHT22 if you are using a DHT22 sensor.\n\n## Simple Web Server\n\n\u003ci\u003eFile: [Simple_WebServer.py](https://github.com/alankrantas/esp8266-micropython-cookbook/blob/master/Simple_WebServer.py)\u003c/i\u003e\n\nA 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.\n\n\u003ci\u003eFile: [Simple_WebServer_AP.py](https://github.com/alankrantas/esp8266-micropython-cookbook/blob/master/Simple_WebServer_AP.py)\u003c/i\u003e\n\nThis 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.\n\n## Web JSON Query Template\n\n\u003ci\u003eFile: [WebJSONQuery_Template.py](https://github.com/alankrantas/esp8266-micropython-cookbook/blob/master/WebJSONQuery_Template.py)\u003c/i\u003e\n\nA 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.\n\nNote: 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.\n\n## Deep Sleep/Cloud Data Update\n\n\u003ci\u003eFile: [DeepSleep_Cloud_Update.py](https://github.com/alankrantas/esp8266-micropython-cookbook/blob/master/DeepSleep_Cloud_Update.py)\u003c/i\u003e\n\nThis 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).\n\nThe 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. \n\n## WS2812 NeoPixel Rainbow/Rotation Effect\n\n\u003ci\u003eFile: [WS2812_NeoPixelRainbow.py](https://github.com/alankrantas/esp8266-micropython-cookbook/blob/master/WS2812_NeoPixelRainbow.py)\u003c/i\u003e\n\nBased on Adafruit's NeoPixel example code, basically a wrapper class with rainbow/rotate methods, slice assignment and brightness control.\n\n## Conway's Game of Life on SSD1306\n\n\u003ci\u003eFile: [ConwayGameOfLife_SSD1306.py](https://github.com/alankrantas/esp8266-micropython-cookbook/blob/master/ConwayGameOfLife_SSD1306.py)\u003c/i\u003e\n\nRun the simulation of [Conway's Game of Life](https://en.wikipedia.org/wiki/Conway%27s_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.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falankrantas%2Fesp8266-micropython-cookbook","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falankrantas%2Fesp8266-micropython-cookbook","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falankrantas%2Fesp8266-micropython-cookbook/lists"}