mysensors / MySensorsSampleController

A small example controller implemented in NodeJS

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

MySensors with NodeJS Controller and OTA bootloader on RPi

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)

  • login as pi / raspberry
  • sudo nano /etc/network/interfaces

    #iface eth0 inet dhcp
    iface eth0 inet static
    address 10.0.1.5
    netmask 255.255.255.0
    gateway 10.0.1.1
    nameserver 10.0.1.1

  • sudo raspi-config
    • 1. Expand Filesystem
    • 2. Change User Password

      test

    • 4. Internationalisation Options
      • I2. Change Timezone

        Europe / Berlin

    • 8. Advanced Options
      • A2. Hostname

        mysensors

      • A3. Memory Split

        16

  • Finish
  • Reboot

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)

  • login as pi / test
  • sudo mkdir /opt/node
  • wget http://nodejs.org/dist/v0.10.28/node-v0.10.28-linux-arm-pi.tar.gz
  • tar xvzf node-v0.10.28-linux-arm-pi.tar.gz
  • rm node-v0.10.28-linux-arm-pi.tar.gz
  • sudo cp -r node-v0.10.28-linux-arm-pi/* /opt/node
  • rm -rf node-v0.10.28-linux-arm-pi/
  • sudo nano /etc/profile

    NODE_JS_HOME="/opt/node"
    PATH="$PATH:$NODE_JS_HOME/bin"
    export PATH

  • sudo reboot
  • to confirm it's working as expected login as pi/test and run "node -v" which should return the installed node version (here: v0.10.28)

Install MongoDB

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
  • 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

  • as described for the standard Serial Gateway - using the standard Serial 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
  • Connect directly to the serial GPIO pins of the RPi:

    Arduino GND <-> Raspberry Pi GND (pin 6)
    Arduino RX <-> Raspberry Pi TX (pin 8)
    Arduino TX <-> Raspberry Pi RX (pin 10)
    Arduino RESET <-> Raspberry Pi GPIO22 (pin 15)

  • Right now I'm using a separate power supply for the serial gateway instead of the 3.3V or 5V pins of the RPi because I ran in some issues with power consumption during programming

Disable default use of RPi serial port

  • sudo nano /etc/inittab

    #T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100

  • sudo nano /boot/cmdline.txt - remove part of the file:

    console=ttyAMA0,115200

  • cd /dev
  • sudo ln -s ttyAMA0 ttyUSB9
  • sudo apt-get install minicom
  • sudo reboot

Setup NodeJsController

  • nano ~/Arduino/NodeJsController/NodeJsController.js

    //const gwType = 'Ethernet';
    //const gwAddress = '10.0.1.99';
    //const gwPort = 9999;
    //const gwType = 'Serial';
    //const gwPort = '/dev/ttyAMA0';
    //const gwBaud = 115200;

  • sudo service mysensors restart (at this point stop/restart doesn't work. use "sudo killall node" followed by "sudo service mysensors start" instead)

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

  • nano ~/Arduino/NodeJsController/NodeJsController.js

    const gwAddress = '10.0.1.99';

  • sudo service mysensors restart (at this point stop/restart doesn't work. use "sudo killall node" followed by "sudo service mysensors start" instead)

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

  • cd ~/Arduino/Bootloader
  • nano Makefile
  • adjust avrdude settings to the available programmer. In my case (mySmartUSB MK3 programmer temporarily connected to RPi USB port):

    ISP_PORT = /dev/ttyUSB0
    ISP_SPEED = 115200
    ISP_PROTOCOL = stk500v2
    ISP_MCU = m328p

  • make load

End-to-End Test

Add Firmware to Controller (here: DallasTemperatureSensor)

  • nano ~/Arduino/NodeJsController/NodeJsController.js

    const fwHexFiles = [ '../libraries/MySensors/examples/DallasTemperatureSensor/DallasTemperatureSensor.ino' ];
    const fwDefaultType = 0;

  • sudo service mysensors restart (at this point stop/restart doesn't work. use "sudo killall node" followed by "sudo service mysensors start" instead)

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 once the bootloader finished uploading the firmware

About

A small example controller implemented in NodeJS


Languages

Language:JavaScript 79.2%Language:Shell 20.8%