필터 지우기
필터 지우기

I want to read and display last or latest data only, in my code it's displaying every data I have obtained. Please help, I have mentioned my code below. I just want last water consumption value and total bill amount for one month.

조회 수: 5 (최근 30일)
% Enter your MATLAB Code below
% Read Output Water Quantity over the past month from a ThingSpeak channel and write
% the average to another ThingSpeak channel.
% Channel 1035265 contains data from the MathWorks water supply station, located
% in Natick, Massachusetts. The data is collected once every day. Field
% 3 contains output liquid quantity data.
% Channel ID to read data from
readChannelID = 1035265;
% Output Liquid Quantity Field ID
outputliquidqantityFieldID = 3;
% Channel Read API Key
% If your channel is private, then enter the read API Key between the '' below:
readAPIKey = '';
% Get Output Liquid Quantity data for the last 30 days from the MathWorks water supply
% station channel. Learn more about the THINGSPEAKREAD function by going to
% the Documentation tab on the right side pane of this page.
meter_reading = thingSpeakRead(readChannelID,'Fields', outputliquidqantityFieldID,'Numdays',1,'ReadKey',readAPIKey);
% Calculate the Cost
Billing_cost = 5* (meter_reading/1000);
display(Billing_cost,'Total Billing Cost (INR)');
% Start by setting the channel ID and Alert key. All alert key start with TAK.
data = thingSpeakRead(1035265,"Numdays", 1);
formatSpec = "The Water consumption bill is: %d,%d";
A1 = 5* (meter_reading/1000);
A2 = meter_reading
apiKey = 'TAK1S83KIQN0BBLNS';
alertURL = "https://api.thingspeak.com/alerts/send";
options = weboptions("HeaderFields", ["ThingSpeak-Alerts-API-Key", apiKey ]);
alertBody = sprintf(formatSpec,A1,A2)
alertSubject = sprintf(" Water consumption exceeded 100 kl!");
if meter_reading >= 100
webwrite(alertURL, "body", alertBody, "subject", alertSubject, options);
end

채택된 답변

Rene Chan
Rene Chan 2020년 5월 20일
편집: Rene Chan 2020년 5월 20일
Hi Aman,
You are calling thingSpeakRead with NumDays value of 1. That will give you all the data points (up to 8000) for the past day. If you want only the last data point, you need to substitue NumDays with NumPoints like the following:
meter_reading = thingSpeakRead(readChannelID,'Fields', outputliquidqantityFieldID,'NumPoints',1,'ReadKey',readAPIKey);
For additional information on the thingSpeakRead function, please refer to the document at https://www.mathworks.com/help/thingspeak/thingspeakread.html
Rene

추가 답변 (0개)

커뮤니티

더 많은 답변 보기:  ThingSpeak 커뮤니티

카테고리

Help CenterFile Exchange에서 ThingSpeak에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by