How to send email once inside loop on temperature changes over 30C

I have a temp. & humidity program on esp-01 which display temp. & humidity in a web page
And it works fine, But I need to send email alert when the temp reaches 30C and I do that
but it sends a lot of emails as the temperature is 30C
I need to send the email one time only when the temp. changes above 30C

My code is :

=============

void Mail()
{
// Notice the email sent message only if ip is auto
// When I try static ip it fail to send
EMailSender::EMailMessage message;
message.subject = "Data Center Temprature is HIGH";
// message.message = "Warrning
The Temperature is high
The Temprature is Temperature" ;
message.message = Temperature;
EMailSender::Response resp = emailSend.send("myemail@gmail.com", message);
Serial.println("Sending status: ");
Serial.println(resp.status);
Serial.println(resp.code);
Serial.println(resp.desc);
}

void setup() {
Serial.begin(115200);
delay(100);

pinMode(DHTPin, INPUT);
pinMode(0, OUTPUT);
dht.begin();

Serial.println("Connecting to ");
Serial.println(ssid);

//connect to your local wi-fi network
// this line to connect with atatic ip
// if you remove it it will take dhcp ip
// WiFi.config(staticIP, subnet, gateway );
WiFi.begin(ssid, password);

//check wi-fi is connected to wi-fi network
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected..!");
Serial.print("Got IP: "); Serial.println(WiFi.localIP());

server.on("/", handle_OnConnect);
server.onNotFound(handle_NotFound);

server.begin();
Serial.println("HTTP server started");

}

void loop() {
digitalWrite(0, HIGH); // this switch led on gpio00 on
if ( Temperature > 30) // if temprature is over than 30
Mail(); // Send warning Email
server.handleClient();

digitalWrite(0, LOW);
delay(1000);

}

void handle_OnConnect() {

Temperature = dht.readTemperature(); // Gets the values of the temperature
Humidity = dht.readHumidity(); // Gets the values of the humidity
server.send(200, "text/html", SendHTML(Temperature,Humidity));
}

void handle_NotFound(){
server.send(404, "text/plain", "Not found");
}

String SendHTML(float TempCstat,float Humiditystat){
String ptr = " \n";
ptr +="<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">\n";
ptr +="<link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,600\" rel="stylesheet">\n";
ptr +=" Temp @ humidity \n";
ptr +="html { font-family: 'Open Sans', sans-serif; display: block; margin: 0px auto; text-align: center;color: #333333;}\n";
ptr +="body{margin-top: 50px;}\n";
ptr +="h1 {margin: 50px auto 30px;}\n";
ptr +=".side-by-side{display: inline-block;vertical-align: middle;position: relative;}\n";
ptr +=".humidity-icon{background-color: #3498db;width: 30px;height: 30px;border-radius: 50%;line-height: 36px;}\n";
ptr +=".humidity-text{font-weight: 600;padding-left: 15px;font-size: 19px;width: 160px;text-align: left;}\n";
ptr +=".humidity{font-weight: 300;font-size: 60px;color: #3498db;}\n";
ptr +=".temperature-icon{background-color: #f39c12;width: 30px;height: 30px;border-radius: 50%;line-height: 40px;}\n";
ptr +=".temperature-text{font-weight: 600;padding-left: 15px;font-size: 19px;width: 160px;text-align: left;}\n";
ptr +=".temperature{font-weight: 300;font-size: 60px;color: #f39c12;}\n";
ptr +=".superscript{font-size: 17px;font-weight: 600;position: absolute;right: -20px;top: 15px;}\n";
ptr +=".data{padding: 10px;}\n";
ptr +="\n";
ptr +="\n";
ptr +="\n";
ptr +="\n";

ptr +="<div id="webpage">\n";

ptr +="

Temp @ humidity

\n";
ptr +="<div class="data">\n";
ptr +="<div class="side-by-side temperature-icon">\n";
ptr +="<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg\" xmlns:xlink="http://www.w3.org/1999/xlink\" x="0px" y="0px"\n";
ptr +="width="9.915px" height="22px" viewBox="0 0 9.915 22" enable-background="new 0 0 9.915 22" xml:space="preserve">\n";
ptr +="<path fill="#FFFFFF" d="M3.498,0.53c0.377-0.331,0.877-0.501,1.374-0.527C5.697-0.04,6.522,0.421,6.924,1.142\n";
ptr +="c0.237,0.399,0.315,0.871,0.311,1.33C7.229,5.856,7.245,9.24,7.227,12.625c1.019,0.539,1.855,1.424,2.301,2.491\n";
ptr +="c0.491,1.163,0.518,2.514,0.062,3.693c-0.414,1.102-1.24,2.038-2.276,2.594c-1.056,0.583-2.331,0.743-3.501,0.463\n";
ptr +="c-1.417-0.323-2.659-1.314-3.3-2.617C0.014,18.26-0.115,17.104,0.1,16.022c0.296-1.443,1.274-2.717,2.58-3.394\n";
ptr +="c0.013-3.44,0-6.881,0.007-10.322C2.674,1.634,2.974,0.955,3.498,0.53z"/>\n";
ptr +="\n";
ptr +="\n";
ptr +="<div class="side-by-side temperature-text">Temperature\n";
ptr +="<div class="side-by-side temperature">";
ptr +=(int)TempCstat;
ptr +="<span class="superscript">°C\n";
ptr +="\n";
ptr +="<div class="data">\n";
ptr +="<div class="side-by-side humidity-icon">\n";
ptr +="<svg version="1.1" id="Layer_2" xmlns="http://www.w3.org/2000/svg\" xmlns:xlink="http://www.w3.org/1999/xlink\" x="0px" y="0px"\n"; width="12px" height="17.955px" viewBox="0 0 13 17.955" enable-background="new 0 0 13 17.955" xml:space="preserve">\n";
ptr +="<path fill="#FFFFFF" d="M1.819,6.217C3.139,4.064,6.5,0,6.5,0s3.363,4.064,4.681,6.217c1.793,2.926,2.133,5.05,1.571,7.057\n";
ptr +="c-0.438,1.574-2.264,4.681-6.252,4.681c-3.988,0-5.813-3.107-6.252-4.681C-0.313,11.267,0.026,9.143,1.819,6.217">\n";
ptr +="\n";
ptr +="\n";
ptr +="<div class="side-by-side humidity-text">Humidity\n";
ptr +="<div class="side-by-side humidity">";
ptr +=(int)Humiditystat;
ptr +="<span class="superscript">%\n";
ptr +="\n";

ptr +="\n";
ptr +="\n";
ptr +="\n";
return ptr;
}

===============

Please help

The usual way to do something once is to have a variable to keep track of whether it has been sent - let's call it messageSent. Start with messageSent = false; and then your sending code can be like this

if (messageSent == false) {
   // code to send the message
   messageSent = true;
}

...R

Yes that could work but I need to send one email on every temperature change over 30C
Means
when temp. is 30 send one email
when it becomes 31 send one email
when it becomes 32 send one email

And so on

amsteen:
Yes that could work but I need to send one email on every temperature change over 30C

Perhaps something like this

if (temp - previousTemp >= 1 and messageSent == true) {
   messageSent = false;
}

...R

First of all .... many thanks to you

I do it like this

// IF Temperature < 30 and email is not sent before ===> send email and mark that email is sent
// otherwise do not send
if ( Temperature < 30 and messageSent == false ) {
Mail0();
messageSent = true;
}
// IF Temperature > 30 and email is not sent before ===> send email and mark that email is sent
// otherwise do not send
if ( Temperature > 30 and messageSent == false ) {
Mail();
messageSent = true; }

previousTemp=Temperature ;

// read Temperature again
server.handleClient();

// IF Temperature is changed up and email is already sent before ===> mark that email is not sent
// otherwise leave email is marked as sent
if (Temperature - previousTemp >= 2 and messageSent == true) {
messageSent = false;

It works but I have a problem that if the temp. fulls down under 30C it should send only one email message to tell that temp return to normal but it send one email every 2 degree decreases.

Can you check it ..

another issue sending email take a while and during this sending web server is stop until finish sending
Is there any modification to keep web server running during email message sending ??

amsteen:
It works but I have a problem that if the temp. fulls down under 30C it should send only one email message to tell that temp return to normal but it send one email every 2 degree decreases.

I think you now have enough hints so that you should be able to figure that out yourself. Try a bit more and if you still have a problem post the program that represents your best attempt and tell us in detail what it actually does and what you want it to do that is different. It will make it much easier to focus on the parts you need help with rather than wasting time on things that you can do.

another issue sending email take a while and during this sending web server is stop until finish sending
Is there any modification to keep web server running during email message sending ??

You need to post the complete program.

...R

Sorry as I am beginner I do my best but it is not stable and I suspect there is a problem with 3 if statement in same place

my full code is :

==============

#include "Arduino.h"
#include <EMailSender.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include "DHT.h"

uint8_t connection_state = 0;
uint16_t reconnect_interval = 10000;
EMailSender emailSend("**", "");

const char* STEEN;

#define DHTTYPE DHT11 // DHT 11

const char* ssid = "*********"; // Enter SSID here
const char
password = "***********"; //Enter Password here

ESP8266WebServer server(80);

// DHT Sensor
uint8_t DHTPin = 2;

// Initialize DHT sensor.
DHT dht(DHTPin, DHTTYPE);

float Temperature;
float Humidity;
float By;
float messageSent;
float previousTemp;

void Mail()
{
EMailSender::EMailMessage message;
message.subject = "Data Center Temprature is HIGH";
message.message = Temperature;
EMailSender::Response resp = emailSend.send("*****************", message);
}

void Mail0()
{
EMailSender::EMailMessage message;
message.subject = "Data Center Temprature Back To Normal";
message.message = Temperature;
EMailSender::Response resp = emailSend.send("************", message);
}

void setup() {
Serial.begin(115200);
delay(100);

pinMode(DHTPin, INPUT);
pinMode(0, OUTPUT);
dht.begin();

Serial.println("Connecting to ");
Serial.println(ssid);

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected..!");
Serial.print("Got IP: "); Serial.println(WiFi.localIP());

server.on("/", handle_OnConnect);
server.onNotFound(handle_NotFound);

server.begin();
Serial.println("HTTP server started");

}

void loop() {
digitalWrite(0, HIGH); // this switch led on gpio00 on

// this should send email one time when temp full down 30C
if ( Temperature < 30 and messageSent == false ) {
Mail0(); // Send temp back to normal Email
messageSent = true;
}
// IF Temperature > 30 and email is not sent before ===> send one email and mark that email is sent
if ( Temperature > 30 and messageSent == false ) { // if temprature is over than 30
Mail(); // Send warning Email
messageSent = true; }

previousTemp=Temperature ;

// read Temperature again
server.handleClient();

// IF Temperature is rised over 30C and email is already sent before ===> mark that email is not sent
if (Temperature - previousTemp >= 2 and messageSent == true) {
messageSent = false; }

digitalWrite(0, LOW);
delay(1000);

}

void handle_OnConnect() {

Temperature = dht.readTemperature(); // Gets the values of the temperature
Humidity = dht.readHumidity(); // Gets the values of the humidity
server.send(200, "text/html", SendHTML(Temperature,Humidity));

}

void handle_NotFound(){
server.send(404, "text/plain", "Not found");
}

String SendHTML(float TempCstat,float Humiditystat){
String ptr = " \n";
ptr +="<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">\n";
ptr +="<link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,600\" rel="stylesheet">\n";
ptr +=" ESP8266 Weather Report \n";
ptr +="html { font-family: 'Open Sans', sans-serif; display: block; margin: 0px auto; text-align: center;color: #333333;}\n";
ptr +="body{margin-top: 50px;}\n";
ptr +="h1 {margin: 50px auto 30px;}\n";
ptr +=".side-by-side{display: inline-block;vertical-align: middle;position: relative;}\n";
ptr +=".humidity-icon{background-color: #3498db;width: 30px;height: 30px;border-radius: 50%;line-height: 36px;}\n";
ptr +=".humidity-text{font-weight: 900;padding-left: 15px;font-size: 25px;width: 160px;text-align: left;}\n";
ptr +=".humidity{font-weight: bold;font-size: 60px;color: #3498db;}\n";
ptr +=".temperature-icon{background-color: #f39c12;width: 30px;height: 30px;border-radius: 50%;line-height: 40px;}\n";
ptr +=".temperature-text{font-weight: bold;padding-left: 15px;font-size: 25px;width: 160px;text-align: left;}\n";
ptr +=".temperature{font-weight: bold;font-size: 60px;color: #f39c12;}\n";
ptr +=".by-text{font-weight: bold;padding-left: 15px;font-size: 15px;width: 400px;text-align: left;text-decoration: underline;color: #1221f3;}\n";
ptr +=".superscript{font-size: 19px;font-weight: 600;position: absolute;right: -20px;top: 15px;}\n";
ptr +=".data{padding: 10px;}\n";
ptr +="\n";
ptr +="\n";
ptr +="\n";
ptr +="\n";

ptr +="<div id="webpage">\n";

ptr +="

Temprature and humidity

\n";
ptr +="<div class="data">\n";
ptr +="<div class="side-by-side temperature-icon">\n";
ptr +="<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg\" xmlns:xlink="http://www.w3.org/1999/xlink\" x="0px" y="0px"\n";
ptr +="width="9.915px" height="22px" viewBox="0 0 9.915 22" enable-background="new 0 0 9.915 22" xml:space="preserve">\n";
ptr +="<path fill="#FFFFFF" d="M3.498,0.53c0.377-0.331,0.877-0.501,1.374-0.527C5.697-0.04,6.522,0.421,6.924,1.142\n";
ptr +="c0.237,0.399,0.315,0.871,0.311,1.33C7.229,5.856,7.245,9.24,7.227,12.625c1.019,0.539,1.855,1.424,2.301,2.491\n";
ptr +="c0.491,1.163,0.518,2.514,0.062,3.693c-0.414,1.102-1.24,2.038-2.276,2.594c-1.056,0.583-2.331,0.743-3.501,0.463\n";
ptr +="c-1.417-0.323-2.659-1.314-3.3-2.617C0.014,18.26-0.115,17.104,0.1,16.022c0.296-1.443,1.274-2.717,2.58-3.394\n";
ptr +="c0.013-3.44,0-6.881,0.007-10.322C2.674,1.634,2.974,0.955,3.498,0.53z"/>\n";
ptr +="\n";
ptr +="\n";
ptr +="<div class="side-by-side temperature-text">Temperature\n";
ptr +="<div class="side-by-side temperature">";
ptr +=(int)TempCstat;
ptr +="<span class="superscript">°C\n";
ptr +="\n";
ptr +="<div class="data">\n";
ptr +="<div class="side-by-side humidity-icon">\n";
ptr +="<svg version="1.1" id="Layer_2" xmlns="http://www.w3.org/2000/svg\" xmlns:xlink="http://www.w3.org/1999/xlink\" x="0px" y="0px"\n"; width="12px" height="17.955px" viewBox="0 0 13 17.955" enable-background="new 0 0 13 17.955" xml:space="preserve">\n";
ptr +="<path fill="#FFFFFF" d="M1.819,6.217C3.139,4.064,6.5,0,6.5,0s3.363,4.064,4.681,6.217c1.793,2.926,2.133,5.05,1.571,7.057\n";
ptr +="c-0.438,1.574-2.264,4.681-6.252,4.681c-3.988,0-5.813-3.107-6.252-4.681C-0.313,11.267,0.026,9.143,1.819,6.217">\n";
ptr +="\n";
ptr +="\n";
ptr +="<div class="side-by-side humidity-text">Humidity\n";
ptr +="<div class="side-by-side humidity">";
ptr +=(int)Humiditystat;
ptr +="<span class="superscript">%\n";
ptr +="\n";
ptr +="\n";
ptr +="\n";
return ptr;
}

As I tell I have two issues :-

  1. When the temp. full down under 30C it should send only one email message to tell that temp return
    ** to normal but it send one email every 2 degree decreases.**
  2. Sending email take a while and during this sending web server is stop until finish sending
    ** Is there any modification to keep web server running during email message sending ??**

To make it easy for people to help you please modify your post and use the code button </>
codeButton.png

so your code 
looks like this

and is easy to copy to a text editor. See How to use the Forum

Also please use the AutoFormat tool to indent your code consistently for easier reading.

Your code is too long for me to study quickly without copying to my text editor.

From Reply #6 ...

As I tell I have two issues :-

  1. When the temp. full down under 30C it should send only one email message to tell that temp return
    to normal but it send one email every 2 degree decreases.

What have you done to try to solve that problem?

...R

Sorry as I am beginner I do my best but it is not stable and I suspect there is a problem with 3 if statement in same place

my full code is :

#include "Arduino.h"
#include <EMailSender.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include "DHT.h"

    uint8_t connection_state = 0;
    uint16_t reconnect_interval = 10000;
    EMailSender emailSend("***********", "*********");


const char* STEEN;

#define DHTTYPE DHT11   // DHT 11

const char* ssid = "**********";  // Enter SSID here
const char* password = "***********";  //Enter Password here

ESP8266WebServer server(80);

// DHT Sensor
uint8_t DHTPin = 2;
               
// Initialize DHT sensor.
DHT dht(DHTPin, DHTTYPE);               

float Temperature;
float Humidity;
float By;
float messageSent;
float previousTemp;


void Mail()
{
   EMailSender::EMailMessage message;
    message.subject = "Data Center Temprature is HIGH";
    message.message = Temperature;
    EMailSender::Response resp = emailSend.send("*****************", message);
}

void Mail0()
{
   EMailSender::EMailMessage message;
    message.subject = "Data Center Temprature Back To Normal";
    message.message = Temperature;
    EMailSender::Response resp = emailSend.send("************", message);
}


void setup() {
  Serial.begin(115200);
  delay(100);
 
  pinMode(DHTPin, INPUT);
  pinMode(0, OUTPUT);
  dht.begin();             

  Serial.println("Connecting to ");
  Serial.println(ssid);


  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
  delay(1000);
  Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected..!");
  Serial.print("Got IP: ");  Serial.println(WiFi.localIP());
 
  server.on("/", handle_OnConnect);
  server.onNotFound(handle_NotFound);

  server.begin();
  Serial.println("HTTP server started");

   

}


void loop() {
  digitalWrite(0, HIGH);  // this switch led on gpio00 on

     //  this should send email one time when temp full down 30C
     if ( Temperature < 30 and messageSent == false )   {     
         Mail0();               // Send temp back to normal Email
         messageSent = true;
                                                        }
    //  IF Temperature  > 30   and  email is not sent before ===> send one email and mark that email is sent
    if ( Temperature > 30 and messageSent == false )   {  // if temprature is over than 30   
         Mail();               // Send warning Email
         messageSent = true;                           }
   
                                                       
    previousTemp=Temperature ;
   
    // read Temperature again
    server.handleClient();
 
    //  IF Temperature is rised over 30C and email is already sent before ===> mark that email is not sent
    if (Temperature - previousTemp >= 2 and messageSent == true) {     
          messageSent = false;                                   }

     
  digitalWrite(0, LOW);
  delay(1000);
 
  }

void handle_OnConnect() {

 Temperature = dht.readTemperature(); // Gets the values of the temperature
  Humidity = dht.readHumidity(); // Gets the values of the humidity
  server.send(200, "text/html", SendHTML(Temperature,Humidity));

}

void handle_NotFound(){
  server.send(404, "text/plain", "Not found");
}

String SendHTML(float TempCstat,float Humiditystat){
  String ptr = "<!DOCTYPE html> <html>\n";
  ptr +="<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
  ptr +="<link href=\"https://fonts.googleapis.com/css?family=Open+Sans:300,400,600\" rel=\"stylesheet\">\n";
  ptr +="<title> ESP8266 Weather Report </title>\n";
  ptr +="<style>html { font-family: 'Open Sans', sans-serif; display: block; margin: 0px auto; text-align: center;color: #333333;}\n";
  ptr +="body{margin-top: 50px;}\n";
  ptr +="h1 {margin: 50px auto 30px;}\n";
  ptr +=".side-by-side{display: inline-block;vertical-align: middle;position: relative;}\n";
  ptr +=".humidity-icon{background-color: #3498db;width: 30px;height: 30px;border-radius: 50%;line-height: 36px;}\n";
  ptr +=".humidity-text{font-weight: 900;padding-left: 15px;font-size: 25px;width: 160px;text-align: left;}\n";
  ptr +=".humidity{font-weight: bold;font-size: 60px;color: #3498db;}\n";
  ptr +=".temperature-icon{background-color: #f39c12;width: 30px;height: 30px;border-radius: 50%;line-height: 40px;}\n";
  ptr +=".temperature-text{font-weight: bold;padding-left: 15px;font-size: 25px;width: 160px;text-align: left;}\n";
  ptr +=".temperature{font-weight: bold;font-size: 60px;color: #f39c12;}\n";
  ptr +=".by-text{font-weight: bold;padding-left: 15px;font-size: 15px;width: 400px;text-align: left;text-decoration: underline;color: #1221f3;}\n";
  ptr +=".superscript{font-size: 19px;font-weight: 600;position: absolute;right: -20px;top: 15px;}\n";
  ptr +=".data{padding: 10px;}\n";
  ptr +="</style>\n";
  ptr +="<script>\n";
ptr +="setInterval(loadDoc,200);\n";
ptr +="function loadDoc() {\n";
ptr +="var xhttp = new XMLHttpRequest();\n";
ptr +="xhttp.onreadystatechange = function() {\n";
ptr +="if (this.readyState == 4 && this.status == 200) {\n";
ptr +="document.getElementById(\"webpage\").innerHTML =this.responseText}\n";
ptr +="};\n";
ptr +="xhttp.open(\"GET\", \"/\", true);\n";
ptr +="xhttp.send();\n";
ptr +="}\n";
ptr +="</script>\n";
  ptr +="</head>\n";
  ptr +="<body>\n";
 
   ptr +="<div id=\"webpage\">\n";
   
   ptr +="<h1> Temprature and humidity </h1>\n";
      ptr +="<div class=\"data\">\n";
   ptr +="<div class=\"side-by-side temperature-icon\">\n";
   ptr +="<svg version=\"1.1\" id=\"Layer_1\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" x=\"0px\" y=\"0px\"\n";
   ptr +="width=\"9.915px\" height=\"22px\" viewBox=\"0 0 9.915 22\" enable-background=\"new 0 0 9.915 22\" xml:space=\"preserve\">\n";
   ptr +="<path fill=\"#FFFFFF\" d=\"M3.498,0.53c0.377-0.331,0.877-0.501,1.374-0.527C5.697-0.04,6.522,0.421,6.924,1.142\n";
   ptr +="c0.237,0.399,0.315,0.871,0.311,1.33C7.229,5.856,7.245,9.24,7.227,12.625c1.019,0.539,1.855,1.424,2.301,2.491\n";
   ptr +="c0.491,1.163,0.518,2.514,0.062,3.693c-0.414,1.102-1.24,2.038-2.276,2.594c-1.056,0.583-2.331,0.743-3.501,0.463\n";
   ptr +="c-1.417-0.323-2.659-1.314-3.3-2.617C0.014,18.26-0.115,17.104,0.1,16.022c0.296-1.443,1.274-2.717,2.58-3.394\n";
   ptr +="c0.013-3.44,0-6.881,0.007-10.322C2.674,1.634,2.974,0.955,3.498,0.53z\"/>\n";
   ptr +="</svg>\n";
   ptr +="</div>\n";
   ptr +="<div class=\"side-by-side temperature-text\">Temperature</div>\n";
   ptr +="<div class=\"side-by-side temperature\">";
   ptr +=(int)TempCstat;
   ptr +="<span class=\"superscript\">°C</span></div>\n";
   ptr +="</div>\n";
   ptr +="<div class=\"data\">\n";
   ptr +="<div class=\"side-by-side humidity-icon\">\n";
   ptr +="<svg version=\"1.1\" id=\"Layer_2\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" x=\"0px\" y=\"0px\"\n\"; width=\"12px\" height=\"17.955px\" viewBox=\"0 0 13 17.955\" enable-background=\"new 0 0 13 17.955\" xml:space=\"preserve\">\n";
   ptr +="<path fill=\"#FFFFFF\" d=\"M1.819,6.217C3.139,4.064,6.5,0,6.5,0s3.363,4.064,4.681,6.217c1.793,2.926,2.133,5.05,1.571,7.057\n";
   ptr +="c-0.438,1.574-2.264,4.681-6.252,4.681c-3.988,0-5.813-3.107-6.252-4.681C-0.313,11.267,0.026,9.143,1.819,6.217\"></path>\n";
   ptr +="</svg>\n";
   ptr +="</div>\n";
   ptr +="<div class=\"side-by-side humidity-text\">Humidity</div>\n";
   ptr +="<div class=\"side-by-side humidity\">";
   ptr +=(int)Humiditystat;
   ptr +="<span class=\"superscript\">%</span></div>\n";
  ptr +="</div>\n";
  ptr +="</body>\n";
  ptr +="</html>\n";
  return ptr;
}

As I tell I have two issues :-

  1. When the temp. full down under 30C it should send only one email message to tell that temp return
    to normal but it send one email every 2 degree decreases.
  2. Sending email take a while and during this sending web server is stop until finish sending
    Is there any modification to keep web server running during email message sending ??

I remove some unnecessary comments and re arrange if statements but it becomes more unstable
So I return to first arrangement again

Try this. I have introduced a separate variable called normalMessageSent

void loop() {
  digitalWrite(0, HIGH);  // this switch led on gpio00 on

        //  this should send email one time when temp full down 30C
    if ( Temperature < 30 and normalMessageSent == false )   {     
         Mail0();               // Send temp back to normal Email
         normalMessageSent = true;
    }
       //  IF Temperature  > 30   and  email is not sent before ===> send one email and mark that email is sent
    if ( Temperature > 30 and messageSent == false )   {  // if temprature is over than 30   
         Mail();               // Send warning Email
         messageSent = true;
         normalMessageSent = false;
    }
                                                
    previousTemp=Temperature ;
   
      // read Tempera ture again
    server.handleClient();
 
      //  IF Temperature is rised over 30C and email is already sent before ===> mark that email is not sent
    if (Temperature - previousTemp >= 2 and messageSent == true) {     
          messageSent = false; 
    }

     
  digitalWrite(0, LOW);
  delay(1000);
 
  }

Please use the AutoFormat tool to indent your code consistently. For example the closing } should be under the IF that it belongs to so when your eye runs down the code it can easily see where the IF block ends.

