Home Dynamic IoT Module Protocol v1.ino 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #include <ESP8266WiFi.h>
  2. #include <WiFiClient.h>
  3. #include <ESP8266WebServer.h>
  4. const char *ssid = "YOUR WIFI NAME HERE";
  5. const char *password = "WIFI PASSWORD HERE";
  6. char wiFiHostname[] = "Home Dynamic";
  7. ESP8266WebServer server(80);
  8. const char* www_username = "homedynamic";
  9. const char* www_password = "homedynamic";
  10. const int led = 2; //GPIO2
  11. void handleRoot() {
  12. if(!server.authenticate(www_username, www_password))
  13. return server.requestAuthentication();
  14. int size=1000;
  15. char temp[size];
  16. int sec = millis() / 1000;
  17. int min = sec / 60;
  18. int hr = min / 60;
  19. snprintf ( temp, size,
  20. "<p>Wifi Enabled Switch_On:switch|on_Off:switch|on</p>");
  21. server.send ( 200, "text/html", temp );
  22. }
  23. void setup() {
  24. Serial.begin(9600);
  25. delay(1000);
  26. WiFi.softAP(ssid, password);
  27. IPAddress myIP = WiFi.softAPIP();
  28. pinMode(led, OUTPUT);
  29. digitalWrite ( led, HIGH );
  30. //URLs available to query
  31. while (WiFi.status() != WL_CONNECTED) {
  32. delay(500);
  33. Serial.print(".");
  34. }
  35. server.on("/info", handleRoot);
  36. server.on ( "/switch/on", turnON );
  37. server.on ( "/switch/off", turnOFF );
  38. server.begin();
  39. Serial.println("Home Dynamic server started");
  40. Serial.println(WiFi.localIP());
  41. }
  42. void turnON(){
  43. if(!server.authenticate(www_username, www_password))
  44. return server.requestAuthentication();
  45. digitalWrite ( led, HIGH );
  46. int size=1000;
  47. char temp[size];
  48. int sec = millis() / 1000;
  49. int min = sec / 60;
  50. int hr = min / 60;
  51. snprintf ( temp, size,
  52. "<p>ESP-1 is now on</p>");
  53. server.send ( 200, "text/html", temp);
  54. }
  55. void turnOFF(){
  56. if(!server.authenticate(www_username, www_password))
  57. return server.requestAuthentication();
  58. digitalWrite ( led, LOW );
  59. int size=1000;
  60. char temp[size];
  61. int sec = millis() / 1000;
  62. int min = sec / 60;
  63. int hr = min / 60;
  64. snprintf ( temp, size,
  65. "<p>ESP-1 is now off</p>"
  66. );
  67. server.send ( 200, "text/html", temp);
  68. }
  69. void loop() {
  70. server.handleClient();
  71. }