mqtt via ESP8266 connected but not subscribing

14 vues (au cours des 30 derniers jours)
Christian Seniuk
Christian Seniuk le 16 Nov 2019
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();
}

Réponse acceptée

Christopher Stapels
Christopher Stapels le 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 commentaire
Christian Seniuk
Christian Seniuk le 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.

Connectez-vous pour commenter.

Plus de réponses (3)

Christopher Stapels
Christopher Stapels le 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.
  2 commentaires
Christian Seniuk
Christian Seniuk le 17 Nov 2019
Thank you very much.
So when testing, i can connect another ESP8266 with another clientID, right?
Sorry for the silly questions
Christopher Stapels
Christopher Stapels le 18 Nov 2019
Yes you can connect up to three. It is definitely a good question.

Connectez-vous pour commenter.


Andrey Fedorov
Andrey Fedorov le 9 Déc 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 le 9 Déc 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.
  6 commentaires
Andrey Fedorov
Andrey Fedorov le 19 Déc 2019
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.
Christopher Stapels
Christopher Stapels le 19 Déc 2019
Modifié(e) : Christopher Stapels le 19 Déc 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?

Connectez-vous pour commenter.

Communautés

Plus de réponses dans  ThingSpeak Community

Catégories

En savoir plus sur ThingSpeak dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by