diff --git a/espat/adapter.go b/espat/adapter.go index 2af80bf..f10b743 100644 --- a/espat/adapter.go +++ b/espat/adapter.go @@ -11,7 +11,10 @@ func (d *Device) ConnectToAccessPoint(ssid, pass string, timeout time.Duration) return net.ErrWiFiMissingSSID } - d.SetWifiMode(WifiModeClient) + if err := d.SetWifiMode(WifiModeClient); err != nil { + return err + } + return d.ConnectToAP(ssid, pass, 10) } diff --git a/espat/espat.go b/espat/espat.go index 41a9893..ed2523b 100644 --- a/espat/espat.go +++ b/espat/espat.go @@ -152,17 +152,17 @@ func (d *Device) ReadSocket(b []byte) (n int, err error) { // The call will retry for up to timeout milliseconds before returning nothing. func (d *Device) Response(timeout int) ([]byte, error) { // read data - var size int var start, end int - pause := 100 // pause to wait for 100 ms - retries := timeout / pause + pause := 10 * time.Millisecond + starting := time.Now() for { - size = d.bus.Buffered() - - if size > 0 { + if size := d.bus.Buffered(); size > 0 { end += size - d.bus.Read(d.response[start:end]) + _, err := d.bus.Read(d.response[start:end]) + if err != nil { + return nil, err + } // if "+IPD" then read socket data if strings.Contains(string(d.response[:end]), "+IPD") { @@ -185,12 +185,11 @@ func (d *Device) Response(timeout int) ([]byte, error) { } // wait longer? - retries-- - if retries == 0 { + if time.Since(starting) > time.Duration(timeout)*time.Millisecond { return nil, errors.New("response timeout error:" + string(d.response[start:end])) } - time.Sleep(time.Duration(pause) * time.Millisecond) + time.Sleep(pause) } } diff --git a/espat/tcp.go b/espat/tcp.go index 94506ea..d43581c 100644 --- a/espat/tcp.go +++ b/espat/tcp.go @@ -36,7 +36,7 @@ func (d *Device) GetDNS(domain string) (string, error) { // Currently only supports single connection mode. func (d *Device) ConnectTCPSocket(addr, port string) error { protocol := "TCP" - val := "\"" + protocol + "\",\"" + addr + "\"," + port + ",120" + val := "\"" + protocol + "\"," + addr + "," + port + ",120" err := d.Set(TCPConnect, val) if err != nil { return err @@ -51,7 +51,7 @@ func (d *Device) ConnectTCPSocket(addr, port string) error { // ConnectUDPSocket creates a new UDP connection for the ESP8266/ESP32. func (d *Device) ConnectUDPSocket(addr, sendport, listenport string) error { protocol := "UDP" - val := "\"" + protocol + "\",\"" + addr + "\"," + sendport + "," + listenport + ",2" + val := "\"" + protocol + "\"," + addr + "," + sendport + "," + listenport + ",2" err := d.Set(TCPConnect, val) if err != nil { return err @@ -67,7 +67,7 @@ func (d *Device) ConnectUDPSocket(addr, sendport, listenport string) error { // Currently only supports single connection mode. func (d *Device) ConnectSSLSocket(addr, port string) error { protocol := "SSL" - val := "\"" + protocol + "\",\"" + addr + "\"," + port + ",120" + val := "\"" + protocol + "\"," + addr + "," + port + ",120" d.Set(TCPConnect, val) // this operation takes longer, so wait up to 6 seconds to complete. _, err := d.Response(6000) @@ -123,7 +123,10 @@ func (d *Device) GetTCPTransferMode() ([]byte, error) { // StartSocketSend gets the ESP8266/ESP32 ready to receive TCP/UDP socket data. func (d *Device) StartSocketSend(size int) error { val := strconv.Itoa(size) - d.Set(TCPSend, val) + err := d.Set(TCPSend, val) + if err != nil { + return err + } // when ">" is received, it indicates // ready to receive data