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.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
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.
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
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.
################################### 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.
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')
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.