Difference between revisions of "Home Automation Kits with pcDuino and BLE4.0"

From LinkSprite Playgound
Jump to: navigation, search
(Install openHAB)
(Package List)
 
(6 intermediate revisions by the same user not shown)
Line 25: Line 25:
 
We need to install python serial first before we can use serial from pcDuino3B by doing:
 
We need to install python serial first before we can use serial from pcDuino3B by doing:
  
?
+
[[File:Code.jpg| 600px]]
1
 
$sudo apt-get install python-serial
 
(1) test code of python-mqtt:
 
  
?
+
[[File:Code 1.jpg| 600px]]
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:
 
  
?
+
[[File:Code 2.jpg| 600px]]
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
 
  
?
+
[[File:Code 3.jpg| 600px]]
1
+
 
2
+
[[File:Code 4.jpg| 600px]]
3
+
 
4
+
== Hardware Perparation ==
5
+
 
6
+
pcduino3 x1
7
+
 
8
+
ArduinoUNO x1
9
+
 
10
+
BLE4.0 Bee x1
11
+
 
12
+
BLE4.0 Shield x1
13
+
 
14
+
Xbee Shield x1
15
+
 
16
+
Door Sensor x1
17
+
 
18
+
Linker LED x1
19
+
 
20
+
pcDuino3 x1
21
+
 
22
+
== Test Execution ==
23
+
 
24
+
'''(1) install openhab on pcDuino and APP on mobile device:'''
25
+
 
26
+
[[File:Test.jpg| 600px]]
27
+
 
28
+
'''(2) build a file “mqtt-BLE.py”, copy the code of python-mqtt in it, remember to edit IP address to the IP address that read now:'''
29
+
 
30
+
*'''edit IP address based on distribution by pcDuino ( check by $ifconfig ), following is the location:'''
31
+
 
32
+
[[File:Test 1.jpg| 600px]]
33
+
 
34
+
'''(3) open “/home/ubuntu/c_environment/sample”, add a c file, copy the test code in it (need to add socket in Makefile of current directory, besides, edit IP address into ( the same as (2) ) ;'''
35
+
 
36
+
[[File:Test 2.jpg| 600px]]
37
+
 
38
+
'''(4) Connect the door sensor to D8 and GND of Arduino (set D8 as pull-up input, when the magnet of the door sensor approached, the IO level will drop ) , then connect linker LED to D13 and GND of Arduino, and install BLE4.0 Xbee on Arduino, and then download the test code of Arduino ( both switches of Xbee Shield should on the left side) :'''
39
+
 
40
+
[[File:Test 3.jpg| 600px]]
41
+
 
42
+
[[File:Test 4.jpg| 600px]]
43
+
 
44
+
'''(5) turn on pcDuino, run openhab, python MQTT, socket:'''
45
+
 
46
+
'''#turn on openhab:'''
47
+
 
48
+
'''$cd/opt/openhab'''
#include <SoftwareSerial.h>
+
 
+
'''$sudo ./start.sh'''
#define RxD 11
+
 
#define TxD 12
+
'''(if openhab mirror image has been build, add a self-start in it : sudo /opt/openhab/start.sh )'''
SoftwareSerial mySerial(RxD,TxD);
+
 
+
'''#run MQTT :'''
char dat;
+
 
int i,flag=1; 
+
'''$cd/ home/ ubutu'''
void setup()  
+
 
{
+
'''$sudo python ./mqtt_publish.py'''
    pinMode(RxD, INPUT);
+
 
    pinMode(TxD, OUTPUT);
+
[[File:Test 5.jpg| 400px]]
    pinMode(13,OUTPUT);
+
 
    pinMode(8,INPUT);  
+
'''#run socket ( this program should be run after python mqtt ):'''
    digitalWrite(8,HIGH);
+
 
    mySerial.begin(9600);              // the ble4.0 baud rate 
+
'''$cd /home/ubuntu/c_enviroment/sample'''
    Serial.begin(9600);                // the terminal baud rate 
+
 
}
+
'''$sudo ./socket'''
+
 
void loop()
+
'''(6the socket connection was build, after the program above start up.'''
{
+
 
    if(Serial.available())
+
'''(7) Now we can open the APP of mobile device, click settings, edit setting below :'''
    {
+
 
      mySerial.print((char)Serial.read());
+
[[File:Test 6.jpg| 600px]]
    } 
+
 
+
'''Edit IP address based on pcDuino, set up user name and code as following:'''
    if(mySerial.available())
+
 
    {
+
'''$cd /opt/openhab/configurations'''
      dat = char(mySerial.read());
+
 
      Serial.print(dat);
+
'''$vim ./users.cfg'''
      if(dat == 'S') i++;
+
 
      if(i%2) digitalWrite(13,LOW);
+
'''“test” is user name, “12345678” is code.'''
      else digitalWrite(13,HIGH);
+
 
    } 
+
[[File:Test 7.jpg| 600px]]
+
 
    if( (digitalRead(8)==1)&&(flag==1)) 
+
'''(8) after setting, click “save” at right side, then it will be return to the main interface ( click button can control light) :'''
    {
+
 
      flag = 0 ;
+
'''#when door is open, LED turn off :'''
      mySerial.write("123\r\n");
+
 
      Serial.write("123\r\n");
+
[[File:Test 8.jpg| 600px]]
    }
+
 
    if( (digitalRead(8)==0)&&(flag==0))  
+
[[File:Test 9.jpg| 600px]]
    { 
+
 
      flag =1 ;
+
'''#when door is close, LED will open :'''
      mySerial.write("321\r\n");
+
 
      Serial.write("321\r\n");
+
[[File:Test 10.jpg| 600px]]
    }
+
 
}
+
[[File:Test 11.jpg| 600px]]
 +
 
 +
