MicroPython – Nodes examples with DHT11, Relay and MQTT

In a series of articles seen previously  on Mqtt, Beaglebone and ESP8266

Mqtt with Beaglebone and ESP8266 – Articles List

we have created the infrastructure for managing the ESP8266 nodes .

In this article we program the two ESP8266 nodes  using MicroPython instead of Nodemcu and Lua.

The infrastructure remains identical; on the Beaglebone there is Mqtt Mosquitto Server and Freeboard for the management of the mqtt topics, visualization and interaction.

The ESP8266 node examples with relay and DHT11 sensor use the umqtt MicroPython libraries, available on the site

umqtt simple

In the following examples, only the umqtt simple is used; in the code several delays have been inserted to allow the board to manage the messages and not run into errors; in the case the Mqtt server is not available the code restarts the board.

Below the code used for these examples.

Node with DHT11 sensor

  • boot.py: code executed at startup

  • umqtt/simple.py:  mqtt library

  • dht11.py: module for reading the temperature and humidity on the GPIO2

  • main.py: code executed after boot

  •  client_mqtt.py: Main module for node management

  • riavvia.py: service code to restart the module from uPyCraft

 

Node with relay

  • boot.py: code executed at startup

  • umqtt/simple.py: mqtt library

    • main.py: code executed after boot

    •  client_mqtt.py: Main module for node management

  • riavvia.py: service code to restart the module from uPyCraft

Below is the filesystem on ESP8266 for the relays sample

ESPFilesystemBy accessing Freeboard we have the same informations obtained with Nodemcu and LUA on ESP8266 nodes

FreeboardMicroPythonBelow are the screens with MQTT Dashboard installed on an Android smartphone, with the possibility to check the status of the relay, read the sensor parameters, restart and sleep the two nodes

MqttPublish

MqttSubscribe

Here are the links to download the two examples

DHT11 sensor sample

Relay sample

In the next article we will compile the MicroPython firmware for ESP8266

MicroPython – MicroPython compiling for ESP8266