esp32 使用mqtt 通讯

 Golang  2024-03-30  admin  786  1061

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,两个客户端就可以相互通讯了

如果文章对您有帮助,点击下方的广告,支持一下作者吧!

转载必须注明出处:

esp32 使用mqtt 通讯 —— code.cent123.com

相关推荐


GoFrame 框架中使用 mqtt 协议通讯

GoFrame 框架中使用 mqtt//SPDX-License-Identifier:MIT //SPDX-FileCopyrightText:2022mochi-mqtt,mochi-co //SPDX-FileContributor:mochi-co packagemqtt import( "flag" "gfmqtt/internal/

workerman是一个高性能的PHP socket 服务器框架

Workerman是一款开源高性能异步PHP socket即时通讯框架。支持高并发,超高稳定性,被广泛的用于手机app、移动通讯,微信小程序,手游服务端、网络游戏、PHP聊天室、硬件通讯、智能家居、车联网、物联网等领域的开发。 支持TCP长连接,支持Websocket、HTTP等协议,支持自定义协议。拥有异步Mysql、异步Redis、异步Http、MQTT物联网客户端、异步消息队列等众多高性能组