Reading from Thingspeak: Daily averages are easy. How do I get a monthly average?
8 次查看(过去 30 天)
显示 更早的评论
I use the REST API to read data from Thingspeak and this works well:
This request correctly returns one value per day. However I need a monthly average - a single value per month. Ideally something like:
Is there a way to achieve something like this?
2 个评论
Vinod
2019-11-14
One way I can think of doing this is to have MATLAB code that is scheduled to run once a month using the TimeControl app. The MATLAB code in the MATLAB analysis app reads the values form the "raw data" channel and computes the monthly average which gets written into a "derived channel". You can then directly query your "derived channel" to get monthly data.
Can you tell us more about what you are doing? That can help guide us to the appropriate solution.
采纳的回答
Christopher Stapels
2019-11-15
There are built in template codes to help you develop MATLAB code. If you go to the MATLAB analysis or MATLAB Visualizations apps, you can select from the templates after choosing to create a new instance of the app.
For your example, I think you will proably want to preprocess the data and store it in another channel. Reads are limited to 8000 points, so you want be able to read all the data in a single call.
I would reccomend writing a script that reads the data and writes the daily avaerages to another channel. Then your Mobile app can read the values or call the charts API for the derived channel. Going forward, you can set a timeControl to write that daily average to the derived channel every day (as Vinod suggested). Historicall you may have to process them in a few batches.
I can provide more details if you get stuck with the code.
更多回答(1 个)
Christopher Stapels
2019-11-18
编辑:Christopher Stapels
2023-1-21
I also like the look of your result. But I am afraid that it makes a bunch of API requests to the server in a big burst. Its fine for once in a while, but it you are regularly using this kind of code, you could eventually trigger a rate limiting response from the ThingSpeak server. Your code already generates the monthly data, you can write the historical data to the derived channel, and reduce the future burden.
If you didn't want to use your own server-side resources, you could use TimeControl to schedule the MATLAB code to run at regular intervals. I understand not wanting to use a new language, but MATLAB tries pretty hard to be easy to learn.
Just in case you are considering it, the format would look something like this:
Create a new MATLAB analysis with this pseudocode
edited per comments below
numDays=30; % for monthly
readChannelId =123455;
writeChannelId = 543211;
myReadKey='XXXXXXXXXXXXXXXX';
myWriteKey='yyyyyyyyyyyyyyyy';
myData=thingSpeakRead(readChannelID, 'ReadKey',myReadKey,'OutputFortmat','timetable','numDays',numDays);
aveData=retime(myData,'regular','mean','TimeStep',days(numDays));
thingSpeakWrite(writeChannelId, aveData, 'WriteKey', writeAPIKey);
Then use TimeControl to run the Analysis every numDays.
3 个评论
abba baab
2023-1-21
Thanks for the code. Error on last line. aveCar should be aveData.
"MATLAB tries pretty hard to be easy to learn." So true. It is easily the hardest language so far (coming from HTML, CSS, PHP, Arduino and Python). No copy paste wtf. Documentation is horrible and lacks real examples.
Christopher Stapels
2023-1-21
Thanks for the syntax suggestion, I have fixed it above.
I'm sorry to hear you are frustrated with our documentation, its realtively well known as very high quality from most reports. If you provide an example that you didnt like, I can look into it.
社区
更多回答在 ThingSpeak Community
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Read Data from Channel 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!