The WiFi Network Interface - uraich/IoT4AQ GitHub Wiki

WiFi

In addition to all the interfaces used to speak to external devices the ESP32 provides BlueTooth and Wifi for communication with other computers. During the workshop we will not use BlueTooth and for this reason you will not find any information on it withing these Wiki pages. The WiFi interface can be configured to be

  • an access point
  • a station interface When being configured as a station interface there is no need for a router. The ESP32 communicates with other computers through a local network. It has a fixed IP address: 192.168.4.1 and it has no access to the Internet.

In case of a station interface, and this is what we will use, the ESP32 connects to a router and will be seen on the network with an IP address attributed be DHCP (Dynamic Host Configuration Protocol). In this case you need to know the SSID (Service Set IDentidier) which is the name of your network, and the password of your router. The ESP32 becomes a normal node on your network and it has Internet access.

The WiFi library

The WiFi library is part of the ESP32 package for the Arduino and must not be separately installed. We do not have the time to explore all the possibilities of network access, the different protocols, clients, servers ... and this page is therefore restricted to the bare minimum needed to make the IoT4AQ demo run. For more information you may want to look at this ESP32 WiFi tutorial.

To get connected to the router, this is what needs to be done:

#include <WiFi.h>
const char *ssid =     "your SSID";  // of course you must replace "your SSID" with the name of your WiFi network
const char* password = "your WiFi password";

void setup() {
    WiFi.begin(ssid,password);                 // connect to the network
    while (WiFi.status() != WL_CONNECTED) {    // waits until connected
        delay(500);                            // 1/2 seconds between each try
        Serial.print(".");                     // print a "." to show we are waiting
    }
    Serial.println("");                        
    Serial.print("WiFi connected on ");        // once we are connected
    Serial.println(WiFi.localIP());            // print the IP address that we have been attributed by DHCP
}

Scanning the networks

In order to scan visible WiFi networks we must first disconnect from the network we may be connected to. After that a call to

int noOfNetworks = WiFi.scanNetworks(false,true); //parameters: asynchronous, show hidden networks
for (i=0; i<noOfNetworks; i++) {
    String ssidName = WiFi.SSID(i);                         // gives you the SSID
    wifi_auth_mode_t encrType = WiFi.encryptionType(i);     // the encryption type
    int32_t rssi = Wifi.RSSI(uint8_t networkItem);          // the signal strength
    uint8_t *bssid = Wifi.BSSID(uint8_t networkItem);       // the MAC address as 6 uint8_t values
    String bssid_str = Wifi.BSSIDstr(uint8_t networkItem);  // the MAC address as a String
    int32_t channel_no = Wifi.channel(uint8_t networkItem); // the channel number      
}

This information can be printed in a table, which will give you: (these are the networks I see in my home)

wifi scan

Using the network

Once your ESP32 is connected to the network you can use Internet services:

  • Get the current date and time from NTP (Network Time Protocol)
  • Run TCP and/or UDP protocols
  • Use the socket interface to implement a TCP client or TCP server
  • Implement a WEB server
  • Run the REST (HTTP) or MQTT protocols to connect to cloud services like ThingSpeak
⚠️ **GitHub.com Fallback** ⚠️