{"id":1787,"date":"2019-01-25T11:33:06","date_gmt":"2019-01-25T19:33:06","guid":{"rendered":"http:\/\/www.donluc.com\/?p=1787"},"modified":"2019-01-25T11:33:06","modified_gmt":"2019-01-25T19:33:06","slug":"project-10-esp8266-thing-web-server-mk03","status":"publish","type":"post","link":"https:\/\/www.donluc.com\/?p=1787","title":{"rendered":"Project #10: ESP8266 Thing \u2013 Web Server &#8211; Mk03"},"content":{"rendered":"<div style=\"width: 720px;\" class=\"wp-video\"><video class=\"wp-video-shortcode\" id=\"video-1787-1\" width=\"720\" height=\"406\" preload=\"metadata\" controls=\"controls\"><source type=\"video\/mp4\" src=\"https:\/\/www.donluc.com\/wp-content\/uploads\/2019\/01\/DL1901Mk02.mp4?_=1\" \/><a href=\"https:\/\/www.donluc.com\/wp-content\/uploads\/2019\/01\/DL1901Mk02.mp4\">https:\/\/www.donluc.com\/wp-content\/uploads\/2019\/01\/DL1901Mk02.mp4<\/a><\/video><\/div>\n<p><a href=\"https:\/\/www.donluc.com\/wp-content\/uploads\/2019\/01\/DL1901Mk02a.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.donluc.com\/wp-content\/uploads\/2019\/01\/DL1901Mk02a.png\" alt=\"\" width=\"720\" height=\"597\" class=\"alignnone size-full wp-image-1790\" srcset=\"https:\/\/www.donluc.com\/wp-content\/uploads\/2019\/01\/DL1901Mk02a.png 720w, https:\/\/www.donluc.com\/wp-content\/uploads\/2019\/01\/DL1901Mk02a-300x249.png 300w\" sizes=\"auto, (max-width: 720px) 100vw, 720px\" \/><\/a><\/p>\n<p><a href=\"https:\/\/www.donluc.com\/wp-content\/uploads\/2019\/01\/DL1901Mk02b.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.donluc.com\/wp-content\/uploads\/2019\/01\/DL1901Mk02b.png\" alt=\"\" width=\"720\" height=\"540\" class=\"alignnone size-full wp-image-1791\" srcset=\"https:\/\/www.donluc.com\/wp-content\/uploads\/2019\/01\/DL1901Mk02b.png 720w, https:\/\/www.donluc.com\/wp-content\/uploads\/2019\/01\/DL1901Mk02b-300x225.png 300w\" sizes=\"auto, (max-width: 720px) 100vw, 720px\" \/><\/a><\/p>\n<p><strong>AP Web Server<\/strong><\/p>\n<p>Not only can the ESP8266 connect to a WiFi network and interact with the Internet, but it can also set up a network of its own, allowing other devices to connect directly to it. This example demonstrates how to turn the ESP8266 into an access point (AP), and serve up web pages to any connected client.<\/p>\n<p>After uploading this sketch, find another device that you can connect to a WiFi network \u2013 phone, laptop, etc. Look for a network called \u201cThing-XXXX\u201d, where XXXX is the last 2 bytes of the Thing\u2019s MAC address. <\/p>\n<p>WiFi => Yes<\/p>\n<p>ESP8266 Thing XXXX<\/p>\n<p>He sketch sets the network\u2019s password to \u201cdonlucmk01\u201d.<\/p>\n<p>After connecting to your Thing\u2019s AP network, load up a browser and point it to 192.168.4.1\/read. The Thing should serve up a web page showing you its ADC and digital pin 12 readings:<\/p>\n<p>Analog Pin = XXX<br \/>\nDigital Pin: XXX<br \/>\nHumidity and Temperature<br \/>\nHumidity: XX.XX%<br \/>\nCelsius: XX.XX*C<br \/>\nFahrenheit: XX.XX*F<\/p>\n<p><strong>LED Green<\/strong><\/p>\n<p>After that, give 192.168.4.1\/led\/0 (No) and 192.168.4.1\/led\/1 (Yes) a try, and keep an eye on the Thing\u2019s green LED while you do.<\/p>\n<p>RHT03 Humidity and Temperature Sensor<\/p>\n<p>The RHT03 is a low cost humidity and temperature sensor with a single wire digital interface. The sensor is calibrated and doesn\u2019t require extra components so you can get right to measuring relative humidity and temperature.<\/p>\n<p><strong>DonLuc1901Mk02<\/strong><\/p>\n<p>1 x SparkFun ESP8266 Thing<br \/>\n1 x SparkFun FTDI Basic Breakout &#8211; 3.3V<br \/>\n1 x RHT03 Humidity and Temperature Sensor<br \/>\n3 x Jumper Wires 6&#8243; M\/M<br \/>\n1 x Full-Size Breadboard<br \/>\n1 x SparkFun Cerberus USB Cable<\/p>\n<p><strong>SparkFun ESP8266 Thing<\/strong><\/p>\n<p>LG1 &#8211; Digital 5<br \/>\nRHT &#8211; Digital 4<br \/>\nGND &#8211; GND<br \/>\nVIN &#8211; +3.3V<\/p>\n<p><strong>DonLuc1901Mk02p.ino<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"default\" data-enlighter-title=\"DonLuc1901Mk02p.ino\">\r\n\/\/ ***** Don Luc Electronics *****\r\n\/\/ Software Version Information\r\n\/\/ Project #10: SparkFun ESP8266 Thing \u2013 AP Web Server - Mk02\r\n\/\/ 01-02\r\n\/\/ DonLuc1901Mk01p.ino 01-02\r\n\/\/ SparkFun ESP8266 Thing\r\n\/\/ AP Web Server\r\n\/\/ RHT03 Humidity and Temperature Sensor\r\n\r\n\/\/ Include Library Code\r\n#include &lt;ESP8266WiFi.h&gt;\r\n#include &lt;SparkFun_RHT03.h&gt;\r\n\r\n\/\/ WiFi Definitions \r\nconst char WiFiAPPSK[] = &quot;donlucmk01&quot;;\r\n\r\n\/\/ Pin Definitions \r\nconst int LED_PIN = 5;                  \/\/ Thing&#039;s onboard, green LED\r\nconst int ANALOG_PIN = A0;              \/\/ The only analog pin on the Thing\r\nconst int DIGITAL_PIN = 12;             \/\/ Digital pin to be read\r\n\r\n\/\/ WiFi\r\nWiFiServer server(80);\r\n\r\n\/\/ RHT Humidity and Temperature Sensor\r\nconst int RHT03_DATA_PIN = 4;           \/\/ RHT03 data pin Digital 4\r\nRHT03 rht;                              \/\/ This creates a RTH03 object, which we&#039;ll use to interact with the sensor\r\nfloat latestHumidity;\r\nfloat latestTempC;\r\nfloat latestTempF;\r\n  \r\nvoid loop() \r\n{\r\n\r\n  \/\/ RHT03 Humidity and Temperature Sensor\r\n  isRHT03();\r\n\r\n  \/\/ Check if a client has connected\r\n  WiFiClient client = server.available();\r\n  if (!client) {\r\n    return;\r\n  }\r\n\r\n  \/\/ Read the first line of the request\r\n  String req = client.readStringUntil(&#039;\\r&#039;);\r\n  Serial.println(req);\r\n  client.flush();\r\n\r\n  \/\/ Match the request\r\n  int val = -1; \/\/ We&#039;ll use &#039;val&#039; to keep track of both the request type (read\/set) and value if set.\r\n  if (req.indexOf(&quot;\/led\/0&quot;) != -1)\r\n    val = 0; \/\/ Will write LED low\r\n  else if (req.indexOf(&quot;\/led\/1&quot;) != -1)\r\n    val = 1; \/\/ Will write LED high\r\n  else if (req.indexOf(&quot;\/read&quot;) != -1)\r\n    val = -2; \/\/ Will print pin reads\r\n  \/\/ Otherwise request will be invalid. We&#039;ll say as much in HTML\r\n  \/\/ Set GPIO5 according to the request\r\n  if (val &gt;= 0)\r\n    digitalWrite(LED_PIN, val);\r\n\r\n  client.flush();\r\n\r\n  \/\/ Prepare the response. Start with the common header:\r\n  String s = &quot;HTTP\/1.1 200 OK\\r\\n&quot;;\r\n  s += &quot;Content-Type: text\/html\\r\\n\\r\\n&quot;;\r\n  s += &quot;&lt;!DOCTYPE HTML&gt;\\r\\n&lt;html&gt;\\r\\n&quot;;\r\n  \/\/ If we&#039;re setting the LED, print out a message saying we did\r\n  if (val &gt;= 0)\r\n  {\r\n    s += &quot;LED is now &quot;;\r\n    s += (val)?&quot;on&quot;:&quot;off&quot;;\r\n  }\r\n  else if (val == -2)\r\n  { \/\/ If we&#039;re reading pins, print out those values:\r\n    s += &quot;Analog Pin = &quot;;\r\n    s += String(analogRead(ANALOG_PIN));\r\n    s += &quot;&lt;br&gt;&quot;;                                       \/\/ Go to the next line.\r\n    s += &quot;Digital Pin 12 = &quot;;\r\n    s += String(digitalRead(DIGITAL_PIN));\r\n    s += &quot;&lt;br&gt;&quot;;                                       \/\/ Go to the next line.\r\n    s += &quot;Humidity and Temperature&quot;;\r\n    s += &quot;&lt;br&gt;&quot;;                                       \/\/ Go to the next line.    \r\n    s += &quot;Humidity : &quot;;\r\n    s += String(latestHumidity);                       \/\/ Humidity\r\n    s += &quot;%&quot;;\r\n    s += &quot;&lt;br&gt;&quot;; \/\/ Go to the next line.\r\n    s += &quot;Celsius: &quot;;\r\n    s += String(latestTempC);                          \/\/  Temperature *C\r\n    s += &quot;*C&quot;;\r\n    s += &quot;&lt;br&gt;&quot;; \/\/ Go to the next line.\r\n    s += &quot;Fahrenheit: &quot;;\r\n    s += String(latestTempF);                          \/\/ Temperature *F \r\n    s += &quot;*F&quot;;        \r\n  }\r\n  else\r\n  {\r\n    s += &quot;Invalid Request.&lt;br&gt; Try \/led\/1, \/led\/0, or \/read.&quot;;\r\n  }\r\n  s += &quot;&lt;\/html&gt;\\n&quot;;\r\n\r\n  \/\/ Send the response to the client\r\n  client.print(s);\r\n  delay(1);\r\n  Serial.println(&quot;Client disonnected&quot;);\r\n\r\n  \/\/ The client will actually be disconnected when the function returns and &#039;client&#039; object is detroyed\r\n  \r\n}\r\n<\/pre>\n<p><strong>getRHT.ino<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"default\" data-enlighter-title=\"getRHT.ino\">\r\n\/\/ RHT03 Humidity and Temperature Sensor\r\nvoid isRHT03(){\r\n\r\n  \/\/ Call rht.update() to get new humidity and temperature values from the sensor.\r\n  int updateRet = rht.update();\r\n\r\n  \/\/ The humidity(), tempC(), and tempF() functions can be called -- after \r\n  \/\/ a successful update() -- to get the last humidity and temperature value \r\n  latestHumidity = rht.humidity();\r\n  latestTempC = rht.tempC();\r\n  latestTempF = rht.tempF();\r\n  \r\n}\r\n<\/pre>\n<p><strong>setWiFi.ino<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"default\" data-enlighter-title=\"setWiFi.ino\">\r\n\/\/ WiFi\r\nvoid setupWiFi()\r\n{\r\n  \r\n  \/\/ WiFi mode WIFI_AP\r\n  WiFi.mode(WIFI_AP);\r\n\r\n  \/\/ Append the last two bytes of the MAC (HEX&#039;d) to &quot;Thing-&quot;:\r\n  uint8_t mac[WL_MAC_ADDR_LENGTH];\r\n  WiFi.softAPmacAddress(mac);\r\n  String macID = String(mac[WL_MAC_ADDR_LENGTH - 2], HEX) +\r\n                 String(mac[WL_MAC_ADDR_LENGTH - 1], HEX);\r\n  macID.toUpperCase();\r\n  String AP_NameString = &quot;ESP8266 Thing &quot; + macID;\r\n\r\n  char AP_NameChar[AP_NameString.length() + 1];\r\n  memset(AP_NameChar, 0, AP_NameString.length() + 1);\r\n\r\n  for (int i=0; i&lt;AP_NameString.length(); i++)\r\n    AP_NameChar[i] = AP_NameString.charAt(i);\r\n\r\n  WiFi.softAP(AP_NameChar, WiFiAPPSK);\r\n  \r\n}\r\n\/\/ init Hardware\r\nvoid initHardware()\r\n{\r\n\r\n  \/\/ Serial\r\n  Serial.begin(115200);\r\n  \/\/ LED Green\r\n  pinMode(DIGITAL_PIN, INPUT_PULLUP);\r\n  pinMode(LED_PIN, OUTPUT);\r\n  digitalWrite(LED_PIN, LOW);\r\n  \/\/ RHT03 Humidity and Temperature Sensor\r\n  \/\/ Call rht.begin() to initialize the sensor and our data pin\r\n  rht.begin(RHT03_DATA_PIN);\r\n  \r\n}\r\n<\/pre>\n<p><strong>setup.ino<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"default\" data-enlighter-title=\"setup.ino\">\r\n\/\/ Setup\r\nvoid setup() \r\n{\r\n\r\n  \/\/ Hardware\r\n  initHardware();\r\n  \/\/ WiFi\r\n  setupWiFi();\r\n  server.begin();\r\n  \r\n}\r\n<\/pre>\n<p><strong>Don Luc<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>AP Web Server Not only can the ESP8266 connect to a WiFi network and interact with the Internet, but it can also set up a network of its own, allowing other devices to connect directly to it. This example demonstrates how to turn the ESP8266 into an access point (AP), and serve up web pages &#8230; <a title=\"Project #10: ESP8266 Thing \u2013 Web Server &#8211; Mk03\" class=\"read-more\" href=\"https:\/\/www.donluc.com\/?p=1787\" aria-label=\"Read more about Project #10: ESP8266 Thing \u2013 Web Server &#8211; Mk03\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[77,38,78,59,5,79,10,43],"tags":[],"class_list":["post-1787","post","type-post","status-publish","format-standard","hentry","category-esp8266","category-digitalelectronics","category-esp8266-microcontrollers","category-fritzing","category-microcontrollers","category-program_esp8266","category-projects","category-sparkfun"],"_links":{"self":[{"href":"https:\/\/www.donluc.com\/index.php?rest_route=\/wp\/v2\/posts\/1787","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.donluc.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.donluc.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.donluc.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.donluc.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1787"}],"version-history":[{"count":25,"href":"https:\/\/www.donluc.com\/index.php?rest_route=\/wp\/v2\/posts\/1787\/revisions"}],"predecessor-version":[{"id":4240,"href":"https:\/\/www.donluc.com\/index.php?rest_route=\/wp\/v2\/posts\/1787\/revisions\/4240"}],"wp:attachment":[{"href":"https:\/\/www.donluc.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1787"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.donluc.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1787"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.donluc.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1787"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}