More Particle madness

The graphics in this post will no longer work now that I have started a new project with this particle.

Now that I’ve mastered (!) publishing sensor readings from a Particle device and visualizing them with ThingSpeak, it’s time to step up my game a little bit. I have an old BMP180 temperature and pressure sensor that I thought was toast. I’m glad I didn’t throw it out because it seems to be working well.

A static view of temperature and pressure data from a BMP180 sensor connected to a Particle Xenon.

I was interested in this sensor because it reports two values. If I want to send both values to ThingSpeak using the Particle tutorial, I would have to set up two webhooks (and possibly two channels). That seems like a lot of work. I sought an alternative. The particle community has several discussions (such as this one) that show how to do it.

The solution involves publishing an event that is formatted in Javascript Oject Notation (JSON). The format is rather straightforward: a comma-separated list of keys and values, where the keys and values are separated by a colon and the list is enclosed in curly braces:

{ <KEY1> : <VALUE1>, <KEY2> : <VALUE2> }

The entire object must be converted into a string, which is most easily (I think) done with the following type of command:

Particle.publish("<EVENT_NAME>",
  String::format("{\"<KEY1>\":%f, \"<KEY2>\":%d}", <VALUE1>, <VALUE2>)
);

Now, the form data that will be sent along with the webhook should look like this:

          
{
  "api_key": <API_KEY>,
  "field1": "{{<KEY1>}}",
  "field2": "{{<KEY2>}}"
}

   

Remember that in order for the event publishing to trigger the webhook, the name of the event needs to be the same as the name of the webhook. Also, keep in mind that the length of the data that can be published is limited to 622 characters, so a proper JSONify function should include a length check prior to publishing. Note to self – as development of these products matures, it will be wise to prefix events with a unique/custom string in order to identify them as mine.

I’ve worked more-or-less backwards here. I’ve set up the communication between the sensor hardware and the web-based visualization tool; however, I have not yet obtained any sensor data to send. It turns out that the Adafruit tutorial for the BMP180 works out of the box with Particle. I’ve attached the sensor to a Xenon particle, which has I2C communication capabilities. Further, a search of the libraries currently available through the web IDE shows that the older version Adafruits BMP library is available. A minute or two of wiring plus the following code got me the data I wanted.

// This #include statement was automatically added by the Particle IDE.
#include <Adafruit_BMP085.h>

Adafruit_BMP085 bmp;

double temp;
double pressure;

void setup() {
    bmp.begin();  
}

void loop() {
    temp = bmp.readTemperature();
    pressure = bmp.readPressure();
    Particle.publish("Mb data", String::format("{\"temp\":%.1f, \"pressure\":%.0f}",temp,pressure));
    delay(60000);
}

Finally, here’s the live view of my sensor (assuming that I still have this sensor up and running):

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.