Thursday, October 1, 2015

I-SHAMBA 00.02


I-SHAMBA PREQUISITIES
 
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 Polymath Universata 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 http://sourceforge.net/projects/pyserial/files/pyserial/2.5/
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 setup.py 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 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.
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.

No comments:

Post a Comment