So we got to rent a new room in the cellar. It is a bit exposed, and we are not absolutely sure that it is actually frost-free. Or how humid the room is for that matter. How to track that? An arduino with a digital thermometer? With an SD-card reader? Or a Raspberry Pi?
The problem is that the room is too far from our appartment ruling out something connected to the internet – unless I want to go full IoT and set up something with a mobile connection.
After a couple of hours considering this, I came up with this basic plan:
- Connect a digital thermometer and hygrometer to a Raspberry Pi Zero.
- Write something that reads the values, and collect the data.
- Write something else, that tries to connect to a mobile hotspot from my cell-phone.
- When the connection is made – dump the collected data somewhere.
- Make sure that you pass the room on a regular basis with the mobile hotspot active.
How to do that? I already had a Raspberry Pi Zero v 1.1 lying around. It has build-in wifi. First step is to set it up with a fresh SD-card:
- Download the Raspbian Buster Lite image from https://www.raspberrypi.org/downloads/raspbian/
- Inset the sd-card in the computer, and follow the instructions on https://www.raspberrypi.org/documentation/installation/installing-images/README.md. I already had Balena Etcher installed.
Next, it would be nice to connect to it via USB, rather than connecting the Pi to a screen and work directly on it. In order to be able to do that, I go to the boot-folder on the newly flashed SD-card. There I do this:
- add the line “dtoverlay=dwc2″ at the very bottom of config.txt
- Open the file cmdline.txt, locate the parameter “rootwait” and immediately after that, add this “modules-load=dwc2,g_ether”.
- Make af file called “ssh”
Now, in principle, you move the sd-card to your Pi, connect it to the UBS-port on your computer, and you should be able to ssh into pi@raspberrypi.local, using raspberry as the password.
However, it is not quite that simple. When the Pi has been connnected to the laptop, go to settings, choose the wired connection, choose IPV4, and change the “method” to only link-local (do the same for IPV6). And under Identity, change the name to raspberrypi.local.
And now we can ssh into it.
Next step is to get some temperature and humidity readings.
For that purpose, I’m using a DHT22 sensor. More specifically an already wired up sensor. It has four connections, but I’m only using three. The red wire, power is connected to a 3.3V, the black wire to ground, and the yellow data-wire to any GPIO pin. I’m using pin 2
Next – after the Pi is again connected to power, and I have SSH’ed into it, some packages need to be installed.
Before that can be done, I need to get it connected to wifi.
As I am going to use my cell-phone for that I set that up to provide a hotspot. The ssid (or name of the hotspot) is set to NusseMobil, and the password to 3.141592653.
Then I edit the file /etc/wpa_supplicant/wpa_supplicant.conf file on the Pi:
sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
And add this to the end of the file:
network={
ssid=”NusseMobil”
psk=”3.141592653″
Then I run this command:
wpa_cli -i wlan0 reconfigure
to get the Pi to take in the new network configuration.
Testing: Unplug the Pi from the laptop, and connect it to a separate power source. Does the Pi connect to the hotspot? If so, stuff is working, and I plug it into the laptop again.
Now I ssh into the Pi, make sure that there is live connection to the web (ping www.dr.dk is the standard way in Denmark to test that)
Now the necessary packages can be installed:
sudo apt-get update
sudo apt-get install build-essential python-dev python-openssl git
Lets see if it works
cd examples
sudo ./AdafruitDHT.py 22 2
22 because I am working with the DHT22 sensor, 2 because it is connected to GPIO 2 on the board.
Neat. Currently the temperature is 27.2 and the humidity is 55.1%
I have not compared with other thermometers, but it seems plausible.
The sensors in this package is only ready every two seconds. That should be plenty, but I should take that into consideration in the following steps.
I want to save data to a google spreadsheet. In order for that to work, I need an API-key. I follow the instructions on this page:
https://gspread.readthedocs.io/en/latest/oauth2.html
More or less – small changes appears to have crept in. Anyway, I create a new service account caled newcellar, and give it the role of editor.
I get at json file contain interesting and incomprehensible stuff.
I transfer it to the Raspi using ssh:
sudo rsync /path/to/local/file username@PCB:/path/to/remote/destination
Within the file is a mail-adress. I make a new google drive sheet, and share it with that mailadress.
editing the example from the package:
sudo nano google_spreadsheet.py
I add json filename and spreadsheet name
And try to run it:
sudo ./google_spreadsheet.py
Nope! A couple of packages are still missing:
pip install gspread
pip install –upgrade oauth2client
And then I can run it. I get an error telling me that I have forgotten to activate the API. Luckily the error provides the link I need to go to.
And after a couple of minutes the change has propagated through Googles servers, and things are working. The spreadsheet are now steadily being populated with temperature and humidity readings every 30 seconds.
Nice!
How long can I run it on batteries?
That depends on how much power is drawn by the Pi.
This site:
has measured the power consumption by a Pi Zero w to about 250 mA. Battery capacity is measured in Ah (amp hours, usually as mAh).
The powerbank I am going to use initially, is 15.000 mAh, giving me 15000/250 60 hours of operation.