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 SSIDconst char pass [] = "xxxxxxxxx"; /Insert Your passwordconst 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 IDconst char* APIKey = "xxxxxxxxxxxxxxxx"; // Thingspeak Channel Read API KeyString Temp_String;WiFiClient espClient;PubSubClient client(espClient);const byte ledPin = 0; // Pin with LED on Adafruit Huzzahvoid 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();}
Best Answer