...R

The Sequence works very good But the device is completely unstable and temperature readings
in very bad the emails messages Comes like this:-

Message Temp. Time
High 31 2.27
Normal 26 2.26
High 33 2.26
Normal 26 2.25
High 31 2.25
Normal 26 2.24
High 31 2.24
Normal 26 2.22
High 33 2.22

High 33 2.22
Normal 26 2.22
High 31 2.22
Normal 26 2.21
High 31 2.21
Normal 26 2.19

and when I try web server browsing readings becomes worth ( Readings changes faster ).
So I try after removing the 3 if statements and I found out that the
Device working perfect and temperature reading is stable.
I do not know what is the problem ??
One Notice sending email message takes a while ( about 3 seconds )
and during this time the web server Stops completely ( I notice that
from a led connected on port 00 and staying on during sending email ).
Can you help please ??

amsteen:
So I try after removing the 3 if statements and I found out that the
Device working perfect and temperature reading is stable.

I do not know what is the problem ??

You have not posted the latest version of your program so I, too, am at a loss.

One Notice sending email message takes a while ( about 3 seconds )
and during this time the web server Stops completely ( I notice that
from a led connected on port 00 and staying on during sending email ).

What is the purpose of the web server? Can you try the program without it?

The only web programming I have done has been on a PC and I have no experience with Arduino SMS stuff.

