Advertisement
Guest User

SmartTable

a guest
Jan 25th, 2020
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. #include "Arduino.h"
  2. #include <ESP8266WiFi.h>
  3. #include <WiFiClient.h>
  4. #include <ESP8266WebServer.h>
  5.  
  6. //0x0A, 0x2B, 0x49, 0x50, 0x44, 0x2C, 0x30, 0x2C, 0x34, 0x3A,
  7. byte relON1[] = {0xA0, 0x01, 0x01, 0xA2}; //Hex command to send to serial for open relay
  8. byte relOFF1[] = {0xA0, 0x01, 0x00, 0xA1}; //Hex command to send to serial for close relay
  9. byte relON2[] = {0xA0, 0x02, 0x01, 0xA3}; //Hex command to send to serial for open relay
  10. byte relOFF2[] = {0xA0, 0x02, 0x00, 0xA2}; //Hex command to send to serial for close relay
  11.  
  12. int ledState = false;
  13. unsigned long previousMillis = 0;
  14. const long interval = 2000; // 2 seconds
  15. ESP8266WebServer server(80);
  16.  
  17. //0 = idle
  18. //1 = up
  19. //2 = down
  20. unsigned int state = 0;
  21. unsigned long moveMillisLeft = 0;
  22.  
  23. //---------------------------------------------------------------
  24. //Our HTML webpage contents in program memory
  25. const char MAIN_page[] PROGMEM = R"=====(
  26. <!DOCTYPE html>
  27. <html>
  28. <body>
  29. <center>
  30. <h1>WiFi LED on off demo: 1</h1><br>
  31. Ciclk to turn <a href="ledOn">UP</a><br>
  32. Ciclk to turn <a href="ledOff">DOWN</a><br>
  33. </center>
  34.  
  35. </body>
  36. </html>
  37. )=====";
  38.  
  39. const char* ssid = "Hogwarts";
  40. const char* password = "helahavetstormar";
  41.  
  42. void handleRoot() {
  43. Serial.println("You called root page");
  44. String s = MAIN_page; //Read HTML contents
  45. server.send(200, "text/html", s); //Send web page
  46. }
  47.  
  48. void handleLEDon() {
  49. moveMillisLeft = millis() + server.arg("time").toInt();
  50. state = 1;
  51. Serial.write(relON1, sizeof(relON1));
  52. Serial.flush();
  53. delay(100);
  54. server.send(200, "text/html", "LED is ON"); //Send ADC value only to client ajax request
  55. }
  56.  
  57. void handleLEDoff() {
  58. moveMillisLeft = millis() + server.arg("time").toInt();
  59. state = 2;
  60. Serial.write(relON2, sizeof(relON2));
  61. Serial.flush();
  62. delay(100);
  63. server.send(200, "text/html", "LED is OFF"); //Send ADC value only to client ajax request
  64. }
  65. //==============================================================
  66. // SETUP
  67. //==============================================================
  68. void setup(void){
  69. Serial.begin(115200);
  70.  
  71. WiFi.begin(ssid, password); //Connect to your WiFi router
  72.  
  73. // Wait for connection
  74. while (WiFi.status() != WL_CONNECTED) {
  75. delay(500);
  76. Serial.print(".");
  77. }
  78.  
  79. Serial.println("");
  80. Serial.print("Connected to ");
  81. Serial.println(ssid);
  82. Serial.print("IP address: ");
  83. Serial.println(WiFi.localIP()); //IP address assigned to your ESP
  84.  
  85. server.on("/", handleRoot); //Which routine to handle at root location. This is display page
  86. server.on("/upp", handleLEDon); //as Per <a href="ledOn">, Subroutine to be called
  87. server.on("/ner", handleLEDoff);
  88.  
  89. server.begin(); //Start server
  90. Serial.println("HTTP server started");
  91. }
  92.  
  93. void loop()
  94. {
  95. server.handleClient();
  96. unsigned long currentMillis = millis();
  97. if(currentMillis > moveMillisLeft && state != 0) {
  98. if (state == 1) {
  99. Serial.write(relOFF1, sizeof(relOFF1));
  100. Serial.flush();
  101. delay(100);
  102. } else if (state == 2) {
  103. Serial.write(relOFF2, sizeof(relOFF2));
  104. Serial.flush();
  105. delay(100);
  106. }
  107. state = 0;
  108. }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement