esp32 使用mqtt 通讯
基于go的mqtt 客户端
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" "github.com/gogf/gf/v2/frame/g" "gobot.io/x/gobot" "gobot.io/x/gobot/platforms/mqtt" "time" ) func main() { mqttAdaptor := mqtt.NewAdaptor("tcp://0.0.0.0:1883", "pinger") work := func() { mqttAdaptor.On("hello", func(msg mqtt.Message) { fmt.Println(time.Now().Format(time.TimeOnly), "msg=", msg.Topic(), msg.MessageID(), msg.Duplicate(), msg.Retained(), msg.Qos(), string(msg.Payload())) }) mqttAdaptor.On("hola", func(msg mqtt.Message) { fmt.Println("msg2=", msg) }) mqttAdaptor.On("myLed", func(msg mqtt.Message) { fmt.Println("myLed=", string(msg.Payload())) }) mqttAdaptor.On("result", func(msg mqtt.Message) { fmt.Println("result=", string(msg.Payload())) }) //data := []byte("测试") data := []byte("{\"name\":\"qqq\",\"age\":100}") gobot.Every(3*time.Second, func() { mqttAdaptor.Publish("hello", data) _, _ = mqttAdaptor.PublishWithQOS("result", 0, []byte(gjson.New(g.MapStrInt{"result": 1, "time": 2000}).String())) time.Sleep(1 * time.Second) }) /*gobot.Every(5*time.Second, func() { mqttAdaptor.Publish("hola", data) })*/ /*gobot.Every(5*time.Second, func() { mqttAdaptor.PublishAndRetain("abc", data) }) data2 := []byte("{\"name\":\"pinger\",\"age\":12}") gobot.Every(5*time.Second, func() { token, _ := mqttAdaptor.PublishWithQOS("hello", 1, data2) token.Wait() token.WaitTimeout(1) })*/ //data3 := []byte("{\"blue\":1,\"time\":2000}") gobot.Every(20*time.Second, func() { //token, _ := mqttAdaptor.PublishWithQOS("myLed", 1, data3) //token.Wait() //token.WaitTimeout(1) _, _ = mqttAdaptor.PublishWithQOS("myLed", 0, []byte(gjson.New(g.MapStrInt{"red": 1, "time": 2000}).String())) time.Sleep(5 * time.Second) _, _ = mqttAdaptor.PublishWithQOS("myLed", 0, []byte(gjson.New(g.MapStrInt{"blue": 1, "time": 2000}).String())) time.Sleep(2 * time.Second) token, _ := mqttAdaptor.PublishWithQOS("myLed", 0, []byte(gjson.New(g.MapStrInt{"green": 1, "time": 2000}).String())) time.Sleep(3 * time.Second) token.Wait() token.WaitTimeout(1) _, _ = mqttAdaptor.PublishWithQOS("myLed", 0, []byte(gjson.New(g.MapStrInt{"red": 1, "blue": 1, "green": 1, "time": 2000}).String())) time.Sleep(3 * time.Second) _, _ = mqttAdaptor.PublishWithQOS("myLed", 0, []byte(gjson.New(g.MapStrInt{"blue": 1, "green": 1, "time": 2000}).String())) time.Sleep(2 * time.Second) _, _ = mqttAdaptor.PublishWithQOS("myLed", 0, []byte(gjson.New(g.MapStrInt{"red": 1, "green": 1, "time": 3000}).String())) time.Sleep(2 * time.Second) _, _ = mqttAdaptor.PublishWithQOS("myLed", 0, []byte(gjson.New(g.MapStrInt{"red": 1, "blue": 1, "green": 1, "time": 2000}).String())) time.Sleep(3 * time.Second) _, _ = mqttAdaptor.PublishWithQOS("result", 0, []byte(gjson.New(g.MapStrInt{"red": 1, "blue": 1, "green": 1, "time": 2000}).String())) time.Sleep(1 * time.Second) }) } robot := gobot.NewRobot("mqttBot", []gobot.Connection{mqttAdaptor}, work, ) robot.Start() }
基于 toit esp32开发语言的mqtt客户端
import mqtt import encoding.json import gpio CLIENT-ID ::= "esp32qqq" // Use a random client ID. HOST ::= "192.168.1.85" TOPIC ::= "myLed" main: red := gpio.Pin 32 --output green := gpio.Pin 33 --output blue := gpio.Pin 25 --output client := mqtt.Client --host=HOST --port=1883 client.start --client-id=CLIENT-ID payload2 := json.encode { "value": "ok" } client.subscribe TOPIC:: | topic/string payload/ByteArray | decoded := json.decode payload print "Received value on '$topic': $decoded" if topic=="myLed": if decoded.contains "red" and decoded["red"]==1: red.set 1 //sleep --ms=(decoded["time"] >0 ? decoded["time"] : 1000) //red.set 0 if decoded.contains "blue" and decoded["blue"]==1: blue.set 1 //sleep --ms=(decoded["time"] >0 ? decoded["time"] : 1000) //blue.set 0 if decoded.contains "green" and decoded["green"]==1: green.set 1 sleep --ms=(decoded["time"] >0 ? decoded["time"] : 1000) red.set 0 blue.set 0 green.set 0 client.publish "result" payload2 //sleep --ms=(decoded["time"] >0 ? decoded["time"] : 1000) //green.set 0 //client.publish TOPIC payload2 //while true: //client.publish TOPIC payload //sleep --ms=1000 //client.publish TOPIC payload //client.close
同时连接服务代理的mqtt,两个客户端就可以相互通讯了