MATLAB Answers

mqtt via ESP8266 connected but not subscribing

29 views (last 30 days)
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
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 = "";
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();
TS_Temp = 0;
Temp_String = "";
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("ESP8266", mqttUserName, mqttPass)) {
// ... 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.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
void setup()
client.setServer(mqtt_server, 1883);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
void loop()
if (!client.connected()) {

Accepted Answer

Christopher Stapels
Christopher Stapels on 17 Nov 2019
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
Christian Seniuk
Christian Seniuk on 17 Nov 2019
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)

Christopher Stapels
Christopher Stapels on 17 Nov 2019
You can have up to three simultaneous MQTT connections with a free license. Thanks for posting that you got it working again.
Christopher Stapels
Christopher Stapels on 18 Nov 2019
Yes you can connect up to three. It is definitely a good question.

Sign in to comment.

Andrey Fedorov
Andrey Fedorov on 9 Dec 2019
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.

Christopher Stapels
Christopher Stapels on 9 Dec 2019
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.
Christopher Stapels
Christopher Stapels on 19 Dec 2019
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.


More Answers in the  ThingSpeak Community

Community Treasure Hunt

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

Start Hunting!