Line: 1 to 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
![]() Slide 1: Setting up and IoTSession 2: Micropython and
Slide 2: The WeMos D1 mini CPU card
Slide 3: CPU Pinout
Slide 4: CPU pinouts for reference![]() Slide 5: Meaning of pins on ESP8266
Slide 6: Meaning of pins on ESP32![]() Slide 7: How to program the processor | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Changed: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
< < |
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
> > |
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Flashing the code![]() esptool
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Changed: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
< < |
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
> > |
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
In the above cases the use of esptool is hidden to us.
We can however also execute esptool directly.
How to write a Micropython program?First we need a Micropython interpreter! You find the sources here: https://github.com/micropython/micropython/![]() ![]() 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.![]() What is REPL?![]() The communication tools: minicom![]() The command line tool ampy![]() IDE for Micropython: uPyCraft![]() uPyCraftuPyCraft is a rather complete Integrate Development Environment (IDE) which lets you
Flashing MicropythonThis 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.![]() 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. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Changed: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
< < | The Linux version did not work for me | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
> > | The Linux version did not work for me when running Ubuntu 18.04 or later. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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![]() Thonny![]() 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. ![]() IoT Hello World programA “Hello World” program, just printing “Hello World” on the screen does not look very exciting. However, this is generally used to verify that the infrastructureCompiler, linker, downloader, flash program are working correctly In embedded systems printing can be quite complex and a blinking LED is used instead. Switching on and off a LEDThe ESP8266 and the ESP32 have a “user LED” connected to GPIO 2. How do we control this LED?
Micropython hardware functions![]() The machine.Pin class![]() Switch the LED on, version 1![]() Switch the LED on, version 2![]() The blinking LEDNow we put the code into a script and run it![]() Changing the light intensityThe 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.![]() PWM in Micropython![]() Our PWM implementation![]() The WS2812B LEDA more complex LED:
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Changed: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
< < |
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
> > |
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Changed: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
< < | WS2812B timing | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
> > | WS2812B timing | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
For all the details on the ws2812b look at https://cdn-shop.adafruit.com/datasheets/WS2812B.pdf ![]() ![]() ![]() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Changed: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
< < | Cascading the WS2812B | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
> > | Cascading the WS2812B | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() Using the neopixel library![]() … and our codeWe have a single neopixel connected to GPIO pin 4 (ESP8266) or GPIO pin 21 (ESP32) This code works on both CPUs!![]() The I2C busI2C stands for Inter-Integrated-Circuit. It was invented by Philips Semiconductor in 1982. Slow, short distance. Quite a number of sensors in the "WeMos D1 sensor shield collection use the I2C bus
The physical busThe CPU (master) connects to the sensors (slaves) through 2 digital lines:
![]() I2C start and stop sequenceWhen the master want to talk to the slave it issues a start sequenceIt terminates the transfer with a stop sequence ![]() I2C addressingWhen talking to a slave the master sends a seven bit address followed by a read/write bitThis allows to access at most 128 devices ![]() I2C data transferData is transmitted 8 bits at a time followed by an acknowledge bit.If acknowledge is low, transfer ok, otherwise: send stop sequence ![]() I2C write cycle
I2C read cycle
I2C in Micropython![]() Scanning the I2C bus![]() The SHT30 digital temperature and relative humidity sensor | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Changed: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
< < | The SHT30 is a digital temperature and humidity sensor based on the I2C bus | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
> > | The SHT30 is a digital temperature and humidity sensor based on the I2C bus | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Here is its data sheet![]() Temperature precision: +- 0.3 °C Relative humidity: +- 3 % Works on 2.4V – 5.5 V ![]() A look at the SHT30 driverhttps://afnog.iotworkshop.africa/pub/AFNOG/HardwareAccessAndMicropython/sht30.py.txtReading out the SHT30![]() Results from the SHT30![]() Where to find the demo codeWhile for the course we only use the CPU and 2 sensor shields:
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Changed: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
< < |
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
> > |
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
there are many more available on the market. We have a dozen such shields here for demo. All demo programs can be found at: https://github.com/uraich/MicroPython_IoTDemos ![]() WeMos D1 mini sensor and actuator shields (1)
Wemos D1 mini sensor and actuator shields (2)
Cost![]() Pinouts of shields![]() Documentation for the demo programsDemo programs for all the shields are available on github: https://github.com/uraich/MicroPython_IoTDemos![]() The data logger
![]() The push button shield
DS18B20 shield
SSD1306 48x64 OLED displayThis one is also an I2C deviceIt provides 48*64 pixels and allows to
framebuf class included in Micropython such that all drawing methods of the framebuf are available for drawing: https://docs.micropython.org/en/latest/library/framebuf.html ![]() For an example see: https://github.com/uraich/MicroPython_IoTDemos/tree/master/drivers/oled ![]() ![]() The DHT11 shield
The LED matrix
The buzzer
The prototype module
The triple baseThis module allows to easily stack a rather larger number of shields to a sandwich.Make sure however that the GPIO lines used by the modules do not clash ![]() ExercisesNow it is up toYouto do the work!
https://afnog.iotworkshop.africa/do/view/AFNOG/Session2 %SLIDESHOWEND%![]() Comments
|