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).
- 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)
- Download the mqtt-temp-publisher.ino file or clone this repository using
git clone
- Change the required variables as shown below
- Add additional boards as explained here (or use the Arduino CLI as explained here)
- Build the binary
- Flash the firmware to your ESP8266 or NodeMCU board
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
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.
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
.
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
.