= Package List =
 +
 
 +
*1 X [http://linksprite.com/wiki/index.php5?title=PcDuino3B PcDuino3B][MP_PCDUINO3B][102305006]
 +
 
 +
*1 X [http://linksprite.com/wiki/index.php5?title=Bluetooth_4.0_BLE_Pro_Shield_for_Arduino_(Master/Slave_and_iBeacon) Bluetooth 4.0 BLE Pro Shield for Arduino(Master/Slave and iBeacon)][SHD_BLE4_Pro][101101133]
 +
 
 +
*1 X Arduino UNO
 +
 
 +
*1 X [http://linksprite.com/wiki/index.php5?title=Bluetooth_4.0_BLE_Pro_Xbee_Form_factor_(Master/Slave_and_iBeacon) Bluetooth 4.0 BLE Pro Xbee Form factor (Master/Slave and iBeacon)][BB_XBEE_BLE4][105101117]
 +
 
 +
*1 X [http://linksprite.com/wiki/index.php5?title=Xbee_Shield Xbee Shield][SHD_XBEE][101101017]
 +
 
 +
*1 X Access control sensor
 +
 
 +
*1 X battery case
 +
 
 +
*1 X Dupont Line
 +
 
 +
*1 X [http://linksprite.com/wiki/index.php5?title=Mosfet_Module Mosfet Module][LINKER_SSR][118101014]
 +
 
 +
*1 X Power Supply for pcDuino
 +
 
 +
*1 X microUSB Line
 +
 
 +
*1 X plastic box

Latest revision as of 07:55, 19 July 2016

Introduction

OpenHAB.jpg

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:

Code.jpg

Code 1.jpg

Code 2.jpg

Code 3.jpg

Code 4.jpg

Hardware Perparation

pcduino3 x1

ArduinoUNO x1

BLE4.0 Bee x1

BLE4.0 Shield x1

Xbee Shield x1

Door Sensor x1

Linker LED x1

pcDuino3 x1

Test Execution

(1) install openhab on pcDuino and APP on mobile device:

Test.jpg

(2) build a file “mqtt-BLE.py”, copy the code of python-mqtt in it, remember to edit IP address to the IP address that read now:

  • edit IP address based on distribution by pcDuino ( check by $ifconfig ), following is the location:

Test 1.jpg

(3) open “/home/ubuntu/c_environment/sample”, add a c file, copy the test code in it (need to add socket in Makefile of current directory, besides, edit IP address into ( the same as (2) ) ;

Test 2.jpg

(4) Connect the door sensor to D8 and GND of Arduino (set D8 as pull-up input, when the magnet of the door sensor approached, the IO level will drop ) , then connect linker LED to D13 and GND of Arduino, and install BLE4.0 Xbee on Arduino, and then download the test code of Arduino ( both switches of Xbee Shield should on the left side) :

Test 3.jpg

Test 4.jpg

(5) turn on pcDuino, run openhab, python MQTT, socket:

#turn on openhab:

$cd/opt/openhab

$sudo ./start.sh

(if openhab mirror image has been build, add a self-start in it : sudo /opt/openhab/start.sh )

#run MQTT :

$cd/ home/ ubutu

$sudo python ./mqtt_publish.py

Test 5.jpg

#run socket ( this program should be run after python mqtt ):

$cd /home/ubuntu/c_enviroment/sample

$sudo ./socket

(6) the socket connection was build, after the program above start up.

(7) Now we can open the APP of mobile device, click settings, edit setting below :

Test 6.jpg

Edit IP address based on pcDuino, set up user name and code as following:

$cd /opt/openhab/configurations

$vim ./users.cfg

“test” is user name, “12345678” is code.

Test 7.jpg

(8) after setting, click “save” at right side, then it will be return to the main interface ( click button can control light) :

#when door is open, LED turn off :

Test 8.jpg

Test 9.jpg

#when door is close, LED will open :

Test 10.jpg

Test 11.jpg

Package List

  • 1 X Arduino UNO
  • 1 X Access control sensor
  • 1 X battery case
  • 1 X Dupont Line
  • 1 X Power Supply for pcDuino
  • 1 X microUSB Line
  • 1 X plastic box