Email with the WIFI shield when a pin is set high

Hi Everyone. I would like to send an email when a pin goes high on my arduino. I plan to accomplish this using the wifi shield and an Arduino UNO.

I used the code from the arduino wifi shield page on arduino’s website and it works great after I installed the latest firmware on the shield.

This code works by opening the serial monitor and pressing “e” and then enter. I have successfully done this.
However… I spent a few hours trying to modify the code to work so that instead of pressing “e”, the code would just look for a pin going high, then send the email.

When I run this code, it just goes into a loop saying “connected, No socket available, connection failed”

Can anyone help me out and spot my error? Hopefully it’s something easy!

Thanks in advance

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

const int SimSig = 2; //pin that I want to send email when it goes high
int Status = 0; //variable for reading the SimSig condition

WiFiClient client;
char ssid = “XXXX”; // the name of your network (XXXX for forum only)
char pass = “XXXX”; // your network password (XXXX for forum only)
int status = WL_IDLE_STATUS; // the Wifi radio’s status

// change server to your email server ip or domain
// IPAddress server( 1, 2, 3, 4 );
char server = “mail.smtp2go.com”;
int port = 2525;

void setup()
{
pinMode(SimSig, INPUT); //set SimSig signal as an input

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: "));
Serial.println(WiFi.firmwareVersion());

// 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,pass);

// 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);
}

void loop()
{
Status = digitalRead(SimSig); // read the state of the SimSig

if(Status == HIGH)
{
if(sendEmail()) Serial.println(F(“Email sent”));
else Serial.println(F(“Email failed”));
}
}

byte sendEmail()
{
byte thisByte = 0;
byte respCode;
char tBuf[64];

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

if(Status == HIGH) return 0;

Serial.println(F(“Sending hello”));
// change to the IP of your Arduino
strcpy_P(tBuf,PSTR(“EHLO 192.168.100.105\r\n”));
client.write(tBuf);
if(Status == HIGH) return 0;

Serial.println(F(“Sending auth login”));
strcpy_P(tBuf,PSTR(“auth login\r\n”));
client.write(tBuf);
if(Status == HIGH) return 0;

Serial.println(F(“Sending User”));
strcpy_P(tBuf,PSTR(“XXXXX\r\n”)); // (XXXX for forum only)

client.write(tBuf);
if(Status == HIGH) return 0;

Serial.println(F(“Sending Password”));
strcpy_P(tBuf,PSTR(“XXXXX\r\n”)); // (XXXX for forum only)
client.write(tBuf);
if(Status == HIGH) return 0;

// change to your email address (sender)
Serial.println(F(“Sending From”));
strcpy_P(tBuf,PSTR(“MAIL From: \r\n”)); // (XXXX for forum only)
client.write(tBuf);
if(Status == HIGH) return 0;

// change to recipient address
Serial.println(F(“Sending To”));
strcpy_P(tBuf,PSTR(“RCPT To: \r\n”)); // (XXXX for forum only)
client.write(tBuf);
if(Status == HIGH) return 0;

Serial.println(F(“Sending DATA”));
strcpy_P(tBuf,PSTR(“DATA\r\n”));
client.write(tBuf);
if(Status == HIGH) return 0;

Serial.println(F(“Sending email”));

// change to recipient address
strcpy_P(tBuf,PSTR(“To: Matt Gale \r\n”)); // (XXXX for forum only)
client.write(tBuf);

// change to your address
strcpy_P(tBuf,PSTR(“From: OIC SIMS \r\n”)); // (XXXX for forum only)
client.write(tBuf);

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

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

client.println(".");
if(Status == HIGH) return 0;

Serial.println(F(“Sending QUIT”));
strcpy_P(tBuf,PSTR(“QUIT\r\n”));
client.write(tBuf);
if(Status == HIGH) return 0;

client.stop();

Serial.println(F(“disconnected”));

return 1;
client.flush();
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 MAC address of the router you’re attached to:
byte bssid[6];
WiFi.BSSID(bssid);
Serial.print(F(“BSSID: “));
Serial.print(bssid[5],HEX);
Serial.print(F(”:”));
Serial.print(bssid[4],HEX);
Serial.print(F(":"));
Serial.print(bssid[3],HEX);
Serial.print(F(":"));
Serial.print(bssid[2],HEX);
Serial.print(F(":"));
Serial.print(bssid[1],HEX);
Serial.print(F(":"));
Serial.println(bssid[0],HEX);

// 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();
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);

// 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);
}

If the Status is HIGH, call the function. In the function, connect, and then, if status is HIGH, return. I don't get it.

Note that if you just check for a pin high without having a pin reset setup in your code, you may have an email spew.

Please read #7 below:

http://forum.arduino.cc/index.php/topic,148850.0.html

I also don’t get it! I’m lost lol.

and thanks for the tip on properly pasting code into the forum.

Here is the original code that works by pressing the “e” in the serial monitor (which works for me). I was hoping it would be simple to just change the if statement that contains serial read with the Pin status (when a pin is high, send email). I guess not… I will keep trying and post the code when I figure it out.

/*
   Email client sketch for WiFi shield
   Posted 29 May 2015 by SurferTim
*/
#include <SPI.h>
#include <WiFi.h>
 
WiFiClient client;
char ssid[] = "MySSID";     // the name of your network
char pass[] = "mypassphrase";  // your network password
int status = WL_IDLE_STATUS;     // the Wifi radio's status
 
