[SOLVED] First email sent from ESP8266 Fails - Subsequent Succeed

Hi all,

I am writing a small arduino program to monitor and alert me when temperature surpasses a certain threshold.

In the setup procedure of my code, I initialize everything and send a mail to the user telling him the device is up and running.
Afterwords I send a test mail every 80 seconds.

Problem is 1st mail (from the setup proc) is lost.

Below is my program and my logfile

Starting ESP8266
Starting Sensors
Starting Blynk
[20293] Connecting to AndreasVDSL
[20293] Connected to WiFi
[20293] My IP: 10.10.10.12
[20293] Blynk v0.3.1
[20293] Connecting to cloud.blynk.cc:8442
[25294] Connecting to cloud.blynk.cc:8442
[25489] <msg 2,1,32
<1f168cdb271c45c5992b477a092c37cb
[25694] >msg 0,1,200
[25694] Ready (ping: 1ms).

Connected
IP:10.10.10.12
Sending Startup Mail and waiting for 70sec
[85695] Cmd not sent
Mail Sent
[85695] Cmd not sent
Notification Sent
Starting Timers
[146696] Cmd not sent
[147696] Cmd not sent
[148696] Cmd not sent
[149696] Cmd not sent
[150696] Cmd not sent
[150697] Connecting to cloud.blynk.cc:8442
[150864] <msg 2,1,32
<1f168cdb271c45c5992b477a092c37cb
[151021] >msg 0,1,200
[151021] Ready (ping: 1ms).
[151696] <msg 20,1,6
<vw10
[152696] <msg 20,2,8
<vw1255
[153696] <msg 20,3,6
<vw10
[154696] <msg 20,4,8
<vw1255
[155696] <msg 20,5,6
<vw10
[156696] <msg 20,6,8
<vw1255
[157696] <msg 20,7,6
<vw10
[158696] <msg 20,8,8
<vw1255
[159696] <msg 20,9,6
<vw10
[160696] <msg 20,10,8
<vw1255
[161022] <msg 6,11,0
[161174] >msg 0,11,200
[161696] <msg 20,12,6
<vw10
[162696] <msg 20,13,8
<vw1255
[163696] <msg 20,14,6
<vw10
[164696] <msg 20,15,8
<vw1255
[165696] <msg 20,16,6
<vw10
[166696] <msg 20,17,8
<vw1255
[167696] <msg 20,18,6
<vw10
[168696] <msg 20,19,8
<vw1255
[169696] <msg 20,20,6
<vw10
[170696] <msg 20,21,8
<vw1255
[171175] <msg 6,22,0
[171326] >msg 0,22,200
[171696] <msg 20,23,6
<vw10
[172696] <msg 20,24,8
<vw1255
[173696] <msg 20,25,6
<vw10
[174696] <msg 20,26,8
<vw1255
BlynkGraphProc()
[176476] <msg 20,27,11
<vw418.312
[176660] <msg 20,28,6
<vw10
[176815] <msg 20,29,8
<vw1255
[177696] <msg 20,30,6
<vw10
[178696] <msg 20,31,8
<vw1255
[179696] <msg 20,32,6
<vw10
[180696] <msg 20,33,8
<vw1255
[181327] <msg 6,34,0
[181479] >msg 0,34,200
[181696] <msg 20,35,6
<vw10
[182696] <msg 20,36,8
<vw1255
[183696] <msg 20,37,6
<vw10
[184696] <msg 20,38,8
<vw1255
[185696] <msg 20,39,6
<vw10
[186696] <msg 20,40,8
<vw1255
[187696] <msg 20,41,6
<vw10
[188696] <msg 20,42,8
<vw1255
[189696] <msg 20,43,6
<vw10
[190696] <msg 20,44,8
<vw1255
[191480] <msg 6,45,0
[191636] >msg 0,45,200
[191696] <msg 20,46,6
<vw10
[192696] <msg 20,47,8
<vw1255
[193696] <msg 20,48,6
<vw10
[194696] <msg 20,49,8
<vw1255
[195696] <msg 20,50,6
<vw10
[196696] <msg 20,51,8
<vw1255
[197696] <msg 20,52,6
<vw10
[198696] <msg 20,53,8
<vw1255
[199696] <msg 20,54,6
<vw10
[200696] <msg 20,55,8
<vw1255
[201637] <msg 6,56,0
[201789] <msg 20,57,6
<vw10
[201980] >msg 0,56,200
[202696] <msg 20,58,8
<vw1255
[203696] <msg 20,59,6
<vw10
[204696] <msg 20,60,8
<vw1255
[205696] <msg 20,61,6
<vw10
BlynkGraphProc()
[206629] <msg 20,62,11
<vw418.312
[206782] <msg 20,63,8
<vw1255
[207696] <msg 20,64,6
<vw10
[208696] <msg 20,65,8
<vw1255
[209696] <msg 20,66,6
<vw10
[210696] <msg 20,67,8
<vw1255
[211696] <msg 20,68,6
<vw10
[211981] <msg 6,69,0
[212134] >msg 0,69,200
[212696] <msg 20,70,8
<vw1255
[213696] <msg 20,71,6
<vw10
[214696] <msg 20,72,8
<vw1255
[215696] <msg 20,73,6
<vw10
HighTempAlarmRepeatProc()
[215852] <msg 13,74,118
<kokkinos.andreas@gmail.comM30 Rittal [10.10.10.12] Device High ALARM ONM30 Rittal [10.10.10.12] Device High ALARM ON
[216696] <msg 20,75,8
<vw1255
[217696] <msg 20,76,6
<vw10
[218696] <msg 20,77,8
<vw1255
[219048] >msg 0,74,200
[219696] <msg 20,78,6
<vw10
[220696] <msg 20,79,8
<vw1255