...R

Very Sorry I forget to sent latest code

#include "Arduino.h"
#include <EMailSender.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include "DHT.h"

uint8_t connection_state = 0;
uint16_t reconnect_interval = 10000;
EMailSender emailSend("********@gmail.com", "************");


//Static IP address configuration
IPAddress staticIP(192, 168, 60, 100); //ESP static ip
IPAddress gateway(192, 168, 60, 1);   //IP Address of your WiFi Router (Gateway)
IPAddress subnet(255, 255, 255, 0);  //Subnet mask
IPAddress dns(8, 8, 8, 8);  //DNS
const char* STEEN;
// Uncomment one of the lines below for whatever DHT sensor type you're using!
#define DHTTYPE DHT11   // DHT 11
// #define DHTTYPE DHT21   // DHT 21 (AM2301)
// #define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321

/*Put your SSID & Password*/
const char* ssid = "************";  // Enter SSID here
const char* password = "************";  //Enter Password here

ESP8266WebServer server(80);

// DHT Sensor
uint8_t DHTPin = 2;

// Initialize DHT sensor.
DHT dht(DHTPin, DHTTYPE);

float Temperature;
float Humidity;
float By;
float messageSent;
float previousTemp;
float normalMessageSent;


void Mail()
{
  // Notice the email sent message only if ip is auto
  // When I try static ip it fail to send
  EMailSender::EMailMessage message;
  message.subject = "Data Center Temprature is HIGH";
  //   message.message = "Warrning 
 The Temperature is high 
 The Temprature is Temperature" ;
  message.message = Temperature;
  EMailSender::Response resp = emailSend.send("************@gmail.com", message);
  Serial.println("Sending status: ");
  Serial.println(resp.status);
  Serial.println(resp.code);
  Serial.println(resp.desc);
}

