Send email with the value of a channel

guy caluwaerts on 9 Dec 2023
Latest activity Reply by Christopher Stapels on 21 Dec 2023

I want to control the level my water tank with a esp8266 and Thingspeak.
I can see the level in Thingspeak. This is working.
Now I want to send 2 emails :
  1. One daily email with the actual level
  2. Alarm email when level is below a setpoint.
How can I incorporate the value from a channel in the email ?
What I have now for the daily email , but with errors :
alert_body = 'huidig peil regenput';
channelID = ..........;
% Provide the ThingSpeak alerts API key. All alerts API keys start with TAK.
alertApiKey = 'TAK...............';
% 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("Niveau regenput " );
% Read the recent data.
peil = thingSpeakRead(channelID,'Fields',1);
% Check to make sure the data was read correctly from the channel.
% Set the outgoing message
webwrite(alertUrl , "body", alertBody, "subject", alertSubject,'Fields',peil);
% Catch errors so the MATLAB code does not disable a TimeControl if it fails
try
webwrite(alertUrl , "body", alert_body, "subject", alertSubject, options);
catch someException
fprintf("Failed to send alert: %s\n", someException.message);
end
The errors I receive :
Unrecognized function or variable 'alert_Body'.
Error in Read Channel to Trigger Email 1 (line 24)
webwrite(alertUrl , "body", alert_Body, "subject", alertSubject,"Fields",peil);
Manny thanks in advance
Christopher Stapels
Christopher Stapels on 11 Dec 2023
MATLAB is case sensitive.
alert_body is not equal to alert_Body
Be careful with daily emails, you are limited to 800 yearly alert emails. (but there are enough for one per day or more)
Your code example shows how to put a channel value in the email.
peil = thingSpeakRead(channelID,'Fields',1);
% Check to make sure the data was read correctly from the channel.
% Set the outgoing message
webwrite(alertUrl , "body", alertBody, "subject", alertSubject,'Fields',peil);
This will make the subject equal to the value from field 1
guy caluwaerts
guy caluwaerts on 20 Dec 2023
Hi Christopher,
I did the notification and don't receive no errors anymore.
Unfortunately I also don't receive the emails in my mailbox.
% 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("Niveau regenput " );
% Read the recent data.
peil = thingSpeakRead(channelID,'Fields',1);
% Set the outgoing message
try
webwrite(alertUrl , "body", alertBody, "subject", alertSubject,'Fields',peil);
end
Christopher Stapels
Christopher Stapels on 20 Dec 2023
Check your junk folder and make sure you arent blocking thingspeak.com at your email server. You can also take the try ..end.. syntax off, and remove the semicolon on the webwrite line to test and make sure the command was sent.
guy caluwaerts
guy caluwaerts on 20 Dec 2023
What do you mean with junk folder ?
When I do save and run I receive 2 messages.
  1. MATLAB code ran successfully.
  2. Failed to send alert: The server returned the status 401 with message "" in response to the request to URL https://api.thingspeak.com/alerts/send.
Christopher Stapels
Christopher Stapels on 20 Dec 2023
401 means unauthorized. Check your alerts api key. By junk folder i menat for email, but its clear its not sending.
guy caluwaerts
guy caluwaerts on 20 Dec 2023
I copy past het API key again but no result.
Christopher Stapels
Christopher Stapels on 20 Dec 2023
does the api key begin with TAK?
guy caluwaerts
guy caluwaerts on 20 Dec 2023
yes. I use the alertapikey
alertApiKey = 'TAKMAxjn5Ad1TrzrNWH';
I also renewed already the key, but no result.
Christopher Stapels
Christopher Stapels on 20 Dec 2023
You are forgetting the options when you webwrite. Thats why its 401 unauthorized. No key included with the requiest.
webwrite(alertUrl , "body", alertBody, "subject", alertSubject, options);
guy caluwaerts
guy caluwaerts on 20 Dec 2023
webwrite(alertUrl , "body", alertBody, "subject", alertSubject, peil, options);
doesn't work
webwrite(alertUrl , "body", alertBody, "subject", alertSubject, options);
Was sending an email once, but without the value of channel 1
Afterwards I received the message when I run the app :
"Failed to send alert: Each postName must be given a corresponding postValue."
Christopher Stapels
Christopher Stapels on 21 Dec 2023
try seperating the actions.
bob = thingSpeakRead(channelID,'Field',1)); % assumes data is numeric, otherwise use 'outputformat','timetable'
alertBody = "The value of my field is " + bob;
guy caluwaerts
guy caluwaerts on 21 Dec 2023
Christopher,
I followed this example. You only send an email with text. I want to send text + the last value from channel 1.
What is working for the moment:
1. sending text with : alertBody = 'huidig peil regenput :';
2. Sending value of channel 1 : alertBody = thingSpeakRead(channelID,'Field',1);
What is not working is the combination of both. It must be something like this, but this syntax is not accepted : alertBody = ('huidig peil regenput :' ,thingSpeakRead(channelID,'Field',1));