

Continuamos haciendo la TSRB430 BLE pero ahora desde una Raspberry Pi 🙂
Requisitos
- RPi2
- Modulo Serial HM10 BLE
- TSRB430 Relay Board
Raspberry Pi2 Code
[code]</pre>
<pre>#!/usr/bin/env python
import serial
def convert_hex_to_int(hexChars):
#convert string of hex chars to a list of ints
try:
ints = [ord(char) for char in hexChars]
return ints
except TypeError:
pass
return []
def convert_hex_to_bin_str(hexChars):
#convert hex char into byte string
response = convert_hex_to_int(hexChars)[0]
# convert int to binary string
responseBinary = bin(response)
# first 2 chars of binary string are ‘0b’ so ignore these
return responseBinary[2:]
ser = serial.Serial(
port=’/dev/serial0′,
baudrate=9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1
)
print “Serial esta abierto: ” + str(ser.isOpen())
print “Escribiendo…”
ser.write(“[“)
#ser.write(“AT+CONNL”)
print “Escrito! Ahora leamos”
x = ser.readline()
print “got ‘” + x + “‘”
responseBits = convert_hex_to_bin_str (x)
# binary conversion drops values until a 1 is encountered
# assume missing values are 0 and pad to give a value for all relays
responseBits = responseBits.zfill(4)
# reverse chars so that relay 1 is first
responseBits = list(responseBits)
responseBits.reverse()
# create dictionary of relay states
relayStates = {}
relay = 1
for bit in responseBits:
relayStates[relay] = int(bit)
relay += 1
print relayStates
ser.close()
</pre>
<pre>[/code]
Con esto ocupamos un poco mas de codigo para interpretar la respuesta del TSRB430.
Ahora podemos escribir y leer a los relays remotamente.
pi@raspberrypi:~/Documents/python $ python sendat5.py
Serial is open: True
Now Writing
Did write, now read
got ”
{1: 0, 2: 0, 3: 0, 4: 0}
pi@raspberrypi:~/Documents/python $
En el proximo tutorial veremos como usar estos códigos para crear un asistente personal que controla nuestra casa.