// change server to your email server ip or domain
// IPAddress server( 1, 2, 3, 4 );
char server[] = "smtpcorp.com";
int port = 2525;
 
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: "));
  Serial.println(WiFi.firmwareVersion());
 
 // 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,pass);
 
    // 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;
  char tBuf[64];
 
  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("EHLO 192.168.0.2\r\n"));  
  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("dGltQHByb2xlY3Ryb24uY29t\r\n"));  
 
  client.write(tBuf);
  if(!eRcv()) return 0;
 
  Serial.println(F("Sending Password"));
  strcpy_P(tBuf,PSTR("cGhlbGFu\r\n"));  
  client.write(tBuf);
  if(!eRcv()) return 0;
 
// change to your email address (sender)
  Serial.println(F("Sending From"));
  strcpy_P(tBuf,PSTR("MAIL From: <tim@prolectron.com>\r\n"));  
  client.write(tBuf);
  if(!eRcv()) return 0;
 
// change to recipient address
  Serial.println(F("Sending To"));
  strcpy_P(tBuf,PSTR("RCPT To: <tim@prolectron.com>\r\n"));  
  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: Tim Dicus <tim@prolectron.com>\r\n"));  
  client.write(tBuf);
 
// change to your address
  strcpy_P(tBuf,PSTR("From: Tim Dicus <tim@prolectron.com>\r\n"));  
  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 MAC address of the router you're attached to:
  byte bssid[6];
  WiFi.BSSID(bssid);    
  Serial.print(F("BSSID: "));
  Serial.print(bssid[5],HEX);
  Serial.print(F(":"));
  Serial.print(bssid[4],HEX);
  Serial.print(F(":"));
  Serial.print(bssid[3],HEX);
  Serial.print(F(":"));
  Serial.print(bssid[2],HEX);
  Serial.print(F(":"));
  Serial.print(bssid[1],HEX);
  Serial.print(F(":"));
  Serial.println(bssid[0],HEX);
 
  // 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();
  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);
 
  // 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);
}

Below is somebody else’s servo button code that appears to have button functions you may need. It should only increment when a button is pushed, released, and pushed again.

#include <Servo.h>
Servo servo;
const byte buttonPin = A1;
byte currentButtonState = HIGH;
byte previousButtonState = HIGH;
int servoPosition = 0;
int servoIncrement = 10;

void setup()
{
  pinMode(buttonPin, INPUT_PULLUP);
  servo.attach(A5);
  servo.write(0);
}

void loop()
{
  currentButtonState = digitalRead(buttonPin);
  if (currentButtonState != previousButtonState && currentButtonState == LOW)
  {
    servoPosition += servoIncrement;
    servo.write(servoPosition);
  }
  previousButtonState = currentButtonState;
}

Thanks zoomkat! That looks like what I need, I appreciate it. I will post when I get it figured out.

zoomkat I can’t thank you enough! I got it working using your fixes. Hopefully this will help someone else who wants to shoot off an email when a pin is set high.
Also big thanks to SurferTim for posting the original code

#include <SPI.h>
#include <WiFi.h>

byte byteRead;
const byte simPin = A1;
byte currentSimState = HIGH;
byte previousSimState = HIGH;

WiFiClient client;
char ssid[] = "networkName";     // the name of your network
char pass[] = "Password";  // your network password
int status = WL_IDLE_STATUS;     // the Wifi radio's status

// change server to your email server ip or domain
// IPAddress server( 1, 2, 3, 4 );
char server[] = "mail.smtp2go.com";
int port = 2525;

void setup()
{
  pinMode(simPin, INPUT_PULLUP);
  
  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: "));
  Serial.println(WiFi.firmwareVersion());

 // 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,pass);

    // 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);
}

void loop()
{
  currentSimState = digitalRead(simPin);
  if (currentSimState != previousSimState && currentSimState == HIGH)
  {      
    if(sendEmail()) Serial.println(F("Email sent"));
    else Serial.println(F("Email failed"));
  }
  previousSimState = currentSimState;

 {
}
}

byte sendEmail()
{
  byte thisByte = 0;
  byte respCode;
  char tBuf[64];

  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("EHLO 192.168.43.242\r\n"));  
  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("usernameconverted\r\n"));  

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

  Serial.println(F("Sending Password"));
  strcpy_P(tBuf,PSTR("passwordConverted\r\n"));  
  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@gmail>\r\n"));  
  client.write(tBuf);
  if(!eRcv()) return 0;

// change to recipient address
  Serial.println(F("Sending To"));
  strcpy_P(tBuf,PSTR("RCPT To: <recipient@gmail>\r\n"));  
  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: Matt Gale <address>\r\n"));  
  client.write(tBuf);

// change to your address
  strcpy_P(tBuf,PSTR("From: OIC SIMS <address>\r\n"));  
  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 MAC address of the router you're attached to:
  byte bssid[6];
  WiFi.BSSID(bssid);    
  Serial.print(F("BSSID: "));
  Serial.print(bssid[5],HEX);
  Serial.print(F(":"));
  Serial.print(bssid[4],HEX);
  Serial.print(F(":"));
  Serial.print(bssid[3],HEX);
  Serial.print(F(":"));
  Serial.print(bssid[2],HEX);
  Serial.print(F(":"));
  Serial.print(bssid[1],HEX);
  Serial.print(F(":"));
  Serial.println(bssid[0],HEX);

  // 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();
  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);

  // 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);
}