iot_esp8266_cloud_webserver.ino 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include <ESP8266WiFi.h>
  2. #include <WiFiClient.h>
  3. #include <ESP8266WebServer.h>
  4. #include <ESP8266mDNS.h>
  5. #include <SimpleTimer.h>
  6. MDNSResponder mdns;
  7. // Replace with your network credentials
  8. const char* ssid = "hazard18";
  9. const char* password = "pu1f0xylu1";
  10. // Host
  11. const char* host = "dweet.io";
  12. SimpleTimer timer;
  13. ESP8266WebServer server(80);
  14. // Pin
  15. #define LIGHTSENSOR A0
  16. String webPage = "";
  17. int gpio2_pin = 2;
  18. boolean done = false;
  19. void setup(void){
  20. webPage += "<h1>ESP8266 Web Server</h1><p>Socket #1 <a href=\"socket1On\"><button>ON</button></a>&nbsp;<a href=\"socket1Off\"><button>OFF</button></a></p>";
  21. webPage += "<p>Socket #2 <a href=\"socket2On\"><button>ON</button></a>&nbsp;<a href=\"socket2Off\"><button>OFF</button></a></p>";
  22. // prepare timer
  23. timer.setInterval(5000, reportLightIntensity);
  24. Serial.begin(115200);
  25. delay(100);
  26. WiFi.begin(ssid, password);
  27. Serial.println("Connecting...");
  28. // Wait for connection
  29. while (WiFi.status() != WL_CONNECTED) {
  30. delay(500);
  31. Serial.print(".");
  32. }
  33. Serial.println("");
  34. Serial.print("Connected to ");
  35. Serial.println(ssid);
  36. Serial.print("IP address: ");
  37. Serial.println(WiFi.localIP());
  38. if (mdns.begin("esp8266", WiFi.localIP())) {
  39. Serial.println("MDNS responder started");
  40. }
  41. // preparing GPIOs
  42. pinMode(gpio2_pin, OUTPUT);
  43. digitalWrite(gpio2_pin, LOW);
  44. delay(1000);
  45. server.on("/", [](){
  46. server.send(200, "text/html", webPage);
  47. });
  48. server.on("/socket2On", [](){
  49. server.send(200, "text/html", webPage);
  50. digitalWrite(gpio2_pin, HIGH);
  51. delay(1000);
  52. });
  53. server.on("/socket2Off", [](){
  54. server.send(200, "text/html", webPage);
  55. digitalWrite(gpio2_pin, LOW);
  56. delay(1000);
  57. });
  58. server.begin();
  59. Serial.println("HTTP server started");
  60. }
  61. void reportLightIntensity(){
  62. // Use WiFiClient class to create TCP connections
  63. WiFiClient client;
  64. const int httpPort = 80;
  65. if (!client.connect(host, httpPort)) {
  66. Serial.println("connection failed");
  67. return;
  68. }
  69. int h = (unsigned int) analogRead(LIGHTSENSOR);
  70. Serial.println("a0 val: " + String(h));
  71. // This will send the request to the server
  72. client.print(String("GET /dweet/for/esp1881?light_intensity=") + String(h) + " HTTP/1.1\r\n" +
  73. "Host: " + host + "\r\n" +
  74. "Connection: close\r\n\r\n");
  75. delay(10);
  76. // Read all the lines of the reply from server and print them to Serial
  77. while(client.available()){
  78. String line = client.readStringUntil('\r');
  79. Serial.print(line);
  80. }
  81. Serial.println();
  82. Serial.println("closing connection");
  83. }
  84. void loop(void){
  85. server.handleClient();
  86. timer.run();
  87. }