NERDY

Logging sensor data with firebase

2019-04-27

So I was looking for a way to log things like temperature, atmospheric pressure, humidity and other things for my Particle Photons. There are a couple of them in my home and it would be nice to log data and be able to visialize it later on.

I was able to use the Particle cloud templates to get data saved to firebase by sending events from the Photons.

Chart with data


The code

This would be your .ino file

1
2
3
4
#include "app/logging.h"
LogStore *myLogging = new LogStore;

myLogging->save("temperature", "livingroom", myBaroTemp->getTemp(), 0);

app/logging.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class LogStore {
private:
String storeName = "logging";

public:
LogStore() {
// __debug("store.setup", "Setting up logging");
}

void save(char* category, char* title, double value, int date) {
save(category, title, String(value).c_str(), String(date).c_str());
}

void save(const char* category, const char* title, const char* value, const char* date) {
char buf[256];
snprintf(buf,
sizeof(buf),
"{\"category\":\"%s\",\"title\":\"%s\",\"value\":\"%s\",\"date\":\"%s\"}",
category,
title,
value,
date);
Particle.publish("firebase." + storeName + ".commit", buf, PRIVATE);
}
};

Now you need to setup the webhook at https://console.particle.io (go to integrations). Select a new webhook integration and click the “custom template” tab. Paste the template below into the field and fill in the needed values.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{
"event": "firebase.logging.commit",
"url": "https://[FIREBASE SUBDOMAIN HERE].firebaseio.com/firebase-logging/data.json",
"requestType": "POST",
"noDefaults": true,
"rejectUnauthorized": false,
"json": {
"category": "{ {category} }",
"title": "{ {title} }",
"value": "{ {value} }",
"date": "{ {date} }",
"ts": "{ {PARTICLE_PUBLISHED_AT} }"
},
"query": {
"auth": "[FIREBASE AUTH KEY HERE]"
}
}