Slide 1: Setting up and IoT
Session 2: Micropython and
Hardware access
Uli Raich
Formally CERN, Geneva, Switzerland
Slide 2: The WeMos D1 mini CPU card
|
|
ESP8266 cost: 2.21 Euros |
ESP32 cost: 4.1o Euros |
Slide 3: CPU Pinout
|
|
ESP8266 |
ESP32 |
Please note: The pin numbers IOxx on the ESP32 correspond
to the GPIO pin numbers
The pins in the white fields go to the WeMos D1 mini bus
and are also available on the ESP8266
The other pins are only accessible on the ESP32 CPU card
The pin numbers Dx do not correspond to GPIO pin numbers!
Please refer to the next slide for correspondence
Slide 4: CPU pinouts for reference
Slide 5: Meaning of pins on ESP8266
- GPIO: General Purpose Input Output.
Drives a single digital line which can be programmed input or output - SCL/SDA: The I2C bus:a 2 wire bus
interfacing sensors or actuators to the CPU
- SCL,MISO,MOSI,SS: SPI
the Serial Peripheral Interface. Used fast communication with external device |
|
Slide 6: Meaning of pins on ESP32
Slide 7: How to program the processor
Slide 8: Flashing the code
Slide 9: esptool
- esptool is called from the Makefiles in ESP-IDF
- esptool is used when we upload code from the Arduino IDE to the processor flash
- esptool is used when the Micropython IDE uPyCraft
installs Micropython onto the processor flash
In the above cases the use of esptool is hidden to us.
We can however also execute esptool directly.
Slide 10: How to write a Micropython program?
First we need a Micropython interpreter!
You find the sources here:
https://github.com/micropython/micropython/
In the repository you find ports for the ESP8266 and the ESP32.
In order to compile the code you need the ESP-IDF and its cross compilers
The code compiles into a binary file (firmware-combined.bin) which contains a boot loader and the interpreter.
This binary must be uploaded and flashed.
For documentation of the ESP8266 port of Micropython look at
https://docs.micropython.org/en/latest/esp8266/tutorial/index.html
Slide 11: How to communicate with the Micropython interpreter?
We use a serial connection passing through the micro USB connection.
As soon as we connect the processor card to the PC we see the
UART bridge and a new device: dev/ttyUSB0 is created.
This device is used to communicate with the Micropython REPL.
You see the command prompt and you can interact with Micropython. But … how to upload scripts?
Slide 12: What is REPL?
Slide 13: The communication tools: minicom
You see the command prompt and you can interact with Micropython.
But … how to upload scripts?
Slide 14: The command line tool ampy
Slide 15: IDE for Micropython: uPyCraft
Slide 16: uPyCraft
uPyCraft is a rather complete Integrate Development Environment (IDE)
which lets you
- Access the REPL
- Create directories on the Micropython file system
- Upload scripts
- Syntax check scripts
- Run scripts
- Install Micropython on your processor board
Slide 17: Flashing Micropython
This has already been done for you! However, it is easy if you want to do it
at home with a new processor board.
Compiling a new version of Micropython is substantially harder but also perfectly possible.
Slide 18: uPyCraft(2)
uPyCraft is based on QT4 and is available for Linux, Windows and Mac.
It is written in PyQt4 the Python language binding to Qt4.
The Linux version did not work for me
I found a version based on
PyQt5 (new version of QT) which was even worse.
I tried to correct as much as I could to make the
PyQt5 version usable on Linux:
https://github.com/uraich/uPyCraft-Qt5
Slide 19: Thonny
Slide 20: Thonny (2)
Thonny is an IDE for Python which has provisions for Micropython.
Under Tools → Options button you can select the type of
Python interpreter you intend to use.
Slide 21: IoT Hello World program
A “Hello World” program, just printing “Hello World” on the screen
does not look very exciting.
However, this is generally used to verify that the infrastructure
Compiler, linker, downloader, flash program
are working correctly
In embedded systems printing can be quite complex
and a blinking LED is used instead.
Slide 22: Switching on and off a LED
The ESP8266 and the ESP32 have a “user LED” connected to GPIO 2.
How do we control this LED?
- Define that the LED is connected to GPIO 2
- Program this pin as output
- Write a logic 1 to the pin to switch it on
- Write a logic 0 to the pin to switch it off
- The logic state may be inverted if the LED is active low
Slide 23: Micropython hardware functions
Slide 24: The machine.Pin class
Slide 25: Switch the LED on, version 1
Slide 26: Switch the LED on, version 2
Slide 27: The blinking LED
Now we put the code into a script and run it
Slide 28: Changing the light intensity
The LED is connected to a digital line which can only be set to 0 or Vcc.
How can we change the light intensity and dim the LED?
The light intensity depends on the average current flowing through the LED.
The answer is PWM: pulse width modulation.
Slide 29: PWM in Micropython
Slide 30: Our PWM implementation
Slide 31: The WS2812B LED
A more complex LED:
- rgb LED used in LED chains.
- each ws18b12 contains the 3 colored LEDs and a controller.
- Can be cascaded and individually addressed, depending on its position in the chain
- Needs precise timing
- To use it we pass through the neopixel library built into MicroPython
Slide 32: WS2812B timing
For all the details on the ws2812b look at
https://cdn-shop.adafruit.com/datasheets/WS2812B.pdf
24 bit of colour data puts 2e24 colours at your disposal
Coding of a single bit
The control word:
Slide 33: Cascading the WS2812B
Slide 34: Using the neopixel library
Slide 35: … and our code
We have a single neopixel connected to
GPIO pin 4 (ESP8266)
or
GPIO pin 21 (ESP32)
This code works on both CPUs!
Uli Raich - 2019-05-13
Comments