Home Automation Kits with pcDuino and BLE4.0
Introduction
Before this article, there is post about DIY Smart home connecting through Wifi. Nowadays, the wearable devices such as smartbands and smart watch are extremely popular. So it’s time to talk about Bluetooth4.0, this article is all about how to DIY your own smart home using BLE4.0 Shield and BLE4.0 Bee as connection devices, pcduino as openhab server, Arduino as device.
Usage
Install openHAB
Install openHAB server software on pcDuino, get more detail form linksprite learn center:
http://learn.linksprite.com/?s=openhab
or, download the image file below, then update ( install openh under “/opt/openhab” folder):
http://pan.baidu.com/s/1ntHtCyX?qq-pf-to=pcqq.c2c code: fh6c
For more detail about how to use BLE4.0 shield ( the usage of BLE4.0 Bee is the same as BLE4.0 Shield )
http://learn.linksprite.com/arduino/shields/how-to-use-ble4-0-shield/
Test Code
We need to install python serial first before we can use serial from pcDuino3B by doing:
? 1 $sudo apt-get install python-serial (1) test code of python-mqtt:
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 import socket import sys import paho.mqtt.publish as publish import paho.mqtt.client as mqtt
import serial myport=serial.Serial('/dev/ttyS1',9600,timeout=1)
myport.write('test')
client_connect=0
def on_connect(mqttc, obj, flags, rc):
print("rc: "+str(rc))
def on_message(mqttc, obj, msg):
if client_connect==1: myport.write('S') print(msg.topic+" "+str(msg.qos)+" "+str(msg.payload)) connection.send(msg.topic+" "+str(msg.payload))
def on_publish(mqttc, obj, mid):
print("mid: "+str(mid))
def on_subscribe(mqttc, obj, mid, granted_qos):
print("Subscribed: "+str(mid)+" "+str(granted_qos))
def on_log(mqttc, obj, level, string):
print(string)
- The following for TCP/IP from Arduino-style part #############
- Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.setsockopt( socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
- Bind the socket to the port
- server_address = ('localhost', 10000)
server_address = ("192.168.1.134", 10000) print >>sys.stderr, 'starting up on %s port %s' % server_address
sock.bind(server_address)
- Listen for incoming connections
sock.listen(1)
- The following for subscribing to MQTT ##############
mqttc = mqtt.Client() mqttc.on_message = on_message mqttc.on_connect = on_connect mqttc.on_publish = on_publish mqttc.on_subscribe = on_subscribe
mqttc.connect("localhost", 1883, 60)
- Subscribe to topic '4033', which is the ID of the relay of garage
mqttc.subscribe("4033", 0) mqttc.loop_start()
while True:
# Wait for a connection print >>sys.stderr, 'waiting for a connection' connection, client_address = sock.accept() client_connect=1 try: print >>sys.stderr, 'connection from', client_address # Receive the data in small chunks and retransmit it while True: # debug #connection.send('Hello world') ble_data = myport.readline() if len(ble_data)>0: print(ble_data) #data = connection.recv(3) #print(data) #if len(data)==0: #connection.close() #break if "test" in ble_data : print('okay') if "321" in ble_data: print >>sys.stderr, 'publish 0' publish.single("3032", "0",hostname="localhost") if "123" in ble_data: print >>sys.stderr, 'publish 1' publish.single("3032", "1",hostname="localhost") except KeyboardInterrupt: # Clean up the connection connection.close() sys.exit(1)
(2) test code of Socket:
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
- include "core.h"
- include "stdio.h"
- include "stdlib.h"
- include "string.h"
- include <sys/socket.h>
- include <netinet/in.h>
- include <arpa/inet.h>
int sockfd,n, flags; socklen_t addr_len; struct sockaddr_in servaddr,cliaddr; char sendline[1000]; char recvline[1000]; int rc;
void setup() {
sockfd=socket(AF_INET,SOCK_STREAM,0); bzero(&servaddr,sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr=inet_addr("192.168.1.134"); servaddr.sin_port=htons(10000); Serial.begin(9600); if((rc = connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)))<0) { perror("Client-connect () error"); exit(-1); } flags=fcntl(sockfd,F_GETFL); flags|=O_NONBLOCK; fcntl(sockfd,F_SETFL,flags);
} void loop() {
} (3) test code of Arduino
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
- include <SoftwareSerial.h>
- define RxD 11
- define TxD 12
SoftwareSerial mySerial(RxD,TxD);
char dat; int i,flag=1; void setup() {
pinMode(RxD, INPUT); pinMode(TxD, OUTPUT); pinMode(13,OUTPUT); pinMode(8,INPUT); digitalWrite(8,HIGH); mySerial.begin(9600); // the ble4.0 baud rate Serial.begin(9600); // the terminal baud rate
}
void loop() {
if(Serial.available()) { mySerial.print((char)Serial.read()); } if(mySerial.available()) { dat = char(mySerial.read()); Serial.print(dat); if(dat == 'S') i++; if(i%2) digitalWrite(13,LOW); else digitalWrite(13,HIGH); } if( (digitalRead(8)==1)&&(flag==1)) { flag = 0 ; mySerial.write("123\r\n"); Serial.write("123\r\n"); } if( (digitalRead(8)==0)&&(flag==0)) { flag =1 ; mySerial.write("321\r\n"); Serial.write("321\r\n"); }
}