MySensors with NodeJS Controller and OTA bootloader on RPi
My RPi runs Arduino to compile and flash sketches and the bootloader. With this setup no separate PC/Mac is needed. Exactly the same could be achieved (faster) using a PC/Mac for all the Arduino compilation and flashing activities – just skip the steps and execute on PC/Mac instead.
Hardware
Headless Raspberry Pi model B with wired Ethernet
I'm connecting a monitor only for first time boot to get the IP address and then connect via Putty/xRDP
Download and install latest Raspbian version on a fresh SD-Card
Tutorials for this task are available on the internet (download, copy to SD)
Setup base parameters
(adjust to your needs - here: RPi-IP: 10.0.1.5 / Router-IP: 10.0.1.1 / password: test / timezone: Europe,Berlin / hostname: mysensors)
Install latest updates
- login as pi / test
- sudo apt-get update
- sudo apt-get upgrade
- sudo reboot
Install NodeJS (here: latest stable version available for RPi: 0.10.28)
Install MongoDB
- login as pi / test
- sudo useradd -c mongodb -d /home/mongodb -m -U mongodb
- sudo mkdir -p /opt/mongo/bin
- sudo mkdir /var/lib/mongodb
- sudo chown mongodb:nogroup /var/lib/mongodb
- sudo mkdir /var/log/mongodb
- sudo chown mongodb:nogroup /var/log/mongodb
- wget https://github.com/brice-morin/ArduPi/raw/master/mongodb-rpi/mongo/bin/mongo
- wget https://github.com/brice-morin/ArduPi/raw/master/mongodb-rpi/mongo/bin/mongod
- wget https://github.com/brice-morin/ArduPi/raw/master/mongodb-rpi/mongo/bin/mongodump
- wget https://github.com/brice-morin/ArduPi/raw/master/mongodb-rpi/mongo/bin/bsondump
- sudo mv mongo /opt/mongo/bin/
- sudo mv mongod /opt/mongo/bin/
- sudo mv mongodump /opt/mongo/bin/
- sudo mv bsondump /opt/mongo/bin/
- sudo chmod -R 775 /opt/mongo/bin
- sudo ln -s /opt/mongo/bin/mongod /usr/bin/mongod
- wget https://raw.githubusercontent.com/skrabban/mongo-nonx86/master/debian/mongodb.conf
- wget https://raw.githubusercontent.com/skrabban/mongo-nonx86/master/debian/init.d
- sudo mv mongodb.conf /etc/mongodb.conf
- sudo mv init.d /etc/init.d/mongodb
- sudo chmod 755 /etc/init.d/mongodb
- sudo update-rc.d mongodb defaults
- sudo reboot
- to confirm it's working as expected login as pi/test and run "/opt/mongo/bin/mongo" which should start the mongo shell (type "exit" to close the shell again)
- if the RPi loses power without proper shutdown while mongod is running, you can't restart without manually removing the pid file first. To do so run “sudo rm /var/lib/mongodb/mongod.lock”
Install MySensors from Git branch development (here: password: test)
- login as pi / test
- git clone https://github.com/mysensors/Arduino.git
- cd Arduino
- git checkout development
- cd NodeJsController
- npm install
- nano NodeJsController.js (optional : removing sample firmwares)
const fwHexFiles = [ ];
- to confirm that the NodeJsController is working as expected and connects to the local mongo DB: "node NodeJsController.js" which should start the controller, connect to the database and run into an error connecting to the gateway. Stop node again via Ctrl-c
Start NodeJsController on boot
- cd ~/Arduino/NodeJsController
- chmod 755 *.sh
- sudo cp MySensorsInitScript.sh /etc/init.d/mysensors
- sudo mkdir -p /usr/local/var/run
- sudo chmod -R 777 /usr/local/var/run
- sudo update-rc.d mysensors defaults
- sudo reboot
- to confirm that NodeJsController is starting at boot, check the log file /home/pi/logs/NodeJsController.log
Serial Gateway
Hardware
Disable default use of RPi serial port
Setup NodeJsController
Ethernet Gateway
Hardware
- as described for the standard Ethernet Gateway - using the standard Ethernet Gateway sketch with DEBUG (in MyConfig.h) disabled
- I'm using the version with inclusion mode button and LEDs based on no Arduino but a plain ATmega328p on a breadboard and the ENC28J60 Ethernet module
- here: 10.0.1.99 / port: 9999
Setup NodeJsController
Sensor Node Bootloader
Hardware
- as described for a simple Sensor Node
- here: no Arduino but a plain ATmega328p on a breadboard
- using DallasTemperature example (one sensor connected to pin 3)
Compile Bootloader (can be skipped but good to see if everything is working)
- cd ~/Arduino/Bootloader
- make
- this should compile the bootloader and produce three files in the same directory: MyOtaBootloader.o / MyOtaBootloader.elf / MyOtaBootloader.hex
Flash Bootloader
End-to-End Test
Add Firmware to Controller
Flash Firmware OTA
- tail -f ~/logs/NodeJsController.log
- Power up sensor node with OTA bootloader
Check functionality
- in addition to watching the log file (tail command above) you can connect to the mongo db using a client (e.g. Robomongo) and check if new values are written to the database as a real end-to-end test