How to make page index.html send request for NodeMCU 1.0
Posted: Tue Jan 03, 2017 1:38 pm
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
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");
}