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(“—————-“);
}
}
Esp32 timer issue
Jump to
- English Forum
- Explore
- News
- General Discussion
- FAQ
- Documentation
- Documentation
- Sample Code
- Discussion Forum
- Hardware
- ESP-IDF
- ESP-BOX
- ESP-ADF
- ESP-MDF
- ESP-WHO
- ESP-SkaiNet
- ESP32 Arduino
- IDEs for ESP-IDF
- ESP-AT
- ESP IoT Solution
- ESP RainMaker
- Rust
- ESP8266
- Report Bugs
- Showcase
- Chinese Forum 中文社区
- 活动区
- 乐鑫活动专区
- 讨论区
- 全国大学生物联网设计竞赛乐鑫答疑专区
- ESP-IDF 中文讨论版
- 《ESP32-C3 物联网工程开发实战》书籍讨论版
- 中文文档讨论版
- ESP-AT 中文讨论版
- ESP-BOX 中文讨论版
- ESP IoT Solution 中文讨论版
- ESP-ADF 中文讨论版
- ESP Mesh 中文讨论版
- ESP Cloud 中文讨论版
- ESP-WHO 中文讨论版
- ESP-SkaiNet 中文讨论版
- ESP 生产支持讨论版
- 硬件问题讨论
- 项目展示
Who is online
Users browsing this forum: No registered users and 84 guests
- All times are UTC
- Top
- Delete cookies
About Us
Espressif Systems is a fabless semiconductor company providing cutting-edge low power WiFi SoCs and wireless solutions for wireless communications and Internet of Things applications. ESP8266EX and ESP32 are some of our products.