Community
OpenEnergyMonitor

Community

HOWTO Openwrt and IOT platforms with NODE-RED

mqtt
Tags: #<Tag:0x00007f16af4a5b38>

(stephen krywenko) #1

Openwrt is a nice LEDE based router platform works on many embedded devices. I am starting to move my IOT platform to it for better easier after install configuration and a much smaller foot print

Getting Openwrt

if installing on pi
https://wiki.openwrt.org/toh/raspberry_pi_foundation/raspberry_pi
if installing on orange pi bananapi (sunxi) or other platform
https://downloads.openwrt.org/releases/17.01.4/targets/

flash image to your SD as you normally do
expanding the File structure is not so straight forward the as you have to do it externally- the simplest method down load a copy of hiren’s boot cd and use its graphical partitioning software-- also a good little tools to have for a variety of reasons

now log into openwrt and install your related software you may or may not want. perl, collectd, RRD, mosquitto, nano, bash, samba, apache …ETC but most importantly you want to node.js for NODE-RED… you can also setup network related software such as snort (intrusion detection software) wiidog ( captive portal ) .

Installing NODE-RED

installing NODE-RED is easy but not straight forward, you have to compile and setup NODE-RED on another device or platform first- once you do that and NODE-RED has all that you want in it - compress to iot.tar.gz and copy it to a USB stick ( you can also strip it down removing unnecessary files if you like to reduce its size)

USB mounting

ssh into your openwrt -ie:

 ssh [email protected]

then

opkg update
opkg install kmod-usb-storage kmod-fs-ext4 kmod-fs-vfat kmod-nls-cp437 kmod-nls-iso8859-1
mkdir -p /mnt/usb
mount -t vfat /dev/sda1 /mnt/usb

enter Dir

cd /mnt/usb/

Tar extraction

 tar -zxvf iot.tar.gz -C /root

if you need to copy file ( ie such as my mqtt to RRD or influxdb from other howto’s )

cp file.ext /root/iot/file.ext

to list dir "ls "

what i had the most trouble with was setting up NODE-RED and other Software to start at boot as @reboot does not work in cron
the simplest method is in openwrt go to System>Startup near the bottom of the page Local Startup and enter in a format like this, you need the logging otherwise depending on the program it may never move onto the next is also will prevent the device from rebooting normally

/root/iot/./grid.pl & echo "Started Grid RRD" > /root/iot.log
/root/iot/./solar.pl & echo "Started Solar RRD" >> /root/iot.log
/root/iot/./wind.pl & echo "Started Wind RRD" >> /root/iot.log
/usr/bin/node-red >> /root/iot.log & echo "Starting Node-Red" >> /root/iot.log

since it meant for smaller embeded platform if using RRD you could use this for creating RRD it should hold 5 years of data and consumes about 80kb of space

 RRDs::create "grid.rrd",
	"-s", "300",
	"DS:kw:GAUGE:600:U:U",
	"RRA:AVERAGE:0.5:1:2016",
	"RRA:MIN:0.5:1:2016",
	"RRA:MAX:0.5:1:2016",
	"RRA:AVERAGE:0.5:6:1344",
	"RRA:MIN:0.5:6:1344",
	"RRA:MAX:0.5:6:1344",
	"RRA:AVERAGE:0.5:24:2190",
	"RRA:MIN:0.5:24:2190",
	"RRA:MAX:0.5:24:2190",
	"RRA:AVERAGE:0.5:144:3650",
	"RRA:MIN:0.5:144:3650",
	"RRA:MAX:0.5:144:3650";

if using freeboard in NODE-RED its WWW dir is located at

node-red/node_modules/node-red-contrib-freeboard/node_modules/freeboard

and base CGI-BIN

/www/cgi-bin

from here you can follow other tutorial on how to set up your IOT platforms or simply use this

and then simply edit settings.js and change it current port setting of 80 to another one otherwise it will conflict with openwrt

okay good luck have fun


(stephen krywenko) #2

okay found a small issue with starting node-red from rc.local it would cause an exception

Error: write EPIPE   

but I notice that earlier if I ran it from rc.local it was like starting node-red under different user then starting it directly from root… so i guess a file it needs is locked under that condition…

so i wrote a small bash program that you load in cron it checks if node -red is running every couple minutes . if it running it does nothing if it not it starts node red

#!/bin/bash
if pgrep -x 'node-red' > /dev/null
then
echo "Node-Red is Running"
else
  /usr/bin/node-red >> /root/iot.log & echo "Starting Node-Red"
fi

