Example sketches for the new Arduino IDE for ESP8266

Moderator: igrr

User avatar
By torntrousers
#19550 As an alternative to having the ESP send the email you could use the FRED NodeRed service - https://fred.sensetecnic.com/

Thats gives you a free cloud based NodeRed server instance and you can set up an http enpoint which an ESP can send data to and then have a Node Red flow that does things with the data like sending emails. There is a little learning curve to learn about NodeRed but its really quite easy.

As an example, here's one i have where an ESP sends in soil moisture readings from a hygrometer sensor and the NodeRed flow looks at the value and if the soil is getting dry it sends a tweet saying it needs watering or if it doesn't get a reading in 24 hours it sends an email saying somethings happened to the sensor (flat battery?). This was easy to do with NodeRed but would have been a lot of work to write and test ESP code to do:
You do not have the required permissions to view the files attached to this post.
User avatar
By tytower
#19634
torntrousers wrote:As an alternative to having the ESP send the email you could use the FRED NodeRed service - https://fred.sensetecnic.com/

Thats gives you a free cloud based NodeRed server instance and you can set up an http enpoint which an ESP can send data to and then have a Node Red flow that does things with the data like sending emails. There is a little learning curve to learn about NodeRed but its really quite easy.

As an example, here's one i have where an ESP sends in soil moisture readings from a hygrometer sensor and the NodeRed flow looks at the value and if the soil is getting dry it sends a tweet saying it needs watering or if it doesn't get a reading in 24 hours it sends an email saying somethings happened to the sensor (flat battery?). This was easy to do with NodeRed but would have been a lot of work to write and test ESP code to do:


Looking at that I think I would rather shoot myself in the foot. It might be good but their front page is just blurb and join me!
Thanks though ,I might explore later along the way . Email should be pretty straightforward and I think its needed here
User avatar
By Stephen
#19703 Thanks guys -- really appreciate the input. I decided to submit a post to a PHP script and let PHP send the email. I'll read up on the "FRED NodeRed" suggestion though, could be useful in the future! :)

e: I just read more on this and it's an awesome suggestion! This would be super handy for anyone doesn't have their own hosting. My only concern is the longevity of the product.; if we build an interface around NodeRed and in 15 months discover they no longer offer the same service (or any at all) then we're SOL.
Last edited by Stephen on Sat Jun 06, 2015 8:46 pm, edited 2 times in total.
User avatar
By Stephen
#19709 Yes, the problem is SSL. From what I've read, most devices cannot compute the SSL certificate. Probably more of an issue on ESP8266 given the small RAM -- perhaps I assume to much?
tytower wrote:Hey Stephen here is a prog I'm working on atm for email
Maybe you can give me a hand . I've gotta stop for a while .
If you put it on your ESP8266 you must change where I have deleted my personal stuff and put yours in . Its pretty scrappy cause I need to see where it hangs.

It seems I can get it to connect but I'm not getting a response from yahoo yet .I think it needs a secure SSL but i've gotta sort that yet
Read that the public mail servers don't let you in so maybe its going to have to be an ISP provided one.
Code: Select all//Adapted from SurferTims EthernetEmail by Ty Tower

#include <ESP8266WiFi.h>

#include <WiFiClient.h>

const char* ssid = "---------";
const char* password = "";

// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(80);
WiFiClient client;

// this must be unique for your module
byte mac[] = { 0x18, 0xFE, 0x34, 0xA1 ,0x5C, 0x44 }; //from ESP8266
// change network settings to yours
IPAddress ip( 192, 168, 0, 3 );   
IPAddress gateway( 192, 168, 0, 1 );
IPAddress subnet( 255, 255, 255, 0 );
//IPAddress server(?,?,?,?);  // numeric IP for Yahoo (no DNS)
char myserver[] = "smtp.mail.yahoo.com";
int port = 465;



void setup()
{
  Serial.begin(115200);
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);
  server.begin();
  delay(10000);
  Serial.println(F("Ready. Press 'e' to send."));
}

void loop()
{
  byte inChar;

  inChar = Serial.read();

  if(inChar == 'e')
  {
      if(sendEmail()) Serial.println(F("Email sent"));
      else Serial.println(F("Email failed"));
  }
}

byte sendEmail()
{
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected"); 
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

 
  byte thisByte = 0;
  byte respCode;

  if(client.connect(myserver,port) == 1) {
    Serial.println(F("connected"));
  } else {
    Serial.println(F("connection failed"));
    return 0;
  }
  if(!eRcv()) {Serial.println("before ehlo");return 0 ;}

  Serial.println(F("Sending hello"));
// replace 1.2.3.4 with your ESP8266's ip
  client.println("EHLO 192.168.0.3");
  if(!eRcv()) {Serial.println("ehlo");return 0 ;}

  Serial.println(F("Sending auth login"));
  client.println("auth login");
  if(!eRcv()) {Serial.println("auth");return 0 ;}

  Serial.println("Sending User");
// Change to your base64 encoded user
  client.println("==========");
 
  if(!eRcv()) {Serial.println("user");return 0 ;}

  Serial.println(F("Sending Password"));
// change to your base64 encoded password
  client.println("===============");

  if(!eRcv()) {Serial.println("ehlo");return 0;}

// change to your email address (sender)
  Serial.println(F("Sending From"));
  client.println("MAIL From: <ESP8266-3>");
  if(!eRcv()) {Serial.println("email");return 0 ;}

// change to recipient address
  Serial.println(F("Sending To"));
  client.println("RCPT To: <==========>");
  if(!eRcv()) {Serial.println("email");return 0 ;}

  Serial.println(F("Sending DATA"));
  client.println("DATA");
  if(!eRcv()) {Serial.println("email");return 0 ;}

  Serial.println(F("Sending email"));

// change to recipient address
  client.println("To: Ty Tower <============>");

// change to your address
  client.println("From: Weather <ESP8266-3>");

  client.println("Subject: ESP8266 email test\r\n");

  client.println("This is from my ESP8266-12 Module 3!");

  client.println(".");

  if(!eRcv()) return 0;

  Serial.println(F("Sending QUIT")); //Serial.println(F("Sending QUIT"));
  client.println("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("10 sec \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("efail \r\nTimeout"));
      return;
    }
  }

  while(client.available())
  { 
    thisByte = client.read();   
    Serial.write(thisByte);
  }

  client.stop();

  Serial.println(F("disconnected"));
}
[/code]