I am attempting to send data from a Adafruit BME280 Sensor, using a Photon, to PubNub so that I can use it for real time streaming in Power BI. With this in mind, I have formatted my data as a JSON String. Previously, I was able to get the code below publish through Azure Stream analytics, however, when I try to do the same thing using PubNub.publish it crashes the photon. For example, I flash the code (below) and the Photon starts blinking red in the SOS pattern. Here is the code I have been using:
#include "application.h"
#include "PubNub.h"
#include "Adafruit_BME280.h"
#include "Adafruit_Sensor.h"
#define BME_SCK D3
#define BME_MISO D2
#define BME_MOSI D1
#define BME_CS D0
#define SEALEVELPRESSURE_HPA (1013.25)
PRODUCT_ID(...);
PRODUCT_VERSION(...);
Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK);
float fahrenheit;
float psiconvo;
float humidity;
float altitude;
char pubkey[] = "...";
char subkey[] = "...";
char channel[] = "...";
SYSTEM_THREAD(ENABLED);
void setup() {
Serial.begin(9600);
Particle.publish("Serial set up");
PubNub.begin(pubkey, subkey);
Serial.println(F("BME280 test"));
if (!bme.begin()) {
Serial.println("Error 404: Sensor not found!");
while (1);
}
}
void loop() {
//Value 1, converts the C° reading from the sensor to F°
fahrenheit = bme.readTemperature() * 1.8 + 32;
//Value 2, changes hPa to PSI
psiconvo = bme.readPressure() / 100.0F * 0.014503773800722;
//Value 3, adds a name to the humidity readout
humidity = bme.readHumidity();
//Value 4, adds a name to the altitude readout
altitude = bme.readAltitude(SEALEVELPRESSURE_HPA);
//Attempt at creating a JSON Object
String payload = String::format( "{ \"t\":%.3f, \"h\":%.3f, \"a\":%.3f, \"p\":%.3f }", fahrenheit, humidity, altitude, psiconvo);
delay(1000);
TCPClient *client;
client = PubNub.publish(channel, payload);
client->stop();
delay(200);
}
I have looked extensively over other topics, yet I can’t seem to make it so that my 4 values of from the BME280 sensor successfully Publish to Pubnub. Because of this, I believe that my formatting on the JSON Object is incorrect and could use help with the proper syntax.
To note, I have gotten the code from the “Particle to PubNub” tutorial to publish successfully: https://www.pubnub.com/blog/2016-07-12-getting-started-with-pubnub-and-particle-devices/
Update:
Problem solved:
My coworker and I realized that out JSON string was formatted incorrectly.
Using an example of this individual trying to send data to and EON chart:
https://stackoverflow.com/questions/40636344/sending-batch-readings-from-photon-via-pubnub-for-display-in-eon-chart
I was able to quickly produce the correct syntax.
New code for JSON String:
String payload = "{\"Temperature\":\"" + String(fahrenheit) + "\", \"PSI\":\"" + String(psiconvo) + "\", \"Humidity\":\"" + String(humidity) + "\", \"Altitude\":\"" + String(altitude) + "\"}";
Works like a charm now.