Blog Barista: Greg Antrim | May 1, 2019 | Internet of Things | Brew time: 12 min

The Internet of Things (IoT) is a wonderful thing. With the wide variety of sensors, development boards, and code samples available, you can monitor or automate just about anything. More often than not though, people struggle with making this data available and usable. It’s a problem that I have ran into during previous projects and one I ran into again when attempting to build a temperature and humidity monitoring system for our server room. In the past, I had built custom solutions and APIs to get to my data but for something as simple as monitoring temperature this seemed like overkill. Luckily, a coworker pointed me towards a company called Particle and their lineup of development boards. What makes particle different is that along with their boards, they give you access to the Particle Cloud allowing you to easily access data collected by your device. Even better, your first 100 devices are free.

In today’s post, I’ll show you how to wire up a DHT22 temperature and humidity sensor to a Particle Photon. We’ll connect the photon to the Particle cloud and then set up an integration between the Particle Cloud and IFTT to automatically alert us when the temperature or humidity reach a certain threshold. Here is a quick overview of the steps we’ll be covering:

  • Materials and Software Needed
  • Particle Account and Device Setup
  • Connecting the DHT22 Sensor to the Photon
  • Web IDE Project Setup
  • Writing the Code
  • Setting Up Email Alerts through IFTTT

Materials and Software

Hardware

Software

Particle Android or iOS App

Particle Account and Device Setup

Before we can use our Photon or the Particle Cloud, we need to create an account and then add our Photon to our account. Luckily, Particle makes the setup process pretty easy. Since the setup steps are somewhat device specific and highly interactive I’m not going to cover them here. Particle provides an excellent setup wizard on their  setup page.

Connecting the DHT22 Sensor to the Photon

Now that we have our device set up and connecting to our Wi-Fi Network it’s time to wire the DHT22 sensor to the Photon. Connect the DHT22 to the photon according to the table and diagram below.

Web IDE Project Setup

With our Photon connected to the internet and wired to our temperature sensor its almost time to start coding. To keep things simple, we will be using Particles Web IDE to write and flash our code. Its worth noting that Particle does offer a dedicated desktop IDE called Workbench that is powered by Visual Studio Code, but as of this writing it is still in beta and setup is a little complicated. Before we start writing our code we first need to set up a new project

1.   Navigate to the Particle Web IDE

2.   Enter a name for your project. I will be using server-room-monitor for my project name.

3.   Save your project by clicking the folder icon on the left sidebar

4.   Click the Libraries button on the left side bar and

5.   Enter DHT in the search box and select PietteTech_DHT from the search results

6.   Click the Include in Project button

7.   Click the Devices button on the left sidebar

8.   Click the star next to your device name to specify that we want to send code to that device

Writing the Code

Declarations

We’ve added the DHT libraries and selected our device so now it’s time to write some code. We’ll start by declaring some variables that will be used throughout our app. Between the #include <PietteTech_DHT.h> and void setup() lines add the following code.

Lines 1-4

Since the PietteTech DHT library works with several different DHT devices we need to specify the type of DHT device we are using. We specify digital pin 4 as the pin we are going to be receiving data on and then we initialize the DHT library with our device type and pin.

Lines 6-11

Here we define some variables that will be used to keep track of device state and to trigger alerts. Specifically:

  • serverTempMax and serverHumidityMax are the maximum temperature in Fahrenheit and humidity percentage that our device can reach before we trigger alerts for overheating or excess humidity
  • serverTemp and serverHumidity will hold the current temperature and humidity values
  • overheatDetected and tooHumid will be used to check for state changes so that we don’t continuously trigger alerts. I will go into more detail on this later

Particle Variables

Particle Variables are variables whose values can be checked through the Particle API. They are great for making simple values quickly accessible. We will be defining two variables, one for current temperature and another for current humidity. Add the following code to the setup() function:

In the code above, we declare two Particle variables: serverTemp and serverHumidity. We pass the Particle variables a reference to our local serverTemp and serverHumidity variables, and we specify that these variables should be of type DOUBLE.

Particle Events

By using Particle variables, we are already able to access the current temperature and humidity values from the Particle cloud. This allows us to manually check the current temperature from wherever we are, but most people don’t want to spend their days manually refreshing those variables. It would be much more useful if our device automatically alerted us when these values go above a certain range. Particle events allow us to do exactly that. Add the following code to the loop() function:

Lines 1-4

Acquire the latest values from our DHT sensor and update our local serverTemp and serverHumidity variables

Lines 6-9

If the temperature is above our specified maximum (81ºF) and we have not already detected an overheat, publish the server_temp  Particle event with a value of overheat.

Lines 10-13

If the temperature is below our specified maximum and we have previously detected an overheat, publish the server_temp  Particle event with a value of normal.

Lines 15-18

If the humidity is above our specified maximum (60%) and we have not already detected the excess humidity, publish the server_temp  Particle event with a value of tooHumid.

