Project tutorial

Send Emails from your Arduino Projects using Particle Photon © CC BY-NC

Adding IoT functionality to an Arduino project with the Particle Photon.

  • 1,209 views
  • 1 comment
  • 5 respects

Components and supplies

About this project

This is a BreakoutBros.com tutorial. See the full version here.

After reviewing the Particle Photon, I wanted to go back to one of our tutorials and add some IoT (Internet of Things) functionality using the Photon. In this tutorial I’ll walk through how to have the Particle Photon email you by adding this capability to our RFID Badge Scanner with LCD tutorial.

If you have not seen the RFID Badge Scanner with LCD tutorial, the first thing you will need to do is visit this article and get setup so that you can scan a RFID badge and display a “welcome home” message to a user you associate with the RFID tag.

What you will need

In addition to the materials needed for the RFID badge scanner you will need:

  • 3x 10K resistors*
  • 3x 2k resistors*
  • Wires and Breadboard*

*note you can get these off amazon or any electronics store or you can check out one of the Arduino Starter Kits that will include these basic components.

5V to 3.3V level shifting

For this tutorial we will be using the Arduino to communicate directly to the Particle Photon using three different I/O pins. If the first pin is High it will indicate that the 1st user’s badge was scanned, if the 2nd pin is High it will indicate that the 2nd user’s badge was scanned and if the 3rd pin is High it will indicate that the 3rd user’s badge was scanned.

The Particle Photon uses 3.3V logic while the standard Arduino uses 5v logic. In order to “safely” have them communicate using I/O pins, we will need to develop a level shifter that shifts 5V to 3.3V. Although it will work to directly connect 5V to the Particle Photon I/O it is not good practice. This stresses the pins and can potentially damage the I/O. This is even more important if we were using the Analog pins as this puts extreme stress on the analog to digital converter. Particle explicitly states not to directly connect 5V to those pins.

The 5V to 3.3V level shifter circuit is a simple NPN transistor and some biasing resistors.

This circuit will take a 5V output on the Arduino Pin and shift it to a 3.3V input on the Photon pin. If the Arduino Output is 0, the Photon input pin will also be 0.

You will need three of these circuits, one for each of the pins that will go between the Arduino and Photon. Follow the pinouts below.

Next wire these three circuits using a breadboard. The Particle Photon starter kit comes with one along with the Arduino Starter Kits.

Power the Photon using the Arduino 5V output by connecting VIN on the Photon to 5V from the Arduino. This will go through the Photon’s voltage regulator to drop the voltage to it’s needed 3.3V. Also do not forget to connect GND from the Photon to GND on the Arduino.

Now your Arduino and Particle Photon are setup to communicate using a 5V to 3.3V level shifter. It’s time to begin coding the Particle Photon.

As discussed in the Particle Photon Review an online IDE is used to program. Go to www.Particle.io/build and login to your particle account. Create a new program. We will be setting this program up to publish an event based on certain inputs the Photon will read from the Arduino. This event will later be used as a trigger to email you. The code will also include some functionality for the LED to visually tell when and to who it is sending an email. The LED will blink at a different rate depending on to whom it is sending an email.

Particle Photon Code

First define all the global variables

int led1 = D0; //LED to show changes in states
int led2 = D7; //LED to show changes in states
 
int firstBitPin = 1; //Pin that will be reading the first Bit to know state
int secondBitPin = 2;//Pin that will be reading the second Bit to know state
int thirdBitPin = 3; //Pin that will be reading the third Bit to know state
 
int firstbitState = LOW; //variable to store the state of the first Bit
int secondbitState = LOW;//variable to store the state of the second Bit
int thirdbitState = LOW; //variable to store the state of the third Bit
 
 
int LEDDelay = 1000; //Variable used to change the LED blink timer based on what Bits are high

This includes all the Pins that will be used and the data bits that will be stored based on what the Arduino outputs. Also define the LED Blink rate at 1 second.

Next we will go through the setup loop mainly to define all pin outputs and variables to initialize as

