Software apps and online services
Sometimes IoT projects need to send email notifications when a triggering event occurs. Particle Devices have an integral cloud connection that allows for triggering with a single Publish command. There are lots of ways to connect a webhook event to email: Azure, AWS, IFTTT. The down side with these methods is the number of interconnections and accounts you need to set up. The SendGrid account is free and easy to integrate.
SendGrid is a major email sending service that boosts billions of emails sent per day. They offer a free account (100 per day limit) that's just right for most small projects. Create your account by signing up at their site for a free trial. Once you are confirmed and activated, create an API Key for this project. Paste this key value into the code where shown. The actual email message is JSON formatted with all of the necessary fields shown. You will need to put your email address in and adjust the subject and message to suit your notification.
The SendGrid API will send to any valid email account. If you use Gmail, and the Gmail phone app, you can get notified in seconds of the event happening. This is because Gmail will push the email to the phone. Other accounts that use a polling method will pull the emails to the phone so the update time is based on that polling rate which is usually 15 minutes or longer.
The Code section below contains the custom webhook template that does the hard part for you. Download it before proceeding with the instructions that follow:
1. Login to your Particle account and select Console.
2. Locate the Integrations icon on the left and select.
3. Locate the New Integration box and select.
4. You will now see the default Webhook creation screen. Click on the Custom Template link.
5. You will now see the default custom template screen. Copy the text from the template file you already downloaded (from the Code section below) and paste it into the edit window. You should now see this result.
6. Edit the following fields:
- event - create a unique name for this event. It will be the one you Publish to.
- Authorization - paste your SendGrid API_KEY after the word Bearer
- email - enter your email address
- name - enter your name
Note: Test this much out first before editing any other fields. The JSON format is specific to SendGrid's API so its easy to break.
7. Click Create Webhook. If all goes well, you should see a successful creation message.
8. Test the Webhook by clicking on the Test icon at the top of the screen.
9. You should now have one of these results:
- Success - check your email inbox and spam folder for a new message.
- Timeout - check your email anyway, a timeout does not mean the message did not go thru.
- Error - check the response from SendGrid and look for the error message. You can find help for them at the SendGrid site by clicking here.
10. Integrate with your Photon project with the example code shown on the Webhook screen like this:
I wanted a very simple way to get an email notification, and I think this custom integration does exactly that. It's really bare bones, so feel free to augment and improve.