update code cho khoa hessun Hai Phong

This commit is contained in:
HaiMD 2020-04-22 10:37:28 +07:00
parent 42bc76cef4
commit a038d3d801
6 changed files with 50 additions and 11 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(9) // 6- raspberry pi 1 / 9 - raspberry 3B+
)
// {
@ -154,9 +166,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,10 +181,14 @@ 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()
for {
if stt_mqtt == false {
mqtt_begin()
}
Check_door()
}
}
@ -183,7 +199,8 @@ func connLostHandler(c mqtt.Client, err error) {
func Check_door() {
if door_state == true {
time.Sleep(time.Second * time_hold)
relay.Low()
//relay.Low()
relay.High()
if Debug == 1 {
fmt.Println("Close Door")
fmt.Println("--------------------------------")
@ -239,10 +256,15 @@ func MQTTLostConnectHandler(c mqtt.Client, err error) {
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)
}
@ -279,7 +301,8 @@ func mqtt_messageHandler(mqtt_bi mqtt.Client, message mqtt.Message) {
if door_state == true {
} else {
relay.High()
//relay.High()
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