Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- //#include "ESP8266WebServer.h"
- const char* ssid = "#####"; // Nome da Rede
- const char* password = "#####";//Password da rede
- //DEFINIÇÃO DE IP FIXO PARA O NODEMCU
- IPAddress ip(192,168,0,175); //COLOQUE UMA FAIXA DE IP DISPONÍVEL DO SEU ROTEADOR. EX: 192.168.1.110 **** ISSO VARIA, NO MEU CASO É: 192.168.0.175
- 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
- WiFiServer server(80);//Porta 80
- int saida1=D5;
- int saida2=D6;
- int value1 = LOW;
- int value2 = LOW;
- void setup() {
- pinMode(saida1,OUTPUT);// Define o D5 como saída
- //digitalWrite(ledPin1,LOW);//O LED começa desligado
- pinMode(saida2,OUTPUT);//Define o D6 como saída
- //digitalWrite(ledPin1,LOW);//O LED começa desligado
- Serial.begin(115200);
- Serial.println();
- Serial.print("tentando conexão ");
- Serial.println( ssid );
- WiFi.begin(ssid, password); //PASSA OS PARÂMETROS PARA A FUNÇÃO QUE VAI FAZER A CONEXÃO COM A REDE SEM FIO
- WiFi.config(ip, gateway, subnet); //PASSA OS PARÂMETROS PARA A FUNÇÃO QUE VAI SETAR O IP FIXO NO NODEMCU
- Serial.println();
- Serial.println("Conectando");
- while( WiFi.status() != WL_CONNECTED ){//Enquanto a ligação não for efectuada com sucesso apresenta a mensagem, tentando conectar
- delay(500);
- Serial.println("tentando conectar…");
- }
- Serial.println();
- Serial.println("Conectado com sucesso!");//Se a ligação é efectuada com sucesso
- Serial.print("Endereço IP : ");
- Serial.println(WiFi.localIP() );
- server.begin();// Comunicação com o servidor
- Serial.println("Servidor iniciado");
- Serial.print("Use esse IP para se conectar ao servidor ");
- Serial.print("http://");
- Serial.print(WiFi.localIP());//Abrindo o Brower com este IP acedemos á pagina HTML de controlo dos LED´s, sendo que este IP só está disponível na rede à qual o ESP8266 se encontra ligado
- Serial.println("/");
- }
- void loop() {
- WiFiClient client = server.available();// Verificação se o cliente está conectado
- if (!client) { return; }
- // Espera até o cliente enviar dados
- Serial.println("Hello New client");//Apresenta esta mensagem quando o cliente se liga ao servidor
- while(!client.available()){
- delay(1); }
- // Ler a primeira linha do pedido
- String request = client.readStringUntil('\r');
- Serial.println(request);
- // Operação do pedido
- if (request.indexOf("/LED1=ON") != -1) {
- digitalWrite(saida1, HIGH);// Se o pedido no LedPin1 for LED=ON, acende o LED
- value1 = HIGH; }
- else if (request.indexOf("/LED1=OFF") != -1) {
- digitalWrite(saida1, LOW);// Se o pedido no LedPin1 for LED=OFF, apaga o LED
- value1 = LOW; }
- if (request.indexOf("/LED2=ON") != -1) {
- digitalWrite(saida2, HIGH);// Se o pedido no LedPin2 for LED=ON, acende o LED
- value2 = HIGH; }
- else if (request.indexOf("/LED2=OFF") != -1) {
- digitalWrite(saida2, LOW);// Se o pedido no LedPin2 for LED=OFF, apaga o LED
- value2 = LOW;}
- // Inicialização da página HTML
- // Retorno do resposta
- client.println("HTTP/1.1 200 OK");
- client.println("Content-Type: text/html");
- client.println(""); //não esqueça esse aqui
- client.println("<!DOCTYPE HTML>"); //inicio html
- client.println("<html>");
- client.println("<head>");
- // client.println("<meta name=\"viewport\" content=\"width=device-width, user-scalable=no\"/>");
- client.println("<title>Acionamento remoto</title>"); // da o titulo da aba
- client.println("<style type=text/css>"); // estilo css
- // client.println("<body style=height:100%; width:100%;>");
- //client.println("body{"); // inicio do corpo da imagem de fundo não esquecer do {
- //client.println("background: url(https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRE4_yFuDoeyA1kIWW7p6b9tMIJo21aIX8c1TFKyfYsLLFgJ4aC");// define a imagem de plano de fundo
- //client.println("no-repeat"); // não repete a imagem de fundo
- // client.println("700px 100px}"); // define a posição da imagem de fundo não esquecer do }
- client.println("</style>"); // fim do estilo da imagem de fundo
- client.println("</head>");//final do head
- client.println("<body style=background-color:#ADD8E6>"); //DEFINE A COR DE FUNDO DA PÁGINA
- client.println("<body>");//inicio do body
- client.println("<h1>Monitoramento Remoto</h1>");
- client.print("Estado da saida 1: ");
- if(value1 == HIGH) {// Se está ligado apresenta “on”
- client.print("ligado");
- } else {
- client.print("desligado");// Se está desligado apresenta “Off”
- }
- client.println("<br><br>");
- client.println("<a href=\"/LED1=ON\"\"><button>ligar </button></a>");// Ligar o LED1 corresponde Turn On
- client.println("<a href=\"/LED1=OFF\"\"><button>desligar </button></a><br />"); // Desligar o LED1 corresponde Turn Off
- client.println("<br><br>");
- client.print("Estado da saida 2: ");
- if(value2 == HIGH) {// Se está ligado apresenta “on”
- client.print("ligado");
- } else {
- client.print("desligado");// Se está desligado apresenta “Off”
- }
- client.println("<br><br>");
- client.println("<a href=\"/LED2=ON\"\"><button>ligar </button></a>");// Ligar o LED2 corresponde Turn On
- client.println("<a href=\"/LED2=OFF\"\"><button>desligar </button></a><br />"); // Desligar o LED2 corresponde Turn Off
- client.println("<br><br>");
- client.println("<meta http-equiv=refresh content=5;URL='//192.168.0.175/'>");// atualiza a pagina a cada 5s
- client.println("</body>");//fim do body
- client.println("</html>");//fim do html
- delay(1);
- Serial.println("Cliente desconectado");
- Serial.println("");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement