Unable to read numbers after decimal point from ThingSpeak data. How to display 4.94 (actual data) when reading the actual data.

Jay Wan on 31 Aug 2022
Latest activity Edit by Christopher Stapels on 10 Oct 2022

Hi
Sent data was 4.94
{"created_at":"2022-08-28T23:38:23-04:00","entry_id":1269,"field4":"4.94"}]}
Read data was 4.00
Code:
long PHTS = ThingSpeak.readLongField(counterChannelNumber, FieldNumber4, myCounterReadAPIKey);
int statusCode = ThingSpeak.getLastReadStatus();
if (statusCode == 200)
{
Serial.print("ThingSpeak pH: ");
Serial.println(PHTS);
}
How to display 4.94 (actual data) when reading the data.
Thanks
Christopher Stapels
Christopher Stapels on 31 Aug 2022
use ThingSpeak.readFloatField
Jay Wan
Jay Wan on 1 Sep 2022
Hello Christopher,
I managed to read the data in 2 decimals points correctly from ThingSpeak.
I tried to pass the 2 data to a Mega with a LCD. I only managed to get the 2nd data. The 1st was 0.00. I am not sure what happened? Do you have any example using a esp8266 to pass the data to an arduino to enable them to be displayed on a LCD?
I'm using a Mega with WIFI mcu.
Thanks
Christopher Stapels
Christopher Stapels on 1 Sep 2022 (Edited on 2 Sep 2022)
Im glad you were able to get the data correctly. Here is an example I wrote I Hackster to write numbers read from ThingSpeak to an OLED display. If I were you, id skip the mega if possible and use the esp8266 directly, or get an esp32.
Jay Wan
Jay Wan on 12 Sep 2022
Hello Christopher,
Hope you are well.
I program the MEGA wifi to read 2 data (6.3/0.21) from thingspeak (on esp itself) and it could connect with my home wifi with ease and fast and display the data on serial motor no issues.
When I try to get the Mega (mcu) to read the data and display on a LCD.
On the serial monitor, it was taking very long time to connect to the wifi, when finally connected, it also taking very long time to read/display the data on the serial monitor, even some were overlaping. Nothing was showing up on the LCD at that moment.
When the connection was terminated, the LCD was showing rows of 1111111. (Probably last digit of the data).
I guess it is a mcu speed issue with wifi speed is many times faster than a mega mcu? Or it could be the program code how to pass the data from esp to mega mcu?
Sorry to disturb you again and hope you could help me out here please. Thanks.
Christopher Stapels
Christopher Stapels on 12 Sep 2022
I dont think you have a speed issue at all. The connection protocols can take care of these kind of differences.
I would break the problem into smaller segments. If your code takes a long time to connect to wifi, move to where you think it it strong and try again. If that still fails, then try to modify your code to reduce the conneciton time.
Then work on the LCD part. Just write one mumber ot the screen. dont even read from ThingSpeak to start. Be sure you can write numbers and erase them before you try to read the data from anywhere else. Then, once all the individual parts work, start putting them together.
Jay Wan
Jay Wan on 4 Oct 2022
Hello Chris,
Hope you are well. I finally managed to understand how they (mcu & wifi) talk to each other. There is a process as always. Got a good source of code to write and read the string from serial mon. and displayed on the LCD!
I got those 2 readings from TS. Is there a way to read the time too? I don't send time to TS and I think TS recorded the time/date as the data are in.
Erase the data at the end with = "" at the end of void loop?
Thanks.
Christopher Stapels
Christopher Stapels on 4 Oct 2022
I’m happy to hear things are working out for you.
You can get the time by reading the created_at field, but that will be the time of the last entry. If you wanted to exact present time, you could write to a channel and then read it immediately after (I’ve used this technique). Then the created_at time will be the present time.
I'm not clear what you mean by :
"Erase the data at the end with = "" at the end of void loop?"
Jay Wan
Jay Wan on 4 Oct 2022 (Edited on 5 Oct 2022)
Hello Chris,
Still couldn't get my head around how to read the time using created_at field? Newbie here with limited knowledge...
below are part of the code, I appreciate your time to look into it and guide me a little please. Thanks.
unsigned long counterChannelNumber = 1555609; // Channel ID
const char * myCounterReadAPIKey = "xxxxxxxxxxxxxxxx"; // Read API Key
const int FieldNumber4 = 4; // PH
const int FieldNumber5 = 5; // EC
PHTS = ThingSpeak.readFloatField(counterChannelNumber, FieldNumber4, myCounterReadAPIKey);
ECTS = ThingSpeak.readFloatField(counterChannelNumber, FieldNumber5, myCounterReadAPIKey);
int statusCode = ThingSpeak.getLastReadStatus();
if (statusCode == 200)
{
str = String("pH:") + String(PHTS) + String(" EC=") + String(ECTS);
Serial.println(str);
Christopher Stapels
Christopher Stapels on 5 Oct 2022 (Edited on 5 Oct 2022)
Are you trying to get the current time, or the time when the post was made? You can get the time a post was made using String readCreatedAt()
You can see the documentation here at the github site.
If you want the present time, you can write to a channel and then read it back immediately to get a time that will be accurte to a second or so.
I'd put a tiny delay between the two read commands, just to be nice to the server.
PHTS = ...
delay(100);
ECTS = ...
I would also advise you against sharing your Read API jeys, they are kind of like passwords.
You can actually read two fields at once with one read command using the ThingSpeak library, but its probably not worth it for just the two fields of data you are getting above.
Im not sure what other kind of help you are looking for with the code. Is it working? Does it do what you need?
Jay Wan
Jay Wan on 7 Oct 2022
Hello Chris,
Thank you for your advise. My little experiment with ThingSpeak with Mega Wifi is completed. Able to get what I wanted from TS and displayed on LCD. Sadly unable to load a video here.
Christopher Stapels
Christopher Stapels on 7 Oct 2022
Will pass on your desire to post a video. Thanks for letting us know about your project, Im glad you got it working. Hackster is a good place to share these kind of projects if you get the chance and want to share what you have done.
Jay Wan
Jay Wan on 6 Oct 2022
I finally able to remove the Z
And then, I realised the time was +11 hours behind (AEST)
2022-10-06T16:29:00+11:00 ====> 2022-10-06 05:29:00 (Serial monitor)
I suppose a string unable to +11 hours?
Thanks again for your advise.
Jay Wan
Jay Wan on 1 Sep 2022
Couldn't agree more on Mega Wifi!
Your project looks awesome. Wish I could do the same! Hope you won't mind me 'copycat'. Have a great day! Thanks again.
Jay Wan
Jay Wan on 31 Aug 2022
Thanks Chris!