Electrical – How to connect the ESP 8266 to local area network

digital-communicationsesp8266ethernetmicrocontrollerwifi

enter image description hereI 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:

#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;
}

Best Answer

If you have a WiFi Access Point (aka WiFi router) on your local LAN then you can connect your ESP8266 to that WiFi network and then its reachable from your PC via a IP address.

The IP address is in most cases automatically configured.

There is also support for "Multicast DNS" and if you use that there is no need to remember IP addresses, just use the name that you put into your Code for your ESP8266.

Ref. Multicast DNS, https://tttapa.github.io/ESP8266/Chap08%20-%20mDNS.html

Related Topic