Photon Temperature Sensor - toSvenson/intro-workshop GitHub Wiki
Build a Photon Temperature Sensor
In this experiment you are going to build a sensor able to measure the current room temperature. The data will be transmitted via Wi-Fi to a cloud service.
Required Parts
To build this sensor you need the following hardware:
- 1x breadboard
- 1x Particle Photon
- 1x BMP180 sensor
- 1x male-male wires
Wire up the circuit on breadboard
Place components as shown in the schema. Don't put the sensor right next to the Photon since it produces some heat which will result in incorrect readings.
The sensor might be mirrored when you compare with the image below, depending how the pins are soldered on the module. Check the printed labels on the module.
| Photon | BMP180 |
|---|---|
| 3V3 | VIN |
| GND | GND |
| D1 | SCL |
| D0 | SDA |
Connect Photon to the cloud
(If your Photon is already connected, skip to next step)
Each Photon has a unique number associated with it, this number (representing the device) can be easily CLAIMED (and DELETED) from your account.
There are different ways to connect your Photon to your account.You are going to perform the setup using the CLI (Command Line Interface) to fully understand what is happening.
( Make sure you have successfully completed all steps of the Laptop Setup )
OS X users, open a new terminal or Windows users, open a new Node.js command prompt
It's time to connect your Photon to a USB port on your computer.
After a few seconds, the status LED should start blinking blue. If this is not the case, press the SETUP button for 3 seconds (until the led starts blinking blue)
Identify device
First you want to know the Device ID of the Photon you just connected.
$ particle identify
Your device id is 3c0123456789012345678901
Copy this device id in your clipboard, you will need it in a few moments.
Setup Wi-Fi connection
Before we can CLAIM the Photon, we need to connect it to the internet through Wi-Fi.
$ particle setup wifi
? Should I scan for nearby Wi-Fi networks? Yes
? Select the Wi-Fi network with which you wish to connect your device: YourWifiNetwork
? Security Type: WPA2
? Wi-Fi Password: YourWifiPassword
Done! Your device should now restart.
Check the Photon status LED, it should be BREADING CYAN when it's happily connected to the internet.
Add device to your account
Now it is time to CLAIM your device and give it a NAME. This will associate it exclusively to your Particle account.
$ particle cloud claim 3c0123456789012345678901
Claiming device 3c0123456789012345678901
Successfully claimed device 3c0123456789012345678901
$ particle cloud name 3c0123456789012345678901 PhotonTemp
Renaming device 3c0123456789012345678901
Successfully renamed device 3c0123456789012345678901 to: PhotonTemp
Check and test the device (optional)
Check if the device is associated to your account
$ particle cloud list
PhotonTemp [3c0123456789012345678901] (Photon) is online
Functions:
int digitalread(String args)
int digitalwrite(String args)
int analogread(String args)
int analogwrite(String args)
If you see these 4 functions above, you can already send a command to the Photon!
$ particle call PhotonTemp digitalWrite "D7,HIGH"
1
Congratulations, you have completed your first IoT command!
Did you notice what happend with your Photon? The blue LED is connected to D7 (Digital port 7) and you have set it the HIGH making the LED light up. Now try to turn it off again.
$ particle call PhotonTemp digitalWrite "D7,LOW"
1
Very good! You are ready to write your first sketch.
If you can't get enough.. A complete overview of the Particle CLI can be found here.
Write sketch for the Photon
Open the online build environment of Particle.io on build.particle.io.
At the left-hand side you can see the vertical menu which you will use during the process. Hover you mouse over each item to get familiar with the different options:
FLASH VERIFY SAVE CODE LIBRARIES DOCS DEVICES SETTINGS
Let's get started!
- First we want to select the Photon we connected to our account in the previous step. From the menu, press
DEVICES - Check if your device is listed and select it by clicking the yellow star in front of the device name.
- From the menu, press
"< >" Codeto open the code editor - Press
CREATE NEW APPand enter a Title (e.g.PhotonTempApp) - Again at the left-hand side press
Libraries(first item below"< >") - In the Community Libraries search box type:
BMP - Press on the search result
ADAFRUIT_BMP085 - Press
INCLUDE IN APP, select the app you just created (e.g. photon_temp) and pressADD TO THIS APP - Paste the code below in the
.inofile, replacing it's current content
// This #include statement was automatically added by the Particle IDE.
#include "Adafruit_BMP085/Adafruit_BMP085.h"
/*
Wiring
------
BMP085 Vcc to 3.3V
BMP085 GND to GND
BMP085 SCL to D1
BMP085 SDA to D0
*/
Adafruit_BMP085 sensor; // Sensor instance
void setup() {
// Initialize Photon serial debug
Serial.begin(9600);
// Initialize Photon onboard blue LED
pinMode(D7, OUTPUT);
// Initialize Sensor BMP180
if (!sensor.begin()) {
Serial.println("No sensor found, please check wiring!");
while (1) {}
}
}
void loop() {
// Publish events. Wait for 2 second between publishes
PublishSensorInfo();
blinkLed();
delay(5000);
}
// Publish Pressure, Altitude
void PublishSensorInfo(){
Serial.print("Temperature = ");
Serial.print(sensor.readTemperature());
Serial.println(" *C");
Serial.print("Pressure = ");
Serial.print(sensor.readPressure()/100.0);
Serial.println(" hPa");
char sensorInfo[64];
sprintf(sensorInfo, "Temperature=%.2f °C, Pressure=%.2f hPa", sensor.readTemperature(), sensor.readPressure()/100.0);
Particle.publish("PhotonTemp", sensorInfo);
}
// Blink onboard LED
void blinkLed(){
digitalWrite(D7, HIGH);
delay(500);
digitalWrite(D7, LOW);
delay(500);
}
- From the menu, press
VERIFY.
This should result in the messageCode verified! Great work. - Now you are ready to press
FLASH
If everything goes well, you will get a:
Flash successful! Please wait a moment while your device is updated... - The Photon device should return to
breading cyanstatus color
Monitor the published sensor values
- Debug readings from local serial port (Photon need to be connected to USB)
$ particle serial monitor
Opening serial monitor for com port: "COM12"
Temperature = 24.49 *C
Pressure = 1026.92 hPa
- Monitor the values published to the cloud (CLI)
$ particle subscribe PhotonTemp
Subscribing to "PhotonTemp" from the firehose (all devices)
Listening to: /v1/events/PhotonTemp
{"name":"PhotonTemp","data":"Temperature=24.61 °C, Pressure=1026.96 hPa","ttl":"60","published_at":"2015-12-29T19:49:40.553Z","coreid":"3c0123456789012345678901"}
- Monitor the values published to the cloud (Webbrowser)
Go to https://dashboard.particle.io/user/logs and log in with your Particle account.