IoT with openHAB on pcDuino3B

Post on 13-Apr-2017

2.623 views 1 download

Transcript of IoT with openHAB on pcDuino3B

IoT: Internet of Things

There are many home automation solutions and Internet-of-Things (IoT) gadgets on the market, which are all useful on their own. They come with their own way on how to setup and configure devices and are perfect for their intended use cases.

The problem with all of these systems and devices is that these use cases are defined by the manufacturer - but as a user, you will quickly come up with wishes that are not supported out of the box or which require interaction between the different systems.

OpenHAB on pcDuino

openHAB

openHAB

pcDuino

pcDuino is a family of single board computers that runs linux/androids or windows (on some models) and and is compatible with Arduino ecosystem.

pcDuino FamilypcDuino2 pcDuino3 pcDuino8 pcDuino Acadia pcDuino

Canyonlands

CPU Allwinner A101GHz ARM Cortex A8

Allwinner A20, 1GHz, ARM Cortex A7 Dual Core

Allwinner H82GHz, 8-Core Cortex-A7

Freescale Quad ARM Cortex A9 @1.2GHz

Intel ATOM Z3735F Quad Core 1.33-1.88GHz

GPUOpenGL ES2.0OpenVG 1.1 Mali 400 core

OpenGL ES2.0OpenVG 1.1 Mali 400 core

PowerVR SG544 @720MHz

OpenGL/ES 2 x 3D accelerator with OpenCL and OpenVG

Intel HD Graphics, 311-645MHz

DRAM  1GB 1GB 1GB 1GB 2GB

Storage

 2GB Flash (4GB after 2/1/2014)microSD card (TF) slot for up to 32GB

4GB FlashmicroSD card (TF) slot for up to 32GB

microSD card slot for up to 32GB

microSD card slot for up to 128GB 32GB emmC

Video  HDMI HDMI HDMI HDMI HDMI

OS Support •Lbuntu 12.04•Android

•Lbuntu 12.04•Android Linux

Android

Linux Android

Windows 8.1 with bingAndroidWndows 10

Extension Interface Arduino (TM) Headers

Arduino (TM) Headers

Arduino (TM) Headers

Arduino (TM) Headers

NetworkInterface •10/100Mbps RJ45•USB WiFi extension

•10/100Mbps RJ45•WiFi •Gbits RJ45 Gbits RJ45 WiFi

Bluetooth

Power  5V, 2000mA  5V, 2000mA 5V, 2000mA 5V, 2000mA 5V, 3000mA

 

pcDuino3B hardware interfaces

openHAB kit

How to access pcDuino without HDMI monitor Use a TTL UART USB Debugger

http://store.cutedigi.com/serial-debug-cable-with-ft232-for-pcduino-raspberry-pi/

Install Driver on MAC http://www.ftdichip.com/Support/Documents/AppNotes/

AN_134_FTDI_Drivers_Installation_Guide_for_MAC_OSX.pdf

Use PuTTY on Windows Serial port: 115200

How to access pcDuino without HDMI monitor

TX-RXRX-TXGND-GND

How to access pcDuino without HDMI monitor

How to access pcDuino without HDMI monitor

Install OpenHAB on pcDuino$sudo apt-get install git$sudo mkdir /opt/openhab$cd /opt/openhab$sudo wget https://github.com/openhab/openhab/releases/download/v1.6.2/distribution-1.6.2-runtime.zip$sudo unzip distribution-1.6.2-runtime.zip$cd addons$sudo wget https://github.com/openhab/openhab/releases/download/v1.6.2/distribution-1.6.2-addons.zip$sudo unzip distribution-1.6.2-addons.zip$cd ..$sudo wget https://github.com/openhab/openhab/releases/download/v1.6.2/distribution-1.6.2-demo-configuration.zip$sudo unzip distribution-1.6.2-demo-configuration.zip$sudo cp configurations/openhab_default.cfg configurations/openhab.cfg $sudo chmod +x start.sh $sudo ./start.sh

How to enable security in OpenHABThe way to enable security in OpenHAB is to edit the file named openhab.cfg. Look for the line starts with ‘security:option=’.

If we do ‘security:option=EXTERNAL’, it means that only when we try to connect to the OpenHAB server from external network, i.e., Internet, it will ask for username/password. It will not ask for credentials.If we do ‘security:option=ON’, it will always ask for username/password. Once the security is turned on, we can add the username and password to the file user.cfg, for example,Username passwordben=password1scott=password2

Use an image with openHAB preinstalled You can use an image with openHAB

preinstalled. Please download it from:

http://www.linksprite.com/?page_id=1289 The username and password for

the openHAB is Username: patrick Password: ivpcduino

Web Interface

The URL should be: http://xxx:8080/openhab.app?sitemap=demoWhere xxx is the local IP address of the pcDuino3B.

iOS APP

Bluetooth 4.0 Point to Point • Before we configure the openHAB, lets

