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