{"id":2679,"date":"2020-05-10T08:26:19","date_gmt":"2020-05-10T07:26:19","guid":{"rendered":"http:\/\/www.robertprice.co.uk\/robblog\/?p=2679"},"modified":"2020-05-10T08:26:28","modified_gmt":"2020-05-10T07:26:28","slug":"sensing-temperature-using-mqtt","status":"publish","type":"post","link":"https:\/\/www.robertprice.co.uk\/robblog\/sensing-temperature-using-mqtt\/","title":{"rendered":"Sensing temperature using MQTT"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">I recently built a simple temperature sensor using a NodeMCU (ESP8266) and a DHT22. This reads the temperature and humidity roughly every 30 seconds and pushes the data to a MQTT server so I can handle the data elsewhere.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The NodeMCU wakes up every 30 seconds and connects to my local WiFi network. Once connected it takes a sensor reading and publishes it to my MQTT server. It then delays for a few seconds to ensure the message is sent, then goes into deep sleep.<br><br>The deep sleep is a function I wasn&#8217;t aware of on the ESP8266 before. It just a case of wiring up D0 to RST and calling ESP.deepsleep() in the code. When the device wakes up, it&#8217;s like its been turned on for the first time.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The code and wiring is available on my GitHub <a href=\"https:\/\/github.com\/robertprice\/NodeMCU_MQTT_Temperature\">https:\/\/github.com\/robertprice\/NodeMCU_MQTT_Temperature<\/a>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"579\" height=\"711\" src=\"https:\/\/www.robertprice.co.uk\/robblog\/assets\/wiring_bb.png\" alt=\"\" class=\"wp-image-2685\" srcset=\"https:\/\/www.robertprice.co.uk\/robblog\/assets\/wiring_bb.png 579w, https:\/\/www.robertprice.co.uk\/robblog\/assets\/wiring_bb-244x300.png 244w\" sizes=\"auto, (max-width: 579px) 85vw, 579px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Reading the data from MQTT<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">As the purpose of the sensor was to just send the data to a MQTT server in a simple format, I needed to write something to listen to the published messages the device was sending.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I was able to do this using a simple NodeRED program to listen to incoming MQTT messages, and send them to debug console, and to a CSV file for later processing. <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"934\" height=\"154\" src=\"https:\/\/www.robertprice.co.uk\/robblog\/assets\/Screenshot-2020-05-10-at-08.14.58.png\" alt=\"\" class=\"wp-image-2687\" srcset=\"https:\/\/www.robertprice.co.uk\/robblog\/assets\/Screenshot-2020-05-10-at-08.14.58.png 934w, https:\/\/www.robertprice.co.uk\/robblog\/assets\/Screenshot-2020-05-10-at-08.14.58-300x49.png 300w, https:\/\/www.robertprice.co.uk\/robblog\/assets\/Screenshot-2020-05-10-at-08.14.58-768x127.png 768w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">I added a timestamp and built the CSV line using a mustache template. This is then appended to a CSV file I put in my \/tmp directory.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The data looks like this<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">2020-05-07T13:09:17.635Z,29.70\n2020-05-07T13:09:54.586Z,29.80\n2020-05-07T13:10:31.508Z,29.80\n2020-05-07T13:11:06.936Z,29.80\n2020-05-07T13:11:42.409Z,29.80<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Using the CSV data<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To test the data is being logged correctly, I wrote a simple R script parse the data and plot a diagram from it. As the CSV had no header columns, I had to rename the first column names as read_csv used the first line values by default.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>library(tidyverse)\nlibrary(readr)\nlibrary(ggplot2)\n\ntemperatures &lt;- read_csv(\"temperatures.csv\", col_types = cols(`2020-05-07T13:09:17.635Z` = col_datetime()))\ntemperatures &lt;- rename(temperatures, c(\"2020-05-07T13:09:17.635Z\"=\"datetime\", \"29.70\"=\"temperature\"))\nshedtemperatures &lt;- temperatures %>% select(datetime, temperature) %>% filter(datetime >= as.Date(\"2020-05-09\") &amp; datetime &lt; as.Date(\"2020-05-10\"))\nggplot(data=shedtemperatures, aes(x=datetime, y=temperature, group=1)) geom_line() + ggtitle(\"Temperature in the Dev Shed\")<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This gave me the following plot&#8230;<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"600\" height=\"400\" src=\"https:\/\/www.robertprice.co.uk\/robblog\/assets\/devshed.png\" alt=\"\" class=\"wp-image-2689\" srcset=\"https:\/\/www.robertprice.co.uk\/robblog\/assets\/devshed.png 600w, https:\/\/www.robertprice.co.uk\/robblog\/assets\/devshed-300x200.png 300w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The maximum temperature in the shed I placed the sensor in was 33.1C.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I recently built a simple temperature sensor using a NodeMCU (ESP8266) and a DHT22. This reads the temperature and humidity roughly every 30 seconds and pushes the data to a MQTT server so I can handle the data elsewhere. The NodeMCU wakes up every 30 seconds and connects to my local WiFi network. Once connected &hellip; <a href=\"https:\/\/www.robertprice.co.uk\/robblog\/sensing-temperature-using-mqtt\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Sensing temperature using MQTT&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":2681,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[2,84],"tags":[],"class_list":["post-2679","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dev","category-hardware"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Sensing temperature using MQTT - Robert Price<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.robertprice.co.uk\/robblog\/sensing-temperature-using-mqtt\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Sensing temperature using MQTT - Robert Price\" \/>\n<meta property=\"og:description\" content=\"I recently built a simple temperature sensor using a NodeMCU (ESP8266) and a DHT22. This reads the temperature and humidity roughly every 30 seconds and pushes the data to a MQTT server so I can handle the data elsewhere. The NodeMCU wakes up every 30 seconds and connects to my local WiFi network. Once connected &hellip; Continue reading &quot;Sensing temperature using MQTT&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.robertprice.co.uk\/robblog\/sensing-temperature-using-mqtt\/\" \/>\n<meta property=\"og:site_name\" content=\"Robert Price\" \/>\n<meta property=\"article:published_time\" content=\"2020-05-10T07:26:19+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-05-10T07:26:28+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.robertprice.co.uk\/robblog\/assets\/nodemcu_dht22.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"rob\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"rob\" \/>\n\t<meta name=\"twitter:label2\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/sensing-temperature-using-mqtt\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/sensing-temperature-using-mqtt\\\/\"},\"author\":{\"name\":\"rob\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"headline\":\"Sensing temperature using MQTT\",\"datePublished\":\"2020-05-10T07:26:19+00:00\",\"dateModified\":\"2020-05-10T07:26:28+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/sensing-temperature-using-mqtt\\\/\"},\"wordCount\":325,\"image\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/sensing-temperature-using-mqtt\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/assets\\\/nodemcu_dht22.jpg\",\"articleSection\":[\"Dev\",\"Hardware\"],\"inLanguage\":\"en-GB\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/sensing-temperature-using-mqtt\\\/\",\"url\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/sensing-temperature-using-mqtt\\\/\",\"name\":\"Sensing temperature using MQTT - Robert Price\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/sensing-temperature-using-mqtt\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/sensing-temperature-using-mqtt\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/assets\\\/nodemcu_dht22.jpg\",\"datePublished\":\"2020-05-10T07:26:19+00:00\",\"dateModified\":\"2020-05-10T07:26:28+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/sensing-temperature-using-mqtt\\\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/sensing-temperature-using-mqtt\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/sensing-temperature-using-mqtt\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/assets\\\/nodemcu_dht22.jpg\",\"contentUrl\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/assets\\\/nodemcu_dht22.jpg\",\"width\":1200,\"height\":630},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/sensing-temperature-using-mqtt\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Sensing temperature using MQTT\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#website\",\"url\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/\",\"name\":\"Robert Price\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\",\"name\":\"rob\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"caption\":\"rob\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Sensing temperature using MQTT - Robert Price","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.robertprice.co.uk\/robblog\/sensing-temperature-using-mqtt\/","og_locale":"en_GB","og_type":"article","og_title":"Sensing temperature using MQTT - Robert Price","og_description":"I recently built a simple temperature sensor using a NodeMCU (ESP8266) and a DHT22. This reads the temperature and humidity roughly every 30 seconds and pushes the data to a MQTT server so I can handle the data elsewhere. The NodeMCU wakes up every 30 seconds and connects to my local WiFi network. Once connected &hellip; Continue reading \"Sensing temperature using MQTT\"","og_url":"https:\/\/www.robertprice.co.uk\/robblog\/sensing-temperature-using-mqtt\/","og_site_name":"Robert Price","article_published_time":"2020-05-10T07:26:19+00:00","article_modified_time":"2020-05-10T07:26:28+00:00","og_image":[{"width":1200,"height":630,"url":"https:\/\/www.robertprice.co.uk\/robblog\/assets\/nodemcu_dht22.jpg","type":"image\/jpeg"}],"author":"rob","twitter_card":"summary_large_image","twitter_misc":{"Written by":"rob","Estimated reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.robertprice.co.uk\/robblog\/sensing-temperature-using-mqtt\/#article","isPartOf":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/sensing-temperature-using-mqtt\/"},"author":{"name":"rob","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"headline":"Sensing temperature using MQTT","datePublished":"2020-05-10T07:26:19+00:00","dateModified":"2020-05-10T07:26:28+00:00","mainEntityOfPage":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/sensing-temperature-using-mqtt\/"},"wordCount":325,"image":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/sensing-temperature-using-mqtt\/#primaryimage"},"thumbnailUrl":"https:\/\/www.robertprice.co.uk\/robblog\/assets\/nodemcu_dht22.jpg","articleSection":["Dev","Hardware"],"inLanguage":"en-GB"},{"@type":"WebPage","@id":"https:\/\/www.robertprice.co.uk\/robblog\/sensing-temperature-using-mqtt\/","url":"https:\/\/www.robertprice.co.uk\/robblog\/sensing-temperature-using-mqtt\/","name":"Sensing temperature using MQTT - Robert Price","isPartOf":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/sensing-temperature-using-mqtt\/#primaryimage"},"image":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/sensing-temperature-using-mqtt\/#primaryimage"},"thumbnailUrl":"https:\/\/www.robertprice.co.uk\/robblog\/assets\/nodemcu_dht22.jpg","datePublished":"2020-05-10T07:26:19+00:00","dateModified":"2020-05-10T07:26:28+00:00","author":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"breadcrumb":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/sensing-temperature-using-mqtt\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.robertprice.co.uk\/robblog\/sensing-temperature-using-mqtt\/"]}]},{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/www.robertprice.co.uk\/robblog\/sensing-temperature-using-mqtt\/#primaryimage","url":"https:\/\/www.robertprice.co.uk\/robblog\/assets\/nodemcu_dht22.jpg","contentUrl":"https:\/\/www.robertprice.co.uk\/robblog\/assets\/nodemcu_dht22.jpg","width":1200,"height":630},{"@type":"BreadcrumbList","@id":"https:\/\/www.robertprice.co.uk\/robblog\/sensing-temperature-using-mqtt\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.robertprice.co.uk\/robblog\/"},{"@type":"ListItem","position":2,"name":"Sensing temperature using MQTT"}]},{"@type":"WebSite","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#website","url":"https:\/\/www.robertprice.co.uk\/robblog\/","name":"Robert Price","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.robertprice.co.uk\/robblog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-GB"},{"@type":"Person","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5","name":"rob","image":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","caption":"rob"}}]}},"_links":{"self":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/2679","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/comments?post=2679"}],"version-history":[{"count":2,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/2679\/revisions"}],"predecessor-version":[{"id":2691,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/2679\/revisions\/2691"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/media\/2681"}],"wp:attachment":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/media?parent=2679"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/categories?post=2679"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/tags?post=2679"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}