项目作者: udaygin

项目描述 :
Simple Nodejs Script to serve data received by group of mqtt topics as a http endpoint
高级语言: JavaScript
项目地址: git://github.com/udaygin/mqtt2rest.git
创建时间: 2017-06-10T14:06:15Z
项目社区:https://github.com/udaygin/mqtt2rest

开源协议:MIT License

下载


mqtt2rest (Work in Progress)

Simple Nodejs Server to subscribe to a group of mqtt topics on a given mqtt broker and serve the received data as json over http

Warning : This autostarting service capabilities fo the project are desingend for linux(Ubuntu systemd) only. rest of the code should work fine even on windows.

Background

How to

Download

  1. cd /opt/
  2. sudo git clone https://github.com/udaygin/mqtt2rest.git
  3. sudo chown -R $USER:$USER mqtt2rest

Installation

Background service installation

Note : skip this service installation section if you just want to run it manually from command line

Edit username to match yours(or any other user that you want to run this as )

  1. cd /opt/mqtt2rest
  2. sed -i 's/uday/`whoami`/g' /opt/mqtt2rest/mqtt2rest.service

install this as a linux auto starting service

  1. sudo systemctl enable /opt/mqtt2rest/mqtt2rest.service

Standalone

No action needed.

Usage

Configuration

Ath the minimum, you need to configure two things

  • Mqtt broker connection details
  • Topics to watch
  • Port to serve json content on
Configure mqtt broker connection details

Replace values of properties under [mqtt] section in file /opt/mqtt2rest/config.ini with the correct ones to your broker.

  1. [mqtt]
  2. broker.ip=<your mqtt broker ip>
  3. client.id=
  4. client.user=
  5. client.pwd=
Configure mqtt topics to watch

Edit the value of property topic.prefix under [mqtt] section to match your topic prefix
For Ex: if you have below topics for devices in your home

  • /devices/livingroom/speakers
  • /devices/livingroom/thermostat
  • /devices/bedroom/thermostat
  • /devices/bedroom/smartlight
  • /devices/kitchen/eggtray
    and if you want to monitor all the devices in all rooms , your topic.prefix should be “/devices/“
    Note: don’t forget the trailing slash
    on the other hand, if you want only bedroom devices to be monitored your topic.prefix should be “/devices/bedroom/“
    Note: don’t forget the trailing slash
Configure port to start the server on

By default, this script starts the http server on port 3000. you can change it by editing

  1. [rest]
  2. listen.port=3000
Configure database file name (optional)

By default the database file name is mqttdata.json and you can customize it by editing database.name under [misc] section. Please note that yoou dont need to specify .json extenstion in the property value.

  1. [misc]
  2. database.name=mqttdata

Start the server

For Background service

start the service install this as a auto starting service

  1. sudo systemctl start mqtt2rest
For manual run
  1. cd /opt/mqtt2rest
  2. node main.js