Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2019
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <ESP8266mDNS.h>
  3. #include <ArduinoOTA.h>
  4. #include <ESP8266WebServer.h>
  5. #include <RCSwitch.h>
  6. #include <Ticker.h>
  7. #ifndef STASSID
  8. #define STASSID "//////"
  9. #define STAPSK "//////"
  10. #endif
  11. Ticker timp;
  12. unsigned open = 24000;
  13. unsigned int closed = 11500;
  14. unsigned int second= 1000;
  15. RCSwitch mySwitch = RCSwitch();
  16. int cnt=0;
  17. const char* ssid = STASSID;
  18. const char* password = STAPSK;
  19.  
  20. ESP8266WebServer server(80);
  21.  
  22. const char* www_username = "/////";
  23. const char* www_password = "/////";
  24. String webPage = "";
  25.  
  26.  
  27.  
  28.  
  29. void setup() {
  30.  
  31. webPage += "<h1>Gate</h1><p>Poarta <a href=\"socket1On\"><button>open</button></a>&nbsp;<a href=\"socket1Off\"><button>close</button></a></p>";
  32. webPage += "<p>Gate for a second <a href=\"socket2On\"><button>open</button></a>&nbsp;<a href=\"socket2Off\"><button>close</button></a></p>";
  33. mySwitch.enableTransmit(5);
  34. mySwitch.setProtocol(1);
  35. mySwitch.setPulseLength(419);
  36.  
  37. Serial.begin(115200);
  38. WiFi.mode(WIFI_STA);
  39. WiFi.begin(ssid, password);
  40. if (WiFi.waitForConnectResult() != WL_CONNECTED) {
  41. Serial.println("WiFi Connect Failed! Rebooting...");
  42. delay(1000);
  43. ESP.restart();
  44. }
  45. ArduinoOTA.begin();
  46.  
  47. server.on("/", []() {
  48. if (!server.authenticate(www_username, www_password)) {
  49. return server.requestAuthentication();
  50. }
  51. server.send(200, "text/html", webPage);
  52. });
  53. server.on("/socket1On", [](){
  54. if (!server.authenticate(www_username, www_password)) {
  55. return server.requestAuthentication();
  56. }
  57. server.send(200, "text/html", webPage);
  58. //mySwitch.sendTriState(socket1TriStateOn);
  59. //n=1;
  60. for( uint32_t tStart = millis(); (millis()-tStart) < open; ){
  61. mySwitch.send("open/close binary code");
  62.  
  63.  
  64.  
  65.  
  66. //}
  67. });
  68. server.on("/socket1Off", [](){
  69. if (!server.authenticate(www_username, www_password)) {
  70. return server.requestAuthentication();
  71. }
  72. server.send(200, "text/html", webPage);
  73. //mySwitch.sendTriState(socket1TriStateOff);
  74. for( uint32_t tStart = millis(); (millis()-tStart) < closed; ){
  75. mySwitch.send("open/close binary code");}
  76.  
  77. });
  78. server.on("/socket2On", [](){
  79. /* if (!server.authenticate(www_username, www_password)) {
  80. return server.requestAuthentication();
  81. }*/
  82. server.send(200, "text/html", webPage);
  83. //mySwitch.sendTriState(socket2TriStateOn);
  84. for( uint32_t tStart = millis(); (millis()-tStart) < second; ){
  85. mySwitch.send("open/close binary code");}
  86.  
  87. });
  88. server.on("/socket2Off", [](){
  89. if (!server.authenticate(www_username, www_password)) {
  90. return server.requestAuthentication();
  91. }
  92. server.send(200, "text/html", webPage);
  93. //mySwitch.sendTriState(socket2TriStateOff);
  94. for( uint32_t tStart = millis(); (millis()-tStart) < second; ){
  95. mySwitch.send("open/close binary code");}
  96. });
  97.  
  98. server.begin();
  99.  
  100. Serial.print("Open http://");
  101. Serial.print(WiFi.localIP());
  102. Serial.println("/ in your browser to see it working");
  103. }
  104.  
  105. void loop() {
  106. ArduinoOTA.handle();
  107. server.handleClient();
  108.  
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement