Greetings Particle community!
I have had great success using Spark.publish() and webhooks in tutorials. However I seem to be stuck.
I’m am now trying to POST using a different API. The json format should look like this:
{
"time": 233434,
"temp": 84.555
}
My current webhook (without the Bear token) looks like this:
{
"eventName": "kelvin",
"url": "https://senseai-staging.com/api/v2/data",
"requestType": "POST",
"headers": {
"Authorization": "Bearer 12345",
"Content-Type": "application/json"
},
"json": {
"time": "{{time}}",
"temperature": "{{temperature}}"
},
"mydevices": true
}
I think I feel good about the webhook but I’ve been struggling on the firmware side. I’ve tried this (and many other things):
#include "Adafruit_DHT/Adafruit_DHT.h"
// Define Pins
#define DHTPIN 2 // what pin we're connected to
// Setup Sensor
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT dht(DHTPIN, DHTTYPE);
#define publish_delay 10000
unsigned int lastPublish = 0;
void setup()
{
Serial.begin(9600);
RGB.control(true);
RGB.brightness(10); // This sets the RGB LED brightness : 0-256
RGB.control(false); // release the LED to the system
dht.begin(); // Startup the sensor
Serial.println("---------------");
}
void loop(){
// grab some data
int time = Time.now();
float tempf = dht.getTempFarenheit();
String jsonDataString = String( "{ \"time\":" + time + ",\"temperature\":" + tempf +"}");
unsigned long now = millis();
if ((now - lastPublish) < publish_delay) {
// it hasn't been 10 seconds yet...
return;
}
Spark.publish("kelvin", jsonDataString, 60, PRIVATE);
lastPublish = now;
}
the error message: empwebhook.cpp:42:64: error: invalid operands of types ‘const char*’ and ‘const char [16]’ to binary ‘operator+’
I feel like I’m really close. I think I need to convert the float and int to a string? I’ve tried that a few different ways but then it’s unclear to me how to format to the correct json template posted above int my Spark.publish()
I’ve read through most of the documentations and I just cant seem to put this together.
Any suggestions or advice would be greatly appreciated.
Thanks in advance for your time!
I’ve edited your post to properly format the code. Please check out this post, so you know how to do this yourself in the future. Thanks in advance! ~Jordy