{"id":1678,"date":"2015-12-13T18:19:08","date_gmt":"2015-12-13T17:19:08","guid":{"rendered":"http:\/\/labalec.fr\/erwan\/?p=1678"},"modified":"2015-12-13T18:21:34","modified_gmt":"2015-12-13T17:21:34","slug":"esp8266-part-4","status":"publish","type":"post","link":"https:\/\/labalec.fr\/erwan\/?p=1678","title":{"rendered":"ESP8266 Part 4"},"content":{"rendered":"<p>Last ESP8266 example for the day (credit goes <a href=\"http:\/\/www.arduinesp.com\/wifiwebserver\" target=\"_blank\">here<\/a>) : a web server turning a lef off and on.<\/p>\n<p><a href=\"http:\/\/imgur.com\/487Cgoq\"><img decoding=\"async\" src=\"http:\/\/i.imgur.com\/487Cgoq.png\" title=\"source: imgur.com\" \/><\/a><\/p>\n<pre>\r\n\r\n#include <ESP8266WiFi.h>\r\n \r\nconst char* ssid = \"livebox0\";\r\nconst char* password = \"password\";\r\n \r\nint ledPin = 2; \/\/ GPIO2\r\nWiFiServer server(80);\r\n \r\nvoid setup() {\r\n  Serial.begin(115200);\r\n  delay(10);\r\n \r\n  pinMode(ledPin, OUTPUT);\r\n  digitalWrite(ledPin, LOW);\r\n \r\n  \/\/ Connect to WiFi network\r\n  Serial.println();\r\n  Serial.println();\r\n  Serial.print(\"Connecting to \");\r\n  Serial.println(ssid);\r\n \r\n  WiFi.begin(ssid, password);\r\n \r\n  while (WiFi.status() != WL_CONNECTED) {\r\n    delay(500);\r\n    Serial.print(\".\");\r\n  }\r\n  Serial.println(\"\");\r\n  Serial.println(\"WiFi connected\");\r\n \r\n  \/\/ Start the server\r\n  server.begin();\r\n  Serial.println(\"Server started\");\r\n \r\n  \/\/ Print the IP address\r\n  Serial.print(\"Use this URL to connect: \");\r\n  Serial.print(\"http:\/\/\");\r\n  Serial.print(WiFi.localIP());\r\n  Serial.println(\"\/\");\r\n \r\n}\r\n \r\nvoid loop() {\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  \/\/ Wait until the client sends some data\r\n  Serial.println(\"new client\");\r\n  while(!client.available()){\r\n    delay(1);\r\n  }\r\n \r\n  \/\/ Read the first line of the request\r\n  String request = client.readStringUntil('\\r');\r\n  Serial.println(request);\r\n  client.flush();\r\n \r\n  \/\/ Match the request\r\n \r\n  int value = LOW;\r\n  if (request.indexOf(\"\/LED=ON\") != -1)  {\r\n    digitalWrite(ledPin, HIGH);\r\n    value = HIGH;\r\n  }\r\n  if (request.indexOf(\"\/LED=OFF\") != -1)  {\r\n    digitalWrite(ledPin, LOW);\r\n    value = LOW;\r\n  }\r\n \r\n\/\/ Set ledPin according to the request\r\n\/\/digitalWrite(ledPin, value);\r\n \r\n  \/\/ Return the response\r\n  client.println(\"HTTP\/1.1 200 OK\");\r\n  client.println(\"Content-Type: text\/html\");\r\n  client.println(\"\"); \/\/  do not forget this one\r\n  client.println(\"<!DOCTYPE HTML>\");\r\n  client.println(\"<html>\");\r\n \r\n  client.print(\"Led pin is now: \");\r\n \r\n  if(value == HIGH) {\r\n    client.print(\"On\");\r\n  } else {\r\n    client.print(\"Off\");\r\n  }\r\n  client.println(\"<br><br>\");\r\n  client.println(\"Click <a href=\\\"\/LED=ON\\\">here<\/a> turn the LED on pin 2 ON<br>\");\r\n  client.println(\"Click <a href=\\\"\/LED=OFF\\\">here<\/a> turn the LED on pin 2 OFF<br>\");\r\n  client.println(\"<\/html>\");\r\n \r\n  delay(1);\r\n  Serial.println(\"Client disonnected\");\r\n  Serial.println(\"\");\r\n \r\n}\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Last ESP8266 example for the day (credit goes here) : a web server turning a lef off and on. #include const char* ssid = \u00ab\u00a0livebox0\u00a0\u00bb; const char* password = \u00ab\u00a0password\u00a0\u00bb; int ledPin = 2; \/\/ GPIO2 WiFiServer server(80); void setup() { Serial.begin(115200); delay(10); pinMode(ledPin, OUTPUT); digitalWrite(ledPin, LOW); \/\/ Connect to WiFi network Serial.println(); Serial.println(); Serial.print(\u00ab\u00a0Connecting <a href='https:\/\/labalec.fr\/erwan\/?p=1678' class='excerpt-more'>[&#8230;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[83],"class_list":["post-1678","post","type-post","status-publish","format-standard","hentry","category-arduino","tag-esp8266","category-18-id","post-seq-1","post-parity-odd","meta-position-corners","fix"],"_links":{"self":[{"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=\/wp\/v2\/posts\/1678","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1678"}],"version-history":[{"count":6,"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=\/wp\/v2\/posts\/1678\/revisions"}],"predecessor-version":[{"id":1684,"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=\/wp\/v2\/posts\/1678\/revisions\/1684"}],"wp:attachment":[{"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1678"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1678"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1678"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}