Friday, March 22, 2013

Raspberry Pi SOAP Web Service Client Using Python Suds

I wrote about my first attempt at creating a Java SOAP Web service that runs on a Beaglebone. To test the service, I wanted a quick and easy client (besides the invaluable SoapUI, which has a free version in additional to the Pro version). There is a very handy SOAP client library for Python called Suds, so I used that to create a basic script to call the Web service.

In my case, the Java SOAP Web service is running on a Beaglebone connected to my home network. My WSDL is at http://192.168.0.106:9876/temp?wsdl. The service you are using will, of course, vary. The service has two simple methods that return the current temperature from the TMP102 sensor in degrees Celsius (getTempC) or degrees Fahrenheit (getTempF).

Python 2 Code


import time
from suds.client import Client

url = "http://192.168.0.106:9876/temp?wsdl"
client = Client(url)
while True:
   resultC = client.service.getTempC()
   resultF = client.service.getTempF()
   print " %2.3f C / %2.3f F" % (resultC, resultF)


Saturday, March 16, 2013

C Code to Access a Chronodot I2C Real-Time Clock from a Raspberry Pi

The Chronodot is an I2C real-time clock module based on the DS3231 chip that I've used with a couple Arduino aand Netduino projects. In looking into how to start doing some C or C++ programming on the RPi, I found this post at Binnery that shows how to work with a DS1307 RTC from a C program.  The same code works with the Chronodot with only slight modification.

Connections


Chronodot   RPi 
GND         GND
VCC         5V0
SCL         SCL0 (GPIO3)
SDA         SDA0 (GPIO2)

The Raspberry Pi has built-in 1k pull-up resistors on the SDA and SCL lines. These are sufficient for the Chronodot, so no external pull-up resistors are needed.

Code


The C code by Andre Wussow is available from GitHub. There is only one line that needs to be change so the code will work with the Raspberry Pi Model B. Model B uses /dev/i2c-1 as the I2C bus rather than /dev/i2c-0 :

Change line  56 from

if ((deviceHandle = open("/dev/i2c-0", O_RDWR)) < 0) {

to

if ((deviceHandle = open("/dev/i2c-1", O_RDWR)) < 0) {

To add leading zeros appropriately to the date and time data (and rearrange the date), I also modified lines 94 - 95 (but this is not strictly necesary):

printf("Date: %02x-%02x-%02x\n", month, day, year);
printf("Time: %02x:%02x:%02x\n", hours, minutes, seconds);

I compiled the code using the following command at the command line on the Pi:

  gcc rtc_ds1307.c -ortc_ds1307.c 

Run the code at the command line like this -

  ./rtc_ds1307

This prints out the current time and date from the Chronodot.

Wednesday, March 13, 2013

Sending Data from an Arduino to a Raspberry Pi Using I2C

On my Arduino blog I posted the portion of this example for the Arduino Leonardo. See that post for noes on the actual connections between the Arduino and the Raspberry Pi. Below is the example Python 2 code that runs on the Raspberry Pi.


Code:


This code assumes that you have enabled I2C on the Raspberry Pi and installed the SMBus module for Python (which provides I2C support).


import smbus
import time
import struct

bus = smbus.SMBus(1)
address = 0x03

while True:
    try:
        data = bus.read_i2c_block_data(address, 0);
    except:
        print "Error getting data\n"
        continue
    # Example uses 4 sensors. 
    # Each float from the Arduino is 4 bytes long.
    for i in range(0, 4):
        bytes = data[4*i:4*i+4]
        # Python 2 struct.unpack takes the data to be unpacked 
        # in string format, so the bytes need to be joined
        # together first
        print struct.unpack('f', "".join(map(chr, bytes)))[0]
    print
    time.sleep(5);