void setup() {
 
 
  pinMode(led1, OUTPUT);  //Set the LED pin to an Output
  pinMode(led2, OUTPUT); //Set the LED pin to an Output
 
  pinMode(firstBitPin,  INPUT);//Set the BIT 1 pin to an input(used for transfering who is scanned)
  pinMode(secondBitPin, INPUT);//Set the BIT 2 pin to an input(used for transfering who is scanned)
  pinMode(thirdBitPin,  INPUT);//Set the BIT 3 pin to an input(used for transfering who is scanned)
 
  digitalWrite(firstBitPin,  HIGH); //enable pullup
  digitalWrite(secondBitPin, HIGH); //enable pullup
  digitalWrite(thirdBitPin, HIGH);  //enable pullup
 
  digitalWrite(led1, LOW); //Make sure LED 1 starts in LOW state
 
}

The last part of code will be the main loop. This will tell the photon to react based on what Bits it reads from the Arduino.

void loop() {
  firstbitState = digitalRead(firstBitPin);  //Read bit 1 and store
  secondbitState = digitalRead(secondBitPin);//Read bit 2 and store
  thirdbitState = digitalRead(thirdBitPin);  //Read bit 3 and store
 
  if(firstbitState == HIGH) // This will only work for 3 of the combos;(0,0,1)(0,1,0)or (1,0,0)
  {
      Particle.publish("RFID_HOME","JOSH"); // Particle Command to publish the Event "RFID_HOME" with the data JOSH
      LEDDelay = 250;                       // this means Josh's badge was scanned. we also change the LED blinking to 250
      delay(5000);                          // 5 second delay after publshing, this will prevent 2 publishes from one eveent from the arduino.
  }
  else if(secondbitState == HIGH)
  {
      Particle.publish("RFID_HOME","BRAD");  // Particle Command to publish the Event "RFID_HOME" with the data BRAD
      LEDDelay = 500;                        // this means Brads's badge was scanned. we also change the LED blinking to 500
      delay(5000);                           // 5 second delay after publshing, this will prevent 2 publishes from one eveent from the arduino.
  }
  else if(thirdbitState == HIGH)
  {
      Particle.publish("RFID_HOME","SAM");   // Particle Command to publish the Event "RFID_HOME" with the data SAM
      LEDDelay = 1000;                       // this means Josh's badge was scanned. we also change the LED blinking to 1000
      delay(5000);                           // 5 second delay after publishing, this will prevent 2 publishes from one event from the arduino.
  }
  else
  {
      LEDDelay = 2000;                      //If none of the bits are set, we store 2 seconds for the blinking, this is"normal" blink time
 
  }
    digitalWrite(led1, HIGH);               //Set LEDs high as a heartbeat
    digitalWrite(led2, HIGH);
    delay(LEDDelay);                        //Use the delay stored above
    digitalWrite(led1, LOW);
    digitalWrite(led2, LOW);                //Set LEDs LOW as a heartbeat
    delay(LEDDelay);                        //Use the delay stored above
}

The main loop will first read the state of the pins and store these into its appropriate variable. Then depending on which bit is high it will execute the Particle.publish(“EVENT”, “DATA”) command. The first data input to the Particle.publish command is the “event” and the second input is the “DATA”. We will call this the “RFID_HOME” event and the Data is the user badge name . Also, for each user a different LED blink time is used. This way you can visually detect if the Photon sensed a particular badge based on the data. The Delay after executing the Particle.publish command is to ensure that the Photon doesn’t send multiple events for a single scan from the Arduino. The last bit of the code loop is to blink the LED at the LEDDelay rate. You can get the Photon Particle code for this tutorial here.

Setting up IFTTT For the IoT Email

Now that the particle is publishing the RFID_HOME event to the particle cloud, you now need to setup a service that will look for this data being published. For this tutorial we will be using If This Then That. Go ahead and go to ifttt.com and create an account.

Search for particle then click on the Particle Icon under services:

Next you will need to select an Applet that uses Email. Scroll down and select “Test of Electron Beam Switch”

