Emailing instance variable how?(Solved, thanks Tim)

Hello everyone,

I have got a temperature logger for my fridge , once the temperature detected at 9 degree, it will email
me . and the temperature declared as : float temp ;

my question is , how can I or can I send the “temp” as temperature through email .

  • or I have to convert to string ?

I have tried this and got “time out”.

client.print((float)(temp -1));

I am new at this , please be kind :slight_smile:

This is Tim emailing code working great …

void whenTemperatureDetectsendEmail ()
 {


    if (temp >= 9)

{
    if(sendEmail()) Serial.println("Email sent"); //start sendEmail()
   
}  else {
    Serial.println("Email failed");
  }
}

//////////////////////////////// email function
byte sendEmail()  //sendEmail() function that returns 1 or 0
 
    {
      byte thisByte = 0;
      byte respCode;
     
      if(client.connect(mailServer,25) == 1) {
        Serial.println(F("connected"));
      } else {
        Serial.println(F("connection failed"));
        return 0;
      }
     
      if(!eRcv()) return 0;
      Serial.println(F("Sending helo"));
     
    // change to your public ip
      client.println(F("EHLO      "));
     
      if(!eRcv()) return 0;
      Serial.println(F("Sending From"));
     
    // change to your email address (sender)
      client.println(F("MAIL From: <myemail>"));
     
      if(!eRcv()) return 0;
     
    // change to recipient address
      Serial.println(F("Sending To"));
      client.println(F("RCPT To: <myotheremail>"));
     
      if(!eRcv()) return 0;
     
      Serial.println(F("Sending DATA"));
      client.println(F("DATA"));
     
      if(!eRcv()) return 0;
     
      Serial.println(F("Sending email"));
     
    // change to recipient address
      client.println(F("To: <myotheremail>"));
     
    // change to your address
      client.println(F("From:  <myemail>"));
     
      client.println(F("Subject:  Fridge needed Attention\r\n"));
     
      client.println(F("Temperature way above:"));
    


I NEED HELP HERE  ...................... 

  

   // client.print((float)(temp -1));
     


    client.println(F("."));
     
      if(!eRcv()) return 0;
     
      Serial.println(F("Sending QUIT"));
      client.println(F("QUIT"));
     
      if(!eRcv()) return 0;
     
      client.stop();
     
      Serial.println(F("disconnected"));
     
      return 1;
    }
     
    byte eRcv()
    {
      byte respCode;
      byte thisByte;
      int loopCount = 0;
     
      while(!client.available()) {
        delay(1);
        loopCount++;
     
        // if nothing received for 10 seconds, timeout
        if(loopCount > 10000) {
          client.stop();
          Serial.println(F("\r\nTimeout"));
          return 0;
        }
      }
     
      respCode = client.peek();
     
      while(client.available())
      {  
        thisByte = client.read();    
        Serial.write(thisByte);
      }
     
      if(respCode >= '4')
      {
        efail();
        return 0;  
      }
     
      return 1;
    }
     
     
    void efail()
    {
      byte thisByte = 0;
      int loopCount = 0;
     
      client.println(F("QUIT"));
     
      while(!client.available()) {
        delay(1);
        loopCount++;
     
        // if nothing received for 10 seconds, timeout
        if(loopCount > 10000) {
          client.stop();
          Serial.println(F("\r\nTimeout"));
          return;
        }
      }
     
      while(client.available())
      {  
        thisByte = client.read();    
        Serial.write(thisByte);
      }
     
      client.stop();
     
      Serial.println(F("disconnected"));
            
    
    }

I don't think client.print can convert a float. I haven't tried it lately. Last time I had to use dtostrf into a character array, then client.print it.. http://www.atmel.com/webdoc/AVRLibcReferenceManual/group__avr__stdlib_1ga060c998e77fb5fc0d3168b3ce8771d42.html

Thank you for quick reply Tim.. will working on it when I off work :)

I just tested the email send with the client.println((float)12.345,3); and it worked for me.