void Mail0()
{
  // Notice the email sent message only if ip is auto
  // When I try static ip it fail to send
  EMailSender::EMailMessage message;
  message.subject = "Data Center Temprature Back To Normal";
  //   message.message = "Warrning 
 The Temperature is high 
 The Temprature is Temperature" ;
  message.message = Temperature;
  EMailSender::Response resp = emailSend.send("************@gmail.com", message);
  Serial.println("Sending status: ");
  Serial.println(resp.status);
  Serial.println(resp.code);
  Serial.println(resp.desc);
}


void setup() {
  Serial.begin(115200);
  delay(100);

  pinMode(DHTPin, INPUT);
  pinMode(0, OUTPUT);
  dht.begin();

  Serial.println("Connecting to ");
  Serial.println(ssid);

  //connect to your local wi-fi network
  // this line to connect with atatic ip
  // if you remove it it will take dhcp ip
  // WiFi.config(staticIP, subnet, gateway );
  WiFi.begin(ssid, password);

  //check wi-fi is connected to wi-fi network
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected..!");
  Serial.print("Got IP: ");  Serial.println(WiFi.localIP());




  server.on("/", handle_OnConnect);
  server.onNotFound(handle_NotFound);

  server.begin();
  Serial.println("HTTP server started");



}


void loop() {
  digitalWrite(0, HIGH);  // this switch led on gpio00 on


  if ( Temperature < 30 and normalMessageSent == false )   {
    Mail0();              
    normalMessageSent = true;
  }

  if ( Temperature > 30 and messageSent == false )   {  // if temprature is over than 30
    Mail();               
    messageSent = true;
    normalMessageSent = false;
  }


  previousTemp = Temperature ;

  // read Temperature again
  server.handleClient();


  if (Temperature - previousTemp >= 2 and messageSent == true) {      
    messageSent = false;
  }


  digitalWrite(0, LOW);
  delay(1000);

}

