Micropython Tutorial for Wemos D1 Mini
All examples are for Wemos D1 Mini, WS2812B RGB Shield (NeoPixel) and DHT Pro Shield.
Student prerequisities
- Python knowledge
- Micro USB cable
- Python 3 installed
- If you have Python 2 installed you can get into problems mixing Python versions. You must use Python 3. Check what is default version of Python on your system.
- Python IDE installed
- Git client (optional)
Lessons
Computer setup
General
- Internet connection
- Install serial terminal
- Windows: Putty
- Linux, Mac: Use screen or minicom tool
- Install ampy, install instruction
- Add ampy to PATH
- Install drivers for CH340 (Mac, Windows < 8.1)
Linux
- add your user to dialout user group ( usually
sudo usermod -a -G dialout yourUsername) - log out/log in to make the change applied to your user
- to make sure that you'll be using python3 environment for workshop, clone the repository and create a python3 virtualenv
- Clone the repo:
git clone https://github.com/bechynsky/Micropython.git - Go to directory:
cd Micropython - Create python3 virtualenv:
python3 -m venv <absolutePath>/Micropython/venv - Activate the virtualenv:
source ./venv/bin/activate - Update pip and setuptools:
pip install pip setuptools --upgrade - Install python tooling for workshop:
pip install esptool adafruit-ampy
- Clone the repo:
Firmware update
First you need to upload Micropython to ESP8266 chip.
Windows: NodeMCU Flasher
esptool.py
Instalation (if it's not already installed)
Example how to use it
It is important to erase flash first.
Windows
esptool.py.exe --port COM7 erase_flash
esptool.py.exe --port COM7 write_flash -fm dio 0x000000 esp8266-20170823-v1.9.2.bin
Linux
esptool.py -p /dev/ttyUSB0 erase_flash
esptool.py -p /dev/ttyUSB0 write_flash -fm dio 0x000000 <downloaded ESP8266 bin file>
First script
Interactive prompt using serial terminal
Connect device using USB cable to your computer. Device will present as serial port. On Windows it will be COMx. On Linux and Mac it will be /dev/ttyUSBx. x is number.
Open your serial communication appliction and connect to right serial port using baudrate (speed) 115200. After you connect you will probably need to press enter to see promt >>>. No it works es Python interactive console on standard computer.
Linux - use for example minicom -s and set the correct USB serial port path, then exit (not exit the minicom)
Control LED on ESP8266 chip. LED is connected on pin 2 to VCC. You control ground (0 - on, 1 - off). LED is located close antena.
import machine
pin = machine.Pin(2, machine.Pin.OUT)
pin.value(0)
pin.value(1)
Upload script using ampy
Scrip name: 101.py
Close interactive prompt.
Example for Windows. Use your serial port number.
ampy --port COM14 run 101.py
Example for Linux and Mac. Do not forget to use full path to your serial port.
ampy --port /dev/ttyUSB0 run 101.py
File system
Startup scripts
- boot.py
- main.py - runs automatically after start
ampy --port COM4 put main.py
ampy --port /dev/ttyUSB0 put main.py
DHT22 (DHT11) and RGB LED
Using hardware drivers
How it works with pins
Read temperature and humidity dht.py
What is it Neopixel LED and how it works
Control Neopixel RGB LED neopixel.py
Create simple thermometer using RGB LED and DHT11 - temperature is showed using colors (for example red hot, blue cold)
HTTP protocol
How it works
GET vs POST
HTTP Headers
SSL
Internet
Explain Wi-Fi modes - access point vs. client
Connect to local Wi-Fi wifi_connect.py
Download website http_test.py Connect to Wi-Fi first!
ThingSpeak
Create ThingSpeak Account
Create Channel
Send data from DHT11 to channel
Server
How to use Access point mode
Run this code to find mac address of your board.
import network
wlan = network.WLAN(network.STA_IF) # create station interface
wlan.active(True) # activate the interface
print(":".join(map(lambda x: "%02x" % x, wlan.config('mac'))))
Open list of available Wi-Fi and connect to Wi-Fi named MicroPython-xxxxxx where xxxxxx is part of MAC address. Password is micropythoN. IP addres of board is 192.168.4.1.
Create webserver
Important: There is bug in ampy tool. It is recommended to upload code as main.py using put command instaed of to try run it using run command. Do not forget restart ESP8266.
Create web server showing current temperature weather_station.py.
Crete web server controlling on board LED server_led_control.py.