Esp32 timer issue
Posted: Mon Dec 21, 2020 11:29 pm
I found this code on another website that works for me. It reads the temperature and sends me email alerts. I'm trying to get the code to send me a email every morning at a specific time. Im time and timealarm to accomplish this. I getting a error saying 'alarm' does not name a type. Could someone help me out. Thank you.
[#include <WiFi.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include “ESP32_MailClient.h”
#include <TimeLib.h>
#include <TimeAlarms.h>
const char* ssid = “123456”;
const char* password = “123456”;
#define emailSenderAccount “1234567@gmail.com”
#define emailSenderPassword “1234”
#define smtpServer “smtp.gmail.com”
#define smtpServerPort 465
#define emailSubject “[ALERT] CES Building Loop Temperature”
String inputMessage = “1234567@gmail.com”;
String enableEmailChecked = “checked”;
String inputMessage2 = “true”;
// Default Temperature Thresholds
String inputMessage3 = “95”;
String inputMessage4 = “65”;
String lastTemperature;
const char index_html[] PROGMEM = R”rawliteral(
Email Notification with Temperature
DS18B20 Temperature
%TEMPERATURE% °F
ESP Email Notification
Email Address
Enable Email Notification
Upper Temperature Threshold
Lower Temperature Threshold
)rawliteral”;
void notFound(AsyncWebServerRequest *request) {
request->send(404, “text/plain”, “Not found”);
}
AsyncWebServer server(80);
String processor(const String& var){
//Serial.println(var);
if(var == “TEMPERATURE”){
return lastTemperature;
}
else if(var == “EMAIL_INPUT”){
return inputMessage;
}
else if(var == “ENABLE_EMAIL”){
return enableEmailChecked;
}
else if(var == “UPPER_THRESHOLD”){
return inputMessage3;
}
else if(var == “LOWER_THRESHOLD”){
return inputMessage4;
}
return String();
}
bool emailSent = false;
const char* PARAM_INPUT_1 = “email_input”;
const char* PARAM_INPUT_2 = “enable_email_input”;
const char* PARAM_INPUT_3 = “upper_threshold_input”;
const char* PARAM_INPUT_4 = “lower_threshold_input”;
unsigned long previousMillis = 0;
const long interval = 5000;
const int oneWireBus = 4;
OneWire oneWire(oneWireBus);
DallasTemperature sensors(&oneWire);
SMTPData smtpData;
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
if (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println(“WiFi Failed!”);
return;
}
Serial.println();
Serial.print(“ESP IP Address: http://“);
Serial.println(WiFi.localIP());
sensors.begin();
server.on(“/”, HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, “text/html”, index_html, processor);
});
server.on(“/get”, HTTP_GET, [] (AsyncWebServerRequest *request) {
if (request->hasParam(PARAM_INPUT_1)) {
inputMessage = request->getParam(PARAM_INPUT_1)->value();
if (request->hasParam(PARAM_INPUT_2)) {
inputMessage2 = request->getParam(PARAM_INPUT_2)->value();
enableEmailChecked = "checked";
}
else {
inputMessage2 = "false";
enableEmailChecked = "";
}
if (request->hasParam(PARAM_INPUT_3)) {
inputMessage3 = request->getParam(PARAM_INPUT_3)->value();
}
if (request->hasParam(PARAM_INPUT_4)) {
inputMessage4 = request->getParam(PARAM_INPUT_4)->value();
}
}
else {
inputMessage = "No message sent";
}
Serial.println(inputMessage);
Serial.println(inputMessage2);
Serial.println(inputMessage3);
Serial.println(inputMessage4);
request->send(200, "text/html", "HTTP GET request sent to your ESP.<br><a href=\"/\">Return to Home Page</a>");
});
server.onNotFound(notFound);
server.begin();
}
Alarm.alarmRepeat(7,0,0, MorningAlarm); // 7:00am every day
void loop() {
unsigned long currentMillis = millis();
if (currentMillis – previousMillis >= interval) {
previousMillis = currentMillis;
sensors.requestTemperatures();
// Temperature in Fahrenheit degrees
float temperature = sensors.getTempFByIndex(0);
Serial.print(temperature);
Serial.println(” *F”);
// Temperature in Fahrenheit degrees
/*float temperature = sensors.getTempFByIndex(0);
SerialMon.print(temperature);
SerialMon.println(" *F");*/
lastTemperature = String(temperature);
// Check if temperature is above threshold and if it needs to send the Email alert
if(temperature > inputMessage3.toFloat() && inputMessage2 == "true" && !emailSent){
String emailMessage = String("Temperature above threshold. Current temperature: ") +
String(temperature) + String("F");
if(sendEmailNotification(emailMessage)) {
Serial.println(emailMessage);
emailSent = true;
}
void MorningAlarm () {
Serial.println(temerature);
}
else {
Serial.println(“Email failed to send”);
}
}
// Check if temperature is below threshold and if it needs to send the Email alert
if(temperature < inputMessage4.toFloat() && inputMessage2 == “true” && !emailSent){
String emailMessage = String(“Temperature below threshold. Current temperature: “) +
String(temperature) + String(“F”);
if(sendEmailNotification(emailMessage)) {
Serial.println(emailMessage);
emailSent = true;
}
else {
Serial.println(“Email failed to send”);
}
}
}
}
bool sendEmailNotification(String emailMessage){
// Set the SMTP Server Email host, port, account and password
smtpData.setLogin(smtpServer, smtpServerPort, emailSenderAccount, emailSenderPassword);
// For library version 1.2.0 and later which STARTTLS protocol was supported,the STARTTLS will be
// enabled automatically when port 587 was used, or enable it manually using setSTARTTLS function.
//smtpData.setSTARTTLS(true);
// Set the sender name and Email
smtpData.setSender(“CES Loop Sensor”, emailSenderAccount);
// Set Email priority or importance High, Normal, Low or 1 to 5 (1 is highest)
smtpData.setPriority(“High”);
// Set the subject
smtpData.setSubject(emailSubject);
// Set the message with HTML format
smtpData.setMessage(emailMessage, true);
// Add recipients
smtpData.addRecipient(inputMessage);
smtpData.setSendCallback(sendCallback);
// Start sending Email, can be set callback function to track the status
if (!MailClient.sendMail(smtpData)) {
Serial.println(“Error sending Email, ” + MailClient.smtpErrorReason());
return false;
}
// Clear all data from Email object to free memory
smtpData.empty();
return true;
}
// Callback function to get the Email sending status
void sendCallback(SendStatus msg) {
// Print the current status
Serial.println(msg.info());
// Do something when complete
if (msg.success()) {
Serial.println(“—————-“);
}
}
[#include <WiFi.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include “ESP32_MailClient.h”
#include <TimeLib.h>
#include <TimeAlarms.h>
const char* ssid = “123456”;
const char* password = “123456”;
#define emailSenderAccount “1234567@gmail.com”
#define emailSenderPassword “1234”
#define smtpServer “smtp.gmail.com”
#define smtpServerPort 465
#define emailSubject “[ALERT] CES Building Loop Temperature”
String inputMessage = “1234567@gmail.com”;
String enableEmailChecked = “checked”;
String inputMessage2 = “true”;
// Default Temperature Thresholds
String inputMessage3 = “95”;
String inputMessage4 = “65”;
String lastTemperature;
const char index_html[] PROGMEM = R”rawliteral(
Email Notification with Temperature
DS18B20 Temperature
%TEMPERATURE% °F
ESP Email Notification
Email Address
Enable Email Notification
Upper Temperature Threshold
Lower Temperature Threshold
)rawliteral”;
void notFound(AsyncWebServerRequest *request) {
request->send(404, “text/plain”, “Not found”);
}
AsyncWebServer server(80);
String processor(const String& var){
//Serial.println(var);
if(var == “TEMPERATURE”){
return lastTemperature;
}
else if(var == “EMAIL_INPUT”){
return inputMessage;
}
else if(var == “ENABLE_EMAIL”){
return enableEmailChecked;
}
else if(var == “UPPER_THRESHOLD”){
return inputMessage3;
}
else if(var == “LOWER_THRESHOLD”){
return inputMessage4;
}
return String();
}
bool emailSent = false;
const char* PARAM_INPUT_1 = “email_input”;
const char* PARAM_INPUT_2 = “enable_email_input”;
const char* PARAM_INPUT_3 = “upper_threshold_input”;
const char* PARAM_INPUT_4 = “lower_threshold_input”;
unsigned long previousMillis = 0;
const long interval = 5000;
const int oneWireBus = 4;
OneWire oneWire(oneWireBus);
DallasTemperature sensors(&oneWire);
SMTPData smtpData;
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
if (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println(“WiFi Failed!”);
return;
}
Serial.println();
Serial.print(“ESP IP Address: http://“);
Serial.println(WiFi.localIP());
sensors.begin();
server.on(“/”, HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, “text/html”, index_html, processor);
});
server.on(“/get”, HTTP_GET, [] (AsyncWebServerRequest *request) {
if (request->hasParam(PARAM_INPUT_1)) {
inputMessage = request->getParam(PARAM_INPUT_1)->value();
if (request->hasParam(PARAM_INPUT_2)) {
inputMessage2 = request->getParam(PARAM_INPUT_2)->value();
enableEmailChecked = "checked";
}
else {
inputMessage2 = "false";
enableEmailChecked = "";
}
if (request->hasParam(PARAM_INPUT_3)) {
inputMessage3 = request->getParam(PARAM_INPUT_3)->value();
}
if (request->hasParam(PARAM_INPUT_4)) {
inputMessage4 = request->getParam(PARAM_INPUT_4)->value();
}
}
else {
inputMessage = "No message sent";
}
Serial.println(inputMessage);
Serial.println(inputMessage2);
Serial.println(inputMessage3);
Serial.println(inputMessage4);
request->send(200, "text/html", "HTTP GET request sent to your ESP.<br><a href=\"/\">Return to Home Page</a>");
});
server.onNotFound(notFound);
server.begin();
}
Alarm.alarmRepeat(7,0,0, MorningAlarm); // 7:00am every day
void loop() {
unsigned long currentMillis = millis();
if (currentMillis – previousMillis >= interval) {
previousMillis = currentMillis;
sensors.requestTemperatures();
// Temperature in Fahrenheit degrees
float temperature = sensors.getTempFByIndex(0);
Serial.print(temperature);
Serial.println(” *F”);
// Temperature in Fahrenheit degrees
/*float temperature = sensors.getTempFByIndex(0);
SerialMon.print(temperature);
SerialMon.println(" *F");*/
lastTemperature = String(temperature);
// Check if temperature is above threshold and if it needs to send the Email alert
if(temperature > inputMessage3.toFloat() && inputMessage2 == "true" && !emailSent){
String emailMessage = String("Temperature above threshold. Current temperature: ") +
String(temperature) + String("F");
if(sendEmailNotification(emailMessage)) {
Serial.println(emailMessage);
emailSent = true;
}
void MorningAlarm () {
Serial.println(temerature);
}
else {
Serial.println(“Email failed to send”);
}
}
// Check if temperature is below threshold and if it needs to send the Email alert
if(temperature < inputMessage4.toFloat() && inputMessage2 == “true” && !emailSent){
String emailMessage = String(“Temperature below threshold. Current temperature: “) +
String(temperature) + String(“F”);
if(sendEmailNotification(emailMessage)) {
Serial.println(emailMessage);
emailSent = true;
}
else {
Serial.println(“Email failed to send”);
}
}
}
}
bool sendEmailNotification(String emailMessage){
// Set the SMTP Server Email host, port, account and password
smtpData.setLogin(smtpServer, smtpServerPort, emailSenderAccount, emailSenderPassword);
// For library version 1.2.0 and later which STARTTLS protocol was supported,the STARTTLS will be
// enabled automatically when port 587 was used, or enable it manually using setSTARTTLS function.
//smtpData.setSTARTTLS(true);
// Set the sender name and Email
smtpData.setSender(“CES Loop Sensor”, emailSenderAccount);
// Set Email priority or importance High, Normal, Low or 1 to 5 (1 is highest)
smtpData.setPriority(“High”);
// Set the subject
smtpData.setSubject(emailSubject);
// Set the message with HTML format
smtpData.setMessage(emailMessage, true);
// Add recipients
smtpData.addRecipient(inputMessage);
smtpData.setSendCallback(sendCallback);
// Start sending Email, can be set callback function to track the status
if (!MailClient.sendMail(smtpData)) {
Serial.println(“Error sending Email, ” + MailClient.smtpErrorReason());
return false;
}
// Clear all data from Email object to free memory
smtpData.empty();
return true;
}
// Callback function to get the Email sending status
void sendCallback(SendStatus msg) {
// Print the current status
Serial.println(msg.info());
// Do something when complete
if (msg.success()) {
Serial.println(“—————-“);
}
}