-->
Page 1 of 6

WifiClient.connect slow to connect

PostPosted: Sat Apr 11, 2015 8:32 am
by torntrousers
I've a problem where WifiClient.connect wont succeed till about 12 seconds after an ESP8266 has connected to Wifi.

I'm experimenting with a battery powered ESP8266 with a sketch that regularly wakes up and connects to WiFi, does an MQTT publish and then goes to sleep. It works ok (loving this ESP8266 Arduino support), using around 70 - 85 milliamps when active and around 16 micro amps when asleep which is great. It takes about 20 seconds to wake up and do a publish though which seems quite a long time.

The sketch is below, it publishes to the public internetofthings.ibmcloud.com broker so anyone should be able to run it.

Debuging where the time is going most of it is the WifiClient.connect loop at line 66, and it seems to need about 12 seconds after the Wifi getting connected before the client connect will succeed.

I did wonder if it was internetofthings.ibmcloud.com being slow but i've tried a similar sketch using thingspeak.com and that has a very similar delay.

Any ideas what might be going on or what could be done to reduce the time?

Code: Select all#include <PubSubClient.h>
#include <ESP8266WiFi.h>
extern "C" {
  #include "user_interface.h"
}

const char* ssid = "BTHub5-72W5";
const char* password = "xxxxxxxxxx";

char* server = "quickstart.messaging.internetofthings.ibmcloud.com";
char* topic = "iot-2/evt/status/fmt/json";
char clientid[50] = "d:quickstart:iotsample-ESP8266:";

WiFiClient wifiClient;
PubSubClient client(server, 1883, callback, wifiClient);

long startMillis;

void setup() {
  startMillis = millis();
  Serial.begin(115200);
  delay(10); // is this necessary?

  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.print("WiFi connected in: ");   
  Serial.println(millis()-startMillis);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  // get MAC address
  uint8_t mac[6];
  WiFi.macAddress(mac);
  String macString = macToStr(mac);

  Serial.print("MAC address: ");
  Serial.println(macString);

  String clientName;
  clientName += clientid;
  clientName += macString;
  clientName.toCharArray(clientid, clientName.length()+1);

  Serial.println();
  Serial.println("View data at:");
  Serial.print("https://quickstart.internetofthings.ibmcloud.com/#/device/");
  Serial.print(macString);
  Serial.println("/sensor/");

  // Uncomment this delay and then the client.connect will work quickly
  // Serial.println("Wait 15 sec...");
  // delay(15000);

  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(server);
  while (!!!client.connect(clientid)) {
     Serial.print(".");
     delay(500);
  }
  Serial.println("");
  Serial.println("Connected to MQTT broker");
 
  String payload = "{\"d\":{\"myName\":\"ESP8266\",\"counter\":";
  payload += 42; // replace this with a temperature reading or something
  payload += "}}";
 
  if (client.publish(topic, (char*) payload.c_str())) {
    Serial.print("Published ok: ");
    Serial.println(payload);
  } else {
    Serial.println("Publish failed");
  }

  Serial.print("Going to sleep, time taken: ");
  Serial.println(millis()-startMillis);
 
  system_deep_sleep_set_option(0);
  system_deep_sleep(10000000); // deep sleep for 10 seconds
  // delay(100); // seem to need this as otherwise it continues executing loop a bit before going to sleep
}

void loop() {
  // gone to sleep so wont get here
}

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.println("callback invoked");
}

String macToStr(const uint8_t* mac) {
  String result;
  for (int i = 0; i < 6; ++i) {
    result += String(mac[i], 16);
  }
  return result;
}

Re: WifiClient.connect slow to connect

PostPosted: Sat Apr 11, 2015 10:36 am
by ulumu
You probably do not need to call WiFi.begin every time ESP start, because the Wifi config is saved.

Try calling WiFi.SSID() and check for existing config, if it matches, then do nothing.

You also need to add wifi_station_set_auto_connect(true) after WiFi.begin.

Re: WifiClient.connect slow to connect

PostPosted: Sat Apr 11, 2015 11:36 am
by torntrousers
Terrific, thank you. With those two changes it now only takes around 4.5 to 6.5 seconds.

Re: WifiClient.connect slow to connect

PostPosted: Sat Apr 11, 2015 11:39 am
by ulumu
Awesome, glad it helps.