now everything runs much smoother - but I must say running under Openwrt LEDE is alot less resource hungry on my bananapi R1 under bananian OS with out desktop it was always around 20 -30 % CPU and 50% of the memory… now it runs at <4% cpu and 10% of memory and i am doing alot more in the background on openwrt then i was on bananian OS… but we will see how that changes once I fully enable snort.

oh if you wish to edit openwrt remote web port then you can set node-red to port 80

 /etc/config/uhttpd

then

 /etc/init.d/uhttpd restart

but most likely yo still have to reboot the device to allow for firewall setting to change before you can log in under different ports


(stephen krywenko) #3

for those who might be interested-- here a couple images of the devices i configured under openwrt currently I have a few more of each to do…
banana pi R1 and raspberry pi plus a few Orange pi pc plus( not pictured)

the banana Pi R1s are setup as IOT interface and data storage but along with that full firewall and router with network storage (500 gig SSD) . DLNA sever. and IDS ( intrusion detection System). the IOT software is networked shared in a protected folder for easy access and updating all data is stored on the SSD to limit wear and tear on the OS SD

the Raspberry Pi are set up as a IOT interface using the wireless for the IOT devices to connect privately too or using the Raspberry Pi as AP ( AP/Router) and/or two wireless adapters and acting as a wireless extender/bridge in an existing wifi network.

openwrt has a nice wireless info page and other networking info pages

but for general knowledge there a small bug in openwrt when mounting hard drive via the web interface and saving the setting from there - it causes the web interface of openwrt to crash and it becomes unusable you will need to reboot the router then all is good again afterwards… but really loving using this open wrt it made everything oh so much easier to do


(Brian Orpin) #4

Slightly OT, but these has been some notes around (outside this forum) about the network speed of the RaspberryPi as it uses a shared USB bus for both.

Personally I have noticed that, when using Speedtest-cli to check my Broadband speed, the results from the Pi are always significantly lower (20%) than from an HP microserver running a VM. Both machines are the on same DietPi base OS and both are connected to the same switch before hitting the external gateway. I am also told the latency of DNS lookup (I’m using Pi-hole) is inferior on the Pi as well. YMMV.


(stephen krywenko) #5

never notice any difference with bananpi R1actually internet response was probably better then a my tomato flashed router. even with snort enabled … which is intensive to say the least… but on a home network of 10 - 20 devices it was nothing to worry about as the CPU barely hit 10% at peak usage Screenshot_20180205_070232

as to the raspberry Pis it not my main concern as an AP for general internet usage, only as hotspot for IOT devices where as I can go to a site pre programmed and plug it in and go not worrying about other sites wifi or flakey network issues they might have – but i will keep an eye on it and do some more testing to it internet issues now that you mentioned it …

currently with my IOT framework of freeboard locally and sending to ifluxdb remotely I do not see an issue - but perhaps if I can get emoncms working on the Pi and openwrt its load might go up enough that I start having issues - though currently I am stuck on trying to install emoncms into openwrt


(stephen krywenko) #6

I tested the difference in speed test between bpi-ri and my simple Raspberry pi both installed with openwrt. the raspberry it connected behind the BPI_R1 the difference it virtually zero
BPI-R1

RPI

but I will set up iperf and test it more directly but currently it does not seam to be any real difference


(stephen krywenko) #7

okay did a iperf test
BPI_R1 did great at even with snort enabled — 571Mbits/s
Screenshot_20180205_093556

while the RPi was considerably slower at 19.9 Mbits/s but that fine my internet connection is is slower then that so even as AP/router it should preform well enough
Screenshot_20180205_093442

but all things being equally I tested via wifi connection on BPI-R1 and are similar enough
Screenshot_20180205_095142
Update:
I tested through the devices directly through their built in WiFi and attached Usb adapter I got similar throughput of 20Mbits/s .but normally I run my WiFi through a secondary AP (2600AP) So I thought I test through my normal 2600AP that in my house for WiFi connection is 11Mbits/s ( was not quiet AP other users) I also did in dual mode push and pull at the same time they were all the same 570Mbits/s by cable , 20Mbits/s by Wifi – so all in all I think in reality when running openwrt there was not a lag caused by RPi and WiFi USB dongle


(Brian Orpin) #8

That was what I was referring to. It doesn’t matter to you, but some folk use an RPi as a gateway or something like Pi-hole not realising the network speed of the RPi can be seriously detrimental to your overall internet performance.


(stephen krywenko) #9

but if you notice I iperfed REGULAR AP/Router Wifi now they were not running in N as that not what the wireless card supports under linux and neither was the connection to the AP from my laptop… and they all topped out at 20Mbits … the RPi with openwrt preformed equally as well as a regular off the self router with± 20/20Mbits/s

