Since the Raspberry PI was introduced in 2012, it’s so much easier to build the Internet of Things! Connecting sensors and exposing the values they read, so much fun!
Time to get our hands dirty! In the coming months, I will stuff my house with sensors to gather data. Motion, temperature, barometric pressure and any other sensors I may come across?
In this post I’ll explain how to connect a digital temperature to a Raspberry PI (1B) and expose this via a NodeJS api.
Prerequisites
- Any Raspberry PI, I prefer the Raspberry Pi Zero W
- A digital temperature sensor: Sparkfun TMP102
- Jumper wires
Raspbian
Raspbian is the preferred operating system for experimenting with your PI. It’s forked from Debian and is optimized for all the Raspberry PI models. For this project you should download the Lite version, as a GUI is not required at all.
On Windows, I usually create the bootable SD card via Win32 Disk Imager.
SSH
After installing the SD card you can boot the PI. Make sure yours is connected with a screen and keyboard so you can enable SSH (so that you can access it via terminal from another computer).
Login with the default Raspbian user account (pi – raspberry) and enable SSH via raspi-config:
sudo raspi-config
Select Interfacing Options, then P2 SSH, then enable and reboot.
Remote access
Now that SSH is enabled you can access it via Putty. FilleZilla also supports SSH to move files from and on it!
Enabling services
We will also need to enable the I2C interface (to communicate with our temperature chip). You can easily do this with raspi config:
sudo raspi-config
Select Interfacing Options, then P5 I2C, then enable.
Install the sensor
Now to fun part! Connecting the Sparkfun TMP102 with your PI!
Raspberry Pi | TMP102 |
---|---|
3v3 Power | VCC |
Ground | GND |
SDA | SDA |
SCL | SCL |
Ground | ADD0 |
Make sure to connect the correct pins on your Raspberry PI, by googling the pin layout of your Raspberry PI! This is how mine looks:
Read the sensor data
The following Python script will read the temperature (in Celcius) and print the value to the terminal.
tmp102.py
#!/usr/bin/python import smbus I2C_BUS = 1 bus = smbus.SMBus(I2C_BUS) # 7 bit address (will be left shifted to add the read write bit) DEVICE_ADDRESS = 0x48 # Read the temp register temp_reg_12bit = bus.read_word_data(DEVICE_ADDRESS, 0) temp_low = (temp_reg_12bit & 0xff00) >> 8 temp_high = (temp_reg_12bit & 0x00ff) # Convert to temp from page 6 of datasheet temp = (((temp_high * 256) + temp_low)>>4) # Handle negative temps if temp > 0x7FF: temp = temp-4096; temp_C = float(temp) * 0.0625 print "%3.2f" % (temp_C)
If you want to execute it, first mark it executable:
chmod +x tmp102.py
And now:
./tmp102.py
Create the api
Install Node.js
We’ll be using Node.js to display the temperature over an api!
Install Node.js:
curl -sL https://deb.nodesource.com/setup_7.x | sudo -E bash - sudo apt install nodejs
See if it’s installed and the version:
node -v
Add the Node.js files
package.json
#package.json { "main": "index.js", "author": "Elias Lecomte <elias.lecomte@gmail.com>", "dependencies": { "express": "^4.14.1" } }
And an Express app:
server.js
"use strict"; const http = require('http'); const express = require('express'); const exec = require('child_process').exec; const execSync = require('child_process').execSync; // Express app const app = express(); app.get('/', (req, res) => { // Get raw temp data let tempRaw = execSync(__dirname + '/tmp102.py').toString(); // Parse raw temp data let temp = parseFloat(tempRaw.split('\n')[0]); // Response res.json({ temperature: { value: temp, description: 'Temperature in Celcius' } }); }); app.listen(80);
Now let’s download the required dependencies:
npm install
We can now start our Node.js app:
node index.js
You can now see the results by surfing to the ip (when you are on the same network):
Make it start at boot
The last bit. We want to start our Node.js api when the Raspberry PI boots. I found the easiest way to add the startup command to /etc/rc.local. This script is run at the end of the boot cycle.
su pi -c 'sudo node /home/pi/server/server.js < /dev/null &'
Now just reboot your pi (sudo reboot -n) and your Node.js app should start when the boot is finished! Enjoy!!
Sources
- http://raspberrypi.stackexchange.com/questions/48303/install-nodejs-for-all-raspberry-pi/48313#48313
- https://nodejs.org/dist/latest/armv6l .tar.gz
- http://weworkweplay.com/play/raspberry-pi-nodejs/ (start Node.js at boot)
- https://www.allaboutcircuits.com/projects/transmit-temperature-with-raspberry-pi/
- http://thisdavej.com/beginners-guide-to-installing-node-js-on-a-raspberry-pi/