GithubHelp home page GithubHelp logo

nodejs-mqtt-integration-example's Introduction

Drogue IoT: Node.js MQTT integration example

CI Matrix

This is an example of using the MQTT integration of Drogue IoT in combination with Node.js to implement the backend service for the LoRaWAN end-to-end workshop.

What does it do?

It is a small Node.js application, which connects to the MQTT integration endpoint of Drogue IoT, and receives messages from devices that publish to Drogue IoT Cloud.

It will parse messages and extract TTN (The Things Network) uplink messages from that stream. When a TTN uplink message is received for port 1 and with the payload is ping, then it will respond with the current active response command.

The response will be sent back to the device using a downlink message.

NOTE: A downlink message will only be sent shortly after the uplink message was received, as this is when the device expects it. Setting a new response command will not send a new downlink message.

When you send led:on to the device, the blue LED should turn on. When you send led:off, it should turn off again.

NOTE: There is a short time window, between the device sending and receiving. It may happen that the downlink command takes a bit too long, and so it is delivered the next time the device connects.

Configuration

The connection information required to connect to Drogue Cloud is specified in the file config_sample.json. This file should be copied into config.json and the following entries need to be updated:

drogue.application.name=<application-name>
drogue.api.user=<user-name>
drogue.api.token=<api-access-token>

The drogue.application name is the name of the target application in Drogue IoT Cloud to integrate with. The drogue.api.user is your Drogue IoT Cloud username which can be found by logging in to Drogue Cloud and clicking on the current user (a drop down button) which will have a field showing a field named username.

For drogue.api.token use the API access token. If you have created API tokens in the past you can see which ones exist using the following command:

$ curl -s -H "Authorization: Bearer $(drg whoami --token)" https://api.sandbox.drogue.cloud/api/tokens/v1alpha1 | jq

Notice that these will not show any tokens, only the prefix (used to delete the token, and the data it was created. If you don't have the token available any more a new token can be created using:

$ curl -vs -H "Authorization: Bearer $(drg whoami --token)" -XPOST https://api.sandbox.drogue.cloud/api/tokens/v1alpha1 | jq

Node version

This example was tested using Node.js v16.15.1.

Building

$ npm install

Running

$ npm start

> [email protected] start
> node --no-warnings index.js

Server listening on http://0.0.0.0:8080
Connected to mqtts://mqtt-integration.sandbox.drogue.cloud

Open http://0.0.0.0:8080 in a browser and you will be presented with a user interface which should look something like this:

UI start image

Next, press the blue button on the device which will send an uplink message to Drogue IoT Cloud.

Once you press the blue button on the board, you should see an incoming message, and with that, an outgoing message too.

UI with message received image

Enabling MQTT debugging

$ npm run start-debug

Deploying to Kubernetes

This example can be deployed to a Kubernetes cluster. The following instructions require Docker to be installed.

Building an image

$ npm run build-image

Running the image locally

$ npm run run-image

nodejs-mqtt-integration-example's People

Contributors

danbev avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.