Arduino + Raspberry Pi – Measuring Temperature and Humidity


Arduino + Raspberry Pi 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.
Arduino + Raspberry Pi Arduino + Raspberry Pi
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:

1. On your laptop/Mac go to
2. Download pyserial-2.5.tar.gz
3. Unzip it
4. Connect to your Raspberry Pi using WinSCP
5. Upload the pyserial folder to a temp folder on your Raspberry Pi
6. Connect using SSH
7. Go to the temp folder
8. sudo python install

I needed to do a couple of things not listed in the post by Dr. Monk:

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:

1. type python and press ENTER
2. after the >>> type:
2.a. import serial
2.b. ser = serial.Serial(‘/dev/ttyACM0’, 9600)
2.c. while 1 :
2.c.     ser.readline()

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.

The steps:
1. login to the Raspberry Pi using SSH
2. type: sudo Bash to get root access
3. type: apt-get update
4. type: apt-get upgrade
5. type: apt-get install apache2 libapache2-mod-wsgi python-setuptools python-flask
6. type: usermod -a -G dialout www-data
7. edit the virtual host in /etc/apache2/sites-enabled, see the needed code changes here.
8. type: a2enmod wsgi
9. type: /etc/init.d/apache2 restart

This now restarts apache2 and you should be good to go.

Version 1
I used the 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.
Arduino + Raspberry Pi
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:
Arduino + Raspberry Pi
If you leave the “json=1” part out, the page will simply display two gauges:
Arduino + Raspberry Pi
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.

Note: I haven’t added that part of the code yet, you can download the current code here.

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.

Deel dit bericht:

  6 reacties aan “Arduino + Raspberry Pi – Measuring Temperature and Humidity”

Reacties (6)
  1. Hi,

    I followed all the steps, and it was very clear! I’m getting response from the arduino if I enter the python code, like 1, 2a,2b,2c,2d. I also can reach the webpage on my rpi from my laptop.

    The only thing I didn’t manage to work are the values. I guess I need to run the, but that down;t do the job. This is what I get when running the script.

    pi@raspberrypi ~/www $ python
    * Running on
    * Restarting with reloader

    My Arduino ‘exports’ the text ‘Temprature 21\r\n’.

    Can you help me to get this working?


    • This is the webpage i’m getting. Obviously the variables aren’t replaced.

      Python/Arduino/Raspberry Pi Demo

      Showing how Python can work with Apache, reading data from the Arduino on the Raspberry Pi

      Date: %TodaysDate%
      Arduino data: %ArduinoOutput%
      Page refesh in 5 seconds.

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

      • Hi Pierre,
        Zo te zien aan de rest van de site kan ik gewoon Nederlands typen. Goede site trouwens!

        Ik bereik de webpagina’s op m’n rpi via Is het de bedoeling dat ik de html pagina opvraag, of de Als ik namelijk typ, krijg ik droge python code op m’n scherm.

        Nogmaals, als ik die stappen met python typen doen, krijg ik mooi de output van de arduino op m’n scherm, dus de koppeling werkt. Het gaat nu puur om de koppeling naar een webpagina.

        Met vriendelijke groet,

      • Hoi Marc,

        Ja, Nederlands is prima. 🙂
        Nee, de html pagina is alleen het sjabloon. Je hebt nodig, dus inderdaad

        Als hij de droge python code laat zien, dan is er iets mis gegaan bij het koppelen van python aan je apache installatie. Dat zijn de stappen bij “Setting up the webinterface” hierboven en op deze pagina (dat is de bron waar ik het ook van geleerd heb). Daar staat ook een test python script. Ik ga er vanuit dat als jij dat runt, dat dat ook niet werkt. Meest voor de hand liggende plek waar het fout is gegaan is het bewerken van /etc/apache2/sites-enabled
        Check even goed of je ook de delen bij DirectoryIndex index.php index.htm index.html en Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch toegevoegd hebt.

      • Dankjewel. Achteraf gezien een eigen stomme typefout. Het werkt!

        Ik denk graag met je mee over het zenden over de Serial. Op die manier kan je namelijk heel veel leuke dingen doen. Op verschillende websites kan je namelijk ook relais kopen voor de arduino. Zonder al te veel moeite kan je dan apparaten aansturen.

        Op zich is één teken versturen al genoeg. Die kan je dan met de arduino ophalen, en verwerken. Bijvoorbeeld ‘if Serial.get()==a, doe x, else if Serial.get()==b, doe y’ enz.