I have the following sketch which sends an email on startup but I want to make it loop and send the same message every 10 minutes and want it to read a sensor from GPIO 0 and send this value within the email body.
I know I need something in the void loop() section and msg body but am unsure how to do it. Is this possible & can anyone provide the solution?
#include "Arduino.h"
#include <EMailSender.h>
#include <ESP8266WiFi.h>
const char* ssid = "<YOUR-SSID>";
const char* password = "<YOUR-PASSWD>";
uint8_t connection_state = 0;
uint16_t reconnect_interval = 10000;
EMailSender emailSend("[email protected]", "password");
uint8_t WiFiConnect(const char* nSSID = nullptr, const char* nPassword = nullptr)
{
static uint16_t attempt = 0;
Serial.print("Connecting to ");
if(nSSID) {
WiFi.begin(nSSID, nPassword);
Serial.println(nSSID);
}
uint8_t i = 0;
while(WiFi.status()!= WL_CONNECTED && i++ < 50)
{
delay(200);
Serial.print(".");
}
++attempt;
Serial.println("");
if(i == 51) {
Serial.print("Connection: TIMEOUT on attempt: ");
Serial.println(attempt);
if(attempt % 2 == 0)
Serial.println("Check if access point available or SSID and Passwordrn");
return false;
}
Serial.println("Connection: ESTABLISHED");
Serial.print("Got IP address: ");
Serial.println(WiFi.localIP());
return true;
}
void Awaits()
{
uint32_t ts = millis();
while(!connection_state)
{
delay(50);
if(millis() > (ts + reconnect_interval) && !connection_state){
connection_state = WiFiConnect();
ts = millis();
}
}
}
void setup()
{
Serial.begin(115200);
connection_state = WiFiConnect(ssid, password);
if(!connection_state) // if not connected to WIFI
Awaits(); // constantly trying to connect
EMailSender::EMailMessage message;
message.subject = "Hello World";
message.message = "Hello from NodeMCU";
EMailSender::Response resp = emailSend.send("[email protected]", message);
Serial.println("Sending status: ");
Serial.println(resp.status);
Serial.println(resp.code);
Serial.println(resp.desc);
}
void loop()
{
}