Skip to content

Wi‑Fi

What makes the ESP32 (and ESP8266) such great IoT devices is their ability to connect to the internet over Wi‑Fi. The Arduino platform for the ESP32 already includes a few libraries that make this very easy to use.

The simplest sketch you can use is the following:

#include <WiFi.h>

#define WIFI_SSID      "The name of your Wi-fi network"
#define WIFI_PASSWORD  "the password of your Wi-fi network"

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  // Make sure the built-in LED is off on start-up.
  digitalWrite(LED_BUILTIN, HIGH);

  // Let the ESP32 connects to an access point
  WiFi.mode(WIFI_STA);

  // Your WeMos tries to connect to your Wi-fi network
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);

  // Keep the while-statement alive as long as we are NOT connected
  // to the Wi-fi network.
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
  }

  // If we get out of the while-loop it means that we have a connection!
  // Now move on to the loop-function to blink the LED.
}

void loop() {
  digitalWrite(LED_BUILTIN, LOW); // LED on
  delay(500);
  digitalWrite(LED_BUILTIN, HIGH); // LED off
  delay(500);
}

From top to bottom, here is what happens:

  • Include the header file WiFi.h, a standard library for the ESP32 that provides functions to connect to the internet.
  • The WiFi mode is set to WIFI_STA, which means the ESP32 acts as a station and will look for an access point.
  • The WiFi.begin(..., ...) function connects to the network using the given SSID and password.
  • The while loop keeps the program waiting until the Wi‑Fi connection is established.
  • And finally in the loop() function:
    • LED on
    • Wait…
    • LED off
    • Wait…

Because the while loop in the setup() function keeps repeating as long as there is no connection, you only see the LED blinking when there actually is a Wi‑Fi connection.

GET request

Once you’re connected to the internet you can, just like a web browser, send requests to retrieve or send data.

The following example prints the received data from the server over the serial interface, similar to printing text in Java/Python/…. While the sketch is running you can open the Serial Monitor to see the text:

Open Serial Monitor in Arduino IDE

Make sure you’ve selected 115200 baud as the speed:

Select baudrate in Arduino IDE

You can now make a GET request with the following sketch:

#include <WiFi.h>
#include <HTTPClient.h>
#include <WiFiClient.h>

#define WIFI_SSID      "The name of your Wi-fi network"
#define WIFI_PASSWORD  "the password of your Wi-fi network"

void setup() {
  // Initialize the Serial connection at a speed of 115200 b/s
  Serial.begin(115200);

  // Let the ESP32 connects to an access point
  WiFi.mode(WIFI_STA);

  // Your ESP32 tries to connect to your Wi-fi network
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);

  // Keep the while-statement alive as long as we are NOT connected
  // to the Wi-fi network.
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
  }
}

void loop() {
  // Initialize a Wi‑Fi client & HTTP client
  WiFiClient client;
  HTTPClient httpClient;

  // Set the URL the request should be made to.
  httpClient.begin(client, "http://koffiepunthva.nl/api");

  // Make the GET request; this returns the HTTP status code.
  int httpCode = httpClient.GET();

  // Check if the response is OK.
  if(httpCode == HTTP_CODE_OK) { // HTTP_CODE_OK == 200
    // Print the body of the GET response.
    String payload = httpClient.getString();
    Serial.println(payload);
  } else {
    Serial.println("Unable to connect :(");
  }

  delay(5000);
}

What you can see in this code:

  • The setup() function is almost the same as in the previous example, except that the LED initialization has been removed.
  • In the loop() function:
    • WiFiClient and HTTPClient are used to set up the request.
    • httpClient.begin(): prepares the request to http://koffiepunthva.nl/api/
      • Making requests to an HTTPS server is more complex, so this example uses an HTTP server.
    • httpClient.GET(): receives and checks the HTTP status code.
    • httpClient.getString(): retrieves the data (payload) from the response.

JSON parsing

When you work with API endpoints you often deal with structured text; a common format is JSON. An example of JSON:

{
    "coffee": 0,
    "tea": 0,
    "others": 0,
    "chocolate": 0,
    "choco_creme": 0,
    "water": 0
}

To parse this text into something useful you can use a library. You can install it by going to the Arduino IDE menu Sketch > Include library > Manage libraries..., searching for ArduinoJson and installing that library:

Installing the ArduinoJSON library

Follow these steps to integrate the JSON parser into the GET‑request sketch:

Include the library:

#include <ArduinoJson.h>

Declare a buffer in your program that stores the data; make sure it is large enough for the content:

DynamicJsonDocument jsonBuffer(1024);

Deserialize (parse) the payload of the GET request into jsonBuffer:

deserializeJson(jsonBuffer, payload);

Read a value from the JSON, for example coffee:

int coffeeCupsCount = jsonBuffer["coffee"];
Serial.print("Cups of coffee: ");
Serial.println(coffeeCupsCount);

Or tea:

int teaCupsCount = jsonBuffer["tea"];
Serial.print("Cups of tea: ");
Serial.println(teaCupsCount);