void handle_OnConnect() {

  Temperature = dht.readTemperature(); // Gets the values of the temperature
  Humidity = dht.readHumidity(); // Gets the values of the humidity
  server.send(200, "text/html", SendHTML(Temperature, Humidity));

}

void handle_NotFound() {
  server.send(404, "text/plain", "Not found");
}

String SendHTML(float TempCstat, float Humiditystat) {
  String ptr = "<!DOCTYPE html> <html>\n";
  ptr += "<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
  ptr += "<link href=\"https://fonts.googleapis.com/css?family=Open+Sans:300,400,600\" rel=\"stylesheet\">\n";
  ptr += "<title> ESP8266 Weather Report </title>\n";
  ptr += "<style>html { font-family: 'Open Sans', sans-serif; display: block; margin: 0px auto; text-align: center;color: #333333;}\n";
  ptr += "body{margin-top: 50px;}\n";
  ptr += "h1 {margin: 50px auto 30px;}\n";
  ptr += ".side-by-side{display: inline-block;vertical-align: middle;position: relative;}\n";
  ptr += ".humidity-icon{background-color: #3498db;width: 30px;height: 30px;border-radius: 50%;line-height: 36px;}\n";
  ptr += ".humidity-text{font-weight: 900;padding-left: 15px;font-size: 25px;width: 160px;text-align: left;}\n";
  ptr += ".humidity{font-weight: bold;font-size: 60px;color: #3498db;}\n";
  ptr += ".temperature-icon{background-color: #f39c12;width: 30px;height: 30px;border-radius: 50%;line-height: 40px;}\n";
  ptr += ".temperature-text{font-weight: bold;padding-left: 15px;font-size: 25px;width: 160px;text-align: left;}\n";
  ptr += ".temperature{font-weight: bold;font-size: 60px;color: #f39c12;}\n";
  ptr += ".by-text{font-weight: bold;padding-left: 15px;font-size: 15px;width: 400px;text-align: left;text-decoration: underline;color: #1221f3;}\n";
  ptr += ".superscript{font-size: 19px;font-weight: 600;position: absolute;right: -20px;top: 15px;}\n";
  ptr += ".data{padding: 10px;}\n";
  ptr += "</style>\n";
  ptr += "<script>\n";
  ptr += "setInterval(loadDoc,200);\n";
  ptr += "function loadDoc() {\n";
  ptr += "var xhttp = new XMLHttpRequest();\n";
  ptr += "xhttp.onreadystatechange = function() {\n";
  ptr += "if (this.readyState == 4 && this.status == 200) {\n";
  ptr += "document.getElementById(\"webpage\").innerHTML =this.responseText}\n";
  ptr += "};\n";
  ptr += "xhttp.open(\"GET\", \"/\", true);\n";
  ptr += "xhttp.send();\n";
  ptr += "}\n";
  ptr += "</script>\n";
  ptr += "</head>\n";
  ptr += "<body>\n";

  ptr += "<div id=\"webpage\">\n";

  ptr += "<h1> temperature and humidity</h1>\n";
  ptr += "<div class=\"data\">\n";
  ptr += "<div class=\"side-by-side temperature-icon\">\n";
  ptr += "<svg version=\"1.1\" id=\"Layer_1\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" x=\"0px\" y=\"0px\"\n";
  ptr += "width=\"9.915px\" height=\"22px\" viewBox=\"0 0 9.915 22\" enable-background=\"new 0 0 9.915 22\" xml:space=\"preserve\">\n";
  ptr += "<path fill=\"#FFFFFF\" d=\"M3.498,0.53c0.377-0.331,0.877-0.501,1.374-0.527C5.697-0.04,6.522,0.421,6.924,1.142\n";
  ptr += "c0.237,0.399,0.315,0.871,0.311,1.33C7.229,5.856,7.245,9.24,7.227,12.625c1.019,0.539,1.855,1.424,2.301,2.491\n";
  ptr += "c0.491,1.163,0.518,2.514,0.062,3.693c-0.414,1.102-1.24,2.038-2.276,2.594c-1.056,0.583-2.331,0.743-3.501,0.463\n";
  ptr += "c-1.417-0.323-2.659-1.314-3.3-2.617C0.014,18.26-0.115,17.104,0.1,16.022c0.296-1.443,1.274-2.717,2.58-3.394\n";
  ptr += "c0.013-3.44,0-6.881,0.007-10.322C2.674,1.634,2.974,0.955,3.498,0.53z\"/>\n";
  ptr += "</svg>\n";
  ptr += "</div>\n";
  ptr += "<div class=\"side-by-side temperature-text\">Temperature</div>\n";
  ptr += "<div class=\"side-by-side temperature\">";
  ptr += (int)TempCstat;
  ptr += "<span class=\"superscript\">°C</span></div>\n";
  ptr += "</div>\n";
  ptr += "<div class=\"data\">\n";
  ptr += "<div class=\"side-by-side humidity-icon\">\n";
  ptr += "<svg version=\"1.1\" id=\"Layer_2\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" x=\"0px\" y=\"0px\"\n\"; width=\"12px\" height=\"17.955px\" viewBox=\"0 0 13 17.955\" enable-background=\"new 0 0 13 17.955\" xml:space=\"preserve\">\n";
  ptr += "<path fill=\"#FFFFFF\" d=\"M1.819,6.217C3.139,4.064,6.5,0,6.5,0s3.363,4.064,4.681,6.217c1.793,2.926,2.133,5.05,1.571,7.057\n";
  ptr += "c-0.438,1.574-2.264,4.681-6.252,4.681c-3.988,0-5.813-3.107-6.252-4.681C-0.313,11.267,0.026,9.143,1.819,6.217\"></path>\n";
  ptr += "</svg>\n";
  ptr += "</div>\n";
  ptr += "<div class=\"side-by-side humidity-text\">Humidity</div>\n";
  ptr += "<div class=\"side-by-side humidity\">";
  ptr += (int)Humiditystat;
  ptr += "<span class=\"superscript\">%</span></div>\n";
  ptr += "</div>\n";
  ptr += "</body>\n";
  ptr += "</html>\n";
  return ptr;
}

