Monitor the humidity and temperature of your home or apartment from anywhere. The Spark Core connects to your home WIFI and allows you to monitor or control devices remotely. In this post we’ll start with a basic sensor. The HTU21D sensor was the least expensive sensor I could find that monitored both humidity and temperature.

Update: The HTU21D library needs some minor modifications to work with the Spark Core. I’ve created a Gist with code you should use.

This hookup guide is slightly different than hooking the HTU21D to an Arduino UNO. You can find an UNO hookup guide here. The downside to using the UNO is that it requires more effort to make the data accessible via the web. The Spark Core makes web integration a snap! I use my UNO for tinkering and my Spark Core for my connected home projects.

You’ll need:
– A Photon $19.00
HTU21D sensor $11.95
Male to Female wires $1.95
Breakout Headers $1.50
– A soldering iron and solder

Pre-requisites:

Spark Core connected to your WIFI and ready to accept new sketches via the web interface. Solder 4 breakout headers to your HTU21D sensor for easy hookup using the male to female wires.

Step 1:

Connect the HTU21D sensor to the Spark Core. For accurate readings, make sure the sensor is at least 6 inches away from your board and heat sources.

Spark Core HTU21D

Click to zoom the images.

Breadboard Image

Step 2:

Create the Spark Core sketch. This sketch requires the HTU21D library that can be found on the Spark Fun product page. To include this library on the Spark Core, you’ll need to copy and paste the code into the tabs at the top of your sketch.

Humidity Sketch

Humidity.ino

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// This #include statement was automatically added by the Spark IDE.
#include "HTU21D.h"
 
/*
 HTU21D Humidity Sensor Example Code
 */
 
//Create an instance of the object
HTU21D myHumidity;
char resultstr[64];
 
void setup()
{
  myHumidity.begin();
  // expose your char buffer to the Cloud API
  Spark.variable("result", &resultstr, STRING);
}
 
void loop()
{
   
  float humd = myHumidity.readHumidity();
  float temp = myHumidity.readTemperature() * 9 / 5 + 32; // Convert to F
  sprintf(resultstr, "{\"data1\":%f,\"data2\":%f}", humd, temp);
  delay(30000);
}

Flash the new sketch to your device with the humidity.ino and HTU21D library files.

Step 3:

Use the Spark Cloud API to access the char buffer from your device.

https://api.spark.io/v1/devices/YOUR_DEVICE_ID/result?access_token=YOUR_ACCESS_TOKEN

Right now the results are in JSON and not visually appealing. In a future post I’ll walk through setting up a database, cron job and front end website to display the data.