#include #include #include #include #include #include #include const long utcOffsetInSeconds = 3600; char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; // Define NTP Client to get time WiFiUDP ntpUDP; NTPClient timeClient(ntpUDP, "pool.ntp.org", utcOffsetInSeconds); // WiFi Info const char* ssid = "IoT-Zecevic"; const char* password = "IoTSifra123456!"; //MQTT data const char* mqttServer = "192.168.1.11"; const int mqttPort = 1883; const char* mqttUser = "yourMQTTuser"; const char* mqttPassword = "yourMQTTpassword"; WiFiClient espClient; PubSubClient MQTTclient(espClient); //Mail data #define SMTP_HOST "smtp.ionos.de" #define SMTP_PORT 465 #define AUTHOR_EMAIL "arduino@zecevic-ninja.com" #define AUTHOR_PASSWORD "sdkdoeAadsaseKE!$%$1596" #define RECIPIENT_EMAIL "domagoj.zecevic@gmail.com" // The SMTP Session object used for Email sending SMTPSession smtp; // Callback function to get the Email sending status void smtpCallback(SMTP_Status status); // DHT setup #define DHTPIN 5 #define DHTTYPE DHT11 DHT dht(DHTPIN, DHTTYPE); // GAS senzor setup int Sensor_input = A0; // Global Vars float global_temp; float global_hym; int global_gas_value; bool global_alarm = false; //Start web server WiFiServer server(80); // prepare a web page to be send to a client (web browser) String prepareHtmlPage() { String htmlPage; htmlPage.reserve(1024); // prevent ram fragmentation htmlPage = F("HTTP/1.1 200 OK\r\n" "Content-Type: text/html\r\n" "Connection: close\r\n" // the connection will be closed after completion of the response "Refresh: 5\r\n" // refresh the page automatically every 5 sec "\r\n" "" ""); htmlPage += F("

" "

Temperature: "); htmlPage += global_temp; htmlPage += F("

" "

Humidity: "); htmlPage += global_hym; htmlPage += "

"; htmlPage += F("

" "

Gas Value: "); htmlPage += global_gas_value; htmlPage += "

"; htmlPage += F("

" "

Alarm: "); if (global_alarm == true) { htmlPage += F("TRUE

"); } else { htmlPage += F("FALSE

"); } htmlPage += F("" "\r\n"); return htmlPage; } void initWiFi() { WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); Serial.print("Connecting to WiFi .."); while (WiFi.status() != WL_CONNECTED) { Serial.print('.'); delay(1000); } Serial.println(WiFi.localIP()); } /* Callback function to get the Email sending status */ void smtpCallback(SMTP_Status status){ /* Print the current status */ Serial.println(status.info()); /* Print the sending result */ if (status.success()){ Serial.println("----------------"); ESP_MAIL_PRINTF("Message sent success: %d\n", status.completedCount()); ESP_MAIL_PRINTF("Message sent failled: %d\n", status.failedCount()); Serial.println("----------------\n"); struct tm dt; for (size_t i = 0; i < smtp.sendingResult.size(); i++){ /* Get the result item */ SMTP_Result result = smtp.sendingResult.getItem(i); time_t ts = (time_t)result.timestamp; localtime_r(&ts, &dt); ESP_MAIL_PRINTF("Message No: %d\n", i + 1); ESP_MAIL_PRINTF("Status: %s\n", result.completed ? "success" : "failed"); ESP_MAIL_PRINTF("Date/Time: %d/%d/%d %d:%d:%d\n", dt.tm_year + 1900, dt.tm_mon + 1, dt.tm_mday, dt.tm_hour, dt.tm_min, dt.tm_sec); ESP_MAIL_PRINTF("Recipient: %s\n", result.recipients.c_str()); ESP_MAIL_PRINTF("Subject: %s\n", result.subject.c_str()); } Serial.println("----------------\n"); } } void sendMsg () { timeClient.update(); int hours = timeClient.getHours(); int minutes = timeClient.getMinutes(); int secounds = timeClient.getSeconds(); smtp.debug(1); smtp.callback(smtpCallback); ESP_Mail_Session session; /* Set the session config */ session.server.host_name = SMTP_HOST; session.server.port = SMTP_PORT; session.login.email = AUTHOR_EMAIL; session.login.password = AUTHOR_PASSWORD; session.login.user_domain = ""; /* Declare the message class */ SMTP_Message message; String subject = "Alarm at: "; subject += hours; subject += ":"; subject += minutes; subject += ":"; subject += secounds; /* Set the message headers */ message.sender.name = "ESP"; message.sender.email = AUTHOR_EMAIL; message.subject = subject.c_str(); message.addRecipient("Sara", RECIPIENT_EMAIL); //Send HTML message String htmlMsg = "

ALARM Detected

- gas value: "; htmlMsg += global_gas_value; htmlMsg += "

"; message.html.content = htmlMsg.c_str(); message.text.charSet = "us-ascii"; message.html.transfer_encoding = Content_Transfer_Encoding::enc_7bit; if (!smtp.connect(&session)) return; //Start sending Email and close the session if (!MailClient.sendMail(&smtp, &message)) Serial.println("Error sending Email, " + smtp.errorReason()); } void setup() { Serial.begin(115200); initWiFi(); dht.begin(); // Start DHT server.begin(); Serial.printf("Web server started, open %s in a web browser\n", WiFi.localIP().toString().c_str()); //connect to MQTT MQTTclient.setServer(mqttServer, mqttPort); //MQTTclient.setCallback(callback); while (!MQTTclient.connected()) { if (MQTTclient.connect("ESP32Client", mqttUser, mqttPassword )) { Serial.println("Connected to mqtt!"); } else { Serial.print("failed with state "); Serial.print(MQTTclient.state()); delay(2000); } } } void loop() { int sensor_Aout = analogRead(Sensor_input); //Analog value read function global_gas_value = sensor_Aout; Serial.print("Gas Sensor: "); Serial.print(global_gas_value); //Read value printed Serial.print("\t"); Serial.print("\t"); if (global_gas_value > 500) { Serial.println("Gas"); global_alarm = true; sendMsg (); } else { Serial.println("No Gas"); global_alarm = false; } delay(2000); global_temp = dht.readTemperature(); global_hym = dht.readHumidity(); Serial.print("Temperatrure: "); Serial.println(global_temp); Serial.print("Humidity: "); Serial.println(global_hym); MQTTclient.publish("my/3dprinters/Temperature", String(global_temp).c_str()); MQTTclient.publish("my/3dprinters/Humidity", String(global_hym).c_str()); MQTTclient.publish("my/3dprinters/Gas", String(global_gas_value).c_str()); WiFiClient serverClient = server.available(); if (serverClient) { while (serverClient.connected()) { // read line by line what the client (web browser) is requesting if (serverClient.available()) { String line = serverClient.readStringUntil('\r'); if (line.length() == 1 && line[0] == '\n') { serverClient.println(prepareHtmlPage()); break; } } } while (serverClient.available()) { serverClient.read(); } // close the connection: serverClient.stop(); } }