2025年5月21日水曜日

Gobot/mosquitto (Raspi,UNO,66 all ok?)

mosquitto_sub -h localhost -t test :: -d で -h localhostに置き換え可
mosquitto_pub -d -t test -m "sheeptest" sheepstestでもOK

// mosquitttodが動いていればmosquitto_sub -d -t hola でoをうけとるはず(topic is hola) package main 
 import ( "gobot.io/x/gobot" "gobot.io/x/gobot/drivers/gpio" "gobot.io/x/gobot/platforms/raspi" "gobot.io/x/gobot/platforms/mqtt" "fmt" "time" ) 
 func main() { 
   r := raspi.NewAdaptor() 
   led := gpio.NewLedDriver(r, "7") 
   mqttAdaptor := mqtt.NewAdaptor("localhost:1883", "pinger")
 // pinger canbe any , no topic 
 work := func() { 
   mqttAdaptor.On("hello", func(msg mqtt.Message) { fmt.Println(msg) }) 
 // receive topic hello 
 mqttAdaptor.On("hola", func(msg mqtt.Message)
  { fmt.Println(msg) fmt.Printf("%T\n",msg) // msgはstringでないことを証明       led.Toggle() }) // receive topic hola 
 data := []byte("o")
 gobot.Every(1*time.Second, func() { mqttAdaptor.Publish("hello", data) })   gobot.Every(5*time.Second, func() { mqttAdaptor.Publish("hola", data) }) } 
 // helloには1秒毎に、holaには5秒毎にパブリッシュ 
 robot := gobot.NewRobot("mqttBot", 
   []gobot.Connection{r}, 
   []gobot.Device{led},      
   []gobot.Connection{mqttAdaptor},
   work, ) 

 robot.Start() }

0 件のコメント:

コメントを投稿