Built-in project · ESP32-C3-DevKitM-1

API lab: fetch JSON, on an ESP32-C3

Call an API with normal HTTPClient code, edit its JSON response in Cloud and inspect the complete request and response. The whole build, an ESP32-C3, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Intermediate About 15 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $13. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • Joining WiFi from an Arduino sketch
  • Making an HTTP GET request
  • Reading a status code and a JSON reply
  • Testing code against an editable API
  • Difficulty: Intermediate
  • Time: About 15 minutes
  • Board: ESP32-C3-DevKitM-1
  • Parts: 2

How it works

This sketch connects an ESP32-C3 to the WiFi network Mokxi-Lab with the password breadboard. It prints a dot every 250 ms until the connection is up, then tells you it is connected.

Then every 5 seconds it sends a GET request to http://api.mokxi/data and prints the HTTP status and the JSON text it gets back. Open the Cloud panel to change the status and body of the answer, and watch the same firmware handle it.

Parts list

2 parts, plus the jumper wires. They retail for about $13; on Mokxi they're free.

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · ESP32-C3-DevKitM-1
/*
 * API lab: call an endpoint and inspect the JSON it returns.
 *
 * Open the Cloud panel, edit the status and response body for api.mokxi, then
 * watch this same firmware handle the new answer. Nothing leaves this browser.
 */
#include <HTTPClient.h>
#include <WiFi.h>
#include <WiFiClient.h>

const char *SSID = "Mokxi-Lab";
const char *PASSWORD = "breadboard";

void setup() {
  Serial.begin(115200);
  WiFi.begin(SSID, PASSWORD);
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(250);
  }
  Serial.println("\nConnected. Open Cloud and edit the API response.");
}

void loop() {
  WiFiClient client;
  HTTPClient http;
  http.begin(client, "http://api.mokxi/data");
  int status = http.GET();
  Serial.print("HTTP status: ");
  Serial.println(status);
  Serial.print("JSON response: ");
  Serial.println(http.getString());
  http.end();
  delay(5000);
}

Code walkthrough

The key functions of the sketch, and what each one does.

setup sketch.ino

Starts the serial port at 115200 baud and calls WiFi.begin with SSID and PASSWORD. Then it waits in a loop, printing a dot every 250 ms, until WiFi.status() is WL_CONNECTED. Finally it prints a connected message.

void setup() {
  Serial.begin(115200);
  WiFi.begin(SSID, PASSWORD);
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(250);
  }
  Serial.println("\nConnected. Open Cloud and edit the API response.");
}

loop sketch.ino

Opens a connection to the API with http.begin and sends http.GET(), which returns the status code. It prints the status, then the body from http.getString(). It closes the request with http.end() and waits 5 seconds.

void loop() {
  WiFiClient client;
  HTTPClient http;
  http.begin(client, "http://api.mokxi/data");
  int status = http.GET();
  Serial.print("HTTP status: ");
  Serial.println(status);
  Serial.print("JSON response: ");
  Serial.println(http.getString());
  http.end();
  delay(5000);
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • It prints dots forever. This project
    The SSID and PASSWORD must match the network. On a real board, use your own network's name and password.
  • The status is a negative number. This project
    The request failed before an answer came. Check the address in http.begin and that WiFi is connected.
  • Upload fails or the board is not found by the computer. ESP32-C3-DevKitM-1
    Pick the ESP32-C3 board in your tools and the right port. If it still fails, hold BOOT, tap RESET, then release BOOT to enter download mode and try again.
  • GPIO pins act strangely or the board resets. ESP32-C3-DevKitM-1
    GPIO is 3.3 V only, so never feed it 5 V. GPIO 18 and 19 are USB, and GPIO 2, 8 and 9 are boot strapping pins. Avoid loading them at reset.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.