HTTPS – Publishing to Fieldbook Database

I would like to send sensor data via the HTTPSClient to Fieldbook, which makes it super convenient to have a cloud based data-storage with REST-API.

The following node.js-Script does what I need. I wonder how to run the POST-part on a Particle / Core.

/ *
 * Dependencies: Requestify 
 */

var requestify = require('requestify');

var bookId = process.env.FIELDBOOK_BOOK_ID;
var baseUrl = 'https://api.fieldbook.com/v1/' + bookId;
var options = {
    headers: {accept: 'application/json'},

    auth: {
        username: process.env.FIELDBOOK_USER,
        password: process.env.FIELDBOOK_KEY
    }
};

var url = baseUrl + '/activitydata';
var curTimestamp = new Date().toString();
// NOTE: Fieldbook uses only lower case variable names!!
var record = {
    // Set attributes for new record here
    sensorname: "distanceSensor1",
    state: "false",
    timestamp: "1234"
};
// GET
var response = requestify.get(url, options)
.catch(function (err) {
  console.log(err);
})
.then(function(response) {
    var resp = response.getBody(); // Get the response body (JSON parsed)
    console.log(resp);
});

// POST
var response = requestify.post(url, record, options)
.catch(function (err) {
  console.log(err);
})
.then(function(response) {
    var resp = response.getBody(); // Get the response body (JSON parsed)
    console.log(resp);
});

As the HTTPSClient for Particle does not have an options parameter I wonder how to use it to post to Fieldbook.

Some hints or a minimal working example would be highly appreciated!

Btw. it does not matter to me weather or not the data is published to the Particle Cloud via publish() and then sent to Fieldbook via a webhook or sent directly with HTTPSClient (probably faster).

Have you had a look at their glowfish-post-test sample?

Thanks for the tip on fieldbook. It looks really cool and easy to use. I signed up and got it working with a webhook pretty quickly.

Here’s my hook definition JSON:

{
    "event": "test4",
    "url": "https://api.fieldbook.com/v1/<your_book_id>/sheet_1",
    "requestType": "POST",
    "auth": {
    	"username":"key-1",
    	"password":"<your_api_key>"
    },
    "json": {
		"ts": "{{PARTICLE_PUBLISHED_AT}}",
		"a": "{{a}}",
		"b": "{{b}}"
    },
    "mydevices": true,
    "noDefaults": true
}

You need to click on the API button, Manage API Access, then Generate a new API key. The API key is different from your login email and password!

In my sheet I renamed Field 1, Field 2, and Field 3 to ts, a, and b, respectively. Those map up with the names in the webhook definition.

And this is just a little sample program to generate some data from a Photon:

#include "Particle.h"

const unsigned long PUBLISH_PERIOD_MS = 30000;

void publishRecord(); // forward declaration

unsigned long lastPublish = 0;
int a = 1;


void setup() {

}

void loop() {
	if (millis() - lastPublish >= PUBLISH_PERIOD_MS) {
		lastPublish = millis();

		publishRecord();
	}

}


void publishRecord() {

	// Just make up some random data for testing
	char str[4];
	size_t ii;
	for(ii = 0; ii < sizeof(str) - 1; ii++) {
		str[ii] = 'A' + rand() % 26;
	}
	str[ii] = 0;

	++a;

	// Build a JSON string containing the data you want to add to Filebook
	char buf[256];
	snprintf(buf, sizeof(buf), "{\"a\":%d, \"b\":\"%s\"}", a, str);
	Particle.publish("test4", buf, PRIVATE);
}

And here’s what the file book UI looks like. It’s pretty neat, in less than a second after the publish occurs, the web page immediately updates itself with the new rows. Pretty cool.

2 Likes

@ScruffR
Yes, I had a look at it. Looked a bit over-complicated for my task.

@rickkas7
Thanks a lot! This is working fine!

For others, here is the code I used on the particle:

void setup() {
 Time.zone(2); // Berlin summer time UTC, in winter this is 1
}

void loop() {
  String ts = String(Time.local());
  String consistency = String(random(1, 6));
  Particle.publish("sensortest", 
    "{ \"consistency\": \"" + consistency + "\"," +
    "\"timestamp\": \"" + ts + "\" }", 60, PRIVATE);
  delay(30000);               // Wait for 30 seconds
}

Had to google a bit to find out how to create a webhook from a JSON-file.

{
    "event": "sensortest",
    "url": "https://api.fieldbook.com/v1/FIELDBOOK_BOOK_ID/FIELDBOOK_SHEET_NAME",
    "requestType": "POST",
    "auth": {
    	"username":"key-1",
    	"password":"FIELDBOOK_PASSWORD"
    },
    "json": {
		"timestamp": "{{timestamp}}",
		"consistency": "{{consistency}}"
    },
    "mydevices": true,
    "noDefaults": true
}
  • Edit your password, book_id and sheed_id
  • Run particle webhook create hook.json in the same directory as the hook.json-file
  • Events should now appear under dashboard.particle.io/user/logs, there you can check if the publish and webhook are working or not