Skip to content

Instantly share code, notes, and snippets.

@tnedev
Created April 26, 2014 14:11
Show Gist options
  • Save tnedev/11321023 to your computer and use it in GitHub Desktop.
Save tnedev/11321023 to your computer and use it in GitHub Desktop.
Arduino - Send an email when panic switch was pressed
/*
Tihomir Nedev
April 2014
Change the values for the network setting.
Change the emails. NB! Some sorvers won't accept email for unedentified devices as Arduino.
Hardware: Arduino board + Ethernet shield
*/
#include <SPI.h>
#include <Ethernet.h>
// Set the panic button pin
byte panicButton = 2;
boolean statusCheck=false;
// this must be unique
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x59, 0x67 };
// change network settings to yours
IPAddress ip( 192, 168, 100, 19 );
IPAddress gateway( 192, 168, 100, 1 );
IPAddress subnet( 255, 255, 255, 0 );
// change server to your email server ip or domain
// IPAddress server( 1, 2, 3, 4 );
char server[] = "mail.example.com";
EthernetClient client;
void setup()
{
Serial.begin(9600);
pinMode(4,OUTPUT);
digitalWrite(4,HIGH);
Ethernet.begin(mac, ip, gateway, gateway, subnet);
delay(2000);
Serial.println(F("Ready."));
pinMode(panicButton, INPUT);
}
void loop()
{
if(digitalRead(panicButton)==HIGH && statusCheck==false)
{
if(sendEmail()) Serial.println(F("Email sent"));
else Serial.println(F("Email failed"));
statusCheck = true;
}
else if (digitalRead(paniceButton)==LOW)
{
statusCheck = false;
}
}
byte sendEmail()
{
byte thisByte = 0;
byte respCode;
if(client.connect(server,25)) {
Serial.println(F("connected"));
} else {
Serial.println(F("connection failed"));
return 0;
}
if(!eRcv()) return 0;
// change to your public ip
client.println(F("helo 1.2.3.4"));
if(!eRcv()) return 0;
Serial.println(F("Sending From"));
// change to your email address (sender)
client.println(F("MAIL From: <[email protected]"));
if(!eRcv()) return 0;
// change to recipient address
Serial.println(F("Sending To"));
client.println(F("RCPT To: <[email protected]"));
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: You <[email protected]>"));
// change to your address
client.println(F("From: Me <[email protected]>"));
client.println(F("Subject: Panic Alarm!\r\n"));
client.println(F("The panic button was pressed!!!"));
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"));
}
@edy2310
Copy link

edy2310 commented May 8, 2016

m8a2 Write the arduino ip

@orjazu
Copy link

orjazu commented Jun 28, 2016

nice proyect

@Permutor
Copy link

Permutor commented Mar 1, 2017

Hi all,

I tried the sketch with a gmail account, and got the following in the Serial Monitor:
Ready.
connected
220 smtp.gmail.com ESMTP t30sm8108718wra.52 - gsmtp
250 smtp.gmail.com at your service
Sending From
530 5.7.0 Must issue a STARTTLS command first. t30sm8108718wra.52 - gsmtp
221 2.0.0 closing connection t30sm8108718wra.52 - gsmtp
disconnected
Email failed

The t30... code is different every time. But the bottom line is that GMAIL apparently requires an additional command: STARTTLS.
Meaning: The smtp server is saying it won't accept plain text connections.

@glucoseG
Copy link

How would I activate this every time when a switch would off?

Copy link

ghost commented Jan 26, 2018

This is such an awesome and memory-cheap way of sending email - I'm working it into an ethernet relay controller sketch I found elsewhere, works every time.

I have it talking to a local unauthenticated, IP limited postfix server that collects mail for the network. No issues at all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment