Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By fsae99
#32061 Hello All,
A noob here, been working with uno for about 3 months, now working with ESP 8266 for a few days.

I have a esp8266-12 dev board, and the Arduino ide updated to include the ESP 8266. I've modified several examples to suit my needs and they are up and running.

I've been looking all day for examples of using the ESP8266 Arduino to send an email via smtp. Does that capability exist?

I found some examples using uartwifi.h but I do not think that will work since that is used to connect an Arduino board to an ESP 8266.

If you have some example code that would be great but a link will suffice.

Thanks,
Jim
User avatar
By mrburnette
#32114 @Jim

Generally, pure Arduino code using the WIZnet chip sets will work.
http://playground.arduino.cc/Code/Email
http://playground.arduino.cc/Code/WiFiEmailClient

igrr has made a fine effort at mapping the ESP8266 network calls to the Arduino'ish abstract hardware. I would start there with something simple and evolve it.

Ray
User avatar
By fsae99
#32133 Ray, thanks.

Couple of issues with that code and ESP8266WiFi.h. I down to this last one. I pasted in the code and used red on the offending line items.

Appreciate any help.

I never ask the same question twice.

Compile error.
Build options changed, rebuilding all
In file included from C:\Users\IBM_ADMIN\AppData\Roaming\Arduino15\packages\esp8266\hardware\esp8266\1.6.5-947-g39819f0\libraries\ESP8266WiFi\src/ESP8266WiFi.h:32:0,
from Email_Sender2.ino:5:
C:\Users\IBM_ADMIN\AppData\Roaming\Arduino15\packages\esp8266\hardware\esp8266\1.6.5-947-g39819f0\libraries\ESP8266WiFi\src/WiFiClient.h: In instantiation of 'size_t WiFiClient::write(T&) [with T = char [64]; size_t = unsigned int]':
Email_Sender2.ino:88:20: required from here
C:\Users\IBM_ADMIN\AppData\Roaming\Arduino15\packages\esp8266\hardware\esp8266\1.6.5-947-g39819f0\libraries\ESP8266WiFi\src/WiFiClient.h:73:28: error: request for member 'available' in 'src', which is of non-class type 'char [64]'
while (src.available() > 1460){

Code: Select all/*
   Email client sketch for WiFi shield
   Posted 29 May 2015 by SurferTim
*/
#include <ESP8266WiFi.h>
#include <SPI.h>


WiFiClient client;
const char* ssid = "XXXXXX";    // Change to the name of your network
const char* password = "XXXXX"; // Change to your network password
int status = WL_IDLE_STATUS;     // the Wifi radio's status

// change server to your email server ip or domain
//IPAddress server( n,n,n,n ); use this or next line 1 or the other not both
char server[] = "mail.yoursmtp.com"; //Change to your smtp server
int port = 2525; // this port for smtp2go

void setup()
{
  delay(2000);
  Serial.begin(115200);
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);

  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println(F("WiFi shield not present"));
    // don't continue:
    while(true);
  }

  // check firmware version
  // Serial.print(F("Firmware version: ")); did not have firmmware function.


 // attempt to connect to Wifi network:
  while ( status != WL_CONNECTED) {
    Serial.print(F("Attempting to connect to open SSID: "));
    Serial.println(ssid);
    status = WiFi.begin(ssid,password);

    // wait 10 seconds for connection:
    delay(10000);
  }

  // you're connected now, so print out the data:
  Serial.print(F("You're connected to the network"));
  printCurrentNet();
  printWifiData();

  delay(2000);
  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()
{
  byte thisByte = 0;
  byte respCode;
[u][b][color=#FF0000]  char tBuf[64];[/color][/b][/u]

  if(client.connect(server,port) == 1) {
    Serial.println(F("connected"));
  } else {
    client.stop();
    Serial.println(F("connection failed"));
    return 0;
  }

  if(!eRcv()) return 0;

  Serial.println(F("Sending hello"));
  // change to the IP of your Arduino
  strcpy_P(tBuf,PSTR("Hello n.n.n.n\r\n"));  // your wifi ip
  client.write(tBuf);
  if(!eRcv()) return 0;

  Serial.println(F("Sending auth login"));
  strcpy_P(tBuf,PSTR("auth login\r\n")); 
  client.write(tBuf);
  if(!eRcv()) return 0;

  Serial.println(F("Sending User"));
  strcpy_P(tBuf,PSTR("xxxxxxxxxxx\r\n"));  // your smtp2go email id

  client.write(tBuf);
  if(!eRcv()) return 0;

  Serial.println(F("Sending Password"));
  strcpy_P(tBuf,PSTR("xxxxxxxx=\r\n"));  // your smtp2go password
  client.write(tBuf);
  if(!eRcv()) return 0;

// change to your email address (sender)
  Serial.println(F("Sending From"));
  strcpy_P(tBuf,PSTR("MAIL From: <sender>\r\n"));  / enter sender email
  client.write(tBuf);
  if(!eRcv()) return 0;

// change to recipient address
  Serial.println(F("Sending To"));
  strcpy_P(tBuf,PSTR("RCPT To: <receivert>\r\n"));  // enter reciepient eamil
  client.write(tBuf);
  if(!eRcv()) return 0;

  Serial.println(F("Sending DATA"));
  strcpy_P(tBuf,PSTR("DATA\r\n")); 
  client.write(tBuf);
  if(!eRcv()) return 0;

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

// change to recipient address
  strcpy_P(tBuf,PSTR("To: receiver email>\r\n"));  // enter reciepient eamil
  client.write(tBuf);

// change to your address
  strcpy_P(tBuf,PSTR("From: Your Name <sender email>\r\n"));  // enter sender email
  client.write(tBuf);

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

  client.println("This is from my Arduino!");

  client.println(".");
  if(!eRcv()) return 0;

  Serial.println(F("Sending QUIT"));
  strcpy_P(tBuf,PSTR("QUIT\r\n")); 
  client.write(tBuf);
  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("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"));
}