Flip the switch to turn this applet on

Once the Applet is on it will allow you to edit.

This is setting up the service to email. Label the name of this Applet based on the Badge Name you want emailed. You then need to put the Event the Particle published in the “If(Event Name)” box. This was “RFID_HOME”. Then put the contents for this publish event into the “is(Event Contents)”. For this applet it will be JOSH – it MUST match what’s in the Particle Photon Code. Now press save and then go back into edit.

For some reason you must save then go back into the Applet to get the Email portion setup. This is how you format your email. The subject will be “{EventContents} has entered the front door” and the Body will be “Hey, your {DeviceName} has logged that {EventContents} has entered the front door. Welcome them Home.”

All that is left for the IoT is to setup two more applets for the other badge names – SAM and BRAD

Testing the Particle Photon Email

Once all of this is setup, you should test that everything is working with the Particle Photon. Do this by manually pulling each pin (1 through 3) high one at a time on the particle Photon for a couple seconds to confirm that the LED starts blinking at a different rate. Then check the email you have setup for ifttt.com to make sure you receive the email. Pulling Pin 1 HIGH should send the “JOSH” email, pulling pin 2 HIGH should send the “BRAD” email and pulling pin 3 HIGH should send the “SAM” email. Please note that the other pins MUST be low when doing this otherwise you will get the email for the first name in the IF statements. Once this is working you are ready to connect the RFID Badge Scanner with LCD system.

Arduino Code sent to Particle Photon

Since the RFID Badge Scanner is already pulling the RFID badge and attaching a Name to each badge ID, all you will need to do is setup which pins need to go HIGH and LOW for each Name and then add in code that will set these pins appropriately whenever that Badge ID is read.

First define these new pins and arrays for Pin Data:

///Email Variable
int outFirstBitPin = 0; //Pin that will be connected to Photon for bit one data
int outSecondBitPin = 1;//Pin that will be connected to Photon for bit two data
int outThirdBitPin = 2; //Pin that will be connected to Photon for bit three data
 
int RFID_JOSH[]   = {1,0,0}; //Parallel Data for JOSH tag to send to Photon
int RFID_BRAD[]   = {0,1,0}; //Parallel Data for BRAD tag to send to Photon
int RFID_SAM[]    = {0,0,1}; //Parallel Data for SAM  tag to send to  Photon

Next add in code that will set these pins based on the name read:

 if(Tag_Name != "i")
            {
                  lcdprintwelcome(Tag_Name);
 
                  if(Tag_Name == "Brad") //If the name is BRAD we send the parallel data to the Photon to send the email that Brad Scanned
                  {
                    digitalWrite(outFirstBitPin,  RFID_BRAD[0]);   //Send first bit data for Brad
                    digitalWrite(outSecondBitPin, RFID_BRAD[1]);   //Send second bit data for Brad
                    digitalWrite(outThirdBitPin,  RFID_BRAD[2]);   //Send third bit data for Brad
 
                    delay(5000); //Wait 5 seconds after sending the data to ensure the Photon reads it during its loop
 
                    digitalWrite(outFirstBitPin,  0);   //set data back to 0 
                    digitalWrite(outSecondBitPin, 0);   //set data back to 0 
                    digitalWrite(outThirdBitPin,  0);   //set data back to 0 
                  }
                  else if(Tag_Name == "Josh") //If the name is BRAD we send the parallel data to the Photon to send the email that JOSH Scanned
                  {
                    digitalWrite(outFirstBitPin,  RFID_JOSH[0]);   //Send first bit data for Josh
                    digitalWrite(outSecondBitPin, RFID_JOSH[1]);   //Send second bit data for Josh
                    digitalWrite(outThirdBitPin,  RFID_JOSH[2]);   //Send third bit data for Josh
 
                    delay(5000); //Wait 5 seconds after sending the data to ensure the Photon reads it during its loop
 
                    digitalWrite(outFirstBitPin,  0);   //set data back to 0 
                    digitalWrite(outSecondBitPin, 0);   //set data back to 0 
                    digitalWrite(outThirdBitPin,  0);   //set data back to 0 
                  }
                  else if(Tag_Name == "SAM") //If the name is BRAD we send the parallel data to the Photon to send the email that SAM Scanned
                  {
                    digitalWrite(outFirstBitPin,  RFID_SAM[0]);   //Send first bit data for Sam
                    digitalWrite(outSecondBitPin, RFID_SAM[1]);   //Send second bit data for Sam
                    digitalWrite(outThirdBitPin,  RFID_SAM[2]);   //Send third bit data for Sam
 
                    delay(5000); //Wait 5 seconds after sending the data to ensure the Photon reads it during its loop
 
                    digitalWrite(outFirstBitPin,  0);   //set data back to 0 
                    digitalWrite(outSecondBitPin, 0);   //set data back to 0 
                    digitalWrite(outThirdBitPin,  0);   //set data back to 0 
                  }
                  lcdprintmain(); //Go back to Main scan screen
            } 
            else 
            {             
                lcdaccessdenied();//If the read tag isnt in our data base, we will print an Access Denied message
                delay(2000);
                lcdprintmain(); // refresh to home screen
            }

