Danielle Honigstein in MATLAB Answers
Última actividad el 27 de Abr. de 2023

Hi all, I have configured React to send an email when I receive data to my channel. My channel recieves data occasionally from an Android app, once every 15 minutes for the space of some hours. Usually this works fine, but occasionally I do not recieve an email. I see that the app sent the data to the channel correctly and received a reply. The React shows that the code sending the email was run without errors (showing the green checkmark and showing last run 1 minute ago). As it was 15 minutes since the last request, it shouldn't cause the 429 error (too many requests). How can I find what the problem was? The email was not received. Thanks, Danielle
Mohd Shahzrie in MATLAB Answers
Última actividad el 5 de Dic. de 2021

% Store the channel ID for the sensors channel. channelID = 1517893; % Provide the ThingSpeak alerts API key. All alerts API keys start with TAK. alertApiKey = 'TAKQXKX7Xxxxxxxxxxx'; % 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("Weather Monitoring System Update"); % Read the recent data. temperatureData = thingSpeakRead(channelID,'NumDays',1,'Fields',1); humidityData = thingSpeakRead(channelID,'NumDays',1,'Fields',2); % Check to make sure the data was read correctly from the channel. if temperatureData > 27 & humidityData < 80 fprintf("Sunny day \n") alertBody = "The temperature is above 27°C while the humidity is below 75% which indicates that today is probably gonna be a sunny day."; elseif temperatureData < 27 & humidityData > 80 fprintf("Rainy day \n") alertBody = "The temperature is below 27°C while the humidity is above 75% which indicates that rain is likely to be coming."; elseif temperatureData < 27 & humidityData < 80 fprintf("Forecast \n") alertBody = "The temperature is below 27°C while the humidity is below 75% which indicates that there's a high chance of the weather being overcast"; elseif temperatureData > 27 & humidityData > 80 fprintf("Sunshower \n") alertBody = "The temperature is above 27°C while the humidity is above 75% which indicates that there will probably be a rare weather occurence which is sunshower"; else alertBody = "No data read for today's weather." 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
Chris in Discussions
Última actividad el 2 de Jun. de 2021

The timestamp for the ThingSpeak Alerts I create look like this: "Time: 2021-06-01 06-21-45.001 -04:00" Using a dash (-) instead of a colon (:) between the hour and minutes and minutes and seconds seems very non-standard to me. I've always seen it with a colon. Is there any way to change the dashes in the time part of the stamp to colons so the above looks like 06:21:45.001? (I presume this is set in "HeaderFields" but don't know how to change it. Thanks. Change Time Format in ThingSpeak Alert? Can you show how you are generating that timestamp with an alert? I think the standard ThingSpeak format is ISO 8601. The created_at timestamps generally adhere to this, and seen in this snippet from the <https://www.mathworks.com/help/thingspeak/writedata.html write data page> . (Optional) Date when feed entry was created, in ISO 8601 format, for example: 2014-12-31 23:59:59. The date you specify must be unique within the channel. Time zones can be specified using the timezone parameter. This comes out of a MATLAB Analyis: alert_url= "https://api.thingspeak.com/alerts/send"; jsonmessage = sprintf(['{"subject": "%s", "body": "%s"}'], alert_subject,alert_body); options = weboptions("HeaderFields", {'Thingspeak-Alerts-API-Key', alert_api_key; 'Content-Type','application/json'}); result = webwrite(alert_url, jsonmessage, options); See this link: https://www.mathworks.com/help/thingspeak/analyze-channel-data-to-send-email.html on which it is based. The time has the same format in that example. Thanks for pointing that out to me. You cannot change the format of the timestamp part of the response, but you can add a timestamp to the body message. For example, on the line that says alertBody = ' No water needed. '; you can add a timestamp. 'No water needed at 2021-06-01 12:43:24 You can use <https://www.mathworks.com/help/matlab/ref/datetime.html datetime> with a format specifier to put the timestamp however you like it. Then add it to the alert body using <https://www.mathworks.com/help/matlab/ref/sprintf.html sprintf> . OK, but is there a way to eliminate the default timestamp if I create my own? If so, please describe. Or do I end up with two timestamps--one formatted normally, and one with the odd default format? If so, I would hope you will consider changing the default. I will log your request. Can you describe why you need the timestamp? Are you parsing the response with some other code? Thanks. I am not parsing the response. I am using the alert to tell me about the time of a fault in a monitored system. I would rather use the time in the alert than the time of receipt of the email for that purpose. Why does the format matter if you are not parsing the response? bEcAuse I AM rEAding the RespoNSE and wouLD LIKe it in A norMAL formAT, oK? time format alert
Mei Sing Wang in MATLAB Answers
Última actividad el 9 de Mzo. de 2020

I follow the instruction in https://www.mathworks.com/help/thingspeak/analyze-channel-data-to-send-email.html?s_eid=EML_16314 However it shows that Failed to send alert: Unrecognized function or variable 'alertBody'. Anyone know why it does not recognized 'alertBody'? Below is my code: channelID = 995250; alertApiKey = 'MY_ALERTS_API_KEY'; alertUrl="https://api.thingspeak.com/alerts/send"; options = weboptions("HeaderFields", ["ThingSpeak-Alerts-API-Key", alertApiKey ]); alertSubject = sprintf("Surrounding temperature and humidity"); moistureData = thingSpeakRead(channelID,'NumDays',30,'Fields',1); if isempty(moistureData) alertBody = ' No data read from surrounding. '; while humidity < 60 or temperature > 32: alertBody = sprintf(" The weather is too hot! ", "The temperature is %0.2f",temp) ; end end try webwrite(alertUrl , "body", alertBody, "subject", alertSubject, options); catch someException fprintf("Failed to send alert: %s\n", someException.message); end

Acerca de ThingSpeak

The community for students, researchers, and engineers looking to use MATLAB, Simulink, and ThingSpeak for Internet of Things applications. You can find the latest ThingSpeak news, tutorials to jump-start your next IoT project, and a forum to engage in a discussion on your latest cloud-based project. You can see answers to problems other users have solved and share how you solved a problem.