may be instead of Speedtest-cli which is not that accurate for testing network try iperf and see if you get the same 20% reduction

https://iperf.fr/iperf-download.php

just set up one computer as sever

iperf3 -s

and on the other side of your network connection set the other up as client

iperf3 -c " your iperf Server IP"

there are iperf public servers but they are notoriously hard to get into-- but most local universities run an iperf server just whether or not you can find its listing

I think the problem you might of had is the wifi adapter you used was poorly supported under RPi


(Brian Orpin) #10

No, I use a wired connection and it was not something I have measured myself, but a technical item someone posted somewhere that I read suggesting the issue is that the RPi shares the USB bus with the Network adapter so this hardware design was throttling the bandwidth. It should, after all, be 100Mbps. :wink:


(stephen krywenko) #11

oh okay you’re just speaking of the the RPi own network adapter - okay that device when iperfed only supports ~50 Mbits/s - which is still not terrible you could still plug in two usb wifi adapters and still have room left over for an internet gateway… while the BPi-R1 is far from perfect it not nearly as a bad as RPi since it is network is capable of 570Mbits/s by wire – but I am doing my test RPi model B but from what i heard the RBi 2 and 3s are not much better at ~70 Mbits/s for its built in NIC ( i do not have any to test - feel free to test and report back if you wish to verify ) – but you can plug in USB2/3 network dongles and should get up to 170Mbits/s


(Michael Lavelle) #12

Can you tell me the small red product used in this post (no. 3)
44%20PM


(Paul) #13

That looks like an old(er) Raspberry Pi 1 model B which only had 2 stacked USB ports and the rj45 was set back, hence the step in the case at the end where the USB WiFi dongle is plugged in and the projection at the other end to house a full-size SD card (not micro)

http://raspi.tv/wp-content/uploads/2012/06/DSC_31931.jpg


(Michael Lavelle) #14

Thank you.


(stephen krywenko) #15

for those who might be interested here is a small flash of my openwrt Network monitor ( <30meg) for the BPI-R1 it will communicate with influxdb and send any data stored by CollectD .

to configure just follow the Manual Networkinfo Manual.pdf (129.8 KB)

if you wish to use it as IOT device monitor just follow the howto HowTo.doc (1.0 KB)

example data output

it is very resilient current frequency is averaged of 2 seconds but that can be reduced or increased. , all data is store in the volatile memory of the router making the the life span of the SD very very long. but you can store data in non volatile if you wish for permanent data collection on the openwrt, then basically you could shut down your influxdb and start it up a year later and the router will slowly up load all your data to the influxdb with out issue . as it is now it will only upload the data that was stored from the last restart of the router and the data is sent every 30 seconds if influxdb is online

can be found at :slight_smile:


(stephen krywenko) #16

Notice openwrt had a new firmware for BPI-R1 so I made an updated version now using a custom collectd-mod-interface I wrote that only compatible 18.06.0 or higher

works wells and uploads easily to any influx server
has 4 categories for MQTT they are Energy, Temp, Humidity, Flow and Pressure ( more can be added)
format for sending topic
mosquitto_pub -t ‘incoming/OpenWrt/mqtt-Energy/power-grid’ -m ‘N:21.5’
mosquitto_pub -t ‘incoming/OpenWrt/mqtt-Temp/temperature-greenhouse’ -m ‘N:21.5’
mosquitto_pub -t ‘incoming/OpenWrt/mqtt-Humidity/humidity-greenhouse’ -m ‘N:21.5’
mosquitto_pub -t ‘incoming/OpenWrt/mqtt-Flow/flow-heatpump’ -m ‘N:21.5’
mosquitto_pub -t ‘incoming/OpenWrt/mqtt-Pressure/pressure-heatpump’ -m ‘N:21.5’
i thought I mention you can use different Hostname ( ie OpenWrt > XXXX ) so you can have multiple location as well

if only using openwrt router for data collection more RRD out of /tmp folder for permanent storage … rrd graph are created automatically in alphabetic order under each heading … if only using influxdb you do not have to move RRD from /tmp as it will upload data automatically to influx to which you can graph with chonograf or grafana

if not concern with graphing in openwrt and only wished to send to influx. then as long as use the correct the Database Types “/usr/share/collectd/types.db” and you have the categories listed in /runfiles/cp-csv
it will automatically upload it to influx with out anymore intervention

if you wish to install on another Openwrt device follow this for MQTT and this to install Influx sending capability

okay good luck have fun :slight_smile: