Imroy / pubsubclient

A client library for the ESP8266 that provides support for MQTT
MIT License
434 stars 115 forks source link

Change the IP address #4

Closed sticilface closed 9 years ago

sticilface commented 9 years ago

Hi,

I've started using your fork as the older version no longer compiled on the ESP arduino IDE.

I have got it working apart from one thing, I can't assign the IP address dynamically. I want to read the IP address from the EEPROM then start the MQTT based on this. When i define MQTTserver as below it all works...

      IPAddress MQTTserver(192,168,1,24);
      PubSubClient mqttclient(MQTTserver);

If i do not, and try to assign MQTT server from EEPROM... during set up... I get the right IP but it fails to connect...

     for (int i=0; i<4; i++) {  
    MQTTserver[i] = EEPROM.read( mqttAddress+i);
   }

Any ideas why? I had this working before when the IP address was a char array..

Imroy commented 9 years ago

Ah, I see. The server hostname/IP address is only used in connect(), so it should be safe to set/change it before then.

Imroy commented 9 years ago

With this update you create your global client object:

PubSubClient mqttclient;

And then later assign the server ip address:

mqttclient.set_server(MQTTserver);
sticilface commented 9 years ago

haha... I've just been modifying the example script to show that it doesn't work and you've come up with a solution! fantastic. Thank you!

Will try it out

sticilface commented 9 years ago

Works perfectly. Thank you... I've been going bonkers trying to get that to work today...