Skip to content

ESP8266/NodeMCU Script for Measuring and Publishing MQTT Temperature Data

License

Notifications You must be signed in to change notification settings

pixelstuermer/esp8266-mqtt-temp-publisher

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

75 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

License: MIT Build Status

ESP8266/NodeMCU Script for Measuring and Publishing MQTT Temperature Data

The script reads temperature data from a DS18B20 temperature module and publishes it with the public MQTT broker of HiveMQ. Of course, other brokers can be used as well. When subscribing to the corresponding topic, the published messages can be verified (using the HiveMQ websocket client for example).

Prerequisites

  • Arduino IDE (or a text editor and the Arduino CLI)
  • Additional boards (ESP8266 or NodeMCU)
  • Additional libraries (to support Wifi, MQTT, OneWire and the temperature sensor)
  • Possibly Python and Esptool (for flashing the binary)

Getting Started

  1. Download the mqtt-temp-publisher.ino file or clone this repository using git clone
  2. Change the required variables as shown below
  3. Add additional boards as explained here (or use the Arduino CLI as explained here)
  4. Build the binary
  5. Flash the firmware to your ESP8266 or NodeMCU board

Configuration

The following variables need to be set to tailor the application to your needs. mqttBroker and mqttPort can remain unchanged if the public broker of HiveMQ is to be used. Also, the publishing interval can be changed (default is 60000 milliseconds).

wifiSsid = "{WIFI name}";
wifiPassword = "{WIFI password}";

mqttBroker = "{MQTT broker url}";                // default is "broker.hivemq.com"
mqttPort = {MQTT broker port};                   // default is 1883
mqttClientId = "{MQTT client ID}";
mqttTopic = "{MQTT topic}";

oneWire({data pin of your temperature sensor});  // default is D4

Building

Compiling and Flashing in one Step

Using the Arduino IDE, the script can be compiled and uploaded in one step: Select a board (ESP8266 or NodeMCU) from within Tools > Board and choose the correct serial port for uploading from within Tools > Serial Port. Then click Sketch > Upload to compile the code and flash the firmware in one step.

Using the Arduino CLI, the script can be compiled and uploaded using {path to cli}/arduino --upload --board {board information} --port {portname} {path to script}/mqtt-temp-publisher.ino as explained here as well.

Only Compiling the Binary

If you simply want to build the binary without flashing it, select Sketch > Export compiled Binary within the Arduino IDE.

Otherwise, the Arduino CLI can be utilized as well using {path to cli}/arduino --verify --board {board information} --preserve-temp-files --pref build.path={destination of binary} {path to script}/mqtt-temp-publisher.ino.

Installing

When having only compiled the binary (as explained above), the firmware can be flashed using esptool for example: python {path to esptool}/esptool.py --port {portname} write_flash -fm dio 0x00000 {path to binary}/mqtt-temp-publisher.ino.bin.

Required Libraries

Releases

No releases published

Packages

No packages published

Languages