-1

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.

code 1

#include <ESP8266WiFi.h>


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("<!DOCTYPE HTML>");
  client.println("<html>");
  client.println("<head>");
  client.println("<meta name='apple-mobile-web-app-capable' content='yes' />");
  client.println("<meta name='apple-mobile-web-app-status-bar-style' content='black-translucent' />");
  client.println("</head>");
  client.println("<body bgcolor = \"#f7e6ec\">");
  client.println("<hr/><hr>");
  client.println("<h4><center>Wifi Based Project</center></h4>");
  client.println("</center>");
  client.println("<hr/><hr>");
  client.println("<center>");
  client.println("<a href=\"/a\"\"><button>Data </button></a><br />");
  client.println("</center>");
  client.println("<br><br>");
  client.println("<center>");
  client.println("Load 1");
  client.println("<a href=\"/l1on\"\"><button>Turn On </button></a>");
  client.println("<a href=\"/l1off\"\"><button>Turn Off </button></a><br />");
  client.println("</center>");
  client.println("<br><br>");
  client.println("<center>");
  client.println("Load 2");
  client.println("<a href=\"/l2on\"\"><button>Turn On </button></a>");
  client.println("<a href=\"/l2off\"\"><button>Turn Off </button></a><br />");
  client.println("</center>");
  client.println("<br><br>");
  client.println("<center>");
  client.println("Load 3");
  client.println("<a href=\"/l3on\"\"><button>Turn On </button></a>");
  client.println("<a href=\"/l3off\"\"><button>Turn Off </button></a><br />");
  client.println("</center>");
  client.println("<br><br>");
  client.println("<center>");
  client.println("<br><br>");
  client.println("<center>");
  client.println("<table border=\"5\">");
  client.println("<br><br>");
  client.println("<tr>");

  if(digitalRead(pin1)){client.print("<td>Load 1 is ON</td>");}
  else{client.print("<td>Load 1 is OFF</td>");}

  if(digitalRead(pin2)){client.print("<td>Load 2 is ON</td>");}
  else{client.print("<td>Load 2 is OFF</td>");}

  if(digitalRead(pin3)){client.print("<td>Load 3 is ON</td>");}
  else{client.print("<td>Load 3 is OFF</td>");}


  client.println("</tr>");
  client.println("</table>");
  client.println("</center>");
  client.println("</html>");
  delay(1);
  Serial.println("Client disonnected");
  Serial.println("");

}

NodeMCU V1.0 Pinout

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.

code 2

#include <ESP8266WiFi.h>

int pin1 = 14; // GPIO 14 (D5)
int pin2 = 5; // GPIO 5 (D1)
int pin3 = 4; // GPIO 5 (D2)

const char* ssid = "wifi";
const char* password = "12345678";

WiFiServer server(80);

void setup()
{

Serial.begin(115200);
delay(10);

pinMode(pin1, OUTPUT);
pinMode(pin2, OUTPUT);
pinMode(pin3, 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();



if (request.indexOf("/l1on") > 0)  {
digitalWrite(pin1, HIGH);

}
if (request.indexOf("/l1off") >0)  {
digitalWrite(pin1, LOW);

}

if (request.indexOf("/l2on") > 0)  {
digitalWrite(pin2, HIGH);

}
if (request.indexOf("/l2off") >0)  {
digitalWrite(pin2, LOW);

}
if (request.indexOf("/l3on") >0)  {
digitalWrite(pin3, HIGH);

}
if (request.indexOf("/l3off") > 0)  {
digitalWrite(pin3, LOW);

}



// Set ledPin according to the request
//digitalWrite(ledPin, value);

// 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("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<head>");
client.println("<meta name='apple-mobile-web-app-capable' content='yes' />");
client.println("<meta name='apple-mobile-web-app-status-bar-style' content='black-translucent' />");
client.println("</head>");
client.println("<body bgcolor = \"#f7e6ec\">");
client.println("<hr/><hr>");
client.println("<h4><center>Wifi Based Project</center></h4>");
client.println("</center>");
client.println("<hr/><hr>");
client.println("<center>");
client.println("<a href=\"/a\"\"><button>Data </button></a><br />");
client.println("</center>");
client.println("<br><br>");
client.println("<center>");
client.println("Load 1");
client.println("<a href=\"/l1on\"\"><button>Turn On </button></a>");
client.println("<a href=\"/l1off\"\"><button>Turn Off </button></a><br />");
client.println("</center>");
client.println("<br><br>");
client.println("<center>");
client.println("Load 2");
client.println("<a href=\"/l2on\"\"><button>Turn On </button></a>");
client.println("<a href=\"/l2off\"\"><button>Turn Off </button></a><br />");
client.println("</center>");
client.println("<br><br>");
client.println("<center>");
client.println("Load 3");
client.println("<a href=\"/l3on\"\"><button>Turn On </button></a>");
client.println("<a href=\"/l3off\"\"><button>Turn Off </button></a><br />");
client.println("</center>");
client.println("<br><br>");
client.println("<center>");
client.println("<br><br>");
client.println("<center>");
client.println("<table border=\"5\">");
client.println("<br><br>");
client.println("<tr>");

if(digitalRead(pin1)){client.print("<td>Load 1 is ON</td>");}
else{client.print("<td>Load 1 is OFF</td>");}

if(digitalRead(pin2)){client.print("<td>Load 2 is ON</td>");}
else{client.print("<td>Load 2 is OFF</td>");}

if(digitalRead(pin3)){client.print("<td>Load 3 is ON</td>");}
else{client.print("<td>Load 3 is OFF</td>");}


client.println("</tr>");
client.println("</table>");
client.println("</center>");
client.println("</html>");
delay(1);
Serial.println("Client disonnected");
Serial.println("");

}
  • Also asked at: stackoverflow.com/q/52342641 and forum.arduino.cc/index.php?topic=568674 If you're going to do that then please be considerate enough to add links to the other places you cross posted. This will let us avoid wasting time due to duplicate effort and also help others who have the same questions and find your post to discover all the relevant information. – per1234 Sep 15 '18 at 9:36
0

You can't use io 6. IO 6 is for internal use to connect the flash memory to esp8266. IO 6 has no Dx pin on the board. And io 3 is Serial RX connected to USB.

Use Dx constants and the corresponding 'D' labeled pins of the NodeMcu. (and remove conflicting comments)

const int pin1 = D2;
const int pin2 = D3;
const int pin3 = D6;

pin D3 is io 0 used to put the board into flashing mode. do not connect there something that pulls the pin LOW at reset or power-up

  • Actually, I am newbie in arduino and programming . i take this code in demo form, so i have no idea of conflicting comments. But i share both codes (Demo+modified) to understand for everyone to get desired code. – Saif Ullah Sep 19 '18 at 19:12
  • and did the answer help? – Juraj Sep 19 '18 at 19:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.