-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
120 lines (103 loc) · 2.52 KB
/
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright 2023 The Embedded Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Espat is an ESP-AT based TCP echo server. It uses the espat package directly
// instead of espat/espnet. See ../espnet for the example of the same TCP server
// implemented using a much more convenient interface of the espat/espnet
// package.
package main
import (
"fmt"
"strings"
"time"
"github.com/embeddedgo/espat"
"github.com/embeddedgo/imxrt/hal/lpuart"
"github.com/embeddedgo/imxrt/hal/lpuart/lpuart2"
"github.com/embeddedgo/imxrt/devboard/teensy4/board/pins"
)
func logErr(err error) bool {
if err != nil {
fmt.Println("error:", err.Error())
return true
}
return false
}
func fatalErr(err error) {
for err != nil {
fmt.Println("error:", err.Error())
time.Sleep(time.Second)
}
}
func main() {
// IO pins
espTx := pins.P14
espRx := pins.P15
// ESP-AT
u := lpuart2.Driver()
u.Setup(lpuart.Word8b, 115200)
u.UsePin(espRx, lpuart.RXD)
u.UsePin(espTx, lpuart.TXD)
u.EnableRx(512)
u.EnableTx()
time.Sleep(5 * time.Second) // to allow to see messages printed below
fmt.Print("Initializing ESP-AT module... ")
dev := espat.NewDevice("esp0", u, u)
fatalErr(dev.Init(true))
_, err := dev.Cmd("+CIPMUX=1")
fatalErr(err)
_, err = dev.Cmd("+CIPRECVMODE=1")
fatalErr(err)
fmt.Println("OK")
fmt.Println("Waiting for an IP address...")
for msg := range dev.Async() {
fatalErr(msg.Err)
fmt.Println(msg.Str)
if msg.Str == "WIFI GOT IP" {
break
}
}
txt, err := dev.CmdStr("+CIPSTA?")
fatalErr(err)
fmt.Println(strings.ReplaceAll(txt, "+CIPSTA:", ""))
const port = "1111"
dev.SetServer(true)
_, err = dev.Cmd("+CIPSERVER=1," + port)
fatalErr(err)
fmt.Println("Listen on :" + port)
for conn := range dev.Server() {
go handle(conn)
}
}
var welcome = []byte("Echo Server\n\n")
func handle(c *espat.Conn) {
fmt.Println("Connected:", c.ID)
if logErr(send(c, welcome)) {
return
}
var buf [64]byte
for {
if _, ok := <-c.Ch; !ok {
break // connection closed by remote part
}
n, err := c.Dev.CmdInt("+CIPRECVDATA=", buf[:], c.ID, len(buf))
if logErr(err) {
return
}
if logErr(send(c, buf[:n])) {
return
}
}
fmt.Println("Closed: ", c.ID)
}
func send(c *espat.Conn, p []byte) error {
c.Dev.Lock()
defer c.Dev.Unlock()
if _, err := c.Dev.UnsafeCmd("+CIPSEND=", c.ID, len(p)); err != nil {
return err
}
if _, err := c.Dev.UnsafeWrite(p); err != nil {
return err
}
_, err := c.Dev.UnsafeCmd("")
return err
}