i compiled a Arduino Sketch for NodeMCU v1.0 (ESP8266-12E) which have no error while i test with verifiy button in Arduino IDE 1.8.6 but i don't know what is the problem when i upload and test it . it [ NodeMCU v1.0 (ESP8266-12E) ] is not showing an static IP address while there is turned ON WiFi hotspot from my android phone. I'm sharing two codes (code 1, code 2) here. Code 2 connect to the WiFi Hotspot but i make changes in code 2 to make code 1 for desired output which is not working. Need help for making proper code.
For better understanding , what i want you can check this uploaded video on Youtube.
Home automation circuit using NodeMCU v1.0 (ESP8266-12E). - YouTube or
by typing in search bar in youtube.com
Home automation circuit using NodeMCU v1.0 (ESP8266-12E).
code 1
code 1
#include
int pin1 = 2; // GPIO 14 (D5)
int pin2 = 3; // GPIO 5 (D1)
int pin3 = 6; // GPIO 5 (D2)
const char* ssid = "wifi";
const char* password = "12345678";
WiFiServer server(80);
void setup() {
Serial.begin(115200);
delay(10);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(6, OUTPUT);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address
Serial.print("Use this URL to connect: ");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.println("/");
}
void loop() {
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
// Wait until the client sends some data
Serial.println("new client");
while(!client.available()){
delay(1);
}
// Read the first line of the request
String request = client.readStringUntil('\r');
Serial.println(request);
client.flush();
// Match the request
int value = LOW;
if (request.indexOf("/pin1=ON") != -1) {
digitalWrite(2, HIGH);
value = HIGH;
}
if (request.indexOf("/pin1=OFF") != -1) {
digitalWrite(2, LOW);
value = LOW;
}
if (request.indexOf("/pin2=ON") != -1) {
digitalWrite(3, HIGH);
value = HIGH;
}
if (request.indexOf("/pin2=OFF") != -1) {
digitalWrite(3, LOW);
value = LOW;
}
if (request.indexOf("/pin3=ON") != -1) {
digitalWrite(6, HIGH);
value = HIGH;
}
if (request.indexOf("/pin3=OFF") != -1) {
digitalWrite(6, LOW);
value = LOW;
}
// Set ledPin according to the request
//digitalWrite(ledPin, value);
// Return the response
// Return the response
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println(""); // do not forget this one
client.println("");
client.println("");
client.println("");
client.println("");
client.println("");
client.println("");
client.println("<body bgcolor = "#f7e6ec">");
client.println("
");
client.println("
Wifi Based Project
");client.println("");
client.println("
");
client.println("");
client.println("<a href="/a"">Data
");
client.println("");
client.println("
");
client.println("");
client.println("Load 1");
client.println("<a href="/l1on"">Turn On ");
client.println("<a href="/l1off"">Turn Off
");
client.println("");
client.println("
");
client.println("");
client.println("Load 2");
client.println("<a href="/l2on"">Turn On ");
client.println("<a href="/l2off"">Turn Off
");
client.println("");
client.println("
");
client.println("");
client.println("Load 3");
client.println("<a href="/l3on"">Turn On ");
client.println("<a href="/l3off"">Turn Off
");
client.println("");
client.println("
");
client.println("");
client.println("
");
client.println("");
client.println("<table border="5">");
client.println("
");
client.println("");
if(digitalRead(pin1)){client.print("Load 1 is ON");}
else{client.print("Load 1 is OFF");}
if(digitalRead(pin2)){client.print("Load 2 is ON");}
else{client.print("Load 2 is OFF");}
if(digitalRead(pin3)){client.print("Load 3 is ON");}
else{client.print("Load 3 is OFF");}
client.println("");
client.println("");
client.println("");
client.println("");
delay(1);
Serial.println("Client disonnected");
Serial.println("");
}
[enter image description here][1]
// if you will upload this code in your NodeMCU v1.0 (ESP8266-12E) , this will
connect with your Android phone. You can consider it, code 2 and above code 1. i'm also sharing code 2 in other post with same title
}