mathworks / thingspeak-arduino

ThingSpeak Communication Library for Arduino, ESP8266 and ESP32
https://thingspeak.com
438 stars 231 forks source link

[solved] Reading float from ThingSpeak with ESP8266 takes long time #61

Closed 8johnny6 closed 4 years ago

8johnny6 commented 4 years ago

Hello,

i'm using your library with my DIY-Weatherstation. The outside station sends every ten minutes temperature, humidity and pressure to ThingSpeak and after that it goes to deepsleep. The inside station messures the inside temperature and the humidity and fetch the outside data from ThingSpeak to show it on an 2.4" TFT-screen. Both stations use a Wemos D1 Mini Pro (feat. ESP8266)

If i'm using the ThingSpeak library it takes about five seconds to get the temperature, humidity and pressure as float from TS. That's not good, because in this five seconds my display and the rest of the ESP8266 is doing nothing. I'm receiving the time from an NTP server and print it also on the display, and so the time stands still for five seconds and then proceed.

What i tested so far: I tried the example-sketch provided by the library and set the delay to one second (instead of 15 in the example). It took also about five seconds for the data to be shown on the serial monitor. So i think my code ist OK.

Is there a way to get the data faster?

My code of getting the data is:

float TS_Temp = ThingSpeak.readFloatField( ChannelID, 1, APIREADKEY);

I hope you can help me and now what i mean. Sorry for my bad English. I'm from Germany and my English is a little bit rusty.

Regards Johnny

8johnny6 commented 4 years ago

OK, spending really much time the issue is solved with using MQTT to get the data from Thingspeak. I don't use the Library "ThingSpeak" anymore.

Below is my code for testing. It subscribes to the mqtt topic of the ThingSpeak channel and evertime the Data changes it will print the new data (as float) to the serial monitor. The code is adopted from a "baldengineer"-tutorial (https://www.baldengineer.com/mqtt-tutorial.html)

Best regards johnny

#############################################

include

include

include

include

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(); }