Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WebServer.h>
- #include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
- //Creditos Pedro Minatel
- //IP do ESP (para voce acessar pelo browser)
- IPAddress ip(192, 168, 0, 50);
- //IP do roteador da sua rede wifi
- IPAddress gateway(192, 168, 0, 1);
- //Mascara de rede da sua rede wifi
- IPAddress subnet(255, 255, 255, 0);
- //Inicializacao do WiFiManager
- WiFiManager wifiManager;
- //Inicializacao so servidor http na porta 80
- WiFiServer server(80);
- //Status da GPIO
- uint8_t status_gpio = 0;
- const int pin = 12;
- const int fimc1 = 13;
- const int fimc2 = 16;
- void setup() {
- //Configura a serial
- Serial.begin(115200);
- // configura os pinos 13 e 16 para os fim de curso
- pinMode(fimc1, INPUT_PULLUP);
- pinMode(fimc2, INPUT_PULLUP);
- //Configura a GPIO como saida
- pinMode(pin, OUTPUT);
- //Coloca a GPIO em sinal logico baixo
- digitalWrite(pin, LOW);
- WiFi.config(ip, gateway, subnet);
- //Define o auto connect e o SSID do modo AP
- wifiManager.setConfigPortalTimeout(180);
- wifiManager.autoConnect("MeuWebServer");
- //Log na serial se conectar
- Serial.println("Conectado");
- //Inicia o webserver de controle da GPIO
- server.begin();
- }
- void reset_config(void) {
- //Reset das definicoes de rede
- wifiManager.resetSettings();
- delay(1500);
- ESP.reset();
- }
- void loop() {
- //Aguarda uma nova conexao
- WiFiClient client = server.available();
- if (!client) {
- return;
- }
- Serial.println("Nova conexao requisitada...");
- while (!client.available()) {
- delay(1);
- }
- Serial.println("Nova conexao OK...");
- //Le a string enviada pelo cliente
- String req = client.readStringUntil('\r');
- //Mostra a string enviada
- Serial.println(req);
- //Limpa dados/buffer
- client.flush();
- const int efimc1 = digitalRead(fimc1);
- const int efimc2 = digitalRead(fimc2);
- Serial.print("Estado Fim curso 1");
- Serial.println(efimc1);
- Serial.print("Estado Fim curso 2");
- Serial.println(efimc1);
- //Trata a string do cliente em busca de comandos
- if (req.indexOf("abre") != -1) {
- if (efimc1 == LOW) {
- yield();
- digitalWrite(pin, HIGH);
- status_gpio = HIGH;
- }
- if (efimc2 == LOW) {
- yield();
- digitalWrite(pin, LOW);
- status_gpio = LOW;
- }
- }
- //para resetar as configurações de rede wifi para poder habilitar o webconfig
- //reset_config();
- else if (req.indexOf("fecha") != -1) {
- if (efimc2 == LOW) {
- yield();
- digitalWrite(pin, HIGH);
- status_gpio = HIGH;
- }
- if (efimc1 == LOW) {
- yield();
- digitalWrite(pin, LOW);
- status_gpio = LOW;
- }
- }
- else {
- Serial.println("Requisicao invalida");
- }
- //Prepara a resposta para o cliente
- String buf = "";
- buf += "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n";
- buf += "<html lang=\"en\"><head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1, user-scalable=no\"/>\r\n";
- buf += "<title>Porta da Escada</title>";
- buf += "<style>.c{text-align: center;} div,input{padding:5px;font-size:1em;} input{width:80%;} body{text-align: center;font-family:verdana;} button{border:0;border-radius:0.3rem;background-color:#1fa3ec;color:#fff;line-height:2.4rem;font-size:1.2rem;width:100%;} .q{float: right;width: 64px;text-align: right;}</style>";
- buf += "</head>";
- buf += "<h3>Porta da Escada</h3>";
- //De acordo com o status da GPIO envia o comando
- if (status_gpio)
- buf += "<div><h4>Porta</h4><a href=\"?function=abre\"><button>Fecha</button></a></div>";
- else
- buf += "<div><h4>Porta</h4><a href=\"?function=fecha\"><button>Abre</button></a></div>";
- buf += "<h4>Criado por Pedro Minatel</h4>";
- buf += "</html>\n";
- //Envia a resposta para o cliente
- client.print(buf);
- client.flush();
- client.stop();
- Serial.println("Cliente desconectado!");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement