Raspberry PI iC880A shield now have sensors and dashboard

I did it without errors. But “sudo systemctl status sensors-js.service” always failed:

● sensors-js.service - CH2i TTN Gateway Sensors service
Loaded: loaded (/lib/systemd/system/sensors-js.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Sat 2018-04-28 19:08:27 CEST; 6s ago
Process: 5430 ExecStart=/usr/bin/node /opt/ttn-gateway/bin/sensors.js (code=exited, status=1/FAILURE)
Main PID: 5430 (code=exited, status=1/FAILURE)
Apr 28 19:08:27 ttn-gateway systemd[1]: sensors-js.service: Main process exited, code=exited, status=1/FAILURE
Apr 28 19:08:27 ttn-gateway systemd[1]: sensors-js.service: Unit entered failed state.
Apr 28 19:08:27 ttn-gateway systemd[1]: sensors-js.service: Failed with result ‘exit-code’.
Apr 28 19:08:27 ttn-gateway systemd[1]: sensors-js.service: Service hold-off time over, scheduling restart.
Apr 28 19:08:27 ttn-gateway systemd[1]: Stopped CH2i TTN Gateway Sensors service.
Apr 28 19:08:27 ttn-gateway systemd[1]: sensors-js.service: Start request repeated too quickly.
Apr 28 19:08:27 ttn-gateway systemd[1]: Failed to start CH2i TTN Gateway Sensors service.
Apr 28 19:08:27 ttn-gateway systemd[1]: sensors-js.service: Unit entered failed state.
Apr 28 19:08:27 ttn-gateway systemd[1]: sensors-js.service: Failed with result ‘exit-code’.

If I start the service by hand it succeeded.
I have node version 6.9.2. Could this cause the error? How can I update node to version 9.11.1?

Meanwhile, I’m on node v9.11.1 but the problem remains. Starting by hand works but not automatically:

Apr 29 07:19:00 ttn-gateway systemd[1]: Started CH2i TTN Gateway Sensors service.
– Subject: Unit sensors-js.service has finished start-up
– Defined-By: systemd
– Support: Debian -- User Support

– Unit sensors-js.service has finished starting up.

– The start-up result is done.
Apr 29 07:19:01 ttn-gateway sensors-js[3647]: /opt/ttn-gateway/bin/node_modules/bindings/bindings.js:88
Apr 29 07:19:01 ttn-gateway sensors-js[3647]: throw e
Apr 29 07:19:01 ttn-gateway sensors-js[3647]: ^
Apr 29 07:19:01 ttn-gateway sensors-js[3647]: Error: Module version mismatch. Expected 46, got 48.
Apr 29 07:19:01 ttn-gateway sensors-js[3647]: at Error (native)
Apr 29 07:19:01 ttn-gateway sensors-js[3647]: at Object.Module._extensions…node (module.js:434:18)
Apr 29 07:19:01 ttn-gateway sensors-js[3647]: at Module.load (module.js:343:32)
Apr 29 07:19:01 ttn-gateway sensors-js[3647]: at Function.Module._load (module.js:300:12)
Apr 29 07:19:01 ttn-gateway sensors-js[3647]: at Module.require (module.js:353:17)
Apr 29 07:19:01 ttn-gateway sensors-js[3647]: at require (internal/module.js:12:17)
Apr 29 07:19:01 ttn-gateway sensors-js[3647]: at bindings (/opt/ttn-gateway/bin/node_modules/bindings/bindings.js
Apr 29 07:19:01 ttn-gateway sensors-js[3647]: at Object. (/opt/ttn-gateway/bin/node_modules/i2c-bus/i2
Apr 29 07:19:01 ttn-gateway sensors-js[3647]: at Module._compile (module.js:409:26)
Apr 29 07:19:01 ttn-gateway sensors-js[3647]: at Object.Module._extensions…js (module.js:416:10)
Apr 29 07:19:01 ttn-gateway systemd[1]: sensors-js.service: Main process exited, code=exited, status=1/FAILURE
Apr 29 07:19:01 ttn-gateway systemd[1]: sensors-js.service: Unit entered failed state.
Apr 29 07:19:01 ttn-gateway systemd[1]: sensors-js.service: Failed with result ‘exit-code’.
Apr 29 07:19:01 ttn-gateway systemd[1]: sensors-js.service: Service hold-off time over, scheduling restart.
Apr 29 07:19:01 ttn-gateway systemd[1]: Stopped CH2i TTN Gateway Sensors service.
– Subject: Unit sensors-js.service has finished shutting down
– Defined-By: systemd
– Support: Debian -- User Support

– Unit sensors-js.service has finished shutting down.

Any suggestion for me to try out? It would be great to get it work.

I did a workaround using tmux but it‘s not as elegant as your solution - if it really works!

It could be possible that You have more versions of node.js installed ot some problems with scope.

You can check, for example if “sudo node -v” and “node -v” give same version result.

A fast workaround, if is a multiple node.js versions problem, if more version are installed is to change ExecStart in service file from /usr/bin/node to /usr/local/bin/node (or other path to node).

But the best solution is to fix your node.js installation/s.

Thank you, that seems to be a good suggestion.
“sudo node -v” says “v6.9.2” and “node -v” “v9.11.1” so I think I have to delete the v6.9.2

How can I do without touching the v9.11.1?

Here my config, and it works since lot of days

loragw@ttn-gw04:~ $ node -v
v9.11.1
loragw@ttn-gw04:~ $ npm -v
5.6.0
loragw@ttn-gw04:~ $ sudo journalctl -f -u sensors-js
-- Logs begin at Tue 2018-05-22 12:38:23 CEST. --
May 22 16:05:34 ttn-gw04 sensors-js[358]: BME280 data = {
May 22 16:05:34 ttn-gw04 sensors-js[358]:   "temperature_C": 39.64,
May 22 16:05:34 ttn-gw04 sensors-js[358]:   "humidity": 17.388738295232756,
May 22 16:05:34 ttn-gw04 sensors-js[358]:   "pressure_hPa": 1001.6689208701918
May 22 16:05:34 ttn-gw04 sensors-js[358]: }
May 22 16:05:35 ttn-gw04 sensors-js[358]: SI7021 data = {
May 22 16:05:35 ttn-gw04 sensors-js[358]:   "humidity": 45.197052001953125,
May 22 16:05:35 ttn-gw04 sensors-js[358]:   "temperature_C": 25.27628173828125
May 22 16:05:35 ttn-gw04 sensors-js[358]: }
May 22 16:05:35 ttn-gw04 sensors-js[358]: SI7021 data = {
May 22 16:05:35 ttn-gw04 sensors-js[358]:   "humidity": 45.189422607421875,
May 22 16:05:35 ttn-gw04 sensors-js[358]:   "temperature_C": 25.287006835937497
May 22 16:05:35 ttn-gw04 sensors-js[358]: }
May 22 16:05:36 ttn-gw04 sensors-js[358]: BME280 data = {
May 22 16:05:36 ttn-gw04 sensors-js[358]:   "temperature_C": 39.65,
May 22 16:05:36 ttn-gw04 sensors-js[358]:   "humidity": 17.360058411474203,
May 22 16:05:36 ttn-gw04 sensors-js[358]:   "pressure_hPa": 1001.7239804856971
May 22 16:05:36 ttn-gw04 sensors-js[358]: }
loragw@ttn-gw04:~ $

Here the script file /opt/loragw/sensors.js

/*******************************************************************************
 * NodeJS script to send sensors data to cayenne IoT dashboard
 * you can have a BMP280/BME280 and SI7021/HTU21D conencted to I2C bus
 * This sample has been written by Charles-Henri Hallard (ch2i.eu)
 *
 * Requires nodejs to be already installed
 * https://nodejs.org/en/download/package-manager/#debian-and-ubuntu-based-linux-distributions
 *
 * Requires CayenneJS bme280 and si7021 libraries
 * npm install bme280-sensor si7021-sensor cayennejs
 *
 * Don't forget to put your Cayenne credential in this script, all of this
 * stuff can be done with the script installer install.sh of this folder
 * installer will also make the as daemon to start/stop with the system
 *
 * sudo node sensors.js
 *
 *******************************************************************************/

const BME280 = require('bme280-sensor');
const Si7021 = require('si7021-sensor');
var Cayenne = require('cayennejs');

const bme280 = new BME280({ i2cBusNo : 1, i2cAddress : 0x76 });
const si7021 = new Si7021({ i2cBusNo : 1, i2cAddress : 0x40 });

// Update every x seconds
const updateInterval = 60;

// Initiate Cayenne MQTT API
const cayenneClient = new Cayenne.MQTT({
  username: "############",
  password: "###############",
  clientId: "##############"
});

// Read BME280 sensor data, repeat
const readBME280SensorData = () => {
  bme280.readSensorData()
    .then((data) => {

      console.log(`BME280 data = ${JSON.stringify(data, null, 2)}`);

                        // dashboard widget automatically detects datatype & unit
                        cayenneClient.celsiusWrite (0, data.temperature_C.toFixed(1));
                        cayenneClient.rawWrite(1, data.humidity.toFixed(0), "rel_hum" , "p" );
                        cayenneClient.hectoPascalWrite (2, data.pressure_hPa.toFixed(0));

      setTimeout(readBME280SensorData, updateInterval*1000);
    })
    .catch((err) => {
      console.log(`BME280 read error: ${err}`);
      setTimeout(readBME280SensorData, updateInterval*1000);
    });
};

const readSI7021SensorData = () => {
  si7021.readSensorData()
    .then((data) => {

                        console.log(`SI7021 data = ${JSON.stringify(data, null, 2)}`);

                        cayenneClient.celsiusWrite (4, data.temperature_C.toFixed(1));
                        cayenneClient.rawWrite(5, data.humidity.toFixed(0), "rel_hum" , "p" );

      setTimeout(readSI7021SensorData, updateInterval*1000);
    })
    .catch((err) => {
      console.log(`Si7021 read error: ${err}`);
      setTimeout(readSI7021SensorData, updateInterval*1000);
    });
};

cayenneClient.connect((err , mqttClient) => {
        console.log('Cayenne connected')

        // Initialize the BME280 sensor
        bme280.init()
                .then(() => {
                        console.log('BME280 initialization succeeded');
                        readBME280SensorData();
                })
                .catch((err) => console.error(`BME280 initialization failed: ${err} `));

        // Initialize the si7021 sensor
        si7021.reset()
                .then(() => {
                        console.log('SI7021 initialization succeeded');
                        readSI7021SensorData();
                })
                .catch((err) => console.error(`Si7021 reset failed: ${err} `));
})

and the starting service systemd file /lib/systemd/system/sensors-js.service

# as root in terminal
#
# ln -s $PWD/sensors.js /opt/loragw/sensors.js
#
# Enable the service
# systemctl enable sensors-js.service
#
# Start the service
# systemctl start sensors-js.service
#
# Verify it is running
# systemctl status sensors-js.service


[Unit]
Description=CH2i Lora Gateway Sensors service

[Service]
ExecStart=/usr/bin/node /opt/loragw/sensors.js
Restart=always
RestartSec=30
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=sensors-js

[Install]
WantedBy=multi-user.target
2 Likes

Hi Marco,

I changed ExecStart in sensors-js.service file from /usr/bin/node to /usr/local/bin/node an got it working!

Thank you very much
Hans