{"id":274,"date":"2016-08-02T15:09:16","date_gmt":"2016-08-02T23:09:16","guid":{"rendered":"http:\/\/www.nathanbak.com\/?p=274"},"modified":"2026-01-02T20:04:41","modified_gmt":"2026-01-03T04:04:41","slug":"my-new-toy-part-3","status":"publish","type":"post","link":"https:\/\/nathanbak.com\/?p=274","title":{"rendered":"My New Toy (Part 3)"},"content":{"rendered":"<p>In my <a href=\"http:\/\/www.nathanbak.com\/?p=259\">previous &#8220;New Toy&#8221; post<\/a> I managed to wirelessly publish a count, so I determined the next step to be to publish some &#8220;real&#8221; information: the temperature.<\/p>\n<p>I&#8217;m not the first to use an ESP8266 module to publish temperature information and I found an excellent tutorial on <a href=\"https:\/\/learn.adafruit.com\/esp8266-temperature-slash-humidity-webserver\/overview\">Adafruit<\/a>.\u00a0 However, it was written for another (now discontinued) ESP8266 module with significant differences from my Electrow.\u00a0 Also, the sample code provide has the module host a web server and allow clients to connect to it whereas I wanted my module to push the information to a database.\u00a0 This meant I could use the tutorial as a guide, but there were still various things to figure out.<\/p>\n<p>First I had to do the wiring to connect the DHT22 temperature and humidity sensor I purchased like the one used in the tutorial.\u00a0 I ended up spending a lot of time and muttering various working words trying to get it to work right.\u00a0 My module has onboard USB and a voltage regulator, so the wiring is much simpler, but I couldn&#8217;t get any readings.\u00a0 I tried resistor changes, testing connections, and verifying voltages among other things.\u00a0 Finally I discovered that when I specify pin 2 it means D04 (the fifth digital pin) on my board.\u00a0 So here&#8217;s what I ended up with:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-281 alignleft\" src=\"http:\/\/www.nathanbak.com\/wp-content\/uploads\/2016\/08\/2016-08-02-15.35.48-e1470177554648-227x300.jpg\" alt=\"2016-08-02 15.35.48\" width=\"227\" height=\"300\" srcset=\"https:\/\/nathanbak.com\/wp-content\/uploads\/2016\/08\/2016-08-02-15.35.48-e1470177554648-227x300.jpg 227w, https:\/\/nathanbak.com\/wp-content\/uploads\/2016\/08\/2016-08-02-15.35.48-e1470177554648-768x1015.jpg 768w, https:\/\/nathanbak.com\/wp-content\/uploads\/2016\/08\/2016-08-02-15.35.48-e1470177554648-775x1024.jpg 775w, https:\/\/nathanbak.com\/wp-content\/uploads\/2016\/08\/2016-08-02-15.35.48-e1470177554648.jpg 1036w\" sizes=\"auto, (max-width: 227px) 85vw, 227px\" \/><\/p>\n<ul>\n<li>The ground pin connects to the fourth pin of the DHT22 (black wire)<\/li>\n<li>The D04 digital pin connects to the second pin of the DHT22 (white wire)<\/li>\n<li>The 3.3v pin provides voltage (via the red wire) to the both the first pin of the DHT22 and it also goes through a 10K resister to the second pin.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>The code is basically pasting together the Adafruit sample code with the code I wrote previously.\u00a0 It looks like this:<\/p>\n<pre>#include &lt;ESP8266WiFi.h&gt;\r\n#include &lt;DHT.h&gt;\r\n#define DHTTYPE DHT22\r\n#define DHTPIN\u00a0 2\r\n\r\nconst char* ssid = \"name of wifi network\";\r\nconst char* pass = \"wifi network password\";\r\nconst char* ip = \"192.168.1.35\";\r\nconst int port = 7379;\r\n\r\n\/\/ Initialize DHT sensor \r\nDHT dht(DHTPIN, DHTTYPE, 11); \/\/ 11 works fine for ESP8266\r\n\u00a0\r\nvoid setup(void)\r\n{\r\n\u00a0 dht.begin();\r\n\u00a0 WiFi.begin(ssid, pass);\r\n\u00a0 while (WiFi.status() != WL_CONNECTED) {\r\n\u00a0\u00a0\u00a0 delay(1000);\r\n\u00a0 }\r\n}\r\n\u00a0\r\nvoid loop(void)\r\n{\r\n\u00a0\u00a0\u00a0 float temp_f = dht.readTemperature(true);\r\n\r\n\u00a0\u00a0\u00a0 WiFiClient client;\r\n\u00a0\u00a0\u00a0 client.connect(ip, port);\r\n\u00a0\u00a0\u00a0 String url = String(\"\/SET\/temperature\/\") + temp_f;\r\n\u00a0\u00a0\u00a0 client.print(String(\"GET \") + url + \" HTTP\/1.1\\r\\n\" +\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \"Host: \" + String(ip) + \"\\r\\n\" + \r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \"Connection: close\\r\\n\" + \r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \"Content-Length: 0\\r\\n\" + \r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \"\\r\\n\");\r\n\u00a0\u00a0\u00a0 delay(5000);\r\n} \r\n<\/pre>\n<p>I had already started up a Webdis\/Redis server as described before, so I could verify it worked by running curl and confirming that it returned a value (in this case 72.68F).<\/p>\n<pre>$ curl http:\/\/192.168.1.35:7379\/GET\/temperature\r\n{\"GET\":\"72.68\"}<\/pre>\n<p>Searching online, I found the <a href=\"https:\/\/www.amazon.com\/ConnectSense-CS-TH-Wireless-Temperature-Humidity\/dp\/B00ISKS8HG\">ConnectSense CS-TH Wireless Temperature and Humidity Sensor<\/a> that seems pretty much like what I made, but in a pretty box and costing $150 (granted it does come with some nice software as well).\u00a0 Now that I have my own budget version, I can look to do something with that temperature data I&#8217;m collecting.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In my previous &#8220;New Toy&#8221; post I managed to wirelessly publish a count, so I determined the next step to be to publish some &#8220;real&#8221; information: the temperature. I&#8217;m not the first to use an ESP8266 module to publish temperature information and I found an excellent tutorial on Adafruit.\u00a0 However, it was written for another &hellip; <a href=\"https:\/\/nathanbak.com\/?p=274\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;My New Toy (Part 3)&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[13,10],"class_list":["post-274","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-esp8266","tag-making"],"_links":{"self":[{"href":"https:\/\/nathanbak.com\/index.php?rest_route=\/wp\/v2\/posts\/274","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nathanbak.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nathanbak.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nathanbak.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nathanbak.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=274"}],"version-history":[{"count":6,"href":"https:\/\/nathanbak.com\/index.php?rest_route=\/wp\/v2\/posts\/274\/revisions"}],"predecessor-version":[{"id":284,"href":"https:\/\/nathanbak.com\/index.php?rest_route=\/wp\/v2\/posts\/274\/revisions\/284"}],"wp:attachment":[{"href":"https:\/\/nathanbak.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=274"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nathanbak.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=274"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nathanbak.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=274"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}