Saturday, April 13, 2013

Serial (UART) Communication between a Netduino Plus 2 & a Raspberry Pi

This code is part of a small experiment to work out how to send data between a Raspberry Pi and a Netduino Plus 2 via a serial (UART) connection. See this posting on one of my Arduino/Netduino blog for more information (including notes on how the two devices are wired together).

This code waits for a '@', '$', or '#' sent as a 1-byte "command" from the Netduion. The Raspberry Pi then responds with the requested information. The end of the response is marked with a linefeed (0x0A) character.


Python Code


import serial
import datetime

ser = serial.Serial("/dev/ttyAMA0", baudrate=115200)
while True:
        # Read 1-byte command from Netduino
        c = ser.read()
        # @ = get full date & time
        if c == '@':
                now = datetime.datetime.now()
                ser.write(str(now))
                ser.write('\x0A')
        # $ = get date
        elif c == '$':
                today = datetime.date.today()
                ser.write(str(today))
                ser.write('\x0A')
        # # = get time of day
        elif c == '#':
                now = datetime.datetime.now()
                time = "%02d:%02d:%02d" % (now.hour, now.minute, now.second)
                ser.write(time)
                ser.write('\x0A')

Saturday, April 6, 2013

Text to Speech with a EMIC2 & a Raspberry Pi

The EMIC2 text to speech module does not require any special libraries and it communicates with the controller using a serial connection, so it is not difficult to get it working with a Raspberry Pi. Below is an example that uses Python to send messages to the EMIC2.  

The EMIC2 is a 5V device, so a logic level converter is required on the serial connection to and from the Raspberry Pi.

For audio output, I have used a cheap thin speaker from Sparkfun, but you can also use the headphone jack on the module.

The RXD and TXD pins on the Pi connect to the Linux serial device ttyAMA0. By default, though, this device is used for console output. Follow the 3 steps on the Adafruit Learning System page, "Freeing UART on the Pi."

Connections


EMIC2  Logic Level Converter  Raspberry Pi
GND    HV GND     ----->      GND
5V     HV HV      ----->      5V
SOUT   HV RXI     LV RXO      RXD
SIN    HV TXO     LV TXI      TXD
                  LV LV       3V3
                  LV GND      GND

Connect SP- & SP+ to the speaker.

    

Python Code


The following demonstration code counts out load. Depending on your actual output, ou may need to experiment with the time.sleep() calls to get the values right.

import serial
import time

serial = serial.Serial("/dev/ttyAMA0", baudrate=9600)
serial.open()
serial.write("\n")
time.sleep(1)
serial.write("V15\n") # Adjust volume
c = 0
while True:
        serial.write("\n")
        time.sleep(0.75)
        data = serial.read()
        if data == ':':
                buffer = "S%d" % (c)
                c = c + 1
                serial.write(buffer)
                time.sleep(0.5)
        else:
                time.sleep(0.5)

serial.close()

For documentation on the various EMIC2 commands, see the manual.
      




Wednesday, April 3, 2013

Reading Time & Date from a Chronodot Using Python with a Raspberry Pi

Here is a quick example of how to read the time and date from a Chronodot using the Python SMBus module (for I2C communication).

Connections


Chronodot   Raspberry Pi
GND         GND
VCC         3V3
SCL         SCL0
SDA         SDA0

The Raspberry Pi has built-in pull-up resistors on the SCL and SDA lines, so external pull-up resistors are not needed.

Code



import smbus
import time
import time
bus = smbus.SMBus(1)
address = 0x68
data = bus.read_i2c_block_data(address, 0)
ss = (data[0]/16*10) + (data[0]%16)
mm = (data[1]/16*10) + (data[1]%16)
hr = (data[2]/16*10) + (data[2]%16)
day = (data[3]/16*10) + (data[3]%16)
date = (data[4]/16*10) + (data[4]%16)
month = (data[5]/16*10) + (data[5]%16)
year = (data[6]/16*10) + (data[6]%16)
buffer = "%02d:%02d:%02d %02d/%02d/%02d" % (hr, mm, ss, month, day, year)
print buffer