This is a simple Node.js app to collect ambient data using a DHT22/AM2302 sensor attached to a Raspberry Pi and send it to a datastore (under development, ideally PostgreSQL).
- Buy a Raspberry Pi
- Buy a DHT22/AM2302 sensor
- Wire the sensor (instructions to follow)
- Install Node.js
- This app uses bcm2835 (installed during setup)
Open an SSH session on your Raspberry Pi, then type the following commands:
git clone https://github.com/mcdado/raspi-temp-report.git # to run on the Pi
cd raspi-temp-report # to run on the Pi
npm run setup # to run on the Pi
Alternatively, you can checkout the project locally on your coding machine, and use the deploy
script.
npm run deploy raspberrypi.local # to run on your coding machine
You have to change raspberrypi.local
to whatever hostname you Pi has, or even better to the name of the SSH configuration you might have set up.
The deploy script will copy the necessary project files, but will not run the setup on the Pi.
You have to run the setup the first time you deploy the app to the Pi.
In .env
file declare the id of the sensor as UNITNAME, the type of sensor as UNITTYPE, and the pin to which it is connected with UNITPIN.
Setup PostgreSQL with these tables:
create table sensors (
id serial PRIMARY KEY,
name text
);
create table recordings (
id serial PRIMARY KEY,
sensor text,
temperature float(2),
humidity float(2),
timestamp timestamp
);
You can run the app on the Raspberry Pi in debug mode by running with the dedicated npm script:
npm run debug
Behind the scenes, it will will run node app.js
with the inspect flag, binded on the Raspberry Pi's IP address and port 9229. Now you can use Visual Studio Code's built-in debugging tools to start a Debug session.