matlab code stop working (alertbody error)

Gabriel Noira on 2 Jul 2021 (Edited on 2 Jul 2021)
Latest activity Reply by Christopher Stapels on 2 Jul 2021

here is my code, i want to recive an email notification when my "alerta" field increase.

   % Read the soil moisture channel data from the past two weeks.
  % Send an email and tell the user to add water if the value
  % is in the lowest 10 %.
  % Store the channel ID for the moisture sensor channel.
  channelID = 1425554;
  % Provide the ThingSpeak alerts API key.  All alerts API keys start with TAK.
  alertApiKey = 'TXXXXXXXXXXXX';
  % 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("Alertas");
  % Read the recent data.
  Alerta = thingSpeakRead(channelID,'NumDays',30,'Fields',8);
      % Get the most recent point in the array of moisture data.
      lastValue = Alerta(end); 
      % Set the outgoing message
      if (lastValue<=Alerta)
         alertBody='alerta de anomalia de fluxo de agua';    
      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
Christopher Stapels
Christopher Stapels on 2 Jul 2021

The title of your post suggests you got an error. Can you copy the full text of the error? How often are you writing to your channel? 30 days may be a lot of points. ThingSpeakRead is limited to 8000 data points. I've redacted your alerts API key. Others could use the api key to send emails from your account, and use up your yearly allotment. I recommend thinking of API keys as passwords.

Tags