Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

After integrating ThingSpeak calls, ESP8266 attempts reconnection #198

Open
jackmanfam opened this issue Sep 7, 2016 · 8 comments
Open

Comments

@jackmanfam
Copy link

RE: Potential compatibility issue w/ latest thinkspeak library

Your library is awesome and it has been working great for me for quite some time. I am testing an integration to ThingSpeak to collect / log data on activity, uptime, stability, etc. Whenever I make a call to ThingSpeak the loop detects that it's disconnected and runs the reconnect() routine. It reconnects fine, but I'm wondering what is causing this and if it's truly disconnected or a symptom of something worse. Because it reconnects fine there is no reason code or anything else I can figure out for debugging.

I made a pool pump controller and really want to work out any stability bugs.

Here are some of the details and pertinent code snips...
ESP8266-12e
Good RF connection (~30dB of SNR); infrastructure reports good stability. (everything is stable until I make a ThingSpeak publish call)

Libraries I'm using:

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <EEPROM.h>
#include <NTPtimeESP.h>
#include <Timer.h>
#include <ThingSpeak.h>

Logic to throw relays and calls to ThingSpeak:

  if (topicStr.equals(poolLightTopic)) {
    if ((char)payload[0] == '0') {
      lightsOff();
    } else {
      digitalWrite(poolLightRelay, HIGH);   // Turn the relay ON
      client.publish("home/outLog", "poolHouse Light ON");
      //set a timer to turn after after payloadInt minutes
      timerID = timeClock.after((payloadInt * 60 * 1000), lightsOff);
      ThingSpeak.writeField(myChannelNumber, 5, 1, myWriteAPIKey);
    }
  } else if (topicStr.equals(poolPumpTopic)) {
    if ((char)payload[0] == '0') {
      pumpsOff();
    } else {
      digitalWrite(poolPumpRelay, HIGH);   // Turn the relay ON
      client.publish("home/outLog", "poolHouse Pump ON");
      //set a timer to turn after after payloadInt minutes
      timerID = timeClock.after((payloadInt * 60 * 1000), pumpsOff);
//      ThingSpeak.writeField(myChannelNumber, 1, 1, myWriteAPIKey);
    }
...

...
//example of what is called after the timer throws. This causes a second reconnect, which is consistent
void lightsOff() {
  digitalWrite(poolLightRelay, LOW);
  client.publish("home/outLog", "poolHouse Light OFF");
  ThingSpeak.writeField(myChannelNumber, 5, 0, myWriteAPIKey);
}

In snippet above, when I trigger the light it writes out to ThingSpeak and I get the reconnect() routing kicking in. However, when triggering the pump I commented out the call to ThingSpeak and no error.

Reconnect routine:

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection to "); Serial.print(mqtt_server); Serial.print("...");
    // Attempt to connect
    if (client.connect("poolHouseTest")) {
      Serial.println("connected");
      // Once connected, publish an announcement...
      client.publish("home/outLog", "poolHouse CONNECTED");
      // ... and resubscribe
      client.subscribe(poolLightTopic);
      client.subscribe(poolPumpTopic);
      client.subscribe(poolBoosterPumpTopic);
//      ThingSpeak.writeField(myChannelNumber, 4, 1, myWriteAPIKey);  //thinkspeak will not allow updates faster than 15s, so initial boot will not catch the update. Reconnects should work.
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
    //delay(500);
  }
}

My loop:

void loop() {

  timeClock.update();
  if (!client.connected()) {
    reconnect();
  }
  client.loop();

}

Would love to know if others have encountered this. I'm trying to figure out how to capture more debug info to troubleshoot.

Thanks!

@jackmanfam
Copy link
Author

Yes, I had exactly the same experience. I still haven't resolved it. There
is something related to the TCP connection I think. Does anyone know if
there is a bug w/ an ESP maintaining two different open sockets?

On Sat, Sep 10, 2016 at 2:09 AM, Tomoyuki Hisada [email protected]
wrote:

+1

I confronted similar problem without ThingSpeak.
When I commented out all 'client.subscribe()', the connection is alive.
How about you ?


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
#198 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AMyf0M1CGs-yB91PLasv_Ueuu8Io9IQxks5qonPagaJpZM4J2hcW
.

@jackmanfam
Copy link
Author

I responded from my mobile device of someone responding to this issue when I wrote the 2nd reply. Looks like that response is no longer in here, in case anyone is confused.

Has anyone else tried this and had the same issue? I duplicated this using just a super simple program and it doesn't seem to stay connected to MQTT when I make any call to ThingSpeak.

Do any of you have similar disconnect issues when connecting to any other IP destination?

@jackmanfam
Copy link
Author

Tomoyuki, did you find a way to resolve this?

On Sat, Sep 10, 2016 at 2:09 AM, Tomoyuki Hisada [email protected]
wrote:

+1

I confronted similar problem without ThingSpeak.
When I commented out all 'client.subscribe()', the connection is alive.
How about you ?


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
#198 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AMyf0M1CGs-yB91PLasv_Ueuu8Io9IQxks5qonPagaJpZM4J2hcW
.

@coelner
Copy link

coelner commented Feb 17, 2017

Same problem here. If you disconnect before you publish to thingspeak it works fine. I guess, the esp (or the wificlient object) can handle only one tcp socket.

@Suxsem
Copy link

Suxsem commented Feb 17, 2017

Sorry for the basic question... Are you using two different wificlient objects to initialize the pubsubclient and thingspeak libraries?

@coelner
Copy link

coelner commented Feb 17, 2017

In my case I use the same object.

@Suxsem
Copy link

Suxsem commented Feb 17, 2017

Please try to instantiate two different wificlient objects.

@coelner
Copy link

coelner commented Feb 21, 2017

Sorry for the delay: It works fine. I guess you can close the topic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants