If you backed the Particle Electron on Kickstarter, you just received your reward! Now how do you get started using it?
I'll show you how to make an internet-connected thermometer so you can see how the temperature varies throughout the day in various places in your house.
You will need an Electron and a DHT22 temperature and humidity sensor that you can buy from Sparkfun.
So what's in this box you received from Particle?
Let's go to particle.io/start and in the Electron section, click on "Setup my Electron"
Let's go through and active the SIM card. If you are a Kickstarter backer, don't forget to use the code you received by email for the first 3 months of data free!
While the SIM card activates, let's get the hardware out and build our first project.
The Electron indicates its status with the color LED. At first it will blink green while connecting to the cell tower. After a few minutes it will breathe cyan.
We will use ThingSpeak in order to display the temperature and humidity.
Create an account at thingspeak.com and create a channel called "Home Temperature" with 2 channels called "Temperature" and "Humidity".
After hitting Create channel you will need to note down the Channel ID and the Write API key for later.
Let's now attack the task of writing code for the Electron and flashing that code into the Electron.
After clicking through the Electron setup, you'll end up in the online integrated development environment (IDE) where you can program the Electron.
For this project, we will use an existing library to talk to the temperature sensor and send data to Thingspeak.
After naming your app and hitting "Save" (the folder icon), click the bookmark icon on the left to open the library tab.
Search for the "Adafruit_DHT" library and click "Include in app".
Do the same for the "ThingSpeak" library. You will see 2 Thingspeak library. Select the one with the most downloads since it's the one written by the ThingSpeak development team.
Here's the code that we'll be using for this project. The Electron samples the temperature and humidity, sends the data to ThingSpeak and goes to sleep for 15 minutes to conserve battery power. When the Electron wakes up 15 minutes later, the code starts back from the beginning.
Make sure to put your ThingSpeak channel number and write API key from before into your copy of the code.
// This #include statement was automatically added by the Particle IDE.
#include "ThingSpeak/ThingSpeak.h"
// This #include statement was automatically added by the Particle IDE.
#include "Adafruit_DHT/Adafruit_DHT.h"
// Sensor type
#define DHTTYPE DHT22 // DHT 22 (AM2302)
// DHT22 sensor pinout:
// Pin 1 (on the left): +3.3V
// Pin 2: output
// Pin 4 (on the right): GROUND
#define DHT_5V_PIN D1
#define DHT_SENSOR_PIN D2
#define DHT_GROUND_PIN D4
DHT dht(DHT_SENSOR_PIN, DHTTYPE);
/* Thingspeak */
TCPClient client;
unsigned long myChannelNumber = 89137;
const char * myWriteAPIKey = "3D23PQHQIJ3OZXNF";
void setup() {
// Connect to ThingSpeak
ThingSpeak.begin(client);
// Give power to the sensor
pinMode(DHT_5V_PIN, OUTPUT);
pinMode(DHT_GROUND_PIN, OUTPUT);
digitalWrite(DHT_5V_PIN, HIGH);
digitalWrite(DHT_GROUND_PIN, LOW);
// Wait for the sensor to stabilize
delay(1000);
// Initialize sensor
dht.begin();
// Read Sensor
double temperature = dht.getTempCelcius();
double humidity = dht.getHumidity();
// Update the 2 ThingSpeak fields with the new data
ThingSpeak.setField(1, (float)temperature);
ThingSpeak.setField(2, (float)humidity);
// Write the fields that you've set all at once.
ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
// Give time for the message to reach ThingSpeak
delay(5000);
// Sleep for 15 minutes to save battery
System.sleep(SLEEP_MODE_DEEP, 15 * 60);
}
void loop() {
// This will run because the system is sleeping
}
In case you are wondering, the reason we connect the sensor to D1 and D4 for 3.3V and ground instead of connecting the sensor directly to the supply pins is that while the Electron microcontroller is sleeping, we don't want the sensor to be powered. Powering a device through microcontroller pins only works for devices that need very little current since microcontrollers can typically supply less than 20 mA on a pin.
Let's hit the lightning bolt and flash the program into the Electron! You will get a warning that flashing over the air (OTA) uses a bit of data. At this point it's OK to say "Flash OTA anyway". The Electron will flash purple a few times and restart.
You should now see that the Electron goes to sleep after 6 seconds and your first data point appear in ThingSpeak.
After a whole day, you'll be able to see a nice graph of the temperature and humidity.
You'll be able to adjust your heating and cooling to care for the people who matter the most to you.
Comments