/* Create a WiFi access point and provide a web server on it. ** For more details see http://42bots.com. */ #include #include #include // biblioteca para leitura de botões Bounce debouncer = Bounce(); //ip fixo do modo AP-Router IPAddress apIP(192, 168, 1, 1); // Defining a static IP address: local & gateway // Default IP in AP mode is 192.168.4.1 /* This are the WiFi access point settings. Update them to your likin */ const char *ssid = "ESP8266"; const char *password = "esp"; // Define a web server at port 80 for HTTP ESP8266WebServer server(80); // variaveis bool Rele12state; bool LED13state; //byte buttonState = 0; bool rele = 1; // variavel de controle do rele // Tracks the last time event fired unsigned long previousMillis = 0; void readButton() {// lê botão Sonoff basic debouncer.update(); // Executa o algorítimo de tratamento; //buttonState = digitalRead(0); if ( debouncer.fell() ) { rele = !rele; Serial.println(rele); } } //aciona a os pinos em intervalos alternados void executeTimer(const unsigned int onTime, const unsigned int offTime) { delay(100); unsigned int interval = onTime; // Grab snapshot of current time, this keeps all timing // consistent, regardless of how much code is inside the next if-statement unsigned long currentMillis = millis(); // Compare to previous capture to see if enough time has passed if ((unsigned long)(currentMillis - previousMillis) >= interval) { // Change wait interval, based on current LED state if (LED13state && Rele12state) { // LED is currently on, set time to stay off interval = offTime; } else { // LED is currently off, set time to stay on interval = onTime; Serial.println("ligado"); } // Toggle the LED's state, Rele12state = !(Rele12state); LED13state = !(LED13state); // Save the current time to compare "later" previousMillis = currentMillis; } } void handleRoot() { // monta a pagina principal do webserver rele = server.arg("rele").toInt(); //verifica se foi precionado o botão /* Dynamically generate the LED toggle link, based on its current state (on or off)*/ char ledText[80]; if (rele) { //alterna o botão strcpy(ledText, ""); } else { strcpy(ledText, ""); } // Build an HTML page to display on the web-server root address char html[1000]; //monta o cabeçalho do html snprintf ( html, 1000, "\ \ \ ESP8266 WiFi Network\ \ \ \

ESP8266 Wi-Fi Access Point

\

%s

\ \ ", ledText // altera o texto do status do rele ); Serial.println(rele); server.send ( 200, "text/html", html ); } void handleNotFound() { // gera mensagem de erro digitalWrite ( 13, 0 ); String message = "File Not Found\n\n"; message += "URI: "; message += server.uri(); message += "\nMethod: "; message += ( server.method() == HTTP_GET ) ? "GET" : "POST"; message += "\nArguments: "; message += server.args(); message += "\n"; for ( uint8_t i = 0; i < server.args(); i++ ) { message += " " + server.argName ( i ) + ": " + server.arg ( i ) + "\n"; } server.send ( 404, "text/plain", message ); digitalWrite ( 13, 1 ); //turn the built in LED on pin DO of NodeMCU off } void setup() { // inicialização do código pinMode ( 12, OUTPUT ); pinMode ( 13, OUTPUT ); digitalWrite ( 12, 0 ); debouncer.attach(0, INPUT); // Informa que o tratamento de debouce será feito no pino 8; debouncer.interval(25); // Seta o intervalo de trepidação delay(500); Serial.begin(115200); Serial.println(); Serial.println("Configuring access point..."); //set-up the custom IP address WiFi.mode(WIFI_AP_STA); //define o modo AP (off-line) WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0)); // subnet FF FF FF 00 /* You can remove the password parameter if you want the AP to be open. */ WiFi.softAP(ssid, password); IPAddress myIP = WiFi.softAPIP(); Serial.print("AP IP address: "); Serial.println(myIP); server.on ( "/", handleRoot ); server.on ( "/rele=1", handleRoot ); server.on ( "/rele=0", handleRoot); server.on ( "/inline", []() { server.send ( 200, "text/plain", "this works as well" ); } ); server.onNotFound ( handleNotFound ); server.begin(); Serial.println("HTTP server started"); } void loop() { // executa o código digitalWrite(13, LED13state); digitalWrite(12, Rele12state); readButton(); if (rele == 1) { executeTimer(300000, 400000); // time on, time off } else { Rele12state = false; LED13state = true; yield(); } server.handleClient(); }