This is a Node.JS Restful Web API service for the Pressure, Temp and Humidity sensor of Adafruit BME280.
- Download Raspbian Stretch Lite and use Etcher to install the image to the SD card
- Follow the Raspberry Pi Zero Headless Setup instruction by David Maitland to prepare the OS without any peripherals
-
Enable I2C connection with
sudo raspi-config
command -
Check the Raspberry Pi Zero pinouts
-
Connect BME280 by I2C
Signal Raspberry Pi Zero pin BME280 pin Power +5V Pin 4 VIN pin Data SDA Pin 3 SDI pin Clock SCL Pin 5 SCK pin Ground Pin 6 GND pin -
Install I2C tools:
sudo apt install i2c-tools
-
Check the presence of the device by running
sudo i2cdetect -y 1
.
It should show address0×77
if connection is correct. -
Install Adafruit Python GPIO library
-
Test data reading with Adafruit Python BME280 examples
git clone https://github.com/iharosi/sensor-api.git ~/sensor-api
- Install mongodb
apt install mongodb
- Install cron script
To read the sensor every 10 minutes we install a cron script.
Runcrontab -e
and insert the following line at the end of the file:0,10,20,30,40,50 * * * * /usr/local/bin/node ~/sensor-api/sampling.js > /dev/null 2>&1
-
Create sensor-api service unit file at
/etc/systemd/system/sensor-api.service
with the following content:[Unit] Description=Sensor API Requires=networking.service mongodb.service After=networking.service mongodb.service [Service] WorkingDirectory=/home/pi/sensor-api ExecStart=/usr/local/bin/node server.js Restart=always RestartSec=10 StandardOutput=journal StandardError=journal SyslogIdentifier=sensor-api User=pi Group=pi [Install] WantedBy=multi-user.target
-
Reload configuration files
systemctl daemon-reload
-
Install sensor-api service
systemctl enable sensor-api
- Start sensor-api service
systemctl start sensor-api
- Check if the service has successfully started
systemctl status sensor-api
The service will listen on port 3000.
Endpoint | Description |
---|---|
/ | This page |
/sensor/ | List all data |
/sensor/last | Get last measured data |
/sensor/id/{Object ID} | Get data with the given ID |