void printCurrentNet() {
  // print the SSID of the network you're attached to:
  Serial.print(F("SSID: "));
  Serial.println(WiFi.SSID());

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print(F("signal strength (RSSI):"));
  Serial.println(rssi);

  // print the encryption type:
//byte encryption = WiFi.encryptionType(); // does not like this function
 //  Serial.print(F("Encryption Type:"));
 // Serial.println(encryption,HEX);
}

void printWifiData() {
  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
    Serial.print(F("IP Address: "));
  Serial.println(ip);
  Serial.println(ip);

  //did not have WiFi.BSSID function in ESP8266WiFi.h

  // print your MAC address:
  byte mac[6]; 
  WiFi.macAddress(mac);
  Serial.print(F("MAC address: "));
  Serial.print(mac[5],HEX);
  Serial.print(F(":"));
  Serial.print(mac[4],HEX);
  Serial.print(F(":"));
  Serial.print(mac[3],HEX);
  Serial.print(F(":"));
  Serial.print(mac[2],HEX);
  Serial.print(F(":"));
  Serial.print(mac[1],HEX);
  Serial.print(F(":"));
  Serial.println(mac[0],HEX);

  // print your subnet mask:
  IPAddress subnet = WiFi.subnetMask();
  Serial.print(F("NetMask: "));
  Serial.println(subnet);

  // print your gateway address:
  IPAddress gateway = WiFi.gatewayIP();
  Serial.print(F("Gateway: "));
  Serial.println(gateway);
}
User avatar
By Erni
#32186 Hi,
I have used this Arduino example on a Esp8266-01.
I made a few changes but it works as expected and sends mail without problems.

http://playground.arduino.cc/Code/Email?action=sourceblock&num=2

If you have trouble with this, I will try to find the sketch again, and post it here.