{"id":1060,"date":"2022-12-11T19:21:20","date_gmt":"2022-12-11T19:21:20","guid":{"rendered":"https:\/\/thomas.w-p.me.uk\/blog\/?p=1060"},"modified":"2022-12-11T19:24:06","modified_gmt":"2022-12-11T19:24:06","slug":"arduino-as-mqtt-temperature-sensor","status":"publish","type":"post","link":"https:\/\/thomas.w-p.me.uk\/blog\/2022\/12\/arduino-as-mqtt-temperature-sensor\/","title":{"rendered":"arduino as mqtt temperature sensor"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/thomas.w-p.me.uk\/blog\/wp-content\/uploads\/2022\/12\/image-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"458\" height=\"514\" src=\"https:\/\/thomas.w-p.me.uk\/blog\/wp-content\/uploads\/2022\/12\/image-1.png\" alt=\"\" class=\"wp-image-1061\" srcset=\"https:\/\/thomas.w-p.me.uk\/blog\/wp-content\/uploads\/2022\/12\/image-1.png 458w, https:\/\/thomas.w-p.me.uk\/blog\/wp-content\/uploads\/2022\/12\/image-1-267x300.png 267w\" sizes=\"auto, (max-width: 458px) 100vw, 458px\" \/><\/a><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">My office gets pretty cold and I had worried that the heater would come on if it went below 5\u00b0C. This was until I went in there and discovered that the thermostat from Honeywell will not read lower than 5\u00b0C!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now I have grafana I was wondering how to get a reading of the temperature in there. I have lot of kit kicking around from my arduino days so I thought I would give it a whirl. The aim was for the arduino to push the current temperature to the mosquitto broker on openhabian then read it out to influxdb and eventually to grafana.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I ended up using:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>arduino uno<\/li>\n\n\n\n<li>arduino uno ethernet shield<\/li>\n\n\n\n<li>DS18B20 temperature sensor<\/li>\n\n\n\n<li>4.7 k\u03a9 resistor<\/li>\n\n\n\n<li>breadboard and leads<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Recipe<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">I used this to remind me how to get the temperature sensor working: <a href=\"https:\/\/create.arduino.cc\/projecthub\/TheGadgetBoy\/ds18b20-digital-temperature-sensor-and-arduino-9cc806\">https:\/\/create.arduino.cc\/projecthub\/TheGadgetBoy\/ds18b20-digital-temperature-sensor-and-arduino-9cc806<\/a>. Then with a bit of searching and playing around I got the whole kaboodle working. There was a lot of intelligent copy and paste and I have no idea how much of this is redundant but it bloody works (code below).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Weirdly, the graph sparklines did not work in the temperature display in grafana. <a href=\"https:\/\/community.grafana.com\/t\/sparkline-does-not-appear-on-single-stat-graph\/566\">https:\/\/community.grafana.com\/t\/sparkline-does-not-appear-on-single-stat-graph\/566<\/a> helped me work out that I needed to do a GROUPBY in the query (I used 15m). You can see the result at the top of the page.<\/p>\n\n\n\n<pre class=\"wp-block-code has-beige-background-color has-background\"><code>\/********************************************************************\/\n\/\/ First we include the libraries\n\/\/ Temp Sensor\n#include &lt;OneWire.h> \n#include &lt;DallasTemperature.h>\n\/\/ Ethernet Shield\n#include &lt;SPI.h>\n#include &lt;Ethernet.h>\n\/\/ Mqtt\n#include &lt;ArduinoMqttClient.h>\n\/********************************************************************\/\n\/\/ Data wire is plugged into pin 2 on the Arduino \n#define ONE_WIRE_BUS 2 \n\/********************************************************************\/\n\/\/ Setup a oneWire instance to communicate with any OneWire devices  \n\/\/ (not just Maxim\/Dallas temperature ICs) \nOneWire oneWire(ONE_WIRE_BUS); \n\/********************************************************************\/\n\/\/ Pass our oneWire reference to Dallas Temperature. \nDallasTemperature sensors(&amp;oneWire);\n\/********************************************************************\/ \n\n\/\/ Enter a MAC address for your controller below.\n\/\/ Newer Ethernet shields have a MAC address printed on a sticker on the shield\nbyte mac&#91;] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };\n\n\/\/ Set the static IP address to use if the DHCP fails to assign\nIPAddress ip(192, 168, 0, 23);\nIPAddress myDns(192, 168, 0, 1);\n\nEthernetClient net;\n\nMqttClient mqttClient(net);\n\nconst char broker&#91;] = \"openhabian\";\nint        port     = 1883;\nconst char topic&#91;]  = \"arduino\/temperature\/\";\n\nchar clientId&#91;] = \"ToM_arduino_001\";\nchar username&#91;] = \"openhabian\";\nchar password&#91;] = \"averybigsecret\";\n\nconst long interval = 1000;\nunsigned long previousMillis = 0;\n\n\nvoid setup(void) \n{ \n \/\/ start serial port \n Serial.begin(9600); \n Serial.println(\"Dallas Temperature IC Control Library Demo\"); \n \/\/ Start up the library \n sensors.begin(); \n  \/\/ start the Ethernet connection:\n  Serial.println(\"Initialize Ethernet with DHCP:\");\n  if (Ethernet.begin(mac) == 0) {\n    Serial.println(\"Failed to configure Ethernet using DHCP\");\n    \/\/ Check for Ethernet hardware present\n    if (Ethernet.hardwareStatus() == EthernetNoHardware) {\n      Serial.println(\"Ethernet shield was not found.  Sorry, can't run without hardware. :(\");\n      while (true) {\n        delay(1); \/\/ do nothing, no point running without Ethernet hardware\n      }\n    }\n    if (Ethernet.linkStatus() == LinkOFF) {\n      Serial.println(\"Ethernet cable is not connected.\");\n    }\n    \/\/ try to configure using IP address instead of DHCP:\n    Ethernet.begin(mac, ip, myDns);\n  } else {\n    Serial.print(\"  DHCP assigned IP \");\n    Serial.println(Ethernet.localIP());\n  }\n  \/\/ give the Ethernet shield a second to initialize:\n  delay(1000);\n\n  Serial.println(\"You're connected to the network\");\n  Serial.println();\n\n  \/\/ You can provide a unique client ID, if not set the library uses Arduino-millis()\n  \/\/ Each client must have a unique client ID\n  mqttClient.setId(clientId);\n\n  \/\/ You can provide a username and password for authentication\n  mqttClient.setUsernamePassword(username, password);\n\n  Serial.print(\"Attempting to connect to the MQTT broker: \");\n  Serial.println(broker);\n\n  if (!mqttClient.connect(broker, port)) {\n    Serial.print(\"MQTT connection failed! Error code = \");\n    Serial.println(mqttClient.connectError());\n\n    while (1);\n  }\n\n  Serial.println(\"You're connected to the MQTT broker!\");\n  Serial.println();\n} \nvoid loop(void) \n{ \n \/\/ call sensors.requestTemperatures() to issue a global temperature \n \/\/ request to all devices on the bus \n\/********************************************************************\/\n Serial.print(\" Requesting temperatures...\"); \n sensors.requestTemperatures(); \/\/ Send the command to get temperature readings \n Serial.println(\"DONE\"); \n\/********************************************************************\/\n Serial.print(\"Temperature is: \"); \n Serial.print(sensors.getTempCByIndex(0)); \/\/ Why \"byIndex\"?  \n   \/\/ You can have more than one DS18B20 on the same bus.  \n   \/\/ 0 refers to the first IC on the wire \n   delay(10000); \n   \/\/ call poll() regularly to allow the library to send MQTT keep alives which\n  \/\/ avoids being disconnected by the broker\n  mqttClient.poll();\n\n  \/\/ to avoid having delays in loop, we'll use the strategy from BlinkWithoutDelay\n  \/\/ see: File -> Examples -> 02.Digital -> BlinkWithoutDelay for more info\n  unsigned long currentMillis = millis();\n  \n  if (currentMillis - previousMillis >= interval) {\n    \/\/ save the last time a message was sent\n    previousMillis = currentMillis;\n\n    Serial.print(\"Sending message to topic: \");\n    Serial.println(topic);\n\n\n    \/\/ send message, the Print interface can be used to set the message contents\n    mqttClient.beginMessage(topic);\n    mqttClient.print(sensors.getTempCByIndex(0));\n    mqttClient.endMessage();\n\n    Serial.println();\n\n  }\n} <\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>My office gets pretty cold and I had worried that the heater would come on if it went below 5\u00b0C. This was until I went in there and discovered that the thermostat from Honeywell will not read lower than 5\u00b0C! Now I have grafana I was wondering how to get a reading of the temperature &hellip; <a href=\"https:\/\/thomas.w-p.me.uk\/blog\/2022\/12\/arduino-as-mqtt-temperature-sensor\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">arduino as mqtt temperature sensor<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[106,27,104],"tags":[],"class_list":["post-1060","post","type-post","status-publish","format-standard","hentry","category-arduino","category-geekiness","category-openhab"],"_links":{"self":[{"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/posts\/1060","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/comments?post=1060"}],"version-history":[{"count":3,"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/posts\/1060\/revisions"}],"predecessor-version":[{"id":1064,"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/posts\/1060\/revisions\/1064"}],"wp:attachment":[{"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/media?parent=1060"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/categories?post=1060"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/tags?post=1060"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}