Compare commits

...

8 Commits

Author SHA1 Message Date
c9a1252a0c thay doi realy status cho 201 2020-04-23 10:08:17 +07:00
f7b675434d . 2020-04-22 14:51:07 +07:00
344a7e367f . 2020-04-22 14:43:13 +07:00
d1d9c41418 . 2020-04-22 14:34:02 +07:00
8a7f93f1f1 . 2020-04-22 14:23:03 +07:00
cf09ee5fe9 . 2020-04-22 14:16:17 +07:00
daa3e0660d bo log 2020-04-22 13:45:59 +07:00
a038d3d801 update code cho khoa hessun Hai Phong 2020-04-22 10:37:28 +07:00
6 changed files with 94 additions and 39 deletions

BIN
GPIO.rar Normal file

Binary file not shown.

BIN
GPIO.zip

Binary file not shown.

View File

@ -1 +1,9 @@
{"servers": {"name": "MQTT local","host": "tcp://192.168.0.89:1883","user": "beetai","pass": "Admin@123"},"clients": {"name": "Door 1","topic": "device/gpio/1","active": 1}}
{
"servers": {
"name": "MQTT local",
"host": "tcp://192.168.10.200:1883",
"user": "beetai",
"pass": "Admin@123"
},
"clients": { "name": "Door 1", "topic": "device/gpio/1", "active": 1 }
}

BIN
GPIO/GPIO

Binary file not shown.

View File

@ -38,6 +38,12 @@ var CMS_PASS_BI = ""
var CMS_TOPIC_IN = ""
var CMS_TOPIC_OUT = ""
// Counter number reconnect publish
var number_repub = 0
// Counter number reconnect subcriber
var number_resub = 0
// const CMS_HOST_BI = "tcp://broker.beetai.com:21883"
// const CMS_ACCESS_TOKEN_BI = "beeetai"
@ -51,12 +57,18 @@ var CMS_TOPIC_OUT = ""
// var CMS_TOPIC_IN = "gpio/devices/beetin/in/+"
// var CMS_TOPIC_OUT = "gpio/devices/beetin/out/+"
var time_now int = 0
var Name string = ""
var door_state bool
// Check mqtt connect
var stt_mqtt = false
var num_reconnect_pub = 0
var num_reconnect_sub = 0
var (
// Use mcu pin 10, corresponds to physical pin 19 on the pi
relay = rpio.Pin(6) // 6- raspberry pi 1 / 9 - raspberry 3B+
relay = rpio.Pin(6) // (10.200)GPIO 6- raspberry pi 3 === GPIO 9 - raspberry 3B+ (10.201)
)
// {
@ -138,8 +150,9 @@ func LoadConfig(path string) {
}
func main() {
// Bat debug
//mqtt.DEBUG = log.New(os.Stderr, "DEBUG ", log.Ltime)
time.Sleep(7 * time.Second)
// mqtt.DEBUG = log.New(os.Stderr, "DEBUG ", log.Ltime)
fmt.Println("======== START MAIN ==========")
time.Sleep(1 * time.Second)
if state_read_ecf == false {
LoadConfig(Path_config)
fmt.Println("Read config done")
@ -154,9 +167,9 @@ func main() {
}
fmt.Println(door)
mqtt_begin()
//SetConnectionLostHandler(connLostHandler)
mqtt_cms_bi.Subscribe(CMS_TOPIC_IN, 0, mqtt_messageHandler)
// mqtt_cms_bi.Subscribe(CMS_TOPIC_IN, 0, mqtt_messageHandler)
// Open and map memory to access gpio, check for errors
if err := rpio.Open(); err != nil {
@ -169,21 +182,21 @@ func main() {
defer rpio.Close()
// Set pin to output mode
relay.Output()
relay.PullUp()
//relay.PullUp()
relay.PullDown()
// Toggle pin 20 times
//go Check_door()
mqtt_begin()
for {
Check_door()
}
}
func connLostHandler(c mqtt.Client, err error) {
fmt.Printf("Connection lost, reason: %v\n", err)
mqtt_begin()
}
func Check_door() {
if door_state == true {
time.Sleep(time.Second * time_hold)
relay.Low()
relay.Low() // 201
//relay.High()
if Debug == 1 {
fmt.Println("Close Door")
fmt.Println("--------------------------------")
@ -201,48 +214,53 @@ func mqtt_begin() {
opts_cms_bi.SetUsername(CMS_ACCESS_TOKEN_BI)
opts_cms_bi.SetPassword(CMS_PASS_BI)
opts_cms_bi.SetCleanSession(true)
mqtt_cms_bi = mqtt.NewClient(opts_cms_bi)
opts_cms_bi.SetConnectionLostHandler(MQTTLostConnectHandler)
opts_cms_bi.SetOnConnectHandler(MQTTOnConnectHandler)
var reconect = false
for reconect == false {
if token_1 := mqtt_cms_bi.Connect(); token_1.Wait() && token_1.Error() == nil {
//file.Write_log("MQTT CMS Beetsoft Connected\n", path_log)
if Debug == 1 {
fmt.Printf("MQTT CMS Beetsoft Connected\n")
}
reconect = true
} else {
//file.Write_log("MQTT CMS Beetsoft cant not Connected\n", path_log)
if Debug == 1 {
fmt.Printf("MQTT CMS Beetsoft cant not Connected\n")
fmt.Printf("Loi CMS Beetsoft : %v \n", token_1.Error())
fmt.Printf("-------------------\n")
}
reconect = false
mqtt_cms_bi = mqtt.NewClient(opts_cms_bi)
if token_1 := mqtt_cms_bi.Connect(); token_1.Wait() && token_1.Error() == nil {
//file.Write_log("MQTT CMS Beetsoft Connected\n", path_log)
if Debug == 1 {
fmt.Printf("MQTT Server Connected\n")
}
} else {
//file.Write_log("MQTT CMS Beetsoft cant not Connected\n", path_log)
if Debug == 1 {
fmt.Printf("MQTT Server cant not Connected\n")
fmt.Printf("Loi Server : %v \n", token_1.Error())
fmt.Printf("-------------------\n")
}
}
opts_cms_bi.OnConnect = func(c mqtt.Client) {
fmt.Printf("Client connected, subscribing to: " + CMS_TOPIC_IN)
c.Subscribe(CMS_TOPIC_IN, 0, mqtt_messageHandler)
if token := mqtt_cms_bi.Subscribe(CMS_TOPIC_IN, 0, mqtt_messageHandler); token.Wait() && token.Error() != nil {
fmt.Println(token.Error())
} else {
fmt.Println("Subcriber is MQTTOnConnectHandler () OKIE ")
}
// opts_cms_bi.OnConnect = func(c mqtt.Client) {
// fmt.Printf("Client connected, subscribing to: " + CMS_TOPIC_IN)
// c.Subscribe(CMS_TOPIC_IN, 0, mqtt_messageHandler)
// }
}
func MQTTLostConnectHandler(c mqtt.Client, err error) {
// file.Write_log("MQTT CMS Beetsoft Lost Connect\n", box.Path_log_luncher)
fmt.Println("MQTT CMS Beetsoft Lost Connect\n")
fmt.Println("MQTT SERVER Lost Connect\n")
fmt.Println(err)
}
func MQTTOnConnectHandler(client mqtt.Client) {
// file.Write_log("Reconnect: MQTT_OnConnectHandler\n", box.Path_log_luncher)
fmt.Println("Reconnect: MQTT_OnConnectHandler\n")
mqtt_cms_bi.Unsubscribe(CMS_TOPIC_IN)
time.Sleep(10)
mqtt_cms_bi.Subscribe(CMS_TOPIC_IN, 0, mqtt_messageHandler)
fmt.Println("Recall function Subscribe MQTT\n")
//mqtt_cms_bi.Unsubscribe(CMS_TOPIC_IN)
//time.Sleep(10)
//mqtt_cms_bi.Subscribe(CMS_TOPIC_IN, 0, mqtt_messageHandler)
if token := client.Subscribe(CMS_TOPIC_IN, 0, mqtt_messageHandler); token.Wait() && token.Error() != nil {
fmt.Println(token.Error())
} else {
fmt.Println("Subcriber is MQTTOnConnectHandler () OKIE ")
}
//fmt.Println("Recall function Subscribe MQTT\n")
// file.Write_log("Recall function Subscribe MQTT\n", box.Path_log_luncher)
}
@ -256,6 +274,26 @@ func mqtt_messageHandler(mqtt_bi mqtt.Client, message mqtt.Message) {
fmt.Printf("TOPIC: %s\n", message.Topic())
fmt.Printf("MSG:\n %s\n", message.Payload())
}
if string(message.Payload()) == `{"method":"gpio","params":"1"}` {
fmt.Println("MO KHOA")
if door_state == true {
} else {
relay.High() // 201
//relay.Low()
//Name = (list["name"]).(string)
fmt.Println("Xin Chao Mung " + Name)
}
//msg := `{"method":"gpio","params":"1"}`
//CmsResponse(mqtt_bi, topic, msg)
if Debug == 1 {
fmt.Println("Open Door")
fmt.Println("--------------------------------")
}
door_state = true
}
//file.Write_log(string(message.Topic()), path_log)
//file.Write_log(string(message.Payload()), path_log)
dec := json.NewDecoder(bytes.NewReader(message.Payload()))
@ -279,7 +317,8 @@ func mqtt_messageHandler(mqtt_bi mqtt.Client, message mqtt.Message) {
if door_state == true {
} else {
relay.High()
relay.High() //201
//relay.Low()
Name = (list["name"]).(string)
fmt.Println("Xin Chao Mung " + Name)
}

8
GPIO/install_mqtt.sh Normal file
View File

@ -0,0 +1,8 @@
#! /bin/bash
sudo apt-get update
sudo apt-get install mosquitto
echo "user: beetai ; Nhap pass: Admin@123"
sudo mosquitto_passwd -c /etc/mosquitto/passwd beetai
echo "allow_anonymous false \n password_file /etc/mosquitto/passwd" >> /etc/mosquitto/conf.d/default.conf
sudo systemctl restart mosquitto
exit 0