Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
155 changes: 155 additions & 0 deletions do_chi_thanh/OpenHAB/HPCC_IOT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
# IOT Platform OpenHAB và kịch bản triển khai
- Hệ thống được cài đặt trên zone_3 (phòng làm việc của sinh viên).
- Hệ thống gồm 2 Fog Node và 1 Pi3 cài đặt OpenHAB để quản lý các Fog Node.

## Kịch bản cài đặt
- Hệ thống gồm có:
- 02 Arduino
- 02 Esp 8266
- 01 Raspberry Pi 3 cài OpenHAB
- 01 cảm biến chuyển động
- 01 cảm biến ánh sáng
- 01 cảm biến nhiệt ẩm
- 03 đèn LED: vàng, đỏ, xanh
- Chương trình điều khiển thực hiện kịch bản bật tắt đèn khi có chuyển động đi qua.

  - Kịch bản sử dụng
- Khi cảm biến chuyển động phát hiện có chuyển động, chương trình điều khiển sẽ thực hiện bật một trong 3 đèn: đèn xanh khi cường độ sáng lớn (tương ứng giá trị của cảm biến ánh sáng >= 900); đèn đỏ khi cường độ sáng trung bình (giá trị >=500, <900); đèn vàng khi cường độ sáng yếu (giá trị <500)

## Cài đặt hệ thống

