MQTT with BeagleBone and ESP8266 – MQTT on ESP8266 with temperature sensor

After loading the firmware into the device esp8266

MQTT with BeagleBone and ESP8266-MQTT ESP8266

we now connect this module with a DHT11 temperature and humidity sensor. Here is how we connect the sensor to the ESP module

nodemcu dht11

In this example we will consider the option of putting the nodemcu in sleep. To have the node back form the sleep  the system has to reboot and  it is necessary to connect the PIN D0 (GPIO16) to RST PIN as specified in the nodemcu documentation

Nodemcu sleep

Let us take a look at the lua code; It consists of init.lua and tempumid.lua files.

We analyze now the main points of each file

  • init.lua

In the file we set the the parameters to access the wifi network, the address and port of the MQTT broker  and the file to be run on the ESP after wifi connection to the access point is estabilished. If the ESP does’t acquire the netowrk address the system is restarted.

  • tempumid.lua

The temumid.lua file manages the measurement of temperature and humidity, the subscription on MQTT broker with submission of the data. Global parameters have been set to manage the topic, the waiting time in tmr.alarm. There are in particular the following topics

  • /home/room1/temperature: measured temperature topic.
  • /home/room1/humidity: measured humidity topic.
  • /home/room1/status : Status topic. The status is sent at the start and at  request from the command topic /home/room1/command.
  • /home/room1/monitor: Status of the esp sent periodically to this topic.
  • /home/room1/command: Other clients can send commands to the esp:
    • Restart: Restarts the esp.
    • Sleep n: Put to sleep for n seconds the esp.

There are various service functions to manage all. The comments in the code describe each feature.

In the next article we will set up the esp with the relay

MQTT with BeagleBone and ESP8266 – MQTT on ESP8266 with Relay