mqtt via ESP8266 connected but not subscribing

Hello,
yesterday: i changed the way i collect data from my thinspeak channel from the ThingSpeak arduino library to mqtt. it worked like a charm.
today: my ESP8266 tells me that he is connected. but no data comes from the subscription and so the esp8266 will try to reconnect.
Is there a problem with your Server, or is it an license problem?
I think i reached the maximum of mqtt clients (3 clients in free mode). Is there a way to reset the clients to reconnect?
Best regards
Christian
my test code:
#include <EEPROM.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <Wire.h>
const char ssid[] = "xxxxxxx"; //Insert your SSID
const char pass [] = "xxxxxxxxx"; /Insert Your password
const char* mqtt_server = "mqtt.thingspeak.com";
const char* mqttUserName = "xxxxxxxxxxx"; // Thingspeak username.
const char* mqttPass = "xxxxxxxxxxxxxxxx"; // Change to your MQTT API Key from Account > MyProfile.
const char* ChannelID = "xxxxxx"; // Thingspeak Channel ID
const char* APIKey = "xxxxxxxxxxxxxxxx"; // Thingspeak Channel Read API Key
String Temp_String;
WiFiClient espClient;
PubSubClient client(espClient);
const byte ledPin = 0; // Pin with LED on Adafruit Huzzah
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Data received: ");
Serial.print("Temperature ");
for (int i = 0; i < length; i++) {
char receivedChar = (char)payload[i];
Temp_String += (char)payload[i];
}
float TS_Temp = Temp_String.toFloat();
Serial.print(TS_Temp);
TS_Temp = 0;
Temp_String = "";
Serial.println();
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("ESP8266", mqttUserName, mqttPass)) {
Serial.println("connected");
// ... and subscribe to topic
client.subscribe("channels/INSERT YOUR CHANNEL ID HERE/subscribe/fields/field1/INSERT YOUR API READ KEY HERE");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup()
{
Serial.begin(115200);
delay(250);
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
WiFi.disconnect();
delay(100);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.print("OK");
Serial.println();
}
void loop()
{
if (!client.connected()) {
reconnect();
}
client.loop();
}

 Accepted Answer

The mqtt server should reset the count when your devces disconnect, though there may be a short delay. Are you still having this issue after waiting a bit? Do you still have multiple connections?

1 Comment

Thank you.
Today i just started one device and it is working.
from now on i will keep in mind that two devices won't work. Thank you.

Sign in to comment.

More Answers (3)

You can have up to three simultaneous MQTT connections with a free license. Thanks for posting that you got it working again.

2 Comments

Thank you very much.
So when testing, i can connect another ESP8266 with another clientID, right?
Sorry for the silly questions
Yes you can connect up to three. It is definitely a good question.

Sign in to comment.

I have less than three MQTT connections, but MQTT subscription works VERY UNSTABLE. E.g sometimes messages comes ok just immediately, sometimes they are lost completely.I can't find suitable way to get stable result.
I've tested the MQTT subscription using Android IoT MQTT Panel application and create three panels: two switches and one LED indicator. All of this controls was connected to one topic and put and get info with field1. Works verys unstable.
Then I try to use the same three controls with simple CloudMQTT broker, it works pefect. Very stable result of receiving payloads over MQTT subscription.
Do you have any way how to check the number of active connections? E.g. I tested from the web application and Android application and both of them connected successfully, and MQTT subscription passed without any errors, but MQTT messages most of the time lost on the way.
My monitor channel seems is consistently publishing messages with none lost. I have two subscriptions. I would say perhaps your connection in intermittant except that you tested sucessfully with the other broker.
You can check the other settings (QOS, last will, etc) to make sure they are consistent with the ThingSpeak broker. Also check out the troubleshooting tips for publish and subscribe.

6 Comments

When I made subscription and found that it doesn't work - I checked two different Internet channels: via fibre channel Internet and 4G. Both of ways don't work properly. Publishing always work ok. Subscription mostly doesn't work at all. E.g. CloudMQTT always work stably without any problem.
QoS is 0. Last will is not specified. Connection timeout 30, keep alive = 60.
Here is screenshot of the IoT MQTT Panel. I hadn't change any settings in Topic and Payload, during testing. Now it mostly doesn'twork at all. Sometimes changing Client ID helps to solve the problem, but it's temporay solution, after some period of time it stops working also.
Screenshot_20191208_080514_snr.lab.iotmqttpanel.prod.jpg Screenshot_20191208_095441_snr.lab.iotmqttpanel.prod.jpg
Vinod
Vinod on 10 Dec 2019
Edited: Vinod on 11 Dec 2019
I believe the issue here is the app uses the same client ID and may be drooling connections. So, it tries to make a new connection while it has not cleanly disconnected from the server on another connection that has used the same client ID.
I use the MQTT Dash app on Android and I get consistent MQTT subscription updates. I've let it run for hours on end with no trouble. Can you try this app? Remember to leave the "Default (automatically connect on start up)" option unchecked, otherwise on each startup, it tries to make a connection while an existing one alredy exists. Here's screenshots of my app settings:
Thank you for the response. I tried MQTT Dash with disabled encrypted connection option.It works perfect. But, IoT MQTT Panel still work very unstably. I changed user id. Sometimes it works, but mostly not. The same with Windows application MQTTBox.
All of this apps works with the same (сopy-past) subscription topic. In MQTT Dash I used both variant when specified ONLY subscription topic and sub + pub. All variants work without any problems.
MQTTBox and IoT MQTT Panel work very unstably with the same pub & sub topic. Very strange.
Vinod
Vinod on 11 Dec 2019
Edited: Vinod on 11 Dec 2019
I'd reach out to the developers of those applications. If I were to guess, it probably has to do with what they do with the connection when the app is in the background. They're probably not keeping the connections alive, or possibly trying enable concurrent connections with the same client ID.
Yes, it's possible, but the same wrong behavior with MQTTBoxhttp://workswithweb.com/mqttbox.html and IoT MQTT Panel.
It's not good, that only MQTT Dash work stably. And when I tested CloudMQTT https://www.cloudmqtt.com/ - there is no any problems. All three Apps works perfectly.
And when I subscibe to ThingSpeak from ESP8266/ESP32 - I got exactly the same unstable behavior with ThingSpeak.
My ESP8266 subsriptions arer very stable. I have one here on my desk that has is subscribed to a channel an publishes data each minute. It compares the number published to the subscribe callback message. I have seen no errors in many months. Is your code similar to the code shown above?

Sign in to comment.

Communities

More Answers in the  ThingSpeak Community

Categories

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!