The wait is over! You can now publish data from the Spark Core through the Cloud.
Here’s the blog post:
http://blog.spark.io/2014/03/11/spark-publish/
Here’s the Particle Docs reference:
https://docs.particle.io/reference/firmware/electron/#particle-publish-
Don’t want to go to the blog? Well stick around here and let’s chat through it.
Last Friday, we wrapped up Sprint 7 on our team. We released a number of minor bug fixes and improvements, but the one major feature that we delivered is Particle.publish()
. Here are some examples:
// The most basic event, without data.
Particle.publish("motion-detected");
// Some events require additional data.
Particle.publish("temperature", "19 F");
// You can set a TTL to persist data in the cloud...
Particle.publish("lake-depth/1", "28", 21600);
// ...and make events private for secure applications.
Particle.publish("front-door-unlocked", NULL, 60, PRIVATE);
Up until now, the only way to communicate with the Spark Core was to ask it something. You could remotely call a function using Particle.function()
, such as calling the brew()
function on a connected coffee-maker to brew a cup of coffee on the fly. Or you could store the temperature in a local variable temp
, and then check the temperature with Particle.variable()
, and ask for the temperature at any time.
But what if you want the Spark Core to talk to you? Enter Particle.publish()
. This feature lets you publish events from the Spark Core, which can be subscribed to through the API. Events are published to a topic, and can be public or private.
To showcase this feature, I’m going to build a Particle-powered motion detector, using an off-the-shelf PIR sensor. Here’s the hardware:
If you’re following along, here’s a Fritzing diagram showing how the components are wired.
My goal is to have this motion detector inform me when it detects motion (natch). Perhaps it could even text me through Twilio? Hello, ad hoc security system.
First, I’ll connect my Spark Core to my Wi-Fi network. I’m going to use the recently released Particle CLI. Once the CLI is installed through npm install -g particle-cli
, I can start to play.
Now my Core is connected to the internet and to the Cloud, which I know because its little LED is breathing cyan. Breathing = happy and alive.
Next, it’s time to develop my firmware. Adafruit has some great resources for using these sensors.
I threw together a quick 50-line application that will publish to the Cloud every time motion is detected. I chose particle-hq/motion
as the topic; our team will use the top-level particle-hq
topic for data generated at our office. You can find a gist of the code here.
/*
* Connected sensor
* Particle.publish() + PIR motion sensor = awesome
* Thanks to Adafruit for the reference and inspiration
*/
int inputPin = D0; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
int calibrateTime = 10000; // wait for the thingy to calibrate
void setup() {
pinMode(inputPin, INPUT); // declare sensor as input
Serial.begin(9600);
}
void loop(){
if (calibrated()) {
readTheSensor();
reportTheData();
}
}
bool calibrated() {
return millis() - calibrateTime > 0;
}
void readTheSensor() {
val = digitalRead(inputPin);
}
void reportTheData() {
if (val == HIGH) {
if (pirState == LOW) {
// we have just turned on
Serial.println("Motion detected!");
Particle.publish("particle-hq/motion");
// We only want to print on the output change, not state
pirState = HIGH;
}
} else {
if (pirState == HIGH) {
// we have just turned of
Serial.println("Motion ended!");
// We only want to print on the output change, not state
pirState = LOW;
}
}
}
Now I can subscribe to the stream of events using Server-Sent Events. Using curl
as an example, I type this in my terminal:
curl -H "Authorization: Bearer {ACCESS_TOKEN_GOES_HERE}" \
https://api.particle.io/v1/events/particle-hq
and now I’m listening to a stream of events from particle-hq
. Besides my own motion sensor, we’ve got a temperature sensor (69 degrees!), and will be adding more as we put together more prototypes. Since this data is public, anyone can subscribe; go to the Particle Build IDE and get your access token from the “Settings” panel, and you can subscribe to this data too!
Now I don’t just want to see a stream of data in my terminal; I want to do something with it. Luckily, Server-Sent Events are part of the HTML5 protocol, and there are interfaces available in many programming languages. Check out this tutorial from html5rocks for more information on using Server-Sent Events.
Coming soon: more features!
This is just the beginning for Particle.publish()
. A few weeks from now we’ll add even more functionality, such as:
- Setting up webhooks for events to POST a message back to your server
-
Particle.subscribe()
, so that devices can talk with one another
Let us know what you think and share your Particle.publish()
projects here!