Lines 19-24

If the humidity is below our specified maximum (60%) and we have previously detected excess humidity, publish the server_temp  Particle event with a value of normal. Finally, we delay for 10 seconds to make sure we don’t exceed Particle’s rate limit.

That’s it for the coding portion of this post. Go ahead and hit the Flash button to upload the code to our photon. You should see the LED flash several different colors and then return to a slowly pulsing cyan.

Here is the full code for reference.

Testing

Our device is now broadcasting the current temperature and humidity and triggering events when we hit our temperature and humidity thresholds. In order to make sure everything is working we will head over to the Particle Device Console.

1.   Navigate to the Particle Device Console

2.   Select your device from the list of devices

3.   On the bottom right side of the screen you should see the Variables section. Our serverTemp and serverHumidity variables should be listed in that section. Click the Get button next to one of the variables and the current value of that variable will be displayed.

You can also view any triggered events in the event logs section of the page.

Setting Up Email Alerts through IFTTT

Now that our device is publishing temperature and humidity events, it’s time to take action when those events occur. Specifically, we will be sending out an email each time temperature or humidity exceeds the maximum value, and another email when those values return to normal. In order to accomplish our goal, we will be using Particle’s built in Integrations and IFTTT.

Creating an IFTTT Applet

If This Then That (IFTTT) is “…the easy way to get your apps and devices working together”  In our case we will be using it to send an email through Gmail whenever our Particle server_temp event is  published. The process for sending alerts for the server_humidity event is almost identical so I will not cover that in this post.

1.   Navigate to IFTTT and either create an account or log in

2.   Go to My Applets and click on the New Applet button

3.   Click on the plus sign

4.   Choose the Webhooks service

5.   Choose Receive a web request

6.   Enter server_temp in the Event Name input and click Create Trigger

7.   Click on the Plus Sign

8.   Select Gmail from the list of services

9.   Select Send an email

10.   Enter at least one email address in the To address field and

11.   In the Subject field enter: Server temperature status changed to: {{Value1}}

12.   In the Body field enter: Server temperature status changed to: {{Value1}}<br> When: {{OccurredAt}}

13.   Click Create Action

14.   Click Finish

15.   Navigate to the Webhooks Settings page and make a note of the secret key following the last slash of the URL. You will need this for the next step

So, what did we actually do here? We created an applet that will listen for incoming requests. When a request is received, we will parse the server_temp event value (either overheat or normal) from the JSON Body. In this case the event value of is sent in as Value1. We will also grab IFTTT’s built in OccurredAt variable which lets us know when the request was received. Using those variables, we will send an email to the email address specified in Step 11 with the subject we specified in Step 12 and the body we specified in Step 13.

Particle Integrations

We have created an IFTTT applet that will respond to incoming requests, so the next step is to get the Particle cloud to send out requests when temperature events occur. To accomplish this, we will be using Particle’s Integrations feature.

1.   Navigate to the Integrations page and click on New Integration

2.   Select Webhook from the list of integration types

3.   Enter the following Information, replacing <YOUR_IFTTT_SECRET_KEY_HERE> and <YOUR_DEVICE_NAME> with your values

Event Name: server_temp

URL: https://maker.ifttt.com/trigger/server_temp/with/key/<YOUR_IFTTT_SECRET_KEY_HERE>

Request Type: POST

Request Format: JSON

Device: <YOUR_DEVICE_NAME>

4.   Click Create Webhook

With that integration set up, Particle will send a POST request to IFTTT whenever a server_temp event occurs.

And That’s it! Our photon is broadcasting variables and events and we will now receive email alerts when the temperature goes over our threshold and another email when it returns to normal. With a basic understanding of Particle variables, events, and integrations we could easily expand on this post to send SMS alerts, or even build a full web or mobile application to monitor the temperature and humidity. I look forward to seeing what you come up with!

0 Comments

Submit a Comment

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

Other recent posts:

The Art of Taekwondo

The Art of Taekwondo

How Leadership Skills From Martial Arts Can Apply to Your Job
Blog Barista: Casey Herman | August 21, 2019 | Career Hacks | Brew time: 6 min
I expected to learn self-defense, how to perform specific strikes, discipline, and respect through Taekwondo, but as someone that had studied leadership theory as both an undergrad and graduate student, I did not expect to learn about leadership as much as I have from Taekwondo…

read more
Finding a File Management API for Software Development

Finding a File Management API for Software Development

Why You Should Consider Aspose.Words for Microsoft Word to PDF Conversions
Blog Barista: Sokwhan Huh | August 15, 2019 | Development Tools | Brew time: 5 min
John E. Warnock, the cofounder of Adobe, had a mission in 1990. In his very conspicuously named memo “The Camelot Project,” John best summarizes the difficulty many industries faced in sharing documents…

read more

Pin It on Pinterest