tinygo.org/x/drivers@v0.27.1-0.20240509133757-7dbca2a54349/espat/wifi.go (about) 1 package espat 2 3 import ( 4 "strconv" 5 ) 6 7 const ( 8 WifiModeClient = 1 9 WifiModeAP = 2 10 WifiModeDual = 3 11 12 WifiAPSecurityOpen = 1 13 WifiAPSecurityWPA_PSK = 2 14 WifiAPSecurityWPA2_PSK = 3 15 WifiAPSecurityWPA_WPA2_PSK = 4 16 ) 17 18 // GetWifiMode returns the ESP8266/ESP32 wifi mode. 19 func (d *Device) GetWifiMode() ([]byte, error) { 20 d.Query(WifiMode) 21 return d.Response(100) 22 } 23 24 // SetWifiMode sets the ESP8266/ESP32 wifi mode. 25 func (d *Device) SetWifiMode(mode int) error { 26 val := strconv.Itoa(mode) 27 d.Set(WifiMode, val) 28 _, err := d.Response(pause) 29 return err 30 } 31 32 // Wifi Client 33 34 // GetConnectedAP returns the ESP8266/ESP32 is currently connected to as a client. 35 func (d *Device) GetConnectedAP() ([]byte, error) { 36 d.Query(ConnectAP) 37 return d.Response(100) 38 } 39 40 // ConnectToAP connects the ESP8266/ESP32 to an access point. 41 // ws is the number of seconds to wait for connection. 42 func (d *Device) ConnectToAP(ssid, pwd string, ws int) error { 43 val := "\"" + ssid + "\",\"" + pwd + "\"" 44 d.Set(ConnectAP, val) 45 46 _, err := d.Response(ws * 1000) 47 return err 48 } 49 50 // DisconnectFromAP disconnects the ESP8266/ESP32 from the current access point. 51 func (d *Device) DisconnectFromAP() error { 52 d.Execute(Disconnect) 53 _, err := d.Response(1000) 54 return err 55 } 56 57 // GetClientIP returns the ESP8266/ESP32 current client IP addess when connected to an Access Point. 58 func (d *Device) GetClientIP() (string, error) { 59 d.Query(SetStationIP) 60 r, err := d.Response(1000) 61 return string(r), err 62 } 63 64 // SetClientIP sets the ESP8266/ESP32 current client IP addess when connected to an Access Point. 65 func (d *Device) SetClientIP(ipaddr string) error { 66 val := "\"" + ipaddr + "\"" 67 d.Set(ConnectAP, val) 68 _, err := d.Response(500) 69 return err 70 } 71 72 // Access Point 73 74 // GetAPConfig returns the ESP8266/ESP32 current configuration when acting as an Access Point. 75 func (d *Device) GetAPConfig() (string, error) { 76 d.Query(SoftAPConfigCurrent) 77 r, err := d.Response(100) 78 return string(r), err 79 } 80 81 // SetAPConfig sets the ESP8266/ESP32 current configuration when acting as an Access Point. 82 // ch indicates which radiochannel to use. security should be one of the const values 83 // such as WifiAPSecurityOpen etc. 84 func (d *Device) SetAPConfig(ssid, pwd string, ch, security int) error { 85 chval := strconv.Itoa(ch) 86 ecnval := strconv.Itoa(security) 87 val := "\"" + ssid + "\",\"" + pwd + "\"," + chval + "," + ecnval 88 d.Set(SoftAPConfigCurrent, val) 89 _, err := d.Response(1000) 90 return err 91 } 92 93 // GetAPClients returns the ESP8266/ESP32 current clients when acting as an Access Point. 94 func (d *Device) GetAPClients() (string, error) { 95 d.Query(ListConnectedIP) 96 r, err := d.Response(100) 97 return string(r), err 98 } 99 100 // GetAPIP returns the ESP8266/ESP32 current IP addess when configured as an Access Point. 101 func (d *Device) GetAPIP() (string, error) { 102 d.Query(SetSoftAPIPCurrent) 103 r, err := d.Response(100) 104 return string(r), err 105 } 106 107 // SetAPIP sets the ESP8266/ESP32 current IP addess when configured as an Access Point. 108 func (d *Device) SetAPIP(ipaddr string) error { 109 val := "\"" + ipaddr + "\"" 110 d.Set(SetSoftAPIPCurrent, val) 111 _, err := d.Response(500) 112 return err 113 } 114 115 // GetAPConfigFlash returns the ESP8266/ESP32 current configuration acting as an Access Point 116 // from flash storage. These settings are those used after a reset. 117 func (d *Device) GetAPConfigFlash() (string, error) { 118 d.Query(SoftAPConfigFlash) 119 r, err := d.Response(100) 120 return string(r), err 121 } 122 123 // SetAPConfigFlash sets the ESP8266/ESP32 current configuration acting as an Access Point, 124 // and saves them to flash storage. These settings will be used after a reset. 125 // ch indicates which radiochannel to use. security should be one of the const values 126 // such as WifiAPSecurityOpen etc. 127 func (d *Device) SetAPConfigFlash(ssid, pwd string, ch, security int) error { 128 chval := strconv.Itoa(ch) 129 ecnval := strconv.Itoa(security) 130 val := "\"" + ssid + "\",\"" + pwd + "\"," + chval + "," + ecnval 131 d.Set(SoftAPConfigFlash, val) 132 _, err := d.Response(1000) 133 return err 134 } 135 136 // GetAPIPFlash returns the ESP8266/ESP32 IP address as saved to flash storage. 137 // This is the IP address that will be used after a reset. 138 func (d *Device) GetAPIPFlash() (string, error) { 139 d.Query(SetSoftAPIPFlash) 140 r, err := d.Response(100) 141 return string(r), err 142 } 143 144 // SetAPIPFlash sets the ESP8266/ESP32 current IP addess when configured as an Access Point. 145 // The IP will be saved to flash storage, and will be used after a reset. 146 func (d *Device) SetAPIPFlash(ipaddr string) error { 147 val := "\"" + ipaddr + "\"" 148 d.Set(SetSoftAPIPFlash, val) 149 _, err := d.Response(500) 150 return err 151 }