And here is my program:

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

#define BLYNK_DEBUG // Optional, this enables lots of prints
#define BLYNK_PRINT Serial

#include <OneWire.h>
#include <DallasTemperature.h>
#include "SimpleTimer.h"
#include "ESP8266WiFi.h"
#include <BlynkSimpleEsp8266.h>


#define ONE_WIRE_BUS                12
#define MilisHeartBeat              1000
#define MilisBlynkGraph             30000
#define MilisHighTempAlarm          120000
#define MilisHighTempAlarmRepeat    70000
#define HighTempAlarm               22   

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

SimpleTimer HeartBeatLEDTimer;
SimpleTimer BlynkGraphTimer;
SimpleTimer HighTempAlarmTimer;
SimpleTimer HighTempAlarmRepeatTimer;

const String DeviceId = "M30 Rittal";


const char* ssid = "no info";
const char* password = "no pass here";


WidgetLED led1(1);
String  MyIp;
byte AlarmOn = 0;
static unsigned long AlarmMillisStart = 0;


//====================================================================================================================
//====================================================================================================================
//====================================================================================================================

/*
void time(long val)
{
  int days = elapsedDays(val);
  int hours = numberOfHours(val);
  int minutes = numberOfMinutes(val);
  int seconds = numberOfSeconds(val);

  // digital clock display of current time
  Serial.print(days,DEC);  
  printDigits(hours);  
  printDigits(minutes);
  printDigits(seconds);
  Serial.println();  
}

void printDigits(byte digits)
{
  // utility function for digital clock display: prints colon and leading 0
  Serial.print(":");
  if(digits < 10)
  Serial.print('0');
  Serial.print(digits,DEC);  
}
*/

//====================================================================================================================
//====================================================================================================================
//====================================================================================================================


void HeartBeatLEDProc()
{
  if (led1.getValue()) led1.off();
  else led1.on();
}


//====================================================================================================================
//====================================================================================================================
//====================================================================================================================

void BlynkGraphProc()
{
  sensors.requestTemperatures();
  Serial.println("BlynkGraphProc()");  
  Blynk.virtualWrite(4, sensors.getTempCByIndex(0)); 
}


//====================================================================================================================
//====================================================================================================================
//====================================================================================================================


void HighTempAlarmRepeatProc()
{
  String Subject;
  String Body;

  Serial.println("HighTempAlarmRepeatProc()");  
  //if (AlarmOn == 1)
  {
    Subject = DeviceId + " [" + MyIp + "] Device High ALARM ON";
    Body    = DeviceId + " [" + MyIp + "] Device High ALARM ON";
    Blynk.email("kokkinos.andreas@gmail.com", Subject, Body); 
  }
}

//====================================================================================================================
//====================================================================================================================
//====================================================================================================================


void HighTempAlarmProc()
{
  String Subject;
  String Body;
  
  sensors.requestTemperatures();
  Serial.print("HighTempAlarmProc() ");
  Serial.print(sensors.getTempCByIndex(0));
  Serial.print("  Alarm:");
  Serial.println(AlarmOn);

 
  if (sensors.getTempCByIndex(0) >= HighTempAlarm && AlarmOn == 0)
  {
    AlarmOn = 1;
    AlarmMillisStart = millis();

    Subject = DeviceId + " [" + MyIp + "]  Device High ALARM ON";
    Body    = DeviceId + " [" + MyIp + "]  Device High ALARM ON";
    Serial.println("Sending Mail");
    Blynk.email("kokkinos.andreas@gmail.com", Subject, Body); 
  }
  
  if (sensors.getTempCByIndex(0) < HighTempAlarm && AlarmOn == 1)
  {
    AlarmOn = 0;
    long Milliseconds = millis() - AlarmMillisStart;
    int Hours = Milliseconds / (1000*60*60);
    int Minutes = (Milliseconds % (1000*60*60)) / (1000*60);
    int Seconds = ((Milliseconds % (1000*60*60)) % (1000*60)) / 1000;

    Subject = DeviceId + " [" + MyIp + "]  Device High ALARM OFF   Duration: " + Hours + ":" + Minutes + ":" + Seconds;
    Body    = DeviceId + " [" + MyIp + "]  Device High ALARM OFF   Duration: " + Hours + ":" + Minutes + ":" + Seconds;
    Serial.println("Sending Mail");
    Blynk.email("kokkinos.andreas@gmail.com", Subject, Body); 
  }
}