prepare a pair of BLE shield and configure one of them to be host and the other to be slave.

• Baud rate 9600• AT+ROLE? To check role• AT+ROLE0 to set to slave• AT+ROLE1 to set to Host• The role will stay after power cycle• We may need to do AT+RENEW to clear

all previous settings• After set role, they will automatically

pair

Bluetooth 4.0 Point to Point • When we configure

the BLE module, we should jump the UART of the BLE module to softserial of Arduino (D2,D3)

• Load the script shown in next slides

• Open serial terminal from Arduino IDE and enter ‘AT+RENEW’ and then ‘AT+ROLE1’ to set host, and ‘AT+ROLE0’ to set slave.

Bluetooth 4.0 Point to Point #include <SoftwareSerial.h> #define RxD 2#define TxD 3SoftwareSerial mySerial(RxD,TxD); void setup(){ pinMode(RxD, INPUT); pinMode(TxD, OUTPUT); 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()) { Serial.print((char)mySerial.read()); } }

Load this script to Arduino, and then use serial terminal to enter AT commands to configure the BLE shields

openHAB server• Install the

BLE with host role to pcDuino3B

• Move the switch on the BLE shield to 3.3V as pcDuino3B is 3.3V

• Jump the UART to D0 and D1 as shown in picture

Remote Sensor Node • Install the BLE with

slave role to pcDuino3B

• Move the switch on the BLE shield to 5 as Arduino 5V

• Jump the UART to D6 and D7 as shown in the picture

• The door sensor is connected to D8 and GND.

• The LED is connected to D13 and GND as shown in the picture.

• Load the sensor code shown in next slides.

Sensor Arduino Code#include <SoftwareSerial.h> #define RxD 6#define TxD 7SoftwareSerial 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 }

Sensor Arduino Code 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"); } }

openHAB items and bindings

To add a device to OpenHAB, we need to configure four files:

/configurations/openhab.cfg/configurations/items/configurations/sitemap/configurations/rules/

/configurations/openhab.cfg

################################### MQTT Transport ########################################## mqtt:mymosquitto.url=tcp://localhost:1883 mqtt:mymosquitto.qos=0 mqtt:mymosquitto.retain=true mqtt:mymosquitto.async=true

/configurations/items/demo.items:

Number itm_garage_dist "Garage Dist [%.1f Inch]" (ALL) {mqtt="<[mymosquitto:4032:state:default]"} Contact itm_my_gar_door "Garage Door Status [MAP(en.map):%s]" <garage> (ALL)

/configurations/sitemap/demo.sitemap:

Frame label="Garage" {Text item=itm_my_gar_door label="Garage Door Status [%s]"}

/configurations/rules/:rule "Convert Door"when Item itm_garage_dist received update then if(itm_garage_dist.state < 19) { itm_my_gar_door.state = OPEN } else { itm_my_gar_door.state = CLOSED } end

Install MQTT broker “Mosquitto” on pcDuino3/3B for OpenHAB

This MQTT broker bridges the data sent from Arduino sensor in the field to the OpenHAB’s MQTT server.

Arduino sensors in the field can send back data in any format to pcDuino. We can use Arduino-style programming on pcDuino to receive the data, and send the data to TCP port 10000. This MQTT broker will listen to port 10000 and converts the data to MQTT protocol. Then send that data to the MQTT server in OpenHAB.

Install Paho MQTT

$sudo apt-get update$sudo apt-get install mosquitto mosquitto-clients python-mosquitto$sudo apt-get install python-pip$sudo pip install paho-mqtt

Sample Script (Not we will use today)

import socketimport sysimport paho.mqtt.publish as publish

publish.single("4032", "05",hostname="192.168.1.39")

Actual MQTT agent import socket import sys import paho.mqtt.publish as publish import paho.mqtt.client as mqtt import serial

with open("/sys/devices/virtual/misc/gpio/mode/gpio0",'w') as UART_RX: UART_RX.write('3') with open("/sys/devices/virtual/misc/gpio/mode/gpio1",'w') as UART_TX: UART_TX.write('3')

myport=serial.Serial('/dev/ttyS1',9600,timeout=20) myport.write('test') ###

Actual MQTT agent 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)

Actual MQTT agent ### 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) print >>sys.stderr, 'starting up on %s port %s' % server_address sock.bind(server_address) # Listen for incoming connections sock.listen(1)

Actual MQTT agent ################ 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()

Actual MQTT agent 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: #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)

Kick off MQTT agent After the openHAB server starts

up, run python MQTT agent by typing: sudo python ./mqtt_pubish.py

Then we can control the LED and see the status of door sensor on the web browse r and iOS app

Seed funding and incubator program pcDuino works with LaunchHill to

provide seed funding and incubator space in Shenzhen, China to help makers launch their own businesses.

Inquiries can be made to: harry.yu@launchhill.com.

Connect with pcDuino

Facebook.com/linksprite