Particle Photon Email Integration for Arduino Innovations

Introduction

This tutorial is brought to you by BreakoutBros.com. Access the complete version by following this link.

Having explored the Particle Photon, I felt inclined to revisit one of our tutorials and introduce IoT (Internet of Things) capabilities using the Photon. In this guide, I will demonstrate how to enable the Particle Photon to send you emails by integrating this functionality into our RFID Badge Scanner with LCD tutorial.

If you haven’t yet explored the RFID Badge Scanner with LCD tutorial, your initial step is to visit the article and set up the necessary components. This setup enables you to scan an RFID badge and showcase a personalized “welcome home” message linked to the user associated with the RFID tag.

What you will need

Apart from the components required for the RFID badge scanner, you’ll also require the following materials:

– Particle Photon
– 3x 10K resistors*
– 3x 2k resistors*
– 3x NPN 2222A Transistors*
– Wires and Breadboard*
(*Note: These items are available on Amazon or at electronics stores. Alternatively, you can explore Arduino Starter Kits, which typically include these fundamental components.)

5V to 3.3V level shifting

In this tutorial, we’ll utilize the Arduino to establish direct communication with the Particle Photon by employing three distinct I/O pins. When the first pin registers as High, it signifies the scanning of the 1st user’s badge. Similarly, High readings from the second and third pins indicate the scanning of the 2nd and 3rd user’s badges, respectively.

Notably, the Particle Photon operates on 3.3V logic, while the standard Arduino functions on 5V logic. To ensure secure communication through I/O pins, it’s essential to create a level shifter that transitions 5V to 3.3V. While a direct connection of 5V to the Particle Photon I/O may function, it’s not advisable due to the potential strain and probable damage to the pins. This caution is especially crucial when dealing with Analog pins, as it exerts excessive pressure on the analog-to-digital converter. Particle explicitly advises against directly linking 5V to these pins.

The 5V to 3.3V level shifter circuit involves a straightforward setup consisting of an NPN transistor and specific biasing resistors.

This setup involves converting a 5V output from the Arduino Pin to a 3.3V input on the Photon pin. When the Arduino Output is at 0, the corresponding Photon input pin will also register as 0.

You’ll require three of these circuits, each designated for the pins interfacing between the Arduino and Photon. Please adhere to the provided pinout instructions below for proper connectivity.

Subsequently, connect these three circuits using a breadboard. The Particle Photon starter kit includes one, and it is also included in the Arduino Starter Kits.

Power up the Photon through the Arduino’s 5V output by linking VIN on the Photon to the Arduino’s 5V supply. This pathway will pass through the Photon’s voltage regulator, adjusting the voltage to the required 3.3V. Remember to establish a connection between GND on the Photon and GND on the Arduino.

With both your Arduino and Particle Photon configured for communication using a 5V to 3.3V level shifter, it’s time to initiate the coding process for the Particle Photon.

As mentioned in the Particle Photon Review, an online Integrated Development Environment (IDE) is utilized for programming purposes. Navigate to www.Particle.io/build and log in to your Particle account. Create a new program tailored to publish an event based on specific inputs read by the Photon from the Arduino. This event will serve as a trigger for sending you an email later on. Additionally, the code will incorporate functionality for the LED to visually indicate when and to whom an email is being sent, with the LED blinking at varying rates based on the recipient.

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 comprises the complete list of pins to be utilized and the data bits to be stored according to the Arduino outputs. It also specifies setting the LED blink rate to one second.

Following this, we’ll delve into the setup loop, primarily focusing on defining pin outputs and initializing variables.

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 final section of the code serves as the main loop. It directs the photon to respond according to the data it receives from the Arduino’s Bits.

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 primary loop initially retrieves and stores the pin states into their respective variables. Subsequently, it determines the active bit and triggers the execution of the Particle.publish(“EVENT”, “DATA”) command. The initial input in the Particle.publish command represents the “event,” while the second input denotes the “DATA.” Here, we label this event as “RFID_HOME,” with the user badge name serving as the data input. Additionally, a distinct LED blink duration is assigned for each user, allowing for visual identification when the Photon detects a specific badge.

To prevent multiple event transmissions from a single Arduino scan, a delay follows the execution of the Particle.publish command. The remaining segment within the code loop manages LED blinking at the LEDDelay frequency. For access to the Photon Particle code associated with this tutorial, it’s available here.

Setting up IFTTT For the IoT Email

With the Particle device publishing the RFID_HOME event to the Particle Cloud, your next step involves configuring a service to monitor the publication of this data. In this tutorial, If This Then That (IFTTT) will be employed. Begin by visiting ifttt.com and setting up an account.

Search for “Particle” and select the Particle Icon within the services section:

Afterward, proceed to choose an Applet that utilizes Email. Scroll through the options and select “Test of Electron Beam Switch” below.

Flip the switch to turn this applet on

There’s a need to save and revisit the Applet for configuring the Email section, for reasons unknown. Here’s the email format: The subject line will read as “{EventContents} has entered the front door,” while the body text will be: “Hey, your {DeviceName} has logged that {EventContents} has entered the front door. Welcome them Home.”

To complete the IoT setup, two additional applets need to be set up for the other badge names – SAM and BRAD.

Testing the Particle Photon Email

Once the criteria for identifying a valid tag’s success is met, upon finding a name matching “Josh,” “Brad,” or “Sam,” the corresponding pin output is transmitted to the Particle Photon. To allow the Photon time to register the pin change, a 5-second delay is introduced. Following this delay, the pins revert to 0 to prevent further email transmission. Find the complete Arduino code here.

With the successful integration of the Particle Photon, the RFID Badge Scanner with an LCD becomes an IoT-enabled device. Whenever a specified badge is scanned, an email notification is sent. By incorporating this system into your front door and controlling a lock solenoid, you create a monitoring setup for your house’s front door lock. This setup alerts you via email whenever someone uses a badge, providing awareness of home activity regardless of your physical presence.

I trust you found value in and gained insights from this tutorial. To stay updated on future articles, kindly subscribe. We’re planning to introduce additional functionalities to the RFID Badge Scanner, so stay tuned for more updates!

Code

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

About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top