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