Webhook with Firebase not working. Getting Error Status 400

Hello,

I am attempting to setup a webhook with firebase. I followed this tutorial (GitHub - rickkas7/firebase_tutorial: Tutorial for using Google Firebase from the Particle Photon or Electron using Webhooks) and was able to successful get a webhook to operate between Particle WebIDE and Firebase. However, when I tried to create a new modified webhook for my project, I keep receiving the “Error Status 400” each time I run the following command to test the webhook: publish datamarch2 “{"co2":1,"temp":2,"hum":3,"pm2_5":4,"lat":5,"lon":6}”

This is the .json file I used to create the webhook (I doubled checked to make sure that I used the correct database url and secret):

{
    "event": "datamarch2",
    "url": "https://datamarch2-default-rtdb.firebaseio.com/datamarch2/data.json",
    "requestType": "POST",
    "query": {
    	"auth":"database secret inserted here"
    },
    "body": "{\"co2\":{{co2_val}}, \"temp\":{{temp}}, \"hum\":{{hum}}, \"pm2_5\":{{pm2_5}}, \"lat\":{{lat}}, \"lon\":{{lon}}, \"ts\":\"{{PARTICLE_PUBLISHED_AT}}\" }", 
    "mydevices": true,
    "noDefaults": true
}


This is the test I am running from the command line:

particle publish datamarch2 "{\"co2\":1,\"temp\":2,\"hum\":3,\"pm2_5\":4,\"lat\":5,\"lon\":6}"

Your webhook body references {{co2_val}} but the JSON contains uses co2. Since {{co2_val}} doesn’t exist, it’s empty, but since the template expects a number there, it’s not valid JSON, which is why you get a 400.

1 Like

Thank you for the response. I made that change and was able to successfully test the webhook from the command line. Now, I am trying to get my project work, a project in which an Electron receives data from an Arduino UNO via serial and then sends this data to firebase. I tested this setup with the example webhook from the tutorial I linked above and was able to get it to operate. But now with my actual project, I the events are being published to the Particle Console but I am receiving the “Error Status 400” when trying to use the firebase webhook.

Below is the code I have for the Electron:

#include "Particle.h"





// Global variables
const byte numChars = 64;
char receivedChars[numChars];
char tempChars[numChars];        // temporary array for use when parsing
boolean newData = false;


// variables to hold the parsed data / general variables
float temp = 0;
float hum = 0;
float co2 = 0;
float strval = 0;
float pm2_5 = 0;
float lat = 0;
float lon = 0;

const size_t READ_BUF_SIZE = 64;
char readBuf[READ_BUF_SIZE];
size_t readBufOffset = 0;


// Forward declarations
void publishData();
void parseData();
void recvWithStartEndMarkers();

// Particle publish event 
const char *PUBLISH_EVENT_NAME = "datamarch2";


//============

void setup() {
    Serial.begin(9600);
    Serial1.begin(9600);
}

//============

void loop() {
    recvWithStartEndMarkers();
    if (newData == true) {
        strcpy(tempChars, receivedChars);
        parseData();
        publishData();
        // showParsedData();
        newData = false;
    }
}

//============

void recvWithStartEndMarkers() {
    static boolean recvInProgress = false;
    static byte ndx = 0;
    char startMarker = '<';
    char endMarker = '>';
    char rc;
    
    while(Serial1.available() > 0 && newData == false) {
        rc = Serial1.read();
        
        if (recvInProgress == true){
            if(rc != endMarker){
                receivedChars[ndx] = rc;
                ndx++;
                if (ndx >= numChars) {
                    ndx = numChars - 1;
                    }
                }
                else {
			    receivedChars[ndx] = '\0'; // terminate the string
			    recvInProgress = false;
			    ndx = 0;
			    newData = true;
			}
        }
			else if (rc == startMarker) {
			    recvInProgress = true;
			    }
}
}

//============

void parseData() {      // split the data into its parts

    char * strtokIndx; // this is used by strtok() as an index
   
   // CO2
   strtokIndx = strtok(tempChars,",");
   co2 = atof(strtokIndx);
   
   
   // Temperature
   strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
   temp = atof(strtokIndx);
   
   
   // Humidity
   strtokIndx = strtok(NULL, ",");
   hum = atof(strtokIndx);
   
   //PM 2.5
   strtokIndx = strtok(NULL, ",");
   pm2_5 = atof(strtokIndx);
   
   // Latitude
   strtokIndx = strtok(NULL, ",");
   lat = atof(strtokIndx);
  
   // Longitude
   strtokIndx = strtok(NULL, ",");
   lon = atof(strtokIndx);

}

//===========

void publishData() {
    
	char buf[256];
	snprintf(buf, sizeof(buf), "{\"co2\":%.1lf\"temp\":%.2lf,\"hum\":%.2lf,\"pm2_5\":%.2lf,\"lat\":%.2lf,\"lon\":%.2lf}",co2,temp,hum,pm2_5,lat,lon);
	Serial.println(buf);
	Particle.publish(PUBLISH_EVENT_NAME, buf, PRIVATE);
}

//============

void showParsedData() {
    Serial.print("CO2: ");
    Serial.println(co2);
    Serial.print("Temperature (C): ");
    Serial.println(temp);
    Serial.print("Humidity (%): ");
    Serial.println(hum);
    Serial.print("PM 2.5: ");
    Serial.println(pm2_5);
    Serial.print("Latitude: ");
    Serial.println(lat);
    Serial.print("Longitude: ");
    Serial.println(lon);
    Serial.println("///////////////");
}

You’re missing a comma in the JSON data generated using sprintf:

"{\"co2\":%.1lf\"temp\":
               ^ there should be a comma between the f and the backslash
1 Like