Help with Matlab Analysis Thingspeak script

3 visualizaciones (últimos 30 días)
Petre iulian
Petre iulian el 3 de En. de 2023
Comentada: Petre iulian el 26 de En. de 2023
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

Respuesta aceptada

Christopher Stapels
Christopher Stapels el 25 de En. de 2023
Editada: Christopher Stapels el 25 de En. de 2023
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);
  1 comentario
Petre iulian
Petre iulian el 26 de En. de 2023
Hi Christopher,
Thank you for your response. Your suggestion was my first thought, but what I receive is something like this:
102NaNNaN101NaNNaN103NaNNaN
However, the detail in your piece of code helped me figure out how to extract the value I need. Now, the code looks like bellow. In test, running it with a 200 threshold works just fine but I had surprises before. Thank you once again and wish you all the best!
% Send an email and tell the user the ID for device with low battery
% Store the channel ID for channel.
channelID = 15xxxxx;
% Provide the ThingSpeak alerts API key. All alerts API keys start with TAK.
alertApiKey = 'Txxxxxxxxxxxxxxxxx';
readapikey = '11xxxxxxxxxxxx';
% battery low on device with ID value based on recent data.
ThreshAlert = 100;
% 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='11HNxxxxxxxxxxxx');
%% fprintf('%d\n',monitorData);
% Check to make sure the data was read correctly from the channel.
if isempty(monitorData)
alertBody = "No data read!";
else
% Get the most recent point in the array.
last3vals = monitorData(end-2:end);
fprintf("last 3 vals: %d", last3vals);
lastValue = last3vals(end-2);
fprintf("\n last val: %d", lastValue);
% Set the outgoing message
if (lastValue < ThreshAlert)
fprintf("\n // IF //");
message_if = sprintf("Device ID with low battery is: %d",lastValue);
alertBody = message_if;
else
fprintf("\n // ELSE //");
message_else = sprintf("False alarm triggered by: - %d",lastValue);
alertBody = message_else;
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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Propulsion and Power Systems en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by