{"id":259,"date":"2016-07-06T17:43:03","date_gmt":"2016-07-07T01:43:03","guid":{"rendered":"http:\/\/www.nathanbak.com\/?p=259"},"modified":"2026-01-02T20:04:41","modified_gmt":"2026-01-03T04:04:41","slug":"my-new-toy-part-2","status":"publish","type":"post","link":"https:\/\/nathanbak.com\/?p=259","title":{"rendered":"My New Toy (Part 2)"},"content":{"rendered":"<p>In the <a href=\"http:\/\/www.nathanbak.com\/?p=253\">previous post<\/a>, I implemented a simple counter with serial output and today I improved it.\u00a0 The main reason I purchased the ESP8266 module in the first place was to get WiFi for cheap, so I wanted to try out the WiFi capabilities.\u00a0 The resulting sketch is still a counter, but instead of publishing the count via the serial interface, it connects wirelessly to a database and publishes the count there.<\/p>\n<p>To begin with, I needed a simple database that I could access via HTTP.\u00a0 <a href=\"http:\/\/redis.io\/\">Redis<\/a> is a simple key\/value type database, but it doesn&#8217;t have an HTTP interface.\u00a0 I found <a href=\"http:\/\/webd.is\/\">Webdis<\/a> &#8220;A fast HTTP interface for Redis&#8221;.\u00a0 To set things up quickly, I found that someone had already put everything together and published a Docker image on <a href=\"https:\/\/hub.docker.com\/r\/anapsix\/webdis\/\">DockerHub<\/a>.\u00a0 So, through the magic of Docker, all I had to do to get Redis and Webdis up and running on my computer was run this magic command:<\/p>\n<pre>docker run -d -p 7379:7379 -e LOCAL_REDIS=true anapsix\/webdis<\/pre>\n<p>I then wrote a sketch that would publish to my new database:<\/p>\n<pre>#include &lt;ESP8266WiFi.h&gt;\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\nint count = 0;\r\n\r\nvoid setup() {\r\n  WiFi.begin(ssid, pass);\r\n  while (WiFi.status() != WL_CONNECTED) {\r\n    delay(1000);\r\n  }\r\n}\r\n\r\nvoid loop() {\r\n  delay(5000);\r\n  WiFiClient client;\r\n  client.connect(ip, port);\r\n  String url = String(\"\/SET\/count\/\") + count++;\r\n  client.print(String(\"GET \") + url + \" HTTP\/1.1\\r\\n\" +\r\n               \"Host: \" + String(ip) + \"\\r\\n\" + \r\n               \"Connection: close\\r\\n\" + \r\n               \"Content-Length: 0\\r\\n\" + \r\n               \"\\r\\n\");\r\n}\r\n<\/pre>\n<p>Basically, it connects to the wifi network (ssid) using the provided password (pass) and then publishes the count to the database (located at ip).\u00a0 It publishes the new count value every five seconds.<\/p>\n<p>To verify that it was working, I simply plugged this URL into my web browser :<\/p>\n<pre>192.168.1.35.7379\/GET\/count<\/pre>\n<p>It returns the current value of count:<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-263\" src=\"http:\/\/www.nathanbak.com\/wp-content\/uploads\/2016\/07\/getCount-300x108.png\" alt=\"getCount\" width=\"300\" height=\"108\" srcset=\"https:\/\/nathanbak.com\/wp-content\/uploads\/2016\/07\/getCount-300x108.png 300w, https:\/\/nathanbak.com\/wp-content\/uploads\/2016\/07\/getCount.png 439w\" sizes=\"auto, (max-width: 300px) 85vw, 300px\" \/><\/p>\n<p>So now I can not only program my new toy, but also use some of its wireless capabilities.\u00a0 It&#8217;s not useful, but it is a good step to learning how to use the ESP8266.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the previous post, I implemented a simple counter with serial output and today I improved it.\u00a0 The main reason I purchased the ESP8266 module in the first place was to get WiFi for cheap, so I wanted to try out the WiFi capabilities.\u00a0 The resulting sketch is still a counter, but instead of publishing &hellip; <a href=\"https:\/\/nathanbak.com\/?p=259\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;My New Toy (Part 2)&#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":[8,13,10],"class_list":["post-259","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-docker","tag-esp8266","tag-making"],"_links":{"self":[{"href":"https:\/\/nathanbak.com\/index.php?rest_route=\/wp\/v2\/posts\/259","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=259"}],"version-history":[{"count":5,"href":"https:\/\/nathanbak.com\/index.php?rest_route=\/wp\/v2\/posts\/259\/revisions"}],"predecessor-version":[{"id":280,"href":"https:\/\/nathanbak.com\/index.php?rest_route=\/wp\/v2\/posts\/259\/revisions\/280"}],"wp:attachment":[{"href":"https:\/\/nathanbak.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=259"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nathanbak.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=259"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nathanbak.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=259"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}