Once the Success criteria for detecting a valid tag is met, if the name associated to the tag is equal to “Josh” “Brad” or “Sam” the appropriate pin output will be sent to the Particle Photon. A delay of 5 seconds is added after the pins are set so that the Particle Photon has time to detect the pin change. After the delay, the pins are set back to 0 so that no more “emails” will be sent.  Get the full Ardunio code here.

Now you have successfully added the Particle Photon to make the RFID Badge Scanner with LCD an IoT device. Whenever one of the defined badges is scanned an email is sent to you. Now add this to your front door and control a lock solenoid and you have a monitoring system for your house front door lock. You can be emailed when someone badges in so you know if someone is home regardless of you being present or not!

I hope you enjoyed and learned from this tutorial. Please be sure to subscribe so you do not miss future articles. We plan on adding some more functions to the RFID Badge Scanner so stay tuned!

Code

Arduino CodeC/C++
// BreakoutBros RFID+LCD Tutorial 
// 2016
 
// This tutorial uses the RC522 RFID Chip reader and its example
// Along with the 1602A LCD and its example
// Go to BreakoutBros.com to view wiring and step by step instructions
// Additional Code to send data to Particle Photon to email Name when Badge is scanned
 
 
// RFID Setup
#include <AddicoreRFID.h>
#include <SPI.h>
 
 
 
//LCD Setup
#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 8, 9, 6, 4, 3); //(RS,E,D4,D5,D6,D7)
 
 
#define uchar unsigned char
#define uint  unsigned int
 
uchar fifobytes;
uchar fifoValue;
 
AddicoreRFID myRFID; // create AddicoreRFID object to control the RFID module
 
 
 
/////////////////////////////////////////////////////////////////////
//set the pins
/////////////////////////////////////////////////////////////////////
const int chipSelectPin = 10;
const int NRSTPD = 5;
 
//Maximum length of the RFID array
#define MAX_LEN 16
 
  // Define All Key Codes - MAX 4 keys and 4 Names
uchar keys[16] = { 
  134 , 224, 134, 141, //Josh ID - These 4 digits are your 1st users ID Bytes
  19 , 89, 111, 133,   //Brad ID - These 4 digits are your 2nd users ID Bytes
  111 , 111, 111, 111, // Dummy1 ID - These 4 digits are your 3rd users ID Bytes
  222 , 222, 222, 222  // Dummy 2 ID - These 4 digits are your 4th users ID Bytes
};
 
String Names[4] = { 
  "Josh",     // 1st User- what will be printed to LCD *See keys[] for ID
  "Brad",     // 2nd User- what will be printed to LCD *See keys[] for ID
  "Sam",      // 3rd User- what will be printed to LCD *See keys[] for ID
  "Dummy2"    // 4th User- what will be printed to LCD *See keys[] for ID
};
 
