Help with Matlab Analysis Thingspeak script
조회 수: 3 (최근 30일)
이전 댓글 표시
Hi all,
I've built some ESP8266 modules to monitor temperature. A module consists in one ESP8266, one DHT22 and a 18650 to power. It works on 30 min cycles: wake up, connect to wifi, read temperature from sensor, read battery level via ADC, send data to thingspeak, go back to deepsleep. Data sent to thinkspeak is a set of three: Temperature, ADC level (optional, i will discard this once i solve present issue ) and ID tag. ID tags are all gathered by the same chart. My modules send ID tag values of "10x" for good battery level and just "x" for low battery level.
Example: I have three modules with IDs: 1, 2, 3. If all three modules have good battery level, the Thingspeak chart will receive values of 101, 102, 103, 101, 102,103 etc. If 1's battery drops bellow certain level, Thingspeak chart will receive 1, 102, 103, 1, 102, 103.
So I can crate an alert when values drop bellow 100, sending me via email that specific value under 100 received, and i would know what module needs battery replacement.
But I need your help with the script because i can't make it send me the correct email. This one bellow sends me:
"False alarm for: NaN"
I have no experience with MATLAB prior to this:
% Send an email and tell the user the ID for device with low battery
% Store the channel ID for channel.
channelID = 999999999;
% Provide the ThingSpeak alerts API key. All alerts API keys start with TAK.
alertApiKey = 'TAKPdnP+KU0xxx/xxxx';
readapikey = '11HNALxxxxxxxxxx';
% Set the address for the HTTTP call
alertUrl="https://api.thingspeak.com/alerts/send";
% webwrite uses weboptions to add required headers.
% Alerts needs a ThingSpeak-Alerts-API-Key header.
options = weboptions("HeaderFields", ["ThingSpeak-Alerts-API-Key", alertApiKey ]);
% Set the email subject.
alertSubject = sprintf("Low Battery");
% Read the recent data.
monitorData = thingSpeakRead(channelID,Fields=1,NumPoints=9,ReadKey='11HNALGxxxxxxxxxx');
% Check to make sure the data was read correctly from the channel.
if isempty(monitorData)
alertBody = "No data read!";
else
% battery low on device with ID value based on recent data.
ThreshAlert = 100;
% Get the most recent point in the array.
last3vals = monitorData(1:3:end);
lastValue = last3vals(end);
% Set the outgoing message
if (lastValue < ThreshAlert)
alertBody = sprintf("Device ID with low battery is: %d",lastValue);
else
alertBody = sprintf("False alarm for: %d",lastValue);
end
end
% Catch errors so the MATLAB code does not disable a TimeControl if it fails
try
webwrite(alertUrl, "body", alertBody, "subject", alertSubject, options);
catch someException
fprintf("Failed to send alert: %s\n", someException.message);
end
댓글 수: 0
채택된 답변
Christopher Stapels
2023년 1월 25일
편집: Christopher Stapels
2023년 1월 25일
Sorry to leave you hanging for so long.
I would just get the last value (why do you need the last 3 values?
% my suggestion
lastValue = monitorData(end);
% or
% fixing yours
last3vals = monitorData(end-2:end);
lastValue = last3vals(end);
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Propulsion and Power Systems에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!