Giter Site home page Giter Site logo

mqtt_forwarder's Introduction

mqtt_forwarder

Some sensors I'm using at home are running on ESP8266. I've been trying for a long time to sync them to NTP and so to provide the timstamp of each readings. I've failed (in fact it works for some weeks and start failing afterwards, in the long run : not working) so I chose a safer route and created this program that forward the payload add the timestamp. In my case each ESP8266 sends a message to a topic with its Chip Id to keep the same code base for all ESPs.

Usage

Prerequisite

You simply need Python3 (never tested with Python2.7) and the only dependency is paho-mqtt (for MQTT broker interaction) so this line should be enough :

pip3 install paho-mqtt

Using the script

Easy, first try a dry-run command :

./mqtt_forwarder.py -m 127.0.0.1 -n -v

About the path to your credentials, you can also use the json directly instead of a path. See the docker-compose.yml for more details.

and then a real command :

./mqtt_forwarder.py -a '{ "src": "dest" }' -m 127.0.0.1 -n -v

With this exemple any message coming from the topic sensor/esp/src will be forwarded to sensor/raw/dest.

The hashmap can also be set with environment variables, see the help for more detail.

Help

/ # mqtt_forwarder.py --help
usage: mqtt_forwarder.py [-h] [-m HOST] [-a HASHMAP] [-n] [-d DESTINATION]
                         [-t TOPIC] [-T TOPIC] [-v]

Send MQTT payload received from a topic to firebase.

optional arguments:
  -h, --help            show this help message and exit
  -m HOST, --mqtt-host HOST
                        Specify the MQTT host to connect to. (default:
                        127.0.0.1)
  -a HASHMAP, --hash-map HASHMAP
                        Specify the MQTT host to connect to. (default: None)
  -n, --dry-run         No data will be sent to the MQTT broker. (default:
                        False)
  -d DESTINATION, --destination DESTINATION
                        The destination MQTT topic base. (default: sensor/raw)
  -t TOPIC, --topic TOPIC
                        The listening MQTT topic. (default: sensor/esp/#)
  -T TOPIC, --topic-error TOPIC
                        The MQTT topic on which to publish the message (if it
                        wasn't a success). (default: error/transformer)
  -v, --verbose         Enable debug messages. (default: False)

Docker

I added a sample Dockerfile, I personaly use it with a docker-compose.yml like this one :

version: '3'

services:
  mqtt_forwarder:
    build: https://github.com/seblucas/mqtt_forwarder.git
    image: mqtt_forwarder-python3:latest
    restart: always
    command: "-m mosquitto -v"
    environment:
      MQTT_FORWARDER_HASHMAP: >-
        {
          "src": "dest"
        }

Limits

  • None I hope ;).

License

This program is licenced with GNU GENERAL PUBLIC LICENSE version 3 by Free Software Foundation, Inc.

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.