//====================================================================================================================
//====================================================================================================================
//====================================================================================================================


BLYNK_READ(V3)
{
  sensors.requestTemperatures();
  Serial.println("BLYNK_READ(V3)");  
  Blynk.virtualWrite(3, sensors.getTempCByIndex(0)); 
}

//====================================================================================================================
//====================================================================================================================
//====================================================================================================================

BLYNK_READ(V4)
{
  sensors.requestTemperatures();
  Serial.println("BLYNK_READ(V4)");  
  Blynk.virtualWrite(4, sensors.getTempCByIndex(0)); 
}

//====================================================================================================================
//====================================================================================================================
//====================================================================================================================


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

  Serial.println("Starting ESP8266");
  pinMode(BUILTIN_LED, OUTPUT);
  digitalWrite(BUILTIN_LED, LOW);
  
  Serial.println("Starting Sensors");
  sensors.begin();
  delay(30);

  Serial.println("Starting Blynk");
  Blynk.begin("123", ssid, password);
  while (Blynk.connect() == false) Serial.print(".");
  MyIp =  String(WiFi.localIP()[0]) + "." + String(WiFi.localIP()[2]) + "." + String(WiFi.localIP()[2]) + "." + String(WiFi.localIP()[3]);
  Serial.println();
  Serial.println("Connected");
  Serial.print("IP:");
  Serial.println(MyIp);
  Blynk.run();

  Serial.println("Sending Startup Mail and waiting for 70sec");
  delay(60000);
  Blynk.email("kokkinos.andreas@gmail.com", DeviceId + " Device is UP", DeviceId + " Device is UP" + " IP:" + MyIp); 
  Serial.println("Mail Sent");
  Blynk.notify(DeviceId + " Device is UP");
  Serial.println("Notification Sent");
  delay(60000);

  
  Serial.println("Starting Timers");
  HeartBeatLEDTimer.setInterval(MilisHeartBeat, HeartBeatLEDProc);
  BlynkGraphTimer.setInterval(MilisBlynkGraph, BlynkGraphProc);
  HighTempAlarmTimer.setInterval(MilisHighTempAlarm, HighTempAlarmProc);
  HighTempAlarmRepeatTimer.setInterval(MilisHighTempAlarmRepeat, HighTempAlarmRepeatProc);
}
//====================================================================================================================
//====================================================================================================================
//====================================================================================================================



void loop()
{
  Blynk.run();
  HeartBeatLEDTimer.run();
  BlynkGraphTimer.run();
  HighTempAlarmTimer.run();
  HighTempAlarmRepeatTimer.run();
}

You program uses a lot of long delays. This is definitely not good. Delay() prevents anything from happening. For example, in the setup loop you have a Blynk.run (why do you do that anyway?) and a 60s delay and email after that.

I suggest you use the SimpleTimer for that. That library has lots of capabilities including 1-time timers. The logic and flow of your sketch appear in order, but I think there is a lot of room for improvement if you rethink certain steps, especially all the delays. I suggest you focus on getting those out first :slight_smile:

1 Like

Hi and thank you for the answer.

All the delays were added in order to find a solution. The delays are only for debug reasons.
The 60s delay I added was just to be sure I do not fall in the 60s limit between each email Those delays wont be in the final code.

I repeat that the main problem is that the mail send from the setup procedure never arrives.
ALL THE OTHER emails arrive correctly

A.

@vactirio if you located problem to setup only than please remove all unnecessary code and leave only those part that you think should work but it doesn’t.
Next - delay(60000) before Blynk.mail() for sure will not make it work. Cause usually we have 15 sec readTimeout. So if hardware doesn’t do anything in 15 sec - connection is closed (Blynk.run() sends pings messages to avoid closing). So looking in this code - it shouldn’t work.

Finally after a lot of wondering and investigation I found the problem.

email blynk procedure supports a max of 49 characters in the subject.

If you send a mail with 50 chars, it just does not get delivered.

Andreas

We don’t have such restrictions… Maybe this is some ESP specific problem? @vshymanskyy thoughts?

Don’t call this in the setup(), call connect() instead.
delay() won’t help.

Heh, missed that =).

Hi,

Problem is not solved as marked in the subject. Seems some users found that too:

this one is solved and not related with char limit.