/sigfox-nodejs-callbacks

Your first Sigfox callback handler! Logs the POST requests into a mongo collection and show a table of received messages

Primary LanguageJavaScript

Sigfox Node.js Callback Demo

Purpose

  • Logs the message sent by your Sigfox objects
  • Display a table of received messages, with their unique id, data payload and relevant metadata

This is a Node.js/Express application, with two routes:

  • POST / sigfox to log a message
  • GET / to display the dashboard

Installation

Dependencies

Before installing the app itself, check that the main dependencies are installed on your system

Node.js

This app relies on io.js v1.8.1, the Node.js fork.
The main reason is that I like to try new stuff, including the ability to use some ES6 syntax :)

To install, the better is probably to use nvm (Node version manager) that will let you switch between version of Node.

$ curl https://raw.githubusercontent.com/creationix/nvm/v0.25.0/install.sh | bash
$ nvm install v1.8.1
$ nvm use v1.8.1
MongoDB

Follow the instructions on the MongoDB website.

Packages
  • express : Fast, unopinionated, minimalist web framework
  • body-parser : Node.js body parsing middleware.
  • debug : small debugging utility
  • mongojs : Easy to use module that implements the mongo api
  • ejs : Embedded JavaScript templates
  • moment : Parse, validate, manipulate, and display dates

Install

$ npm install

##Run

###MongoDB

Make sure you have mongo up & running :

$ sudo mongod

App

$ npm start

Env vars

You can set the following env vars to adjust your app's behaviour:

  • DEBUG : Will filter the logs displayed in console. Check the debug module documentation for details.
  • DATABASE_URL : URL of the mongoDB database. Defaults to mongodb://localhost:27017/sigfox-callback
  • PORT: the port your app will be listening to. Defaults to 34000

Test requests

Check your dashboard

Navigate to http://localhost:34000/ in your browser.

There should be 0 message displayed at the start.

POST request

$ curl -X POST -d "connect=anything" http://localhost:34000/sigfox

You should get the following JSON response:

{"result":"♡"}

An entry will show up in your dashboard, with invalid data. This is because we didn't provide the full data structure of a Sigfox message.

If you want to emulate a SIGFOX message, try:

$ curl -X POST -d 'id=simulation&time=1500000000&station=future&data=d474' http://localhost:34000/sigfox

A message from the future should now appear on your local dashboard.

Quick deploy on heroku

Note: You can deploy this demo application wherever suited. Heroku is just a quickstart example.

One-Click Deployment

Deploy

Using CLI (Command Line Interface)

  • Make sure you have installed the Heroku Toolbelt
  • Create an application : heroku apps:create {whatever name}. Documentation here
  • Deploy your code : $ git push heroku master

Set up your env

  • Add a sandbox MongoLab add-on (free) : $ heroku addons:add mongolab:sandbox
  • Set the DATABASE_URLenv var to the URL of your mongo lab db
  • heroku config:get MONGOLAB_URI
  • $ heroku config:set DATABASE_URL={your mongolab URL}

All that remains to do is to set up your Sigfox callback on the Sigfox backend

How to set up a Sigfox callback

  • Log into your Sigfox backend account
  • In the device type section, access to the device type of the object you want to track
  • In the sidebar, click on the Callbacks option
  • Click the New button
  • Set your callback as following
    • Type: DATA UPLINK
    • Channel: URL
    • Url syntax : http://{your URL}/sigfox?id={device}&time={time}&snr={snr}&station={station}&data={data}&avgSignal={avgSignal}&rssi={rssi}&lat={lat}&lng={lng}
    • HTTP POST
    • OK