In this simple project I show how to connect a sensor to the photon to transfer data via I2C and publish them in ThingSpeak. I use the inexpensive Adafruit Si7021 sensor:
The sensor has ± 3% relative humidity measurements with a range of 0–80% RH, and ±0.4 °C temperature accuracy at a range of -10 to +85 °C. It uses I2C for data transfer so it works with a wide range of microcontrollers.
Beyond that I give some hints how to publish data to use them with IFTTT and access them with the Particle CLI.
Setup the breadboardConnect the Photon and the Adafruit Si7021 sensor according to the diagram you find in the SCHEMATICS section. Connect the D0 pin of the Photon to the SDA (Data) pin of the Si7021, and the D1 pin of the Photon the SCl (Clock) of the Si7021. See section PIN OUT DIAGRAMS here.
Tip: You find Adafruit parts in "Fritzable" format here.
Setup the webhookCreate a new channel with tow fields - one for the temperature and one for the humidity - in ThingSpeak and get a "Write API Key". Then create two webhooks, one for temperature and one for humidity, in the Particle Console following the instructions from particle.io:
- Set the event name to temp to match the field in ThingSpeak
- Set the URL to https://api.thingspeak.com/update
- Make sure the request type is set to POST (it should be already)
- If you'd like to limit the webhook triggering to a single one of your devices, choose it from the device dropdown
- Next, click on "Advanced Settings," and find "Send Custom Data." Choose "form" from the available options. Drop in the following key/value pairs:
api_key: YOUR_API_KEY
field1: {{PARTICLE_EVENT_VALUE}} in Webhook #1
field2: {{PARTICLE_EVENT_VALUE}} in Webhook #2
Using the Adafruit Si7021 libraryTo let the Photon talk to I2C sensors and to make things easy I use the Adafruit Si7021 library, which seems to be a wrapper of the Arduino Wire library.
Tip: According to this blog post most of the Arduino libraries should work with Particle devices:
Improved Arduino library compatibility: Most Arduino libraries can now be copy/pasted into our library manager without modification
To use, just include the library in your project in the Particle IDE. Have a look at the example provided how to use the library. Please note that you have to instantiate a sensor:
Adafruit_Si7021 sensor = Adafruit_Si7021();
To get access to the temperature and humidity data:
h = sensor.readHumidity();
t = sensor.readTemperature();
Flashing the codeFlash the code you find in the CODE section. For details see here.
Power the PhotonFinally power the Photon. I use the Raspberry Pi Universal Power Supply.
Look at your dataThingSpeak generates diagrams with your data and you can access these from your desktop or mobile devices.
Tip: Set the message update interval limit of events with delay to min. 15 seconds to use the free license option (see here).
Tip: Click the little pencil in the diagram to set the number of results (data points) and reload the page if data points are missing in the diagram.
Option: Publish data with IFTTTYou can also publish your data with IFTTT. For this you have to publish your variables in the setup function:
Particle.variable("t", &t, DOUBLE);
Particle.variable("h", &h, DOUBLE);
I used the Particle and the Telegram services for this. Follow the instructions for these services to set them up.
Option: Access the data with the Particle CLIPublishing the data with Particle.variable gives you also access to the variables with the Particle CLI with particle get - for details see here. This retrieves a variable value from one of your devices. Use particle list to see which devices are online, and what variables are available.
Comments