Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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: <mail@example.com"));
if(!eRcv()) return 0;
// change to recipient address
Serial.println(F("Sending To"));
client.println(F("RCPT To: <mail@example.com"));
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 <mail@example.com>"));
// change to your address
client.println(F("From: Me <mail@example.com>"));
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"));
}
@SundayFunday

This comment has been minimized.

Copy link

@SundayFunday SundayFunday commented Jan 3, 2015

Don't you need to input your domain password somewhere? For example, if we're using Gmail, we would need to send the password to our Gmail account in order to access the SMTP server, correct?

@squidbits

This comment has been minimized.

Copy link

@squidbits squidbits commented Sep 3, 2015

Nope. The cool thing about telnetting into an e-mail server is that credentials are not needed for 'sending' e-mail, just for reading e-mail.

@squidbits

This comment has been minimized.

Copy link

@squidbits squidbits commented Sep 3, 2015

Line 53 has a typo "paniceButton"

@eani

This comment has been minimized.

Copy link

@eani eani commented Oct 5, 2015

Hi guys, has any one tried to use this code to send an email using his/her gmail or yahoo account, and it has worked.
Am new here and really need to use this code for my new Project. Thanks.

@m8a2

This comment has been minimized.

Copy link

@m8a2 m8a2 commented Nov 8, 2015

Hi guys, I cant connect to the internet. I put
// change server to your email server ip or domain
// IPAddress server( 1, 2, 3, 4 );
char server[] = "Smtp.gmail.com"; because im sending from an gmail.com account
but Im not sre what to put in
// change to your public ip
client.println(F("192, 168, 2, 1"));

please if somebody can help me thanks.

@edy2310

This comment has been minimized.

Copy link

@edy2310 edy2310 commented May 8, 2016

m8a2 Write the arduino ip

@orjazu

This comment has been minimized.

Copy link

@orjazu orjazu commented Jun 28, 2016

nice proyect

@Permutor

This comment has been minimized.

Copy link

@Permutor 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

This comment has been minimized.

Copy link

@glucoseG glucoseG commented Sep 24, 2017

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

@ghost

This comment has been minimized.

Copy link

@ghost 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