Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "ESP8266WiFi.h"
- #include "ESP8266WebServer.h"
- const char* ssid = "Pixel"; // Nome da Rede
- const char* password = "djmalcom";//Password da rede
- int ledPin1=14; // GPIO13 do ESP8266 e D7 do NodeMCU
- int ledPin2=12; // GPIO2 do ESP8266 e D4 do NodeMCU
- WiFiServer server(80);//Porta 80
- void setup() {
- pinMode(ledPin1,OUTPUT);// Define o D7 como saída
- digitalWrite(ledPin1,LOW);//O LED começa desligado
- pinMode(ledPin2,OUTPUT);//Define o D4 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);
- 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");
- // Print the IP address
- 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() {
- // Verificação se o cliente está conectado
- WiFiClient client = server.available();
- if (!client) {// Verifica se o cliente está conectado ao servidor, executa este ciclo até estar conectado
- 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);
- client.flush();
- // Operação do pedido
- int value1 = LOW;
- int value2 = LOW;
- if (request.indexOf("/LED1=ON") != -1) {
- digitalWrite(ledPin1, HIGH);// Se o pedido no LedPin1 for LED=ON, acende o LED
- value1 = HIGH;
- }
- else if (request.indexOf("/LED1=OFF") != -1) {
- digitalWrite(ledPin1, LOW);// Se o pedido no LedPin1 for LED=OFF, apaga o LED
- value1 = LOW;
- }
- if (request.indexOf("/LED2=ON") != -1) {
- digitalWrite(ledPin2, HIGH);// Se o pedido no LedPin2 for LED=ON, acende o LED
- value2 = HIGH;
- }
- else if (request.indexOf("/LED2=OFF") != -1) {
- digitalWrite(ledPin2, LOW);// Se o pedido no LedPin2 for LED=OFF, apaga o LED
- value2 = LOW;
- }
- // Set ledPin according to the request
- //digitalWrite(ledPin, value);
- // Inicialização da página HTML
- // Retorno do resposta
- client.println("HTTP/1.1 200 OK");
- client.println("Content-Type: text/html");
- client.println(""); // do not forget this one
- client.println("<!DOCTYPE HTML>"); //inicio html
- client.println("<html>");
- client.println("<head>");
- client.println("<title>supervisorio de botoes</title>"); // da o titulo da aba
- client.println("<style type=text/css>"); // estilo css
- 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>");
- 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("</html>");
- delay(1);
- Serial.println("Client disonnected");
- Serial.println("");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement