ENTS-tock
Environmental NeTworked Sensor firmware on Tock OS
Loading...
Searching...
No Matches

WiFi interface for the esp32. More...

Data Structures

struct  ControllerWiFiResponse
 WiFi response. More...
 

Enumerations

enum  ControllerWiFiStatus {
  CONTROLLER_WIFI_NO_SHIELD = 255 , CONTROLLER_WIFI_IDLE_STATUS = 0 , CONTROLLER_WIFI_NO_SSID_AVAIL , CONTROLLER_WIFI_SCAN_COMPLETED ,
  CONTROLLER_WIFI_CONNECTED , CONTROLLER_WIFI_CONNECT_FAILED , CONTROLLER_WIFI_CONNECTION_LOST , CONTROLLER_WIFI_DISCONNECTED
}
 WiFi status. More...
 

Functions

bool ControllerWiFiConnect (const char *ssid, const char *passwd)
 Initialize WiFi settings on the esp32.
 
bool ControllerWiFiDisconnect (void)
 Disconnects from the WiFi network.
 
ControllerWiFiStatus ControllerWiFiCheckWiFi (void)
 Checks the current status of WiFi.
 
bool ControllerWiFiNtpSync (void)
 Triggers a NTP sync with the server.
 
uint32_t ControllerWiFiTime (void)
 Get the current time from the NTP server.
 
bool ControllerWiFiCheckApi (const char *url)
 Queries the API endpoint to check if it is reachable.
 
bool ControllerWiFiPost (const uint8_t *data, size_t data_len)
 Post data to the configured endpoint.
 
ControllerWiFiResponse ControllerWiFiCheckRequest (void)
 Checks the status of the most recent HTTP request.
 
bool ControllerWiFiHost (const char *ssid, const char *passwd)
 Sets the esp32 as a WiFi access point.
 
bool ControllerWiFiStopHost (void)
 Stops the esp32 from acting as a WiFi access point.
 
void ControllerWiFiHostInfo (char *ssid, char *ip, char *mac, uint8_t *clients)
 Gets information about the current access point.
 

Detailed Description

WiFi interface for the esp32.

The WiFiCommand message is used to control the WiFi hardware on the esp32 module. There are two types: (1) CONNECT which is used to connect to the WiFi network and (2) POST which sends data to an API endpoint.

WiFiCommand Message Structure

The return code (rc) message serves dual purpose based on the type and direction of communication. For the CONNECT, the return code is used to send a WiFi enum directly from Arduino WiFi.status(). See this file for possible values. For POST the return code is used to store the HTTP response code from the POST request. See HTTP status code for possible values.

The software flow for WiFi is as follows:

Esp32 Controller

Enumeration Type Documentation

◆ ControllerWiFiStatus

WiFi status.

The enum is copied from the Arduino WiFi library. See the following for the definitions: https://github.com/arduino-libraries/WiFi/blob/e0e655793d73bfe5c8616bf753c3662dcdf98ad9/src/utility/wl_definitions.h#L59

The meaning of these can be ambiguous. See the following forum for the sequence of status during connect and disconnect events: https://forum.arduino.cc/t/serial-println-wifi-status-codes-meaning/486265/5

Enumerator
CONTROLLER_WIFI_NO_SHIELD 
CONTROLLER_WIFI_IDLE_STATUS 
CONTROLLER_WIFI_NO_SSID_AVAIL 
CONTROLLER_WIFI_SCAN_COMPLETED 
CONTROLLER_WIFI_CONNECTED 
CONTROLLER_WIFI_CONNECT_FAILED 
CONTROLLER_WIFI_CONNECTION_LOST 
CONTROLLER_WIFI_DISCONNECTED 

Definition at line 57 of file wifi.h.

Function Documentation

◆ ControllerWiFiCheckApi()

bool ControllerWiFiCheckApi ( const char *  url)

Queries the API endpoint to check if it is reachable.

Get the http code and response with

See also
ControllerWiFiCheckRequest.
Parameters
urlAPI endpoint url (limited to 256 characters)
portAPI endpoint port
Returns
If the command succeeded

Definition at line 104 of file wifi.c.

References CONTROLLER_SUCCESS, and WiFiCommandTransaction().

Referenced by Check().

◆ ControllerWiFiCheckRequest()

ControllerWiFiResponse ControllerWiFiCheckRequest ( void  )

Checks the status of the most recent HTTP request.

A http_code of 0 indicates that the request was not sent and possibly an error with the WiFi connection.

This function MUST be called immediately after a HTTP request command. Currently unverified how arduino code handles http requests but could cause issues with the server timing out before the esp32 reads the bytes.

Returns
HTTP response code and response data

Definition at line 135 of file wifi.c.

References ControllerWiFiResponse::bytes, ControllerWiFiResponse::http_code, ControllerWiFiResponse::size, and WiFiCommandTransaction().

Referenced by Check(), and Upload().

◆ ControllerWiFiCheckWiFi()

ControllerWiFiStatus ControllerWiFiCheckWiFi ( void  )

Checks the current status of WiFi.

Returns
WiFi status code

Definition at line 69 of file wifi.c.

References WiFiCommandTransaction().

Referenced by Connect(), and Disconnect().

◆ ControllerWiFiConnect()

bool ControllerWiFiConnect ( const char *  ssid,
const char *  passwd 
)

Initialize WiFi settings on the esp32.

Connect to a WiFi network with the given SSID and password. This function should be used in a polling configuration to continually check if the esp32 has connected to the WiFi network. The first call initializes the connection and subsequent calls check the connection status.

Parameters
ssidWiFi SSID
passwdWiFi Password
Returns
If the command succeeded

Definition at line 40 of file wifi.c.

References CONTROLLER_SUCCESS, ssid, and WiFiCommandTransaction().

Referenced by Connect().

◆ ControllerWiFiDisconnect()

bool ControllerWiFiDisconnect ( void  )

Disconnects from the WiFi network.

Returns immediately after signal is sent to the esp32 to disconnect from the current WiFi network.

See also
ControllerWiFiCheckWiFi to check when the action has completed.
Returns
If the command succeeded

Definition at line 56 of file wifi.c.

References CONTROLLER_SUCCESS, and WiFiCommandTransaction().

Referenced by Disconnect().

◆ ControllerWiFiHost()

bool ControllerWiFiHost ( const char *  ssid,
const char *  passwd 
)

Sets the esp32 as a WiFi access point.

This function configures the esp32 to act as a WiFi access point with the given SSID and password. If the password is an empty string, the access point will be open (no password).

Parameters
ssidSSID for the access point
passwdPassword for the access point (empty for open)
Returns
If the command succeeded

Definition at line 156 of file wifi.c.

References CONTROLLER_SUCCESS, ssid, and WiFiCommandTransaction().

Referenced by test_ControllerWiFiHost(), and UserConfigStart().

◆ ControllerWiFiHostInfo()

void ControllerWiFiHostInfo ( char *  ssid,
char *  ip,
char *  mac,
uint8_t *  clients 
)

Gets information about the current access point.

Copies the SSID, IP address, and MAC address of the access point into the provided buffers. The buffers must be large enough to hold the respective strings. SSID should be at least 32 bytes, IP should be at least 16 bytes, and MAC should be at least 18 bytes.

If the pointers are NULL, the corresponding information will not be copied.

Parameters
ssidBuffer to store the SSID of the access point.
ipBuffer to store the IP address of the access point.
macBuffer to store the MAC address of the access point.
clientsPointer to store the number of connected clients.

Definition at line 186 of file wifi.c.

References ssid, and WiFiCommandTransaction().

Referenced by test_ControllerWiFiHostInfo(), UserConfigStart(), and UserConfigStopCallback().

◆ ControllerWiFiNtpSync()

bool ControllerWiFiNtpSync ( void  )

Triggers a NTP sync with the server.

See also
ControllerWiFiTime to get the current time.
Returns
If the command succeeded

Definition at line 80 of file wifi.c.

References CONTROLLER_SUCCESS, and WiFiCommandTransaction().

Referenced by TimeSync().

◆ ControllerWiFiPost()

bool ControllerWiFiPost ( const uint8_t *  data,
size_t  data_len 
)

Post data to the configured endpoint.

Get the http code and response with

See also
ControllerWiFiCheckRequest.
Parameters
dataBinary data
data_lenLength of data
respBuffer to store response
resp_lenPointer to store length of resp
Returns
If the command succeeded

Definition at line 118 of file wifi.c.

References CONTROLLER_SUCCESS, data, data_len, and WiFiCommandTransaction().

Referenced by Upload().

◆ ControllerWiFiStopHost()

bool ControllerWiFiStopHost ( void  )

Stops the esp32 from acting as a WiFi access point.

Does not turn off the WiFi module on the esp32.

Returns
If the command succeeded

Definition at line 173 of file wifi.c.

References CONTROLLER_SUCCESS, and WiFiCommandTransaction().

Referenced by test_ControllerWiFiStopHost(), and UserConfigStopCallback().

◆ ControllerWiFiTime()

uint32_t ControllerWiFiTime ( void  )

Get the current time from the NTP server.

Errors are indicated by a 0 return value.

Returns
Timestamp indicated by the NTP server

Definition at line 93 of file wifi.c.

References WiFiCommandTransaction().

Referenced by TimeSync().