Tuesday, November 12, 2013

The LabRat in its entirety!



So, I figured it was about time I posted the details of the LabRat somewhere. Been tinkering with it for a while. and though it's a pretty basic setup, a lot of people have difficulty lifting off because of mundane hassles. I'll keep updating this post as and when I do something new with it.
For now, to grab the essence of the project , let's take a look at the graphic I made below. This graphic essentially describes the IOT (Internet of Things) feedback loop that starts from sensor circuitry to distributed processors such as the PI, to cloud based analytics like xively and finally to Notification / Actuation systems that may lead to manual/automated steps to help correct discrepancies that may be reported.



Well, pictures are good! So let's flow through this post with their aid. The LabRat is basically a Tablet device based off the Raspberry pi that integrates with sensor circuits and with xively to complete three slots of the loop described above. Now, to drill home the point that this is totally a get-time-do-something exercise, take a look at the setup below:

So, before the cynics descend , it may be essential to clarify that much of this was done with an hour of available time per month - so no time for massive soldering or integration.

With that disclaimer out of the way, first things first - Google's your best friend when it comes to the raspberry pi. The raspberry pi community is extremely active and all niggling questions are answered up at the forums on raspberrypi.org.
The screen shown above is a nice-have, but definitely not a necessity.

Accessing the pi without a screen:
Some people are adept with the command line and others with gui based tools But, really, when you decide to dive into the world of the raspberry pi, getting comfortable with the linux command line should be the first step.
Now, to access the pi without a screen, Irrespective of which operating system you have, OS X, windows or Linux - the idea is the same - the default raspbian wheezy distro brings up the eth0 interface with dhcp enabled. So, run a DHCP server on your laptop and bind to the ethernet interface. OS X allows you to directly share its  wifi connection through the ethernet port by nat-ting clients and running a dhcp server. Linux is easy to set up as well. For windows folks, use the following steps to share the internet connection with the pi:
1)      Download a dhcp server for windows from here:
                  http://www.dhcpserver.de/dhcpsrv.htm
2)      Set up the server based on the following steps: http://www.dhcpserver.de/dhcpsetup.htm
3)      Choose any private ip address range and set up a static ip in the same subnet (but outside the range) on the laptop’s network adaptor.
4)      The generic raspbian image comes with dhcp enabled by default under /etc/network/interfaces
5)      The DHCP server will assign an ip address. You can now ssh into the pi.
For VNC lovers, this is my setup currently :

Note: I faced some trouble initially when sharing the internet connection from my wifi through the ethernet port in Windows. Incidentally, I could only load up Google and associated sites or even bing. Nothing else really. Figured out that the mtu on the ethernet port on windows is set to a lower value by default (1300) - well I never set it earlier. The mtu on the pi is 1500 by default. This ends up dropping packets for a lot of sites.
So use the following commands to set things right:

check the mtu using:

netsh interface ipv4 show subinterfaces
Set it correctly for the required interface using: 

netsh interface ipv4 set subinterface "Local Area Connection" mtu=1500 store=persistent 

Install and set up vnc:
sudo apt-get install tightvncserver
vncserver :1 -geometry 1024x728 -depth 24 >>> set the password here
Run tightvnc viewer and login using   <pi’s ip>:1 and password set above

To start vnc automatically on boot up
Set up the following script :
Create a new file in the init.d directory:
sudo vi /etc/init.d/tightvncserver

#!/bin/sh
# /etc/init.d/tightvncserver
VNCUSER='pi'
case "$1" in
    start)
        su $VNCUSER -c '/usr/bin/tightvncserver :1'
        echo "Starting TightVNC Server for $VNCUSER "
        ;;
    stop)
        pkill Xtightvnc
        echo "TightVNC Server stopped"
        ;;
    *)
        echo "Usage: /etc/init.d/tightvncserver {start|stop}"
        exit 1
        ;;
esac
exit 0

Give the script executable permission:
sudo chmod 755 /etc/init.d/tightvncserver

 We can now start or stop the service manually:
sudo /etc/init.d/tightvncserver start
sudo /etc/init.d/tightvncserver stop

Also, make Tight VNC Server start every time the Raspberry Pi starts up:
sudo update-rc.d tightvncserver default

The Circuit:
The circuit is simple. A couple of analog sensors: LM35 for temperature and an SMD that I picked up at SP road in Bangalore to measure humidity. Remember, the raspberry pi does not accept analog inputs, only digital. So you'll need an ADC to convert the outputs of the sensors appropriately.
The ADC is probably the only real hindrance to getting up and running, so I'll post the code for bit banged SPI using ADS7841 from Texas instruments. These pieces of code are based on the datasheet of the ADC and not everyone may be comfortable decoding them. So, here you go:
P.S. the whole code is written in python. But it should be easy to adapt to any language of your choice. The function can be used by simply providing the channel number for the sensor (channel), and selecting the gpio pins of your choice for SPI setup (MOSI, MISO, CLK and CS).
#Read SPI data from ADS7841 chip, 4 channel ADC

def readadc(clockpin, mosipin, misopin, cspin, channel):
    if ((channel > 3) or (channel < 0)):
        return -1

    GPIO.output(cspin, True)

    GPIO.output(clockpin, False) #start clock low
    GPIO.output(cspin, False)    #bring CS low
    if (channel == 0):
        commandout = 0x97            # control byte = 1001 0111
    else:
        if (channel == 3):
            commandout = 0xE7            # control byte = 1110 0111
    for i in range(8):
        if (commandout & 0x80):
            GPIO.output(mosipin, True)
        else:
            GPIO.output(mosipin, False)
        commandout <<= 1
        GPIO.output(clockpin, True)
        GPIO.output(clockpin, False)

    adcout = 0

    #1 Clock cycle for conversion

    #13 clock cycles for ADC data
    for i in range(15) :
        GPIO.output(clockpin, True)
        GPIO.output(clockpin, False)
        adcout <<= 1
        if (GPIO.input(misopin)):
            adcout |= 0x1
    GPIO.output(cspin, True)
    adcout /= 8        # Last 3 bits are low drop them
    return adcout
Will have to find time for another update to explain how xively.com integration is done, along with email notifier and your very own twitter updater, all in the python script.

For reference, here is the entire python script:
Python Script

To see the effect of the circuit, take a look at the feed at:
https://xively.com/feeds/89297/?from_cosm=true
It's not live, so select a suitable time frame to view the variation.

Here is the email I receive from the python script when the threshold is exceeded



 

And here's the twitter feed:









Until another time then!

The Spiral Equivalence



Image

A spiral, winding about a sea of mundane proceedings, encloses the waters with each turn.
A day comes, and it comes eventually, when the hole is plugged.
A slew of possibilities arise, albeit in a new dimension, whilst the spiral is reduced to a singularity, thus lost in its own existence.
The fear wanes, the blood thickens and an orgasmic flush fills the face.
You re-heat the soup, sip away and are thankful that you're past that phase.