nrf24l01-htu21d-logger
Wireless temperature and humidity logging to Google Spreadsheet (or sqlite3 database or csv file) using a HTU21D temperature & humidity sensor and wireless data transfer with the NRF24L01.
Purpose
To log temperature and humidity data in a Google Spreadsheet.
I do it by measuring with a HTU21D on an Arduino Nano, then transmitting the data to a Raspberry Pi via. a NRF24L01 and then finally saving the data to a Google Spreadsheet.
There are two parts to this:
- Measuring temperature and humidity
- Getting this data to Google Spreadsheet
There are a LOT of different temperature/humidity sensors but I've chosen the HTU21D. It is very accurate and just a little bit more expensive than the well-known DHT22. Both can be bought cheaply on ebay which I highly recommend. I've chosen to use the NRF24L01 because it does two-way communication cheaply. You can also use a 433 MHz RF transmitter/receiver but that doesn't give the same reliable protocol that the NRF24L01 offers. They cost about the same anyway however the NRF24L01 seems to have a bit shorter range which I guess can be an issue for some applications. I will transmit data from my basement just below my apartment so range is not an issue for me.
I've implemented two different schemes:
- Request data form an Arduino at regular intervals
- Listen to data that is sent form an Arduino at regular intervals
The first one enables good timing control since you can request a measurement at any given time. However the Arduino will consume more power because it has to listen for a signal to make a measurement continously. For this reason, the sketch corresponding to the second scheme is called the low power solution.
In action:
Getting readings from the logger.py program that is actively logging to Google Spreadsheet.
Data and live plot in Google Spreadsheet.
The code
Since there are different devices (Arduino and Raspberry Pi) I thought it would be convenient to separate the code and documentation for each device.
Arduino
Dependencies
The Arduino code depends on the following libraries:
The Low-Power library by Rocketscream is also required if using the low power Arduino sketch.
Setup
Upload the Arduino program. That's it :-).
Extra power savings can be achieved by replacing delay(t) statements in the HTU21D_Breakout library code with sleep statements from the Low-Power library, e.g. replace
sleep(55);
with
LowPower.powerDown(SLEEP_60MS, ADC_OFF, BOD_OFF);
I haven't gotten around to measuring the power savings of this change yet but the code still works as intended.
Connections
Connect the NRF24L01 as described in the documentation for the RF24 library by TMRh20. Connect the HTU21D as desribed in the documentation for the HTU21D_Breakout library by Sparkfun.
Raspberry Pi
Dependencies
The Raspberry Pi code depends on the following libraries
Configuration
Set the username, password and the name of the Google Spreadsheet that you want to save the data to in the config.cfg file. Use the readme in https://github.com/burnash/gspread as a guide to set these settings properly.
Alternative set a filepath for a csv or sqlite datastore. You can switch between datastores in the requester.py or listener.py files.
Setup
Install the RF24 library:
git clone https://github.com/TMRh20/RF24
cd RF24
make
sudo make install
Also install the pyNRF library:
cd RPi/pyRF24
sudo python setup.py
Install the gspread module for Python if you wish to log to Google Spreadsheet:
pip install gspread
You can now run the logger by issuing
sudo python requester.py
or
sudo python listener.py
depending on which data gathering scheme you prefer. The program can be conveniently run in the background by using screen in either case.
Connections
Connect the NRF24L01 as described in the documentation for the RF24 library by TMRh20. If you use the listener scheme then you should connect the NRF24L01 IRQ port to GPIO 17.