### Sơ đồ cài đặt
![Image of OpenHAB system](https://github.com/haiquan5396/K59_training/raw/master/HaiQuan/Homeassistant/image/serverRoom.jpg)


### Arduino
- Arduino được sử dụng để nhận các giá trị từ cảm biến và đẩy dữ liệu cho ESP 8266 thông qua các chân RX, TX.
### ESP 8266
- Broker: cài docker image mqtt trên Pi 3 làm một broker tại địa chỉ IP 192.168.60.197
- docker run -itd --name mosquitto -p 1883:1883 haiquan5396/mqtt
- ESP 8266 chuyển dữ liệu lên/nhận lệnh từ broker thông qua giao thức MQTT (Mosquito) vào các topic publish, subscribe.

### OpenHAB
- IOT Platform OpenHAB được cài đặt trên Pi 3 nhận dữ liệu/gửi lệnh cho ESP 8266 thông qua các topic subscribe, publish tương ứng.
- Cài đặt MQTT Binding; JSON Binding trên OpenHAB PaperUI.
- Cầu hình broker cho MQTT binding bằng cách thêm 1 file default.service vào thư mục service của OpenHAB:
```
mybroker.url=tcp://192.168.60.197:1883
```
Khi này, các gói tin MQTT sẽ được chuyển đến broker 192.168.60.197 cổng 1883.
- Các Items có trong hệ thống
- 3 đèn tương ứng với 3 Switch (Switch 1 - đèn vàn; Switch 2 - đèn đỏ; Switch 3 - đèn xanh). Mỗi đèn khi được bật/tắt sẽ gửi về 1 topic trên broker 192.168.60.197 (topic publish: zone_3/box_1/led).
- Cảm biến chuyển động (topic subscribe: zone_3/box_1/motion/id_1).
- Cảm biến ánh sáng (topic subscribe: zone_3/box_1/light/id_1
- Cảm biến nhiệt độ, độ ẩm (topic subscribe: zone_3/box_1/temp/id_1).

    - Data format:
- Của OpenHAB gửi cho đèn: {"name": "Tên đèn", "value": "giá trị"}
    - Của esp 8266 gắn với arduino cảm biến chuyển động gửi cho broker: {"motion": "giá trị"}
  - Của esp 8266 gắn với arduino cảm biến nhiệt độ, độ ẩm gửi cho broker: {"humidity":"độ ẩm", "temperature":"nhiệt độ"}
        - Của esp 8266 gắn với arduino cảm biến ánh sáng gửi cho broker: {"light": "giá trị"} (giá trị thuộc tập: {0, 1, 2} với 0 ứng với giá trị analog <500; 1 ứng với gíá trị analog >=500 và <900; 2 ứng với giá trị analog >=900)
```
Switch Switch1 <light> {mqtt=">[mybroker:zone_3/box_1/led:command:ON:{\"vang\"\\:\"ON\"}],>[mybroker:zone_3/box_1                                                                                        /led:command:OFF:{\"vang\"\\:\"OFF\"}]"}

Switch Switch2 <light> {mqtt=">[mybroker:zone_3/box_1/led:command:ON:{\"do\"\\:\"ON\"}],>[mybroker:zone_3/box_1/led:command:OFF:                                                                                         {\"do\"\\:\"OFF\"}]"}

Switch Switch3 <light> {mqtt=">[mybroker:zone_3/box_1/led:command:ON:{\"xanh\"\\:\"ON\"}],>[mybroker:zone_3/box_1                                                                                                   /led:command:OFF:{\"xanh\"\\:\"OFF\"}]"}

Number Temperature "Temperature: [%.1f oC]" <temperature> {mqtt="<[mybroker:zoner_3/box_1                                                                                                                             /temp/id_1:state:JSONPATH($.temperature)]"}

Number Humidity "Humidity: [%.1f ]" <humidity> {mqtt="<[mybroker:zoner_3/box_1/temp/id_1:state:JSONPATH($.humidity)]"}

Number Motion "Motion: [ %d ]" <motion> {mqtt="<[mybroker:zone_3/box_1/motion/id_1:state:JSONPATH($.motion)]"}

Number Light "Light: [%d ]" <light> {mqtt="<[mybroker:zoner_3/box_1/light/id_1:state:JSONPATH($.light)]"}
```
- Chương trình điều khiển đọc giá trị trả về từ cảm biến ánh sáng trên broker và ra lệnh cho OpenHAB bật các đèn tương ứng theo kịch bản sử dụng bằng cách sử dụng API của OpenHAB.
```
from openhab import openHAB
import paho.mqtt.client as mqtt
import json
import time


base_url = 'http://localhost:8080/rest'
openhab = openHAB(base_url)

# fetch all items
items = openhab.fetch_all_items()
# print (items)

item1 = openhab.get_item("Switch1")
item2 = openhab.get_item("Switch2")
item3 = openhab.get_item("Switch3")

temp_item = openhab.get_item("Temperature")
light_item = openhab.get_item("Light")
light_state = light_item.state

topic_sub_sensor = "zone_3/box_1/motion/id_1"
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))

# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
client.subscribe(topic_sub_sensor)

# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
print(msg.topic+" "+str(msg.payload))
global pre_motion

mes = json.loads(str(msg.payload))
mes = mes['motion']

if (mes == 1 and pre_motion == 0):
# pre_motion = mes
light_state = light_item.state
print (light_state)
if (light_state == 0): # LDRReading < 500
item1.on() # Bat den vang
time.sleep(0.5)
item2.off()
time.sleep(0.5)
item3.off()
print ("Vang: ON, Do: OFF, Xanh: OFF")
elif (light_state == 1): # LDRReading >= 500 và <900
item1.off()
time.sleep(0.5)
item2.on() # Bat den do
time.sleep(0.5)
item3.off()
print ("Vang: OFF, Do: ON, Xanh: OFF")
elif (light_state == 2): # LDRReading >= 900
item1.off()
time.sleep(0.5)
item2.off()
time.sleep(0.5)
item3.on() # Bat den xanh
print ("Vang: OFF, Do: OFF, Xanh: ON")
else:
print ("ERROR!")
exit()

client = mqtt.Client()
pre_motion = 0
pre_temp = 0

client.on_connect = on_connect
client.on_message = on_message

client.connect("192.168.60.197", 1883, 60)
client.loop_forever()
```
- Chương trình sử dụng thư viện paho mqtt để gửi, nhận dữ liệu với broker và thư viện Python-OpenHAB để gọi API của OpenHAB. Chương trình được lặp vô hạn, tại mỗi vòng lặp, các hàm on_connect và on_message subscribe dữ liệu từ broker và xử lý, ra lệnh cho OpenHAB qua API theo kịch bản sử dụng.

### Các giá trị trả về của cảm biến
- Cảm biến chuyển động: Trả về 0 nếu không phát hiện chuyển động, trả về 1 nếu phát hiện chuyển động
- Cảm biến ánh sáng: Trả về giá trị 0-1023, gía trị càng lớn tức cường độ ánh sáng càng lớn.
- Cảm biến nhiệt độ độ ẩm: Trả về 2 giá trị nhiệt độ (độ C) và độ ẩm (%)
- 03 đèn LED: vàng, đỏ, xanh.

### NOTE:
- Cách lắp mạch, mã nguồn của Arduino và ESP 8266 có trong file đính kèm.
- OpenHAB API xem trong file OpenHAB_API

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added do_chi_thanh/OpenHAB/OpenHAB API.xlsx
Binary file not shown.
65 changes: 65 additions & 0 deletions do_chi_thanh/OpenHAB/cam_bien_anh_sang_arduino.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include <SoftwareSerial.h>
#include <DHT.h>
#include <Adafruit_Sensor.h>
#include "ArduinoJson.h"

SoftwareSerial mySerial(10, 11); // RX, TX

const int DHTPIN = 2;
const int DHTTYPE = DHT11; // Khai bao loai cam bien, co 2 loai la DHT11 va DHT22
DHT dht(DHTPIN, DHTTYPE);
const int LDR_Pin = A0;

void setup(){
Serial.begin(115200);
mySerial.begin(115200);
}

StaticJsonBuffer<200> jsonBuffer;
JsonObject& json_buffer = jsonBuffer.createObject();
char buffer_temp[200];

int LDRReading = 0;
int temperature = 0;
int humidity = 0;

void loop()
{
LDRReading = analogRead(LDR_Pin);
Serial.println(LDRReading);

if (LDRReading < 500){
LDRReading = 0;
}
else if (LDRReading >= 500 && LDRReading < 900){
LDRReading = 1;
}
else{
LDRReading = 2;
}



humidity = dht.readHumidity(); //Doc do am
temperature = dht.readTemperature(); // Doc nhiet do
Serial.print("Nhiet do: ");
Serial.println(temperature);
Serial.print("Do am: ");
Serial.println(humidity);
Serial.println();

json_buffer["light"] = LDRReading;
json_buffer["temperature"] = temperature;
json_buffer["humidity"] = humidity;

json_buffer.printTo(buffer_temp, sizeof(buffer_temp));
// Serial.println(buffer_temp);
mySerial.write(LDRReading); // Gui cho Esp8266
mySerial.write(temperature);
mySerial.write(humidity);
// mySerial.write(buffer_temp);

delay(1000);

}

123 changes: 123 additions & 0 deletions do_chi_thanh/OpenHAB/cam_bien_chuyen_dong_arduino.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
#include <SoftwareSerial.h>
#include <ArduinoJson.h>
SoftwareSerial mySerial(10, 11); // RX, TX

int ledXanh = 3; // chọn chân 13 báo hiệu LED
int ledDo = 4;
int ledVang = 5;

int inputPin = 2; // chọn ngõ tín hiệu vào cho cảm biến chuyển động
int val = 0;

void setup(){
pinMode(ledXanh, OUTPUT);
pinMode(ledDo, OUTPUT);
pinMode(ledVang , OUTPUT);
pinMode(inputPin, INPUT);
Serial.begin(9600);
mySerial.begin(115200);
}


StaticJsonBuffer<200> jsonBuffer;
JsonObject& json_buffer = jsonBuffer.createObject();
char buffer_motion[200];

void loop(){
val = digitalRead(inputPin); // đọc giá trị đầu vào.


if(mySerial.available() > 0){
StaticJsonBuffer<200> jsonBufferLed;
String ledString = mySerial.readString();
Serial.println(ledString);

int index=0, pre_index=0;
int num_led=3;
int i=0;
String subLedString;

// Tach tung json trong message
while (i<num_led){
index = ledString.indexOf("}", index+1);
subLedString = ledString.substring(pre_index, index+1);
pre_index = index+1;
Serial.println(subLedString);
JsonObject& ledJson = jsonBufferLed.parseObject(subLedString); // Den xanh

/* OpenHAB*/
if(strcmp(ledJson["vang"],"ON")==0){
digitalWrite(ledVang,HIGH);
Serial.println("Vang On");
}
else if (strcmp(ledJson["vang"], "OFF")==0){
digitalWrite(ledVang,LOW);
Serial.println("Vang OFF");
}

if(strcmp(ledJson["do"],"ON")==0){
digitalWrite(ledDo,HIGH);
Serial.println("Do ON");
}
else if (strcmp(ledJson["do"], "OFF")==0){
digitalWrite(ledDo,LOW);
Serial.println("Do OFF");
}

if(strcmp(ledJson["xanh"],"ON")==0){
digitalWrite(ledXanh,HIGH);
Serial.println("Xanh ON");
}
else if (strcmp(ledJson["xanh"], "OFF")==0){
digitalWrite(ledXanh,LOW);
Serial.println("xanh OFF");
}

//////////////////////////////////
/* Home Assistant*/
if(strcmp(ledJson["name"],"Green Light")==0){
if (strcmp(ledJson["value"], "ON") == 0){
digitalWrite(ledXanh,HIGH);
Serial.println("Xanh On");
}
else{
digitalWrite(ledXanh,LOW);
Serial.println("Xanh OFF");
}
}

if(strcmp(ledJson["name"],"Yellow Light")==0){
if (strcmp(ledJson["value"], "ON") == 0){
digitalWrite(ledVang,HIGH);
Serial.println("Vang On");
}
else{
digitalWrite(ledVang,LOW);
Serial.println("Vang OFF");
}
}

if(strcmp(ledJson["name"],"Red Light")==0){
if (strcmp(ledJson["value"], "ON") == 0){
digitalWrite(ledDo,HIGH);
Serial.println("Do On");
}
else{
digitalWrite(ledDo,LOW);
Serial.println("Do OFF");
}
}
i++;
}
}

json_buffer["motion"] = val;
json_buffer.printTo(buffer_motion, sizeof(buffer_motion));
Serial.println(buffer_motion);
//client.publish(mqtt_topic_pub, buffer_motion);
mySerial.write(val);
delay(3000);


}

52 changes: 52 additions & 0 deletions do_chi_thanh/OpenHAB/cam_bien_nhiet_do_do_am_dht11.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <SoftwareSerial.h>
#include <DHT.h>
#include <Adafruit_Sensor.h>
#include "ArduinoJson.h"

SoftwareSerial mySerial(10, 11); // RX, TX

const int DHTPIN = 2;
const int DHTTYPE = DHT11; // Khai bao loai cam bien, co 2 loai la DHT11 va DHT22
DHT dht(DHTPIN, DHTTYPE);


int ledXanh = 3; // chọn chân 13 báo hiệu LED
int ledDo = 4;
int ledVang = 5;

void setup(){
Serial.begin(9600);
mySerial.begin(115200);
}


int temperature = 0;
int humidity = 0;

StaticJsonBuffer<200> jsonBuffer;
JsonObject& json_buffer = jsonBuffer.createObject();
char buffer_temp[200];

void loop()
{

float h = dht.readHumidity(); //Doc do am
float t = dht.readTemperature(); // Doc nhiet do
Serial.print("Nhiet do: ");
Serial.println(t);
Serial.print("Do am: ");
Serial.println(h);

json_buffer["temperature"] = t;
json_buffer["humidity"] = h;

json_buffer.printTo(buffer_temp, sizeof(buffer_temp));

mySerial.write(t); // Gui cho esp8266
mySerial.write(h); // Gui cho esp8266
mySerial.write(buffer_temp);
Serial.println();
delay(10);

}

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading