1 Star 0 Fork 0

江苏艾雨文承养老机器人有限公司/aywc_judge

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
iot.go 3.10 KB
一键复制 编辑 原始数据 按行查看 历史
dtal 提交于 2021-06-02 14:21 . mtt0408--aywc_1 更改工作目录
package main
import (
"bytes"
"crypto/tls"
"crypto/x509"
"fmt"
"gitee.com/aywc_1/aywc_judge/src/mqtt/lib"
MQTT "github.com/eclipse/paho.mqtt.golang"
"io/ioutil"
"os"
"time"
)
func main() {
// set the device info, include product key, device name, and device secret
var productKey = "a1Wi5Y7Wxh1"
var deviceName = "abc_01"
var deviceSecret = "5c715ef08fb20c888325dfbc31d29cce"
// set timestamp, clientid, subscribe topic and publish topic
var timeStamp = "1528018257135"
var clientId = "go_device_id_0001"
var pubTopic = "/" + productKey + "/" + deviceName + "/user/get"
// var subTopic string = "/" + productKey + "/" + deviceName + "/user/update1";
// set the login broker url
var raw_broker bytes.Buffer
raw_broker.WriteString("tls://")
raw_broker.WriteString(productKey)
raw_broker.WriteString(".iot-as-mqtt.cn-shanghai.aliyuncs.com:1883")
opts := MQTT.NewClientOptions().AddBroker(raw_broker.String())
// calculate the login auth info, and set it into the connection options
auth := lib.Calculate_sign(clientId, productKey, deviceName, deviceSecret, timeStamp)
opts.SetClientID(auth.MqttClientId)
opts.SetUsername(auth.Username)
opts.SetPassword(auth.Password)
opts.SetKeepAlive(60 * 2 * time.Second)
opts.SetDefaultPublishHandler(f)
// set the tls configuration
//tlsconfig := NewTLSConfig()
//opts.SetTLSConfig(tlsconfig)
// create and start a client using the above ClientOptions
c := MQTT.NewClient(opts)
if token := c.Connect(); token.Wait() && token.Error() != nil {
panic(token.Error())
}
// publish 5 messages to pubTopic("/a1Zd7n5yTt8/deng/user/update")
for i := 0; i < 5; i++ {
fmt.Println("publish msg:", i)
text := fmt.Sprintf("ABC #%d", i)
fmt.Println("pubTopic", pubTopic)
token := c.Publish("/ota/device/upgrade/a1Wi5Y7Wxh1/abc_01", 0, false, text)
fmt.Println("publish msg: ", text)
token.Wait()
time.Sleep(2 * time.Second)
}
time.Sleep(200 * time.Second)
//c.Disconnect(250)
}
func NewTLSConfig() *tls.Config {
// Import trusted certificates from CAfile.pem.
// Alternatively, manually add CA certificates to default openssl CA bundle.
certpool := x509.NewCertPool()
fmt.Println(os.Getwd())
pemCerts, err := ioutil.ReadFile("./src/mqtt/x509/root.pem")
if err != nil {
fmt.Println("0. read file error, game over!!")
}
certpool.AppendCertsFromPEM(pemCerts)
// Create tls.Config with desired tls properties
return &tls.Config{
// RootCAs = certs used to verify server cert.
RootCAs: certpool,
// ClientAuth = whether to request cert from server.
// Since the server is set up for SSL, this happens
// anyways.
ClientAuth: tls.NoClientCert,
// ClientCAs = certs used to validate client cert.
ClientCAs: nil,
// InsecureSkipVerify = verify that cert contents
// match server. IP matches what is in cert etc.
InsecureSkipVerify: false,
// Certificates = list of certs client sends to server.
// Certificates: []tls.Certificate{cert},
}
}
// define a function for the default message handler
var f MQTT.MessageHandler = func(client MQTT.Client, msg MQTT.Message) {
fmt.Printf("TOPIC: %s\n", msg.Topic())
fmt.Printf("MSG: %s\n", msg.Payload())
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/aywc_1/aywc_judge.git
git@gitee.com:aywc_1/aywc_judge.git
aywc_1
aywc_judge
aywc_judge
v0.6.4

搜索帮助