I use web server to check the temp. from out site my working place

I am surprised that you only measure the temperature in the web server code. When does the on function you supplied get invoked?

amsteen:
I use web server to check the temp. from out site my working place

That does not seem to be correct. As far as I can see the temperature is acquired with this code

void handle_OnConnect() {

  Temperature = dht.readTemperature(); // Gets the values of the temperature
  Humidity = dht.readHumidity(); // Gets the values of the humidity
  server.send(200, "text/html", SendHTML(Temperature, Humidity));

}

I presume the handle_OnConnect() function is called when there is a web connection but it seems to me you could just have a function called at regular intervals from loop() to read the temperature - something like this

void readTemperature() {
    if (millis() - previousTempTime >= temperatureInterval) {
        previousTempTime = millis();
        Temperature = dht.readTemperature(); // Gets the values of the temperature
         Humidity = dht.readHumidity(); // Gets the values of the humidity
    }
}

The code in the handle_OnConnect() function could then reduce to

void handle_OnConnect() {
    server.send(200, "text/html", SendHTML(Temperature, Humidity));
}

...R

New Code

#include "Arduino.h"
#include <EMailSender.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include "DHT.h"

uint8_t connection_state = 0;
uint16_t reconnect_interval = 10000;
EMailSender emailSend("********@gmail.com", "************");


