The Stalker sensor platform : Part 5 - Logging data using Raspberry PI

Previously - The Stalker sensor platform : Part 4 - Transmitting data using Xbees

Our Stalker Sensor Platform is principally working, but we need still to have a computer connected all the time to collect the information from the Stalker. In this tutorial we'll learn how to use a Raspberry Pi as logging platform.

1.  Raspberry PI static IP setup

We'll set up the Raspberry PI without connecting a monitor. We just have to configure the system on the SD-Card using a static IP, then we can simply connect via Ethernet VNC.

  • Open the file "cmdline.txt" with a text editor and add an IP address (e.g. "ip=192.168.0.57") at the end of the file.

  •  Install PuTTY and Xming on your Computer. PuTTY is a tool to connect through SSH to the Raspberry PI. With Xming we can connect as remote desktop to the Raspberry PI.
  • Insert the SD-Card in your Raspberry Pi and connect it by Ethernet cable to your router or directly to the Ethernet port on your computer.
  • Connect to the Raspberry Pi using PuTTY. Just insert the IP address we defined in the cmdline file and click open.

  • The static IP setup in the cmdline.txt is a "hack" to set a static IP at the first startup and not everything will work because the system isn't configured in a proper way. Now let's configure IP address in the right way. We'll use the small terminal text editor "nano" to modify some system files.Type in:
    sudo nano /etc/network/interfaces

    Change the line:

    iface eth0 inet dhcp
    

    to (use your network settings, this is just an example):

    iface eth0 inet static
    address 192.168.0.57
    netmask 255.255.255.0
    network 192.168.0.0
    broadcast 192.168.0.255
    gateway 192.168.0.1
    

    To save the changes press CTRL+X (exit from nano), then then Y (yes overwrite file) and then Enter to confirm the operation.

  • After this change we can remove the static IP hack in cmdline.txt:
    sudo nano /boot/cmdline.txtRemove the IP configuration at the end and save the file.
    Reboot the system:
    sudo shutdown - r now

!!!Please remember to shutdown the Raspberry PI properly! The SD-card will get corrupted sooner or later if you just unplug the power cable of the Raspberry PI. Use sudo shutdown -r now for a restart and sudo shutdown - h now for a shutdown!!!

  • Check the IP setting with ifconfig command.

 

2. Connect through remote desktop to the Raspberry Pi

  • Launch Xming.
  • Check the X11 option in PuTTY

  • Connect to the Raspberry Pi with PuTTY
  • Type in "startlxde" in the command line window.

Now the desktop user interface of Raspbian should appear. This is a great way to work on the Raspberry Pi without connecting a monitor,  you can also work on your Raspberry Pi project on the go with just a laptop a micro USB cable and a Ethernet cable!

To exit from the visual interface you need to go back to the PuTTY terminal window and press CTRL + C.

3. Get access to serial port of the GPIO connector

We'll have to install first the Python serial library and then modify two files to get access to the serial port of the GPIO connector through Python.

  • Install serial library:
    sudo apt-get install python-serial
  • Restart the Raspberry PI (sudo shutdown -r now)
  • Modify the cmdline.txt:
    sudo nano /boot/cmdline.txt
    remove the text:

    console=ttyAMA0,115200 kgdboc=ttyAMA0,115200
    

    in the cmdline.txt file and save the changes.

  • Modify the inittab file:
    sudo nano /etc/inittab

    Comment out the following line using # in this file:

    #Spawn a getty on Raspberry Pi serial line
    T0:23:respawn:/sbin/getty-L ttyAMA0 115200 vt100
    

    modify it to:

    #Spawn a getty on Raspberry Pi serial line
    #T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100
    

    save the changes and reboot the Raspberry Pi.

 

    4. Write test code

We can write a simple test code to verify if the serial communication is working properly.  In this example we're using the nano text editor in the terminal.

  • Create a new Python file by typing:
    nano serialtest.py
  • Write following code:

 


import serial

port = serial.Serial("/dev/ttyAMA0", baudrate=9600, timeout=3.0)

while True:
port.write("Hello!!!")
rcv = port.readline()
print("received: " + repr(rcv))

  • Exit from nano editor and save the file.
  • At the Raspberry GPIO connector connect RX TX (Pin 8 + 10) together  with a Female to Female Jumper Wire. By doing that we can receive now what we sent.

 

  • Launch the code with the line:
    python serialtest.py 

and we're receiving the String "Hello!!!"  we were sending.

   5.  Receive data from Stalker

We can reuse the "serialtest.py" code also to receive data from our Stalker.

  • Connect the "End device" XBee module to the Raspberry PI GPIO connector:
    Raspberry Pi  --> XBEE
    Pin 1 --> Pin1
    Pin 8 --> Pin2
    Pin 6 --> Pin10
     
    The RX Pin of the XBEE module isn't connected, simply because it isn't needed, we just want receive data from the XBEE module connected to the Raspberry PI:

 

  • Launch again the "serialtest.py" code and you should receive the temperature data from the Stalker:

 

  • With this appended code the temperature data gets stored in a text file called "templog.txt":
import serial

port = serial.Serial("/dev/ttyAMA0", baudrate=9600, timeout=3.0)

def filewrite(rcv):
    logfile = open("templog.txt", "a")
    logfile.write(rcv)
    logfile.close

while True:
    rcv = port.readline()
    print("received" + repr(rcv))
    filewrite(rcv)

Next:

The Stalker sensor platform : Part 6 - Reducing current consumption by putting the Stalker to sleep