Arduino + Raspberry Pi – Measuring Temperature and Humidity

I know that the Raspberry Pi has its own GPIO pins, but since I had absolutely no experience with this kind of thing, I preferred to use an Arduino for that. But, as Dr. Monk has shown in a number of posts, there is nothing that prevents you from hooking up the Arduino to your Raspberry Pi. That saves you the cost of buying an Ethernet-shield for your Arduino.

It was one of those “combine what others have figured out for you” projects. It involves a DHT11 temperature & humidity sensor, a breadboard, a 10K resistor, some wires, an Arduino, a USB-cable, a Raspberry Pi, an Apache webserver, some Pythone code and some time.

Wiring up the DHT11
The nice people at Adafruit not only sell you the DHT11, they also have a number of pages that explain you how to wire up the DHT11, provide you with a library and example code to get started.
The result is that you will be able to see the temperature and humidity in the Arduino IDE.
But that of course is not enough.

Setting up the Raspberry Pi
The cool thing, that makes this setup work, is that the Raspberry Pi can do exactly what the Arduino IDE can do: listen to the data that is sent over the serial interface by the Arduino.
So if we connect the Arduino to one of the two Raspberry Pi USB-ports, you can make the Raspberry Pi listen to that data.
You need a Python library for serial communications called ‘pySerial‘.
Since I do everything over SSH, I couldn’t simply follow Dr. Monk’s instructions but did it slightly different:

9. type: sudo usermod -a -G dialout pi
This give your pi user access to the serial port
10. Add this as 55-odd.rules to /etc/udev/rules.d/KERNEL=="ttyACM0", SYMLINK+="ttyS7"
I needed this to ‘see’ the USB port. Before, it just didn’t show up.

If you now connect the Arduino with the sketch still uploaded to it, you can test to see if the data is coming in, by running Python from the SSH commandline:

Make sure you use a 4 space indentation for 2.c.
Press ENTER twice afterwards.
If all goes as planned you will start seeing the same output as in the Arduino IDE.

Setting up the webinterface
Still not satisfied? No, of course not. Because I didn’t want that data in a SSH-session, but in a browser window.
Now, my problem was that I still know very little about Python, and in particular Python in combination with the Apache webserver (which I already had installed on the Raspberry Pi). Luckily for me, Karl McAuley has described how to setup a Python environment in combination with the Apache webserver. You can read about it here.

Version 1
I used the myinfo.py code on Karl’s page to test the setup and then adapted the second example on his page to create code for the first version of my webpage.
You can download the code here.

OK, it works, but does not look great. There are a number of improvements that could be made. The data collected could be stored in a database so that you can create charts displaying the temperature and humidity change during the day. But the least I could do is make it look a bit prettier.

Version 2
I wanted the temperature and humidity to be displayed in a nice gauge. I found a Javascript library that looked just right for the job.
I changed the Python code so that, when I add ?json=1 to the URL, the page returns the temperature and humidity in JSON format:
If you leave the “json=1” part out, the page will simply display two gauges:
The page with the gauges uses JQuery to retrieve the JSON code and the sets the gauges. The reason for this two-step approach is that it now is easy to have the page load the current humidity and temperature say every 10 seconds. Because that is all done using Javascript, the user doesn’t see the page being reloaded (because it isn’t), he would just see the gauges change if humidity or temperature changes.

What is next?
I think a possible next step would be to have look at the code needed to send data over the serial line to the Arduino and have it do stuff based on that. Dr. Monk has done that before but he only received single letters.
I do have all the parts to do this:

But of course, unlike there, I would set it up using the Arduino linked to the Raspberry Pi. The setup is described here. Others have described how to read data from the serial line, so that should also be doable.
Something like a webpage where you can enter a message which is then displayed on the LCD screen attached to the Arduino would be funny. Or maybe just the current Power level of the Solar Power system. We’ll see. To be continued.

Marc, you shouldn’t run the python script from the command prompt, you should just use a browser to request the page, e.g. http://127.0.0.1:5000/index.py (depending on IP, port, folder etc. setup of your RPi).