Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <ESP8266mDNS.h>
- #include <ArduinoOTA.h>
- #include <ESP8266WebServer.h>
- #include <RCSwitch.h>
- #include <Ticker.h>
- #ifndef STASSID
- #define STASSID "//////"
- #define STAPSK "//////"
- #endif
- Ticker timp;
- unsigned open = 24000;
- unsigned int closed = 11500;
- unsigned int second= 1000;
- RCSwitch mySwitch = RCSwitch();
- int cnt=0;
- const char* ssid = STASSID;
- const char* password = STAPSK;
- ESP8266WebServer server(80);
- const char* www_username = "/////";
- const char* www_password = "/////";
- String webPage = "";
- void setup() {
- webPage += "<h1>Gate</h1><p>Poarta <a href=\"socket1On\"><button>open</button></a> <a href=\"socket1Off\"><button>close</button></a></p>";
- webPage += "<p>Gate for a second <a href=\"socket2On\"><button>open</button></a> <a href=\"socket2Off\"><button>close</button></a></p>";
- mySwitch.enableTransmit(5);
- mySwitch.setProtocol(1);
- mySwitch.setPulseLength(419);
- Serial.begin(115200);
- WiFi.mode(WIFI_STA);
- WiFi.begin(ssid, password);
- if (WiFi.waitForConnectResult() != WL_CONNECTED) {
- Serial.println("WiFi Connect Failed! Rebooting...");
- delay(1000);
- ESP.restart();
- }
- ArduinoOTA.begin();
- server.on("/", []() {
- if (!server.authenticate(www_username, www_password)) {
- return server.requestAuthentication();
- }
- server.send(200, "text/html", webPage);
- });
- server.on("/socket1On", [](){
- if (!server.authenticate(www_username, www_password)) {
- return server.requestAuthentication();
- }
- server.send(200, "text/html", webPage);
- //mySwitch.sendTriState(socket1TriStateOn);
- //n=1;
- for( uint32_t tStart = millis(); (millis()-tStart) < open; ){
- mySwitch.send("open/close binary code");
- //}
- });
- server.on("/socket1Off", [](){
- if (!server.authenticate(www_username, www_password)) {
- return server.requestAuthentication();
- }
- server.send(200, "text/html", webPage);
- //mySwitch.sendTriState(socket1TriStateOff);
- for( uint32_t tStart = millis(); (millis()-tStart) < closed; ){
- mySwitch.send("open/close binary code");}
- });
- server.on("/socket2On", [](){
- /* if (!server.authenticate(www_username, www_password)) {
- return server.requestAuthentication();
- }*/
- server.send(200, "text/html", webPage);
- //mySwitch.sendTriState(socket2TriStateOn);
- for( uint32_t tStart = millis(); (millis()-tStart) < second; ){
- mySwitch.send("open/close binary code");}
- });
- server.on("/socket2Off", [](){
- if (!server.authenticate(www_username, www_password)) {
- return server.requestAuthentication();
- }
- server.send(200, "text/html", webPage);
- //mySwitch.sendTriState(socket2TriStateOff);
- for( uint32_t tStart = millis(); (millis()-tStart) < second; ){
- mySwitch.send("open/close binary code");}
- });
- server.begin();
- Serial.print("Open http://");
- Serial.print(WiFi.localIP());
- Serial.println("/ in your browser to see it working");
- }
- void loop() {
- ArduinoOTA.handle();
- server.handleClient();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement