Connecting to webserver as client error

Hi everyone,
I am working on a project where i am reading data from EEPROM and connecting to a webserver as client using nodemcu.

in the function = client.connect( host, port ); when i am providing a string variable with ip attched to it in string form it is accepting that and connecting to host

But when i am reading data from EEPROM and making it string by combining it and after that fetching that varible in this function.

it stucks on connecting to host...
even i checked the length of the string and there is no issue , it is same

I am attaching the format in which i am combining string and fetching it :

String host_addr_1 = String(EEPROM.read(1));
String host_addr_2 = String(EEPROM.read(2));
String host_addr_3 = String(EEPROM.read(3));
String host_addr_4 = String(EEPROM.read(4));

final_host_ip = host_addr_1 + "." + host_addr_2 + "." + host_addr_3 + "." + host_addr_4;

and in the client.connect function i am using this :

client.connect(final_host_ip, port);

please help me out.
Thanks for your time in advance

Post full code including the one that works

Look at what’s expected in the header for the IP

Check the compilers’ warning or error message

A lot of confusion here... you are reading just 1 byte from EEPROM for a part of string which need 3 bytes.

Aniway, you could use the get() and put() mthod and voich such kind of problems...
it is also recommended to make sure that the length of the saved data is always the same and to do this in my opinion it is better to use a char array.

char myIp[15+1] = {"192.168.0.1"};  // nnn.nnn.nnn.nnn + string char terminator

// Save to EEPROM
EEPROM.put(address, myIp);

// get from EEPROM
EEPROM.get(address, myIp);


It all depends on what was saved in eeprom and how. If OP had saved the byte values in the correct order it’s OK then to read a byte and not 3 chars as you suggest. That’s why we need full code

The issue is more that unless I have the wrong header, the connect() method does not support a String instance parameter for the host.

That’s why I’m curious to see the code that supposedly works

You are right, even if although string reconstruction remains a non-recommended way as my opionion.

He should call...

client.connect(final_host_ip.c_str(), port);

Sure using a String is a wrong idea. Just saving the uint32_t representing the IP address would make more sense

ESP8266: IPAddress Class Reference

Hi jackson, first of all Thanks for your support buddy :slight_smile:
Here is the code where i am defining string and passing it in .coonect function and it is working fine

in the code , final_host_ip1 is the string variable where i am defining the ip

Can you please add me on whatsapp so that we can connect and solve it out because now it is getting pain in my A**;
please help me out bro , here is my contact edited out

Nope… sorry I don’t do WhatsApp or any of this insane social network craziness

Help is provided here, on my own time, for everyone to see and if you do your part starting by reading how to use the forum correctly. Posting image of text is already a big no go signal for me…

sro bro ,

Actually i am not much aware of using this forum, I will definitely go through it.

Here is the full code, Using which it is working fine.

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <EEPROM.h>

WiFiClient client;

//<
// Configuration parameters for nodemcu as Access Point
char * ssid_ap = "server";
char * password_ap = "password";

//Varible to read from client
bool overwriteeeprom = 0;

String wifiname; //of router to which it will connect
String password;

//IPAddress ip(192, 168, 0, 1);
//IPAddress gateway(192, 168, 1, 0);
//IPAddress subnet(255, 255, 255, 0);

// Set up the server object
ESP8266WebServer server(80);  //port
//>

//Router credentials to store data 
String read_ssid;
String read_pwd;

//host credentials
String final_host_ip;
const uint16_t port = 5999;

//if need of static ip > credentials for ESP
//if you want to know router congif then connect with dynamic ip and get print of its ip gateway , dns , and subnet
IPAddress staticIP(192, 168, 0, 111); //ESP static ip
IPAddress gateway(192, 168, 0, 1);   //IP Address of your WiFi Router (Gateway)
IPAddress subnet(255, 255, 255, 0);  //Subnet mask
IPAddress dns(192, 168, 0, 1); //DNS

//defining int function
void ICACHE_RAM_ATTR isr1();
void ICACHE_RAM_ATTR isr2();

//digital input
#define digi_in1_pin A0
#define digi_in2_pin A0
#define digi_in3_pin A0
#define digi_in4_pin A0

#define start_pin D0

#define connected_led_pin A0
#define sending_data_led_pin A0

bool digi_in1_state;
bool digi_in2_state;
bool digi_in3_state;
bool digi_in4_state;

String digi_in1_str;
String digi_in2_str;
String digi_in3_str;
String digi_in4_str;

//counter
#define counter_in1_pin A0
#define counter_in2_pin A0

int counter_in1_value;
int counter_in2_value;

String counter_in1_string;
String counter_in2_string;

unsigned long current_button_pressed_time = 0;
unsigned long last_button_pressed_time = 0;
uint8_t counter_debounce_time = 50;

//for timer
unsigned long StartTime;
unsigned long CurrentTime;
unsigned long ElapsedTime;
String timer_val ;

String send_string = "";



void setup() {
  Serial.begin(115200);
  EEPROM.begin(100);  //EEPROM.begin(Size)
  pinMode(start_pin, INPUT);
  while (!digitalRead(start_pin)) {
    Serial.println("in startmode");
  }

  //Reading data from eeprom and printing 
  String ip_addr_of_host_1 = String(EEPROM.read(1));
  String ip_addr_of_host_2 = String(EEPROM.read(2));
  String ip_addr_of_host_3 = String(EEPROM.read(3));
  String ip_addr_of_host_4 = String(EEPROM.read(4));

  Serial.println("PRINTING HOST ADDRESS : ");

  Serial.println(ip_addr_of_host_1);
  Serial.println(ip_addr_of_host_2);
  Serial.println(ip_addr_of_host_3);
  Serial.println(ip_addr_of_host_4);
  
  final_host_ip = ip_addr_of_host_1 + "." + ip_addr_of_host_2 + "." + ip_addr_of_host_3 + "." + ip_addr_of_host_4;

  Serial.println(final_host_ip);
  
  EEPROM.get(10, read_ssid);
  EEPROM.get(50, read_pwd);
  Serial.print("\nRead SSID from EEPROM : ");
  Serial.print(read_ssid);
  Serial.print("\nRead PWD from EEPROM : ");
  Serial.print(read_pwd);

  call_setup();
}

void loop() {
  // put your main code here, to run repeatedly:
  check_if_connected_to_server();

  handle_input();

  handle_timer();

  handle_string();
  digitalWrite(sending_data_led_pin, HIGH);
  delay(500);
  digitalWrite(sending_data_led_pin, LOW);
  delay(500);
}
void call_setup() {
  //Status LED pinMode
  pinMode(connected_led_pin, OUTPUT);
  pinMode(sending_data_led_pin, OUTPUT);

  //digi and counter pinMode
  pinMode(digi_in1_pin, INPUT);
  pinMode(digi_in2_pin, INPUT);
  pinMode(digi_in3_pin, INPUT);
  pinMode(digi_in4_pin, INPUT);
  pinMode(counter_in1_pin, INPUT_PULLUP);
  pinMode(counter_in2_pin, INPUT_PULLUP);

  //Enabling Interrupt
  attachInterrupt(counter_in1_pin, isr1, FALLING);
  attachInterrupt(counter_in2_pin, isr2, FALLING);

  // Serial.begin(115200);

  //Connecting to router
  WiFi.begin(read_ssid, read_pwd);
  WiFi.config(staticIP, gateway, subnet, dns); // YOU WILL HAVE TO CONFIG THE STATIC IP SETTING
  while (WiFi.status() != WL_CONNECTED) {
    delay(100);
    Serial.print("\nConnecting to router...");
  }

  //PRINT CONNECTED WIFI CREDENTIALS
  Serial.print("WiFi connected with IP: ");
  Serial.println(WiFi.localIP());
  Serial.print("Subnet Mask: ");
  Serial.println(WiFi.subnetMask());
  Serial.print("Gateway IP: ");
  Serial.println(WiFi.gatewayIP());
  Serial.print("DNS 1: ");
  Serial.println(WiFi.dnsIP(0));
  Serial.print("DNS 2: ");
  Serial.println(WiFi.dnsIP(1));

  Serial.println(final_host_ip);
  Serial.println(read_ssid);
  Serial.println(read_pwd);

String final_host_ip1 = "192.168.0.2";

  //connecting to host [Loop until host is not connected]
  while (!client.connected()) {
    client.connect(final_host_ip1, port);
    Serial.println("connecting to host..");
    delay(100);
  }
  Serial.println("Connected to server successful!");
  //  client.print("Hello from 82");
  digitalWrite(connected_led_pin, HIGH);

  StartTime = millis();
}
void check_if_connected_to_server (){
    if (!client.connected()) {
    digitalWrite(connected_led_pin, LOW);
    Serial.println("Disconnected from server");
    Serial.println("Connecting Again");
    while (!client.connected()) {
      client.connect(final_host_ip, port);
      Serial.println("connecting to host..");
      delay(100);
    }
    Serial.println("Connected to server successful Again");
    digitalWrite(connected_led_pin, HIGH);
  }
}
void handle_input (){
   //Read pin state
  digi_in1_state = digitalRead(digi_in1_pin);
  digi_in2_state = digitalRead(digi_in2_pin);
  digi_in3_state = digitalRead(digi_in3_pin);
  digi_in4_state = digitalRead(digi_in4_pin);

  //checking if there is any change
  //INPUT1
  if (digi_in1_state) {
    digi_in1_str = "SSSSSS";
  }
  else {
    digi_in1_str = "RRRRRR";
  }
  //INPUT2
  if (digi_in2_state) {
    digi_in2_str = "SSSSSS";
  }
  else {
    digi_in2_str = "RRRRRR";
  }

  //INPUT3
  if (digi_in3_state) {
    digi_in3_str = "SSSSSS";
  }
  else {
    digi_in3_str = "RRRRRR";
  }
  //INPUT4
  if (digi_in4_state) {
    digi_in4_str = "SSSSSS";
  }
  else {
    digi_in4_str = "RRRRRR";
  }

  //for counter and counter value
  //counter1
  counter_in1_string = String(counter_in1_value);
  //maintaining length of counter_in1_string
  int counter_in1_string_len = counter_in1_string.length();
  if (counter_in1_string_len != 6) {
    if (counter_in1_string_len == 1) {
      counter_in1_string = "00000" + counter_in1_string;
    }
    else if (counter_in1_string_len == 2) {
      counter_in1_string = "0000" + counter_in1_string;
    }
    else if (counter_in1_string_len == 3) {
      counter_in1_string = "000" + counter_in1_string;
    }
    else if (counter_in1_string_len == 4) {
      counter_in1_string = "00" + counter_in1_string;
    }
    else if (counter_in1_string_len == 5) {
      counter_in1_string = "0" + counter_in1_string;
    }
  }


  //counter2
  counter_in2_string = String(counter_in2_value);
  //maintaining length of counter_in2_string
  int counter_in2_string_len = counter_in2_string.length();
  if (counter_in2_string_len != 6) {
    if (counter_in2_string_len == 1) {
      counter_in2_string = "00000" + counter_in2_string;
    }
    else if (counter_in2_string_len == 2) {
      counter_in2_string = "0000" + counter_in2_string;
    }
    else if (counter_in2_string_len == 3) {
      counter_in2_string = "000" + counter_in2_string;
    }
    else if (counter_in2_string_len == 4) {
      counter_in2_string = "00" + counter_in2_string;
    }
    else if (counter_in2_string_len == 5) {
      counter_in2_string = "0" + counter_in2_string;
    }
  }
}
void handle_timer(){
   //timer
  CurrentTime = millis();
  ElapsedTime = CurrentTime - StartTime;
  ElapsedTime = ElapsedTime / 1000;
  timer_val = String(ElapsedTime);
}
void handle_string() {

  send_string = digi_in1_str + "," + digi_in2_str + "," + digi_in3_str + "," + digi_in4_str + "," + counter_in1_string + "," + counter_in2_string + "," + timer_val;
  Serial.println(send_string);
  client.print(send_string);
}
//counterinterrupt
void ICACHE_RAM_ATTR isr1() {
  current_button_pressed_time = millis();
  if (current_button_pressed_time - last_button_pressed_time > counter_debounce_time)
  {
    counter_in1_value++;
    last_button_pressed_time = current_button_pressed_time;
  }
}

//interrupt
void ICACHE_RAM_ATTR isr2() {
  current_button_pressed_time = millis();
  if (current_button_pressed_time - last_button_pressed_time > counter_debounce_time)
  {
    counter_in2_value++;
    last_button_pressed_time = current_button_pressed_time;
  }
}
 

Whenever you are free plz try to resolve this,
I will be waiting for your response here
Thankyou

HI J-M-L
thanks for your response buddy :))
I posted the full code in post 9
Please find it attached there

and also , according to you if we will define ip in uint32_t basically in unsigned int. Then how would you pass it in .connect function

bro can you please refer hoe to use this uint32_t represenattion in code

Guys here is the simplified version of program

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <EEPROM.h>

WiFiClient client;

//<
// Configuration parameters for nodemcu as Access Point

//Varible to read from client
bool overwriteeeprom = 0;

String wifiname; //of router to which it will connect
String password;

//IPAddress ip(192, 168, 0, 1);
//IPAddress gateway(192, 168, 1, 0);
//IPAddress subnet(255, 255, 255, 0);

// Set up the server object
ESP8266WebServer server(80);  //port
//host credentials
char* host;
const uint16_t port = 5999;

//if need of static ip > credentials for ESP
//if you want to know router congif then connect with dynamic ip and get print of its ip gateway , dns , and subnet
IPAddress staticIP(192, 168, 0, 115); //ESP static ip
IPAddress gateway(192, 168, 0, 1);   //IP Address of your WiFi Router (Gateway)
IPAddress subnet(255, 255, 255, 0);  //Subnet mask
IPAddress dns(192, 168, 0, 1); //DNS

String digi_in1_str;
String digi_in2_str;
String digi_in3_str;
String digi_in4_str;

String send_string = "";
String final_host_ip;
String final_host_ip1;

void setup() {
  Serial.begin(115200);
  Serial.println("\n");
  Serial.println("\n");
  EEPROM.begin(100);  //EEPROM.begin(Size

  //address for eeprom
  int addr_ip_1 = 1;
  int addr_ip_2 = 2;
  int addr_ip_3 = 3;
  int addr_ip_4 = 4;

  String host_addr_1 = String(EEPROM.read(1));
  String host_addr_2 = String(EEPROM.read(2));
  String host_addr_3 = String(EEPROM.read(3));
  String host_addr_4 = String(EEPROM.read(4));

  Serial.println(host_addr_1);
  Serial.println(host_addr_2);
  Serial.println(host_addr_3);
  Serial.println(host_addr_4);

  final_host_ip = host_addr_1 + "." + host_addr_2 + "." + host_addr_3 + "." + host_addr_4;
  Serial.println(final_host_ip);

  delay(2000);

  //Connecting to router
  WiFi.begin("Tenda", "Tenda123");
  WiFi.config(staticIP, gateway, subnet, dns); // YOU WILL HAVE TO CONFIG THE STATIC IP SETTING
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to router...");
  }

  //PRINT CONNECTED WIFI CREDENTIALS
  Serial.print("WiFi connected with IP: ");
  Serial.println(WiFi.localIP());
  Serial.print("Subnet Mask: ");
  Serial.println(WiFi.subnetMask());
  Serial.print("Gateway IP: ");
  Serial.println(WiFi.gatewayIP());
  Serial.print("DNS 1: ");
  Serial.println(WiFi.dnsIP(0));
  Serial.print("DNS 2: ");
  Serial.println(WiFi.dnsIP(1));



 final_host_ip1 = "192.168.0.2";

  Serial.println(final_host_ip.length());
  Serial.println(final_host_ip1.length());

  /*
  for (int i = 0; i <= final_host_ip.length(); i++) {
    Serial.println(final_host_ip[i]);
  }
  for (int i = 0; i <= final_host_ip3.length(); i++) {
    Serial.println(final_host_ip[i]);
  }

*/

  if (final_host_ip == final_host_ip1) {
    Serial.println("Both data is equal");
  }
  else {
    Serial.println("Both data is unequal");
  }
  
  //connecting to host [Loop until host is not connected]
  while (!client.connected()) {
    client.connect(final_host_ip1, port);
    Serial.println("connecting to host..");
    delay(100);
  }
  Serial.println("Connected to server successful!");
}

void loop() {

}

what i am tryinng to do is : i am reading data from EEPROM of nodemcu and making it a string and passing it in .connect function for connecting to the host
And the problem is it is not connecting when i am passing the string which is read from EEPROM
but when i am defining a variable and then passing that variable in the .connect function
then it is getting connected to the host
even the length of the data is same
but when i am checking whether the data is equal or not > then i am getting the data is unequal
Can anyone of you please help me out?

I'm not a buddy, a bro, or whatever and bumping up your thread is not well regarded here. waiting a couple days for an answer is not the end of the world. if you need fast answers or someone to do this for you then this seems a great ask for the Jobs and Paid Consultancy forum, make sure you mention your budget.


NodeMCU does not have any EEPROM.


the class IPAdress knows how to convert to a uint32_t

if you run this

IPAddress staticIP(192, 168, 0, 111);
uint32_t testAddress = 0x6F00A8C0;    //read from right to left  0x6F00A8C0, C0 = 192 A8=168, 00=0, 6F= 111

void setup() {
  Serial.begin(115200);
  uint32_t address = staticIP; // will call the uint32_t() operator
  Serial.print("staticIP as a 32 bit 0x"); Serial.println(address, HEX);

  IPAddress testIP(testAddress); //build an IPAdress from a uint32_t representation
  Serial.print("testIP is "); testIP.printTo(Serial);
}

void loop() {}

you'll see 0x6F00A8C0
if you read from right to left and convert to decimal ➜ 0x6F00A8C0, C0 = 192 A8=168, 00=0, 6F= 111

when you want to build an IPAdress back from EEPROM, the class also offers a constructor from a uint32_t as done for the testIP instance, using testAddress. You can see that when it's printed you get
testIP is 192.168.0.111

so you could just store in EEPROM this 32 bits value and instantiate an IPAdress object when you need such a type, for example in the connect() call

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.