/* *ESP8266 IO HTTP *ESTE PROGRAMA FICA LENDO A PORTA 2 (GPIO 2) e a 0 (GPIO 0) de 10 em 10 segundos e informa em *HTML O STATUS. Independe de internet para acesso local. *COINSTALAR ESTE PROGRAMA PARA CADA ESP8266 mudando na parte HTML o nome do sinal *(FECHADO,ABERTO, LIGADO, DESLIGADO, ETC., alem da porta de cada um). *Pode-se mudar tambem o endereço IP e a porta utilizada *duvidas: iot@olintronica.com */ #include #include #include #include "user_interface.h" // criado arquivo necessario para o timer. //#define OUTPUT_PIN 0 /DEFINE PIN GPIO 0 como pino de saida(relé).USO FUTURO #define INPUT_PINA 2 // Define PinA GPIO 2 #define INPUT_PINB 0 // Define pinB como 0 // Define time function for periodic read in of digital input os_timer_t myTimer; #define TIMER_UPDATE 1000 bool inpA = 1; bool inpB = 1; // WiFi parameter const char* ssid = "meuroteadorwifi"; const char* password = "xxxxxxxxxx"; IPAddress ip(192,168,0,xxx); //COLOQUE O IP de TRABALHO DESTE DISPOSITIVO IPAddress gateway(192,168,0,1); //GATEWAY DE CONEXÃO (ALTERE PARA O GATEWAY DO SEU ROTEADOR) IPAddress subnet(255,255,255,0); //MASCARA DE REDE (ALTERE PARA A SUA MÁSCARA DE REDE) ESP8266WebServer server(80xx); String webpage_head, webpage_headA, webpage_highA; String webpage_lowA, webpage_tableoA; String webpage_tablecA,webpage_headB, webpage_highB, webpage_lowB; String webpage_tableoB,webpage_tablecB; String webpage_reflesh; // Start MDNS // If MDNS is supported by client, the ESP8266 can be accessed via // address "http://sensorlocal" MDNSResponder mdns; const char* mdns_name = "APWIFI"; // Setup routine void setup() { pinMode(INPUT_PINA, INPUT); // configura pino A como entrada GPIO 2// pinMode(INPUT_PINB, INPUT); // configura pino B como entrada GPIO 0// Serial.begin(115200); // Configure serial port delay(10); connect_wifi(); // Connect to WiFi // Activate MDNS responder if (mdns.begin(mdns_name, WiFi.localIP())) { Serial.println("MDNS responder started"); } start_httpd(); // Start webserver // Configure timer os_timer_setfn(&myTimer, timerCallback, NULL); os_timer_arm(&myTimer, TIMER_UPDATE, true); } // the loop function runs over and over again forever void loop() { server.handleClient(); } // Timer-controlled read in of digital input void timerCallback(void *pArg) { inpA=digitalRead(INPUT_PINA); inpB=digitalRead(INPUT_PINB); //Serial.print("Read digital input: "); Serial.println ("GPIO 2= "); Serial.println (inpA); Serial.println ("GPIO 0= "); Serial.println(inpB); Serial.println ("-----------------"); } // Conecting to WiFi network void connect_wifi() { Serial.println(); Serial.println(); Serial.println(); Serial.print("Connecting to "); Serial.print(ssid); Serial.print(" "); WiFi.mode(WIFI_STA); // Explicitly set the ESP8266 to be a WiFi-client WiFi.begin(ssid, password); WiFi.config(ip, gateway, subnet); //acrescentado para ip fixo// while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(); Serial.println("WiFi connected"); Serial.print("IP address: "); Serial.println(WiFi.localIP()); Serial.print("MAC: "); Serial.println(WiFi.macAddress()); Serial.println(); } // Setup the webserver void start_httpd() { // -------------INICIO CABEÇALHO COMUM aos 2 ---------------------// webpage_head+="ESP8266 Webserver"; webpage_head+= ""; webpage_head+=""; webpage_head+=""; // -------------INICIO VISUAL ---------------------// webpage_highA+=""; webpage_highB+="

FECHADO"; webpage_lowA+="

ABERTO"; webpage_headA+="

"; webpage_headA+="PORTA NUMERO 1*"; webpage_tablecA+="

FECHADO"; webpage_lowB+="

ABERTO"; webpage_headB+="

"; webpage_headB+="PORTA NUMERO 2 / GAS"; webpage_tablecB+=""; // ----------------FIM de PAGINA ------------------------// server.on("/", [](){ Serial.println("HTTP request"); if (inpA==0 & inpB==0) { //conta 1 e contato 2 abertos// Serial.println("GPIO 2=0 e GPIO 0=0 aterrados "); server.send(200, "text/html",webpage_head+webpage_headA+webpage_lowA+webpage_tablecA+webpage_headB+webpage_lowB+webpage_tablecB); //delay (4000); } if (inpA==1 & inpB==1) { Serial.println("GPIO 2=1 e GPIO 0=0 no ar "); server.send(200, "text/html",webpage_head+webpage_headA+webpage_highA+webpage_tablecA+webpage_headB+webpage_highB+webpage_tablecB); } if (inpA==1 & inpB==0) { //contato 1 // Serial.println("GPIO 2=1 no ar e GPIO 0=0 aterrado "); server.send(200, "text/html",webpage_head+webpage_headA+webpage_highA+webpage_tablecA+webpage_headB+webpage_lowB+webpage_tablecB); //delay(300); //temporiza 300 milisegundos //ESP.restart (); //reinicia o ESP8266 } if (inpA==0 & inpB==1) { //contato 2// Serial.println("GPIO 2=0 aterrado e GPIO 0=0 no ar "); server.send(200, "text/html",webpage_head+webpage_headA+webpage_lowA+webpage_tablecA+webpage_headB+webpage_highB+webpage_tablecB); //delay(300); //temporiza 300 milisegundos //ESP.restart (); //reinicia o ESP8266 } }); server.begin(); Serial.println("Servidor HTTP reiniciado"); }