I am working on an ESP8266 ESP-12E WiFi Module to turn an LED on and off.
Yes, I was able to turn on/turn off the LED with the help of Wifiserver and NodeMCU.
The problem is I want to connect the NodeMCU with my local area network on the PC. My PC does not connect with Wi-Fi, only an Ethernet connection is present.
How can I connect the NodeMCU with my local area network connection, not with the mobile hotspot?
This is my code:
Moderators note : used code tags for the code
Yes, I was able to turn on/turn off the LED with the help of Wifiserver and NodeMCU.
The problem is I want to connect the NodeMCU with my local area network on the PC. My PC does not connect with Wi-Fi, only an Ethernet connection is present.
How can I connect the NodeMCU with my local area network connection, not with the mobile hotspot?
This is my code:
Code:
#include <ESP8266WebServer.h>
/*Put your SSID & Password*/
const char* ssid = "ssid"; // Enter SSID here
const char* password = "password"; //Enter Password here
ESP8266WebServer server(80);
uint8_t LEDpin = D2;
bool LEDstatus = LOW;
void setup() {
Serial.begin(9600);
delay(100);
pinMode(LEDpin, OUTPUT);
Serial.println("Connecting to ");
Serial.println("ssid");
//Connect to your local wi-fi network
WiFi.begin( "ssid", "password");
//check wi-fi is connected to wi-fi network
while (WiFi.status() != WL_CONNECTED)
{
delay(1000);
Serial.print (".");
}
Serial.println("");
Serial.println("WiFi connected..!");
Serial.print("Got IP: ");
Serial.println(WiFi.localIP());
server.on("/", handle_OnConnect);
server.on("/ledon", handle_ledon);
server.on("/ledoff", handle_ledoff);
server.onNotFound(handle_NotFound);
server.begin();
Serial.println("HTTP server started");
}
void loop() {
server.handleClient();
if(LEDstatus)
digitalWrite(LEDpin, HIGH);
else
digitalWrite(LEDpin, LOW);
}
void handle_OnConnect() {
LEDstatus = LOW;
server.send(200, "text/html", SendHTML(false));
}
void handle_ledon() {
LEDstatus = HIGH;
server.send(200, "text/html", SendHTML(true));
}
void handle_ledoff() {
LEDstatus = LOW;
server.send(200, "text/html", SendHTML(false));
}
void handle_NotFound(){
server.send(404, "text/plain", "Not found");
}
String SendHTML(uint8_t led){
String ptr = "<!DOCTYPE html>\n";
ptr +="<html>\n";
ptr +="<head>\n";
ptr +="<title>LED Control</title>\n";
ptr +="</head>\n";
ptr +="<body>\n";
ptr +="<h1>LED</h1>\n";
ptr +="<p>Click to switch LED on and off.</p>\n";
ptr +="<form method=\"get\">\n";
if(led)
ptr +="<input type=\"button\" value=\"LED OFF\" onclick=\"window.location.href='/ledoff'\">\n";
else
ptr +="<input type=\"button\" value=\"LED ON\" onclick=\"window.location.href='/ledon'\">\n";
ptr +="</form>\n";
ptr +="</body>\n";
ptr +="</html>\n";
return ptr;
}
Attachments
-
2 MB Views: 18