{"id":80,"date":"2021-08-14T16:24:33","date_gmt":"2021-08-14T16:24:33","guid":{"rendered":"https:\/\/m1nd.io\/?page_id=80"},"modified":"2021-08-15T12:21:43","modified_gmt":"2021-08-15T12:21:43","slug":"terrarium-temperature-sensors-with-arduino-raspberry-influxdb-and-grafana","status":"publish","type":"page","link":"https:\/\/m1nd.io\/index.php\/terrarium-temperature-sensors-with-arduino-raspberry-influxdb-and-grafana\/","title":{"rendered":"Terrarium Temperature Sensors with Arduino, Raspberry Pi, InfluxDB and Grafana"},"content":{"rendered":"<h2 class=\"wp-block-post-title\">Terrarium Temperature Sensors with Arduino, Raspberry Pi, InfluxDB and Grafana<\/h2>\n\n\n<div class=\"wp-block-media-text alignwide is-stacked-on-mobile is-vertically-aligned-top\"><figure class=\"wp-block-media-text__media\"><img decoding=\"async\" width=\"768\" height=\"1024\" src=\"https:\/\/m1nd.io\/wp-content\/uploads\/2021\/08\/img_0008-768x1024.jpg\" alt=\"\" class=\"wp-image-77 size-full\" srcset=\"https:\/\/m1nd.io\/wp-content\/uploads\/2021\/08\/img_0008-768x1024.jpg 768w, https:\/\/m1nd.io\/wp-content\/uploads\/2021\/08\/img_0008-225x300.jpg 225w, https:\/\/m1nd.io\/wp-content\/uploads\/2021\/08\/img_0008-1152x1536.jpg 1152w, https:\/\/m1nd.io\/wp-content\/uploads\/2021\/08\/img_0008-1536x2048.jpg 1536w, https:\/\/m1nd.io\/wp-content\/uploads\/2021\/08\/img_0008-scaled.jpg 1920w\" sizes=\"(max-width: 768px) 100vw, 768px\" \/><\/figure><div class=\"wp-block-media-text__content\">\n<p>First I started with the Arduino and wrote some code to verify the sensors are working, initially using a Breadboard. <\/p>\n\n\n\n<p>After I could read the metrics, I&#8217;ve set up InfluxDB on a Raspberry to store the data from the Arduino.<\/p>\n\n\n\n<p>Then I extended the Sketch, so the Arduino connects to our Wifi and pushes the data via UDP to the InfluxDB.<\/p>\n\n\n\n<p>The next step was to set up Grafana to have a nice <a href=\"https:\/\/m1nd.io\/index.php\/terratemp\/\" title=\"TerraTemp\">dashboard<\/a> view. <\/p>\n<\/div><\/div>\n\n\n\n<p><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<div class=\"wp-block-media-text alignwide is-stacked-on-mobile\"><figure class=\"wp-block-media-text__media\"><img decoding=\"async\" width=\"768\" height=\"1024\" src=\"https:\/\/m1nd.io\/wp-content\/uploads\/2021\/08\/PHOTO-2021-08-06-18-26-26.jpg\" alt=\"\" class=\"wp-image-209 size-full\" srcset=\"https:\/\/m1nd.io\/wp-content\/uploads\/2021\/08\/PHOTO-2021-08-06-18-26-26.jpg 768w, https:\/\/m1nd.io\/wp-content\/uploads\/2021\/08\/PHOTO-2021-08-06-18-26-26-225x300.jpg 225w\" sizes=\"(max-width: 768px) 100vw, 768px\" \/><\/figure><div class=\"wp-block-media-text__content\">\n<p>As everything was working fine, it was time to solder each DS18B20 sensor to a connector so they can easily be attached and detached if needed.<\/p>\n\n\n\n<p>Then I soldered all the needed cables and the 4.7 K\u03a9 resistor to the prototyping shield that is connected to the Arduino.<\/p>\n\n\n\n<p>Afterwards I added a DHT22 Sensor directly to the shield to measure the room temperature.<\/p>\n\n\n\n<p><\/p>\n<\/div><\/div>\n\n\n\n<p><\/p>\n\n\n\n<hr class=\"wp-block-separator is-style-wide\"\/>\n\n\n\n<div class=\"wp-block-media-text alignwide is-stacked-on-mobile\"><figure class=\"wp-block-media-text__media\"><img decoding=\"async\" width=\"1024\" height=\"768\" src=\"https:\/\/m1nd.io\/wp-content\/uploads\/2021\/08\/IMG-0040-1024x768.jpg\" alt=\"\" class=\"wp-image-210 size-full\" srcset=\"https:\/\/m1nd.io\/wp-content\/uploads\/2021\/08\/IMG-0040-1024x768.jpg 1024w, https:\/\/m1nd.io\/wp-content\/uploads\/2021\/08\/IMG-0040-300x225.jpg 300w, https:\/\/m1nd.io\/wp-content\/uploads\/2021\/08\/IMG-0040-768x576.jpg 768w, https:\/\/m1nd.io\/wp-content\/uploads\/2021\/08\/IMG-0040-1536x1152.jpg 1536w, https:\/\/m1nd.io\/wp-content\/uploads\/2021\/08\/IMG-0040-2048x1536.jpg 2048w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><div class=\"wp-block-media-text__content\">\n<p>Everything was now ready to be put into action.<\/p>\n\n\n\n<p>I placed 3 sensors in the Chameleon enclosure, one in the rain forrest, and one in our smaller aquarium.<\/p>\n\n\n\n<p>To make sure I can access the dashboard from anywhere, I configured a reverse proxy on my server that proxies requests from <a href=\"https:\/\/m1nd.io\/terra\" title=\"https:\/\/m1nd.io\/terra\">m1nd.io\/terra<\/a> to the Raspberry.<\/p>\n\n\n\n<p>As I had a screen left, it was placed next to the terrariums. I hooked up another spare Raspberry and configured it to open the dashboard automatically in a browser. <\/p>\n<\/div><\/div>\n\n\n\n<hr class=\"wp-block-separator is-style-wide\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Hardware:<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>Arduino Uno Wifi Rev. 2<\/li><li>Arduino Prototyping shield<\/li><li>5x DS18B20 Temperature sensors<\/li><li>1x 4.7 K\u03a9 resistor<\/li><li>1x DHT22 Temperature \/ Humidity sensor<\/li><li>Connectors<\/li><li>Raspberry Pi (2x)<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Software<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>Arduino<ul><li>Sketch<\/li><\/ul><\/li><li>Raspberry Pi &#8211; 1<ul><li>InfluxDB<\/li><li>Grafana<\/li><\/ul><\/li><li>Raspberry Pi &#8211; 2<ul><li>Browser<\/li><\/ul><\/li><li>Webserver<ul><li>Apache2 reverse proxy<\/li><\/ul><\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Arduino Sketch:<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ WIFI\n#include &lt;SPI.h&gt;\n#include &lt;WiFiNINA.h&gt;\n#include \"arduino_secrets.h\"\n#include &lt;WiFiUdp.h&gt;\n#include &lt;DHT.h&gt;\n#include &lt;OneWire.h&gt;\n#include &lt;DallasTemperature.h&gt;\nWiFiUDP udp;\nint status = WL_IDLE_STATUS; \/\/ the Wifi radio\u2019s status\n\n\/\/ Network\nIPAddress dns(8, 8, 8, 8);\nIPAddress influxserver(192, 168, 178, 222);\nconst int influxport = 8089;\nunsigned int localport = 8080;\n\n\/\/ DHT22\n#define DHTPIN 2 \/\/ what pin we\u2019re connected to\n#define DHTTYPE DHT22 \/\/ DHT 22 (AM2302)\nDHT dht(DHTPIN, DHTTYPE);\n\n\/\/ DS18B20\n\/\/ Data wire is plugged into digital pin 3 on the Arduino\n#define ONE_WIRE_BUS 3\n\n\/\/ Setup a oneWire instance to communicate with any OneWire device\nOneWire oneWire(ONE_WIRE_BUS);  \n\n\/\/ Pass oneWire reference to DallasTemperature library\nDallasTemperature sensors(&amp;oneWire);\n\n\/\/ Addresses of DS18B20s\nuint8_t sensor1&#91;8] = { 0x28, 0xF6, 0x84, 0xDB, 0x17, 0x20, 0x06, 0xB3 };\nuint8_t sensor2&#91;8] = { 0x28, 0xCF, 0xE5, 0x22, 0x18, 0x20, 0x06, 0xA9 };\nuint8_t sensor3&#91;8] = { 0x28, 0x26, 0xCE, 0x22, 0x18, 0x20, 0x06, 0x9E };\nuint8_t sensor4&#91;8] = { 0x28, 0xDE, 0xF7, 0x0A, 0x18, 0x20, 0x06, 0x56 };\nuint8_t sensor5&#91;8] = { 0x28, 0xA5, 0x2A, 0xF5, 0x17, 0x20, 0x06, 0x30 };\n\nvoid setup() {\n  Serial.begin(9600);\n  \n  \/\/ check for the WiFi module:\n  if (WiFi.status() == WL_NO_MODULE) {\n    Serial.println(\"Communication with WiFi module failed!\");\n    while (true);\n  }\n\n  while (status != WL_CONNECTED) {\n    Serial.print(\"Attempting to connect to WPA SSID: \");\n    Serial.println(WIFI_SSID);\n    status = WiFi.begin(WIFI_SSID, WIFI_PASS);\n    delay(5000); \/\/ wait 5 seconds for connection\n  }\n\n  Serial.println(\"You\u2019re connected to the network\");\n  WiFi.setDNS(dns);\n  WiFi.reconnect()\n\n  delay(10000);\n  dht.begin(); \/\/ Init DHT22\n  sensors.begin(); \/\/ Init DallasTemperature\n  udp.begin(localport);\n}\n\nvoid loop() {\n  delay(5000);\n  sensors.requestTemperatures();\n\n  \/\/ DHT22 Temperature\n  String dht22_tempLine;\n  dht22_tempLine = String(\"Temperature,sensor=DHT22,id=1,location=arduino value=\" + String(dht.readTemperature()));\n  Serial.println(dht22_tempLine);\n  String beginpacket, printpacket, endpacket;\n  udp.beginPacket(influxserver, influxport);\n  udp.print(dht22_tempLine);\n  udp.endPacket();\n\n  \/\/ DHT22 Humidity\n  String dht22_humLine;\n  dht22_humLine = String(\"Humidity,sensor=DHT22,id=1,location=arduino value=\" + String(dht.readHumidity()));\n  Serial.println(dht22_humLine);\n  udp.beginPacket(influxserver, influxport);\n  udp.print(dht22_humLine);\n  udp.endPacket();\n\n  \/\/ DS18B20 - 1\n  String ds18B20_1_tempLine;\n  float ds18B20_1_temp;\n  ds18B20_1_temp = getTemperature(sensor1);\n  if ((ds18B20_1_temp != -127) &amp;&amp; (ds18B20_1_temp != 85)) {\n    ds18B20_1_tempLine = String(\"Temperature,sensor=DS18B20,id=2,location=Chameleon_top value=\" + String(ds18B20_1_temp));\n    Serial.println(ds18B20_1_tempLine);\n    udp.beginPacket(influxserver, influxport);\n    udp.print(ds18B20_1_tempLine);\n    udp.endPacket();\n  }\n  \/\/ DS18B20 - 2\n  String ds18B20_2_tempLine;\n  float ds18B20_2_temp;\n  ds18B20_2_temp = getTemperature(sensor2);\n  if ((ds18B20_2_temp != -127) &amp;&amp; (ds18B20_2_temp != 85)) {\n    ds18B20_2_tempLine = String(\"Temperature,sensor=DS18B20,id=3,location=Chameleon_mid value=\" + String(ds18B20_2_temp));\n    Serial.println(ds18B20_2_tempLine);\n    udp.beginPacket(influxserver, influxport);\n    udp.print(ds18B20_2_tempLine);\n    udp.endPacket();\n  }\n  \/\/ DS18B20 - 3\n  String ds18B20_3_tempLine;\n  float ds18B20_3_temp;\n  ds18B20_3_temp = getTemperature(sensor3);\n  if ((ds18B20_3_temp != -127) &amp;&amp; (ds18B20_3_temp != 85)) {\n    ds18B20_3_tempLine = String(\"Temperature,sensor=DS18B20,id=4,location=Chameleon_bot value=\" + String(ds18B20_3_temp));\n    Serial.println(ds18B20_3_tempLine);\n    udp.beginPacket(influxserver, influxport);\n    udp.print(ds18B20_3_tempLine);\n    udp.endPacket();\n  }\n  \/\/ DS18B20 - 4\n  String ds18B20_4_tempLine;\n  float ds18B20_4_temp;\n  ds18B20_4_temp = getTemperature(sensor4);\n  if ((ds18B20_4_temp != -127) &amp;&amp; (ds18B20_4_temp != 85)) {\n    ds18B20_4_tempLine = String(\"Temperature,sensor=DS18B20,id=5,location=Rainforrest value=\" + String(ds18B20_4_temp));\n    Serial.println(ds18B20_4_tempLine);\n    udp.beginPacket(influxserver, influxport);\n    udp.print(ds18B20_4_tempLine);\n    udp.endPacket();\n  }\n  \/\/ DS18B20 - 5\n  String ds18B20_5_tempLine;\n  float ds18B20_5_temp;\n  ds18B20_5_temp = getTemperature(sensor5);\n  if ((ds18B20_5_temp != -127) &amp;&amp; (ds18B20_5_temp != 85)) {\n    ds18B20_5_tempLine = String(\"Temperature,sensor=DS18B20,id=6,location=Fishtank value=\" + String(ds18B20_5_temp));\n    Serial.println(ds18B20_5_tempLine);\n    udp.beginPacket(influxserver, influxport);\n    udp.print(ds18B20_5_tempLine);\n    udp.endPacket();\n  }\n}\n\nfloat getTemperature(DeviceAddress deviceAddress)\n{\n  float tempC = sensors.getTempC(deviceAddress);\n  return tempC;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>First I started with the Arduino and wrote some code to verify the sensors are working, initially using a Breadboard. After I could read the metrics, I&#8217;ve set up InfluxDB on a Raspberry to store the data from the Arduino. Then I extended the Sketch, so the Arduino connects to our Wifi and pushes the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"ngg_post_thumbnail":0,"footnotes":""},"class_list":["post-80","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/m1nd.io\/index.php\/wp-json\/wp\/v2\/pages\/80","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/m1nd.io\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/m1nd.io\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/m1nd.io\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/m1nd.io\/index.php\/wp-json\/wp\/v2\/comments?post=80"}],"version-history":[{"count":9,"href":"https:\/\/m1nd.io\/index.php\/wp-json\/wp\/v2\/pages\/80\/revisions"}],"predecessor-version":[{"id":270,"href":"https:\/\/m1nd.io\/index.php\/wp-json\/wp\/v2\/pages\/80\/revisions\/270"}],"wp:attachment":[{"href":"https:\/\/m1nd.io\/index.php\/wp-json\/wp\/v2\/media?parent=80"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}