I sent an attached image with the float value and this is what I got in my email box:

This is an image from my Arduino! 12.345

Thank you Tim,

I just have to call the temperature again within the function , and it work great.

sensors.requestTemperatures(); // get data from DS18B20
//Serial.println(“Requesting temperatures”);
sensors.requestTemperatures();

temp = sensors.getTempCByIndex(0);

and added

client.println((float)temp,2);

work great…

Thank you …
oh , one more question . can you give me some hints… what about if email doesn’t send…

I need to wait for 10 minutes and try again ?

void whenTemperatureDetectsendEmail ()
 {


    sensors.requestTemperatures();                         // get data from DS18B20
    //Serial.println("Requesting temperatures");
    sensors.requestTemperatures();
    
    temp = sensors.getTempCByIndex(0);  



    if (temp >= 9)

{
    if(sendEmail()) Serial.println("Email sent"); //start sendEmail()
   
}  else {
    Serial.println("Email failed");
  }
}

//////////////////////////////// email function
byte sendEmail()  //sendEmail() function that returns 1 or 0
 
    {
      byte thisByte = 0;
      byte respCode;
     
      if(client.connect(mailServer,25) == 1) {
        Serial.println(F("connected"));
      } else {
        Serial.println(F("connection failed"));
        return 0;
      }
     
      if(!eRcv()) return 0;
      Serial.println(F("Sending helo"));
     
    // change to your public ip
      client.println(F("EHLO      "));
     
      if(!eRcv()) return 0;
      Serial.println(F("Sending From"));
     
    // change to your email address (sender)
      client.println(F("MAIL From: <myemail>"));
     
      if(!eRcv()) return 0;
     
    // change to recipient address
      Serial.println(F("Sending To"));
      client.println(F("RCPT To: <myotheremail>"));
     
      if(!eRcv()) return 0;
     
      Serial.println(F("Sending DATA"));
      client.println(F("DATA"));
     
      if(!eRcv()) return 0;
     
      Serial.println(F("Sending email"));
     
    // change to recipient address
      client.println(F("To: <myotheremail>"));
     
    // change to your address
      client.println(F("From:  <myemail>"));
     
      client.println(F("Subject:  Fridge needed Attention\r\n"));
     
      client.println(F("Temperature way above:"));
   


 

    client.println((float)temp,2);
     


    client.println(F("."));
     
      if(!eRcv()) return 0;
     
      Serial.println(F("Sending QUIT"));
      client.println(F("QUIT"));
     
      if(!eRcv()) return 0;
     
      client.stop();
     
      Serial.println(F("disconnected"));
     
      return 1;
    }
     
    byte eRcv()
    {
      byte respCode;
      byte thisByte;
      int loopCount = 0;
     
      while(!client.available()) {
        delay(1);
        loopCount++;
     
        // if nothing received for 10 seconds, timeout
        if(loopCount > 10000) {
          client.stop();
          Serial.println(F("\r\nTimeout"));
          return 0;
        }
      }
     
      respCode = client.peek();
     
      while(client.available())
      { 
        thisByte = client.read();   
        Serial.write(thisByte);
      }
     
      if(respCode >= '4')
      {
        efail();
        return 0; 
      }
     
      return 1;
    }
     
     
    void efail()
    {
      byte thisByte = 0;
      int loopCount = 0;
     
      client.println(F("QUIT"));
     
      while(!client.available()) {
        delay(1);
        loopCount++;
     
        // if nothing received for 10 seconds, timeout
        if(loopCount > 10000) {
          client.stop();
          Serial.println(F("\r\nTimeout"));
          return;
        }
      }
     
      while(client.available())
      { 
        thisByte = client.read();   
        Serial.write(thisByte);
      }
     
      client.stop();
     
      Serial.println(F("disconnected"));
           
   
    }

Correction Tim,

my "temp" variable already a global .

I used "client.print " instead of "client.println" .

client.println work great now ..