///Email Variable
int outFirstBitPin = 0; //Pin that will be connected to Photon for bit one data
int outSecondBitPin = 1;//Pin that will be connected to Photon for bit two data
int outThirdBitPin = 2; //Pin that will be connected to Photon for bit three data
 
int RFID_JOSH[]   = {1,0,0}; //Parallel Data for JOSH tag to send to Photon
int RFID_BRAD[]   = {0,1,0}; //Parallel Data for BRAD tag to send to Photon
int RFID_SAM[]    = {0,0,1}; //Parallel Data for SAM  tag to send to  Photon
 
 
// Variable to Store Tag User Name to show on screen
String Tag_Name = "i"; // initialize it as "i" so Access denied is default
 
 
void setup() {                
  //Serial.begin(9600);                        // RFID reader SOUT pin connected to Serial RX pin at 9600bps 
 
  // start the SPI library for RFID
  SPI.begin();
 
  //RFID Config
  pinMode(chipSelectPin,OUTPUT);              // Set digital pin 10 as OUTPUT to connect it to the RFID /ENABLE pin 
    digitalWrite(chipSelectPin, LOW);         // Activate the RFID reader
  pinMode(NRSTPD,OUTPUT);                     // Set digital pin 5 , Not Reset and Power-down
     digitalWrite(NRSTPD, HIGH);
 
  myRFID.AddicoreRFID_Init();  
 
  //LCD Initializations
  lcd.begin(16, 2);
  lcdprintmain();
 
  //Email Pins Setup
  pinMode(outFirstBitPin, OUTPUT);
  pinMode(outSecondBitPin, OUTPUT);
  pinMode(outThirdBitPin, OUTPUT);
  delay(100);
  digitalWrite(outFirstBitPin,  0);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(outSecondBitPin, 0);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(outThirdBitPin,  0);   // turn the LED on (HIGH is the voltage level)
}
 
void loop()
{
  	uchar i, tmp, checksum1;
	uchar status;
        uchar str[MAX_LEN];
        uchar RC_size;
        uchar blockAddr;	//Selection operation block address 0 to 63
        String mynum = "";
 
        str[1] = 0x4400;
	//Find tags, return tag type
	status = myRFID.AddicoreRFID_Request(PICC_REQIDL, str);	
 
	//Anti-collision, return tag serial number 4 bytes
	status = myRFID.AddicoreRFID_Anticoll(str);
 
	if (status == MI_OK)
	{
        uint tagType = str[0] << 8;
        tagType = tagType + str[1];
        checksum1 = str[0] ^ str[1] ^ str[2] ^ str[3]; // Calculate a checksum to make sure there is no error
        if(checksum1 == str[4])
        {
            Tag_Name = testkey(str,keys,Names);
            if(Tag_Name != "i")
            {
                  lcdprintwelcome(Tag_Name);
 
                  if(Tag_Name == "Brad") //If the name is BRAD we send the parallel data to the Photon to send the email that Brad Scanned
                  {
                    digitalWrite(outFirstBitPin,  RFID_BRAD[0]);   //Send first bit data for Brad
                    digitalWrite(outSecondBitPin, RFID_BRAD[1]);   //Send second bit data for Brad
                    digitalWrite(outThirdBitPin,  RFID_BRAD[2]);   //Send third bit data for Brad
 
                    delay(5000); //Wait 5 seconds after sending the data to ensure the Photon reads it during its loop
 
                    digitalWrite(outFirstBitPin,  0);   //set data back to 0 
                    digitalWrite(outSecondBitPin, 0);   //set data back to 0 
                    digitalWrite(outThirdBitPin,  0);   //set data back to 0 
                  }
                  else if(Tag_Name == "Josh") //If the name is BRAD we send the parallel data to the Photon to send the email that JOSH Scanned
                  {
                    digitalWrite(outFirstBitPin,  RFID_JOSH[0]);   //Send first bit data for Josh
                    digitalWrite(outSecondBitPin, RFID_JOSH[1]);   //Send second bit data for Josh
                    digitalWrite(outThirdBitPin,  RFID_JOSH[2]);   //Send third bit data for Josh
 
                    delay(5000); //Wait 5 seconds after sending the data to ensure the Photon reads it during its loop
 
                    digitalWrite(outFirstBitPin,  0);   //set data back to 0 
                    digitalWrite(outSecondBitPin, 0);   //set data back to 0 
                    digitalWrite(outThirdBitPin,  0);   //set data back to 0 
                  }
                  else if(Tag_Name == "SAM") //If the name is BRAD we send the parallel data to the Photon to send the email that SAM Scanned
                  {
                    digitalWrite(outFirstBitPin,  RFID_SAM[0]);   //Send first bit data for Sam
                    digitalWrite(outSecondBitPin, RFID_SAM[1]);   //Send second bit data for Sam
                    digitalWrite(outThirdBitPin,  RFID_SAM[2]);   //Send third bit data for Sam
 
                    delay(5000); //Wait 5 seconds after sending the data to ensure the Photon reads it during its loop
 
                    digitalWrite(outFirstBitPin,  0);   //set data back to 0 
                    digitalWrite(outSecondBitPin, 0);   //set data back to 0 
                    digitalWrite(outThirdBitPin,  0);   //set data back to 0 
                  }
                  lcdprintmain(); //Go back to Main scan screen
            } 
            else 
            {             
                lcdaccessdenied();//If the read tag isnt in our data base, we will print an Access Denied message
                delay(2000);
                lcdprintmain(); // refresh to home screen
            }
         }
         else
         {
                  lcd.setCursor(0,0);
                  lcd.write("    Tag Error   ");
                  lcd.setCursor(0,1);
                  lcd.write("   Try Again!   ");
                  delay(2000);
         }
 
	 myRFID.AddicoreRFID_Halt();      //Command tag into hibernation 
  digitalWrite(outFirstBitPin,  0);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(outSecondBitPin, 0);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(outThirdBitPin,  0);   // turn the LED on (HIGH is the voltage level)
  }
}
 
 
 
