The use of the ESP8266 in the world of IoT

User avatar
By cyberlacs
#60324 I have the following code that works perfectly, if I type in the browser manually http: // http: //192.168.1.49/on or http: // http: //192.168.1.49/off the commands turn on and off the Relay.

I would like to make a page "index.html" that has a link or a button that sends
Http: // IP-esp8266-001 / pin4-on or pin4-off
Http: // IP-esp8266-002 / pin5-on or pin5-off


How could I do this?

I'm waiting

Code below

Code: Select all#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>

const char* ssid = "InvasaoKill+777";
const char* password = "MassaDeTomate183183";

ESP8266WebServer server(80);

int ledPin = 16;
bool ledState = LOW;

//IP do ESP (para voce acessar pelo browser)
IPAddress ip(192, 168, 1, 49);

//IP do roteador da sua rede wifi
IPAddress gateway(192, 168, 1, 1);

//Mascara de rede da sua rede wifi
IPAddress subnet(255, 255, 255, 0);

void setup() {

  pinMode(ledPin, OUTPUT);
 
  Serial.begin(115200);
 
  WiFi.config(ip, gateway, subnet);
  WiFi.begin(ssid, password); //Connect to the WiFi network
 
  while (WiFi.status() != WL_CONNECTED) { //Wait for connection
       delay(500);
       Serial.println("Waiting to connect…");
  }
 
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP()); //Print the local IP
 
  server.on("/on", turnOn);         //Associate the handler function to the path
  server.on("/off", turnOff);        //Associate the handler function to the path
  server.on("/toggle", toggle);   //Associate the handler function to the path

  server.begin(); //Start the server
  Serial.println("Server listening");
}

void loop() {
  server.handleClient();
}

void turnOn()
  ledState = HIGH;
  digitalWrite(ledPin, ledState);
  server.send(200, "text/plain", "LED on");
}

void turnOff(){
  ledState = LOW;
  digitalWrite(ledPin, ledState);
  server.send(200, "text/plain", "LED off");
}

void toggle(){
  ledState = !ledState;
  digitalWrite(ledPin, ledState);
  server.send(200, "text/plain", "LED toggled");
}
User avatar
By cyberlacs
#60331 I did what you said, but it happens when I click on the link, I'm redirected to the destination, then the links disappear.

Does it have to execute the link and keep on the page, this would have to be in javascript.

I'm waiting.

Code: Select all<html>
   <head>
      <title>ESP8266 LED Control</title>
   </head>
   <body>
      <a href="http://192.168.1.49/on">On</a>
      <a href="http://192.168.1.49/off">Off</a>
   </body>
</html>
User avatar
By cyberlacs
#60336 Look now I click on the link I am not redirected but in this case I did not succeed, the command does not trigger the NodeMCU

Code: Select all<html>
   <head>
      <title>ESP8266 LED Control</title>
      
      <script language="JavaScript">
         var myURL='';
         function url1() {
            var myURL = 'http://192.168.1.49/on';
            myMultiLink();
         }
      </script>
      
   </head>
   <body>
      <a onclick="function url1()" href="javascript:void(0); return false">On</a>
   </body>
</html>