forked from atotto/mibot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
37 lines (31 loc) · 841 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package main
import (
"log"
"os"
"bufio"
"fmt"
mqtt "github.com/eclipse/paho.mqtt.golang"
)
func main() {
opts := mqtt.NewClientOptions()
opts.AddBroker("tcp://localhost:1883")
c := mqtt.NewClient(opts)
if token := c.Connect(); token.Wait() && token.Error() != nil {
log.Fatalf("Mqtt error: %s", token.Error())
}
reader := bufio.NewReader(os.Stdin)
fmt.Println("send commands (w:forward d:left a:right s:backward enter:send ctrl-c:exit)")
// tc := time.Tick(200 * time.Millisecond)
for {
b, _ := reader.ReadByte()
msg := string(b)
if b != 0x0a {
fmt.Println("send:", msg, len(msg))
token := c.Publish("m5stack/control", 0, false, msg)
token.Wait()
}
// select {
// case <-tc:
// }
}
}