void lcdprintmain(){
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.write("The BreakoutBros");
  lcd.setCursor(0,1);
  lcd.write(" Scan Your Card");
}
 
void lcdaccessdenied(){
   lcd.clear();
   lcd.setCursor(0,0);
   lcd.write(" Access Denied  "); // print Access denied
}
 
void lcdprintwelcome(String Name){
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("  Welcome Home  ");
    lcd.setCursor(5,1);
    lcd.print(Name);
}
 
String testkey(uchar Read_ID[4], uchar Stored_ID[16], String Stored_Name[4]){
    int n = 0; // Variable for Overall ID loop
    int test = 0; //Variable for testing each Byte
    int i = 0; // Variable for Read Byte Loop
    int g = 0;// Variable for Stored Byte Loop
    for(n=0; n < 4; n++) // Loop for each Name test
    {
      for(i=0; i<4; i++)  //Loop for each ID test
      {
        if(Read_ID[i] == Stored_ID[g]) // test bytes 0-3 of each ID
        {
          test = test+1; // if a test passes increase test by 1
        }
        g = g + 1; // Counter or stored ID indexing
      }
    if( test == 4) // if all 4 byts pass
    {
      return Stored_Name[n]; // return the place in the loop that passed
    }
    else
    {
      test = 0; // if not all loops pass
      i = 0;
     }
    }
    return "i"; // Return "i" for a failure - this is what we will use for access denied
    }

Comments

Similar projects you might like

Sigfox Forest Fire Detector

Project tutorial by Louis Moreau

  • 2,018 views
  • 2 comments
  • 13 respects

TV Tee Shirt

Project showcase by Maddy

  • 3,675 views
  • 1 comment
  • 11 respects

Race Car Dashboard (Online Connectivity)

Project showcase by Team Jatayu

  • 1,925 views
  • 4 comments
  • 7 respects

Arduino Live WiFi Web Scoreboard

Project tutorial by Oli Norwell

  • 2,242 views
  • 0 comments
  • 4 respects
Add projectSign up / Login