hello dear community ,
Lately , i ve been working on a project using the nodeMCU and i noticed that connecting with the command WiFiClient.connect fails when i try to connect to router with static ip address, Any solutions ?
hello dear community ,
Lately , i ve been working on a project using the nodeMCU and i noticed that connecting with the command WiFiClient.connect fails when i try to connect to router with static ip address, Any solutions ?
Use DHCP. Or else, did you specify a DNS server? With static IP connections, you have to supply all the information that DHCP provides to the client automatically.
Actually i am working with esp8266 and i am not using an anrduino ethernet shield so the DHCP is not mandatory i guess .
Otherwise, can you explain to me more ?
mahmoudreg:
Actually i am working with esp8266 and i am not using an anrduino ethernet shield so the DHCP is not mandatory i guess .
Otherwise, can you explain to me more ?
The type of hardware you are running it on makes no difference. This is a TCP/IP networking issue. Anything that applies to the ethernet shield also applies to a wifi sketch running on the ESP8266, in this case.
Why do you need a static IP? In order to have internet access on a wifi network, you need a local IP, a gateway IP, and a DNS server IP. All those have to be initialized in the wifi client class (there are methods for setting them), unless you are using DHCP. The reason is, DHCP allows the router to supply those IP's.
Static IP causes more problems than it ever solves. If you absolutely need an unchanging IP, for example an NAS, then do it in the router with a permanent IP lease from the DHCP server.
If you post your code as described in the how to use this forum sticky, more members will read it.
Okay , i ll try what u said guys thanks .
This is my code , i am trying to control 6 servomotors via an that i build with android studio .
#include <ESP8266WiFi.h>
#include<Servo.h>
const char* ssid = "";
const char password = "*";
const char* host = "192.168.1.8";
Servo myservo0;
Servo myservo1;
Servo myservo2;
Servo myservo3;
Servo myservo4;
Servo myservo5;
// Variable to store the HTTP request
String header;
// Decode HTTP GET value
String valueString = String(5);
int pos1 = 0;
int pos2 = 0;
// Current time
unsigned long currentTime = millis();
// Previous time
unsigned long previousTime = 0;
// Define timeout time in milliseconds (example: 2000ms = 2s)
const long timeoutTime = 2000;
void setup()
{
Serial.begin(115200);
myservo0.attach(2);
myservo1.attach(0);
myservo2.attach(4);
myservo3.attach(5);
myservo4.attach(15);
myservo5.attach(16);
Serial.println();
Serial.printf("Connecting to %s ", ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println(" connected");
}
void loop()
{
WiFiClient client;
Serial.printf("\n[Connecting to %s ... ", host);
if (client.connect(host, 80))
{
Serial.println("congrats");
Serial.println("[Sending a request]");
client.print(String("GET /") + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n" +
"\r\n"
);
Serial.println("[Response:]");
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected() && currentTime - previousTime <= timeoutTime) { // loop while the client's connected
currentTime = millis();
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
header += c;
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
if (header.indexOf("GET /servo0on/?value=")>=0) {
pos1 = header.indexOf('=');
pos2 = header.indexOf('&');
valueString = header.substring(pos1+1, pos2);
//Rotate the servo
myservo0.write(valueString.toInt());
Serial.println(valueString);
}
if (header.indexOf("GET /servo1on/?value=")>=0) {
pos1 = header.indexOf('=');
pos2 = header.indexOf('&');
valueString = header.substring(pos1+1, pos2);
//Rotate the servo
myservo1.write(valueString.toInt());
Serial.println(valueString);
}
if(header.indexOf("GET /servo2on/?value=")>=0) {
pos1 = header.indexOf('=');
pos2 = header.indexOf('&');
valueString = header.substring(pos1+1, pos2);
//Rotate the servo
myservo2.write(valueString.toInt());
Serial.println(valueString);
}
if(header.indexOf("GET /servo3on/?value=")>=0) {
pos1 = header.indexOf('=');
pos2 = header.indexOf('&');
valueString = header.substring(pos1+1, pos2);
//Rotate the servo
myservo3.write(valueString.toInt());
Serial.println(valueString);
}
if(header.indexOf("GET /servo4on/?value=")>=0) {
pos1 = header.indexOf('=');
pos2 = header.indexOf('&');
valueString = header.substring(pos1+1, pos2);
//Rotate the servo
myservo4.write(valueString.toInt());
Serial.println(valueString);
}
if(header.indexOf("GET /servo5on/?value=")>=0) {
pos1 = header.indexOf('=');
pos2 = header.indexOf('&');
valueString = header.substring(pos1+1, pos2);
//Rotate the servo
myservo5.write(valueString.toInt());
Serial.println(valueString);
}
// The HTTP response ends with another blank line
client.println();
// Break out of the while loop
break;
} else { // if you got a newline, then clear currentLine
currentLine = "";
}
} else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
}
}
}
else
{
Serial.println("connection failed!]");
client.stop();
}
delay(5000);
}
Paste your code between code tags </>
You may want to learn how to use the forum first here - General Guidance and how to use the forum
SteveMann:
If you post your code as described in the how to use this forum sticky, more members will read it.
True..
Sorry i am new here
#include <ESP8266WiFi.h>
#include<Servo.h>
const char* ssid = "*";
const char* password = "*";
const char* host = "192.168.1.8";
Servo myservo0;
Servo myservo1;
Servo myservo2;
Servo myservo3;
Servo myservo4;
Servo myservo5;
// Variable to store the HTTP request
String header;
// Decode HTTP GET value
String valueString = String(5);
int pos1 = 0;
int pos2 = 0;
// Current time
unsigned long currentTime = millis();
// Previous time
unsigned long previousTime = 0;
// Define timeout time in milliseconds (example: 2000ms = 2s)
const long timeoutTime = 2000;
void setup()
{
Serial.begin(115200);
myservo0.attach(2);
myservo1.attach(0);
myservo2.attach(4);
myservo3.attach(5);
myservo4.attach(15);
myservo5.attach(16);
Serial.println();
Serial.printf("Connecting to %s ", ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println(" connected");
}
void loop()
{
WiFiClient client;
Serial.printf("\n[Connecting to %s ... ", host);
if (client.connect(host, 80))
{
Serial.println("congrats");
Serial.println("[Sending a request]");
client.print(String("GET /") + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n" +
"\r\n"
);
Serial.println("[Response:]");
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected() && currentTime - previousTime <= timeoutTime) { // loop while the client's connected
currentTime = millis();
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
header += c;
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
if (header.indexOf("GET /servo0on/?value=")>=0) {
pos1 = header.indexOf('=');
pos2 = header.indexOf('&');
valueString = header.substring(pos1+1, pos2);
//Rotate the servo
myservo0.write(valueString.toInt());
Serial.println(valueString);
}
if (header.indexOf("GET /servo1on/?value=")>=0) {
pos1 = header.indexOf('=');
pos2 = header.indexOf('&');
valueString = header.substring(pos1+1, pos2);
//Rotate the servo
myservo1.write(valueString.toInt());
Serial.println(valueString);
}
if(header.indexOf("GET /servo2on/?value=")>=0) {
pos1 = header.indexOf('=');
pos2 = header.indexOf('&');
valueString = header.substring(pos1+1, pos2);
//Rotate the servo
myservo2.write(valueString.toInt());
Serial.println(valueString);
}
if(header.indexOf("GET /servo3on/?value=")>=0) {
pos1 = header.indexOf('=');
pos2 = header.indexOf('&');
valueString = header.substring(pos1+1, pos2);
//Rotate the servo
myservo3.write(valueString.toInt());
Serial.println(valueString);
}
if(header.indexOf("GET /servo4on/?value=")>=0) {
pos1 = header.indexOf('=');
pos2 = header.indexOf('&');
valueString = header.substring(pos1+1, pos2);
//Rotate the servo
myservo4.write(valueString.toInt());
Serial.println(valueString);
}
if(header.indexOf("GET /servo5on/?value=")>=0) {
pos1 = header.indexOf('=');
pos2 = header.indexOf('&');
valueString = header.substring(pos1+1, pos2);
//Rotate the servo
myservo5.write(valueString.toInt());
Serial.println(valueString);
}
// The HTTP response ends with another blank line
client.println();
// Break out of the while loop
break;
} else { // if you got a newline, then clear currentLine
currentLine = "";
}
} else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
}
}
}
else
{
Serial.println("connection failed!]");
client.stop();
}
delay(5000);
}