void thingspeak(float t, float h, float units, int lum, float TempInt, byte eau)
unsigned long myChannelNumber= 00000000;
const char * myWriteAPIKey= "XYZ";
/* sets Thingspeak fields with data */
ThingSpeak.setField(1, t); // assigns the value of field 1 to the outside temperature
ThingSpeak.setField(2, h); // assigns the value of field 2 to the outdoor humidity
ThingSpeak.setField(3, units); // assigns the value of field 3 to the weight
if (PresenceCapteurLuminosite == 1 ){ThingSpeak.setField(4, lum);} // assigns the value of field 4 to the exterior brightness
if (PresenceCapteurTemperatureInterieure == 1 ) {ThingSpeak.setField(5,TempInt);} // assigns the value of field 6 to the indoor temperature
if ( PresenceCapteurPluie == 1 ) {ThingSpeak.setField(6, eau); } // assigns the value of field 6 to rain
ThingSpeak.setStatus(F("Update 2 successful!")); // assigns the Status field with the message Update successful:
/* writing fields in ThingSpeak */
int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey); // we actually write the pre-filled fields above
ecritureJdB(rtc.now(), "We are in ThingSpeak:",'0', x , "=return code"); // send to the log writing function on the SD
Serial.print(F("return code things: "));
reboot = 0; //we have an internet connection so we reinitialize reboot
} // end of ThingSpeak update error step
else {err(6,x); } // if error we go to the error management function
} // end of ThingSpeak function
/* the error code handling function is: */
void err(byte code, byte x)
char tY[5]; // character array for outdoor temperature conversion
String tY1; // character string for outdoor temperature conversion
char hygY[5]; // character array for hygrometry conversion
String hygY1; // character string for hygrometry conversion
reboot = reboot + 1; // we store a non-update
testconnexion(); // we test the internet connection via google
CompteAlertServT = CompteAlertServT + 1; // we increment to trigger the sending of a daily email to report the ThingSpeak update problem
if (AlerteThingSpeak == 1 && OKThingSpeak == 0 && CompteAlertServT >= NbAvantAlerteServeur)
envoimail(rtc.now(),11,String(x),nbessai); // send the daily email on the malfunction
if (soustraitancemail == 1)
FTPMoi(0); // send to the FTP server
OKThingSpeak = 1; CompteAlertServT = 0;
} // if the email sending went well, we set it to send only once a day
ecritureJdB(rtc.now(), "retourmail = ",retourmail, OKThingSpeak , " = OKThingSpeak"); // send to the function for writing the logbook on the SD
commentaires = F("Pb ThingSpeak");
/* and the error management that sends me an alert email after 5 failures per day is: */
/* Email sending function */
boolean envoimail(DateTime now,char quel,String NomFichierMonsuel,int nbessai)
{ //nomfichier mensuel contains either the IP address or the name of the monthly file to send depending on the needs...
EMailSender::EMailMessage message; // structure defined in EmailSender.h
/* Definition of email recipients*/
const char* arrayOfEmail[] = {"mail@laposte.net","mail@gmail.com","rolande.mangin@wanadoo.fr","messagerie@orange.fr" }; // email address of your recipients
byte nb_Destinaire_Mail=3; // number of recipients of the mails (count the email addresses of the line above!
String msgsujet1 = F("Hi, this is hive No. "); // assign a default value
String msgcorps1 = F("Hello, this is hive No. "); // assign a default value
String msgcorps6 = F("<br> Sending mail of the day on ");
String msgcorps7 = F(" H");
String msgcorps8 = F(" Mn");
String msgcorps9 = F(" s.");
String msgcorps10 = F(" to ");
{ // test to know which email to send
{ // send message pb updating ThingSpeak...
String msgsujet2 = F(". There is a problem updating ThingSpeak");
message.subject = msgsujet1 + numRuche + msgsujet2;
String msgcorps2 = F(".<br>The error code of ThingSpeak is: ");
String msgcorps3 = F(".<br>A problem updating ThingSpeak occurred today!<br> We must check if it is just a fleeting problem or if the problem is long-lasting.");
message.message = msgcorps1 + numRuche + msgcorps2 + NomFichierMonsuel + msgcorps3 + msgcorps6 + now.day() + msgcorps10
+ now.hour() + msgcorps7 + now.minute() + msgcorps8 +now.second() + msgcorps9 ; // here I use nomfichiermensuel to transmit the return error code of ThingSpeak