Last summer I came across the
Particle Photon and decided to pick up two of them and give them a try. Fast forward a number of months and I decided on a simple project combining a DHT22 temperature and humidity sensor with the Particle Photon in a simple IOT project.
Introduction
The Photon is a nice development platform in the style of the Arduino. The Photon consists of a STM32 ARM Cortex M3 microcontroller paired with a Broadcom WICED wifi chip. This allows easy access from the board to the Internet.
Development with the Photon can be done a couple of different ways. There is a web based IDE paired with an online compiler known as Particle Build. There is an IDE that can be installed on your computer which also uses the online compiler known as Particle Dev. The online compiler can be accessed using a node.js based command line environment which allows you to use a iDE of your choice. Finally you can roll your own development environment with the ide of your choice, ARM gcc and the DFU boot loader. The flash process can be done over wifi or usb.
The development environment is styled after the popular Arduino development environment with good documentation and core and user contributed libraries I have a good bit of experience with that Arduino and It’s IDE and found that there was a very minimal learning curve.
The most complex part of developing on the Photon is activating your Photon so that it can be flashed via wifi over the Internet. There are a number of methods for doing this. I used the
Android App and the process went very smoothly.
To get started there is excellent documentation on the
Particle website and a very good guide at
Sparkfun.
My plan was to read the DHT22 with the photon and upload temperature and humidity data over the Internet to an IOT analytics service.
Hardware Setup
Minimal hardware is required for this project. I used a mini breadboard, s photon, a DHT22, 10 K resistor and some hook up wire.
The DHT22 has four pins on it. With the DHT22 facing you so you are looking at the vents the pins are numbered 1 – 4 left to right.
- VCC 3V to 5.5V: connects to 3.3V on Photon with a 10K resistor to pin 2.
- Data: connects to D3 on the Photon.
- N/A: is not used
- Ground: connect to GND on the Photon.
Software
Using
Particle Build,
Particle Dev or the
command line development environment a program is written the same way as an Arduino sketch with a setup routine that is executed once and a loop routine that is executed continuously after setup completes.
My simple example was made considerably easier thanks to the user contributed library for the DNT22 sensor. The PietteTech_DHT library can be found in the list of user contributed libraries in Particle Build or as a
GitHub repository.
To build this example I used the command line. When using the command line environment you put your library files, in this caswe PietteTech_DHT.cpp, PietteTech_DHT.h, and your Photon program in the same directory. When you compile or flash you specify the directory as the source.
The example below reads the sensor and then publishes the temperature, humidity and status to the
Particle website.
#include <cstdio>
#include "PietteTech_DHT.h"
#define DHTTYPE DHT22 // Sensor type for PietteTech_DHT
#define DHTPIN 3 // Digital pin for PietteTech_DHT
// Must be declared before PietteTech_DHT
void dht_wrapper();
// Library instantiation
PietteTech_DHT DHT(DHTPIN, DHTTYPE, dht_wrapper);
float temp;
float humidity;
float status;
void setup() {
Serial.begin(9600);
}
// Must be setup like this for PietteTech_DHT to work
void dht_wrapper() {
DHT.isrCallback();
}
void loop() {
int result = DHT.acquireAndWait();
if (result == DHTLIB_OK) {
temp = DHT.getFahrenheit();
humidity = DHT.getHumidity();
} else {
temp = 0;
humidity = 0;
}
char buf[80];
int n = snprintf(buf, 80,
"{ \"temp\": \"%5.2f\", \"humidity\": "
"\"%5.2f\", \"status\": \"%d\" }",
temp, humidity, result);
Particle.publish("THEvent", buf, 60, PRIVATE);
delay(60000);
}
In a command window at the command line compile using the command below where THPublish is a subdirectdory that contains the ino file and library files.
To flash your code to the photon is done using the command below again THPublish is a subdirectory that contains the ino file and library files. Also, photon2 is the name of the photon you are flashing.
The photon led will flash blue/red and then green and back to blue.
If everything is successful you can go to the Particle log and see you events and event data. Starting
at the main Particle web page select
from the upper right corner. Then on the left side of the screen select the log icon
and see your events and event data in the log. The way that logging works it will start logging when you show the log which means you will need to wait for your event to occur before it will be shown.
Thoughts
The folks at Particle has put together a very nice device and a comprehensive development environment. The price of the device and the ease of use has greatly lowered the cost of entry in the IOT space. Beyond traditional IOT the Particle Photon can be used to allow easy interaction with a microcontroller based project over wifi.
The Particle Photon can be purchased for $19 from
Particle,
Sparkfun or
Adafruit making it an attractive option for your next project.