Reading long numbers in thingspeak

Pedro on 25 Jan 2023
Latest activity Reply by Electrical on 18 Jul 2023

Good morning,
I am trying to receive a long number in thingspeak and convert it to a string. However, when I receive it, it converts it to a number using scientific notation. I have tried changing the format to long g and others and was able to get rid of the scientific notation part.
After doing so, I have used commands such as num2str, mat2str, and compose to represent the number as a string, but I keep getting results that are off. An example is a message i am sending as:
field1=100630000553400000000
using an MQTT desktop client. However, when I receive it and print the result, I get:
0100630000553399992320
The numbers are close but the right one. Is there something I am missing?
Thanks in advance!
Pedro Ch.
Christopher Stapels
Christopher Stapels on 25 Jan 2023
Can you share your code to read the number (and perhaps your channel ID if it is public?)
The key is when the string to number conversion is happening. I recommend keeping it a string at long as possible to keep the literal digits.
Electrical
Electrical on 17 Jul 2023
Thank you for your help. The channel ID is 2221937.
I made it public so you can see the message received.
Christopher Stapels
Christopher Stapels on 17 Jul 2023
When I read your field 1 data, I see
{"created_at": "2023-07-17T09:52:45-04:00","entry_id": 2,"field1": "100630000609200000099"},
Based on your criteria above, this looks like what you hoped for? Can you confirm if that is what you expect?
Are you reading it into MATLAB and then printing there? Can you share the code youy use to read tha values?
Electrical
Electrical on 17 Jul 2023
This is my code, i wil change the API key later. You can check the result register variables does not match what we can normally read using
% Enter your MATLAB code below
% Channel ID to read data from
readChannelID = 2221937;
% Channel Read API Key
% If your channel is private, then enter the read API
% Key between the '' below:
readAPIKey = 'VEMNQ70TRRNSVL1O';
format long
registers2 = num2str(thingSpeakRead(readChannelID,'Fields',1,'ReadKey',readAPIKey),25)
registers2 = num2str(thingSpeakRead(readChannelID,'Fields',2,'ReadKey',readAPIKey),25)
registers3 = num2str(thingSpeakRead(readChannelID,'Fields',3,'ReadKey',readAPIKey),21)
registers4 = num2str(thingSpeakRead(readChannelID,'Fields',4,'ReadKey',readAPIKey),21)
registers5 = num2str(thingSpeakRead(readChannelID,'Fields',7,'ReadKey',readAPIKey),21)
Christopher Stapels
Christopher Stapels on 17 Jul 2023
took me a little bit of futzing, but I got this:
url='https://api.thingspeak.com/channels/2221937/feeds.json?results=1'
kim=webread(url)
registers1 = kim.feeds.field1
registers2 = kim.feeds.field2
...
also consider using thingSpeakRead with 'outputformat','timetable' when you want to read string type data.
url='https://api.thingspeak.com/channels/2221937/feeds.json'
btw, you dont need the api key if the channel is set to public.
Electrical
Electrical on 18 Jul 2023
Thank you, Christopher! I will give it a shot today. I tried changing the output format but it was still showing as scientific notation.