//Static IP address configuration
IPAddress staticIP(192, 168, 60, 100); //ESP static ip
IPAddress gateway(192, 168, 60, 1);   //IP Address of your WiFi Router (Gateway)
IPAddress subnet(255, 255, 255, 0);  //Subnet mask
IPAddress dns(8, 8, 8, 8);  //DNS
const char* STEEN;
// Uncomment one of the lines below for whatever DHT sensor type you're using!
#define DHTTYPE DHT11   // DHT 11
// #define DHTTYPE DHT21   // DHT 21 (AM2301)
// #define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321

/*Put your SSID & Password*/
const char* ssid = "************";  // Enter SSID here
const char* password = "************";  //Enter Password here

ESP8266WebServer server(80);

// DHT Sensor
uint8_t DHTPin = 2;

// Initialize DHT sensor.
DHT dht(DHTPin, DHTTYPE);

float Temperature;
float Humidity;
float By;
float messageSent;
float previousTemp;
float normalMessageSent;
unsigned long previousTempTime = 0 ;
unsigned long temperatureInterval = 8000; // 1000 = 1 sec.



void Mail()
{
  // Notice the email sent message only if ip is auto
  // When I try static ip it fail to send
  EMailSender::EMailMessage message;
  message.subject = "Data Center Temprature is HIGH";
  //   message.message = "Warrning 
 The Temperature is high 
 The Temprature is Temperature" ;
  message.message = Temperature;
  EMailSender::Response resp = emailSend.send("************@gmail.com", message);
  Serial.println("Sending status: ");
  Serial.println(resp.status);
  Serial.println(resp.code);
  Serial.println(resp.desc);
}

void readTemperature() {
  if (millis() - previousTempTime >= temperatureInterval) {
    previousTempTime = millis();
    Temperature = dht.readTemperature(); // Gets the values of the temperature
    Humidity = dht.readHumidity(); // Gets the values of the humidity
  }
}

void Mail0()
{
  // Notice the email sent message only if ip is auto
  // When I try static ip it fail to send
  EMailSender::EMailMessage message;
  message.subject = "Data Center Temprature Back To Normal";
  //   message.message = "Warrning 
 The Temperature is high 
 The Temprature is Temperature" ;
  message.message = Temperature;
  EMailSender::Response resp = emailSend.send("************@gmail.com", message);
  Serial.println("Sending status: ");
  Serial.println(resp.status);
  Serial.println(resp.code);
  Serial.println(resp.desc);
}


void setup() {
  Serial.begin(115200);
  delay(100);

  pinMode(DHTPin, INPUT);
  pinMode(0, OUTPUT);
  dht.begin();

  Serial.println("Connecting to ");
  Serial.println(ssid);

  //connect to your local wi-fi network
  // this line to connect with atatic ip
  // if you remove it it will take dhcp ip
  // WiFi.config(staticIP, subnet, gateway );
  WiFi.begin(ssid, password);

  //check wi-fi is connected to wi-fi network
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected..!");
  Serial.print("Got IP: ");  Serial.println(WiFi.localIP());




  server.on("/", handle_OnConnect);
  server.onNotFound(handle_NotFound);

  server.begin();
  Serial.println("HTTP server started");



}


void loop() {
  digitalWrite(0, HIGH);  // this switch led on gpio00 on


  if ( Temperature < 30 and normalMessageSent == false )   {
    Mail0();              
    normalMessageSent = true;
  }

  if ( Temperature > 30 and messageSent == false )   {  // if temprature is over than 30
    Mail();               
    messageSent = true;
    normalMessageSent = false;
  }


  previousTemp = Temperature ;

  // read Temperature again
    readTemperature();


  if (Temperature - previousTemp >= 2 and messageSent == true) {      
    messageSent = false;
  }


  digitalWrite(0, LOW);
  delay(1000);
server.handleClient();
}

void handle_OnConnect() {

  Temperature = dht.readTemperature(); // Gets the values of the temperature
  Humidity = dht.readHumidity(); // Gets the values of the humidity
  server.send(200, "text/html", SendHTML(Temperature, Humidity));

}

void handle_NotFound() {
  server.send(404, "text/plain", "Not found");
}

String SendHTML(float TempCstat, float Humiditystat) {

WEB PAGE NOT CHANGED  
}

I cut some web page section to reduce message max character per message.
I try but the temperature reading still unstable and jumps between 26 ( real reading ) and
33 - 40 - 47
**I think I do mistake with modifications ... please check **

I suggest that you make a copy of that code and remove all the web server stuff and test what remains.

Ok But how to know temperature reading in real time ??

Once you have debugged the reduced version and got it to work, gradually reintroduce the web server code so you can tell if it is breaking something.

amsteen:
I cut some web page section to reduce message max character per message.

Please don't put all your text in bold.

You are still reading the temperature in the handle_OnConnect() function even though I told you not to.

And I agree with the suggestion to take out all the web code and get the basic temperature stuff working.

...R