-
-
Notifications
You must be signed in to change notification settings - Fork 14
MQTT
IoT.js is supporting MQTT protocol, so it's straight forward to build a bridge:
Example:
Resources:
- https://linuxfr.org/news/nouvelle-carte-oshw-pour-mesurer-la-qualite-de-l-air-interieur#
- https://www.slideshare.net/rzrfreefr/mozillathingsfosdem2019/22
- https://archive.fosdem.org/2019/schedule/event/project_things/
Bridge can be installed on Gateway itself along IoTjs, along MQTT broker:
ssh [email protected]
sudo apt-get install mosquitto
Then service can be started on boot using this scriptlet:
First set some variables we will use in our script:
unit="rzr-webthing-example-iotjs-mqtt-level"
unit='webthing-example'
dir="${HOME}/usr/local/opt/$unit"
srcdir="${dir}/src/${unit}"
exe="$dir/$unit.sh"
service="$dir/$unit.service"
sudo=sudo
url=https://github.com/rzr/$unit
branch="iotjs/mqtt/level/master"
web_url="https://github.com/rzr/${unit}/tree/${branch}"
mqtt_host=localhost
mqtt_port=1883
httpd_port=8888
property="level"
mqtt_topic="${branch}/${property}" # TODO update with yours
json_url="http://localhost:${httpd_port}/"
property_url="http://localhost:${httpd_port}/properties/${property}"
Create luncher script (keep pasting):
$sudo mkdir -p "$dir"
cat<<EOF | $sudo tee $exe && $sudo chmod a+rx $exe
#!/bin/sh
set -x
set -e
# TODO: service should wait DCHP answer
ip addr show
ping -c 1 1.1.1.1 || sleep 10 && echo "warning: offline"
curl http://ifconfig.io/all
if [ ! -d ${srcdir} ] ; then
rm -rf "${srcdir}.tmp"
mkdir -p "${srcdir}.tmp"
git clone --depth 1 ${url} -b ${branch} "${srcdir}.tmp"
mv "${srcdir}.tmp" "${srcdir}"
fi
make \
-C ${srcdir} \
start \
port="${httpd_port}" \
mqtt_host="${mqtt_host}" \
mqtt_port="${mqtt_port}" \
mqtt_topic="${mqtt_topic}" \
mqtt_topic_key=${mqtt_topic_key} \
#EOL
EOF
Create systemd service:
cat<<EOF | sudo tee "$service"
[Unit]
Description=$unit $branch $property
Documentation=${url}/tree/$branch
After=network-online.target
Wants=network-online.target
Before=mozilla-iot-gateway.target
[Service]
ExecStart=$exe
User=pi
Restart=always
[Install]
WantedBy=multi-user.target
EOF
Install and start it:
sudo chmod 644 $service
sudo systemctl daemon-reload
sudo systemctl link $service || sudo systemctl enable $service
sudo systemctl enable $unit
sudo systemctl daemon-reload
sudo reboot
It should be started on boot.
$sudo sync \
&& $sudo systemctl status $unit \
|| sudo journalctl -u $unit -xe --no-pager
Make sure it is working as expected:
${unit} - $unit
Loaded: loaded (.../${unit}.service; enabled; vendor preset: enabled)
Active: inactive (dead)
Note that some MCU platforms (TizenRT, Arduino) are also supporting MQTT API, so MQTT can be used in device firmware too.
Then it could be a good exercise to port nanomsg to IoT.js, and then make an adapter Addons for moziot Gateway, the other simpler approach is to use nodejs (I can share ported code ask me)
And then next one can try to do a RUST version:
Check Concept page for overview, Gateway to get started, IotJs page to install runtime to build webthing as explained in Home page.
For further experiments check Social and Sensor, or Extra parts like WebApp (for Tizen or PWA) or MCU info about running on other microcontrollers not supported by TizenRT.
While Home focus mostly on using iotjs to build webthings (on GNU/Linux or TizenRT for ARTIK05X devices).
This document is still in draft state, but reviews are always welcome, if you try to replicate it and stuck on missing instructions I would appreciate that you file issues or even better make pull request (just edit in github) that insert "TODO marks" in following chapters, like:
- TODO: please explain more this chapter and then remove this TODO line
Community contributions are welcome at:
Support is also possible, ask in:
- https://github.com/rzr/webthing-iotjs
- irc://irc.mozilla.org/#iot
WARNING: Developement branches could break over time.
Instead of maintaining "quick and dirty" demo code, I decided to split demo in smaller independents parts (which can reused) and I am upstreaming the most I can.
Then support can be done on mainline branches (or released versions).
Note that, Upstreaming can be a slow process, so snapshots links will remain until 100% of code is upstreamed.
Licence:
Reference documentation is at:
-
Concept:
- Demo Concept and Architecture
-
Gateway:
- Getting started with Mozilla IoT gateway
-
IotJs:
- Install IoT.js needed to run webthings
-
Home:
- Welcome page to build WebThings using IotJs
-
Social:
- Notification service using Mastodon FLOSS
-
TizenRT:
- webthing-iotjs on ARTIK05x
-
Sensor: and Actuator
- Physical interactions
-
Extra hints:
- Docker: About running in container
- MCU: About microcontrollers (not supported by TizenRT)
- WebApp: Alternate browser (Tizen and PWA)
- GnuLinux: Article about Edison and other
- Raspbian: Article about RaspberryPi
- Arduino: Alt For atmel or Esprissif boards
- DigitalTwins : WiP experiments
- TODO: Work in progress