ESP32 WiFi & IoT Simulator
LAB09: Wireless Communication Without Hardware
Overview
This Wokwi simulation demonstrates ESP32 WiFi connectivity and IoT protocols covered in LAB09. Experience wireless communication concepts without physical hardware!
Wokwi simulates WiFi connectivity! Your ESP32 code can connect to a virtual network and make HTTP requests to real web APIs.
ESP32 IoT Sensor Node
This simulation demonstrates:
- WiFi connection to simulated network
- HTTP requests to external APIs
- Sensor reading and transmission
- LED status indicators for connection state
Click “Play” and copy the code below into the Wokwi editor. The simulated ESP32 will connect to Wokwi’s virtual WiFi network.
WiFi Connection Code
// LAB09: ESP32 WiFi IoT Sensor
// Edge Analytics Lab Book
#include <WiFi.h>
#include <HTTPClient.h>
// Wokwi provides a simulated WiFi network
const char* ssid = "Wokwi-GUEST";
const char* password = ""; // Open network
// Sensor pin
const int SENSOR_PIN = 34; // ADC pin
const int LED_WIFI = 2; // Built-in LED (connection status)
const int LED_SEND = 4; // External LED (transmission status)
// Timing
unsigned long lastSendTime = 0;
const unsigned long SEND_INTERVAL = 5000; // 5 seconds
void setup() {
Serial.begin(115200);
pinMode(LED_WIFI, OUTPUT);
pinMode(LED_SEND, OUTPUT);
pinMode(SENSOR_PIN, INPUT);
Serial.println("\n=== ESP32 IoT Sensor Node ===");
Serial.println("Connecting to WiFi...");
WiFi.begin(ssid, password);
// Wait for connection with visual feedback
while (WiFi.status() != WL_CONNECTED) {
digitalWrite(LED_WIFI, !digitalRead(LED_WIFI)); // Blink
delay(500);
Serial.print(".");
}
digitalWrite(LED_WIFI, HIGH); // Solid = connected
Serial.println("\nWiFi connected!");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
// Check WiFi connection
if (WiFi.status() != WL_CONNECTED) {
Serial.println("WiFi lost! Reconnecting...");
digitalWrite(LED_WIFI, LOW);
WiFi.reconnect();
return;
}
// Read sensor
int sensorValue = analogRead(SENSOR_PIN);
float voltage = sensorValue * (3.3 / 4095.0);
Serial.print("Sensor: ");
Serial.print(sensorValue);
Serial.print(" (");
Serial.print(voltage, 2);
Serial.println("V)");
// Send data periodically
if (millis() - lastSendTime >= SEND_INTERVAL) {
sendSensorData(sensorValue, voltage);
lastSendTime = millis();
}
delay(1000);
}
void sendSensorData(int raw, float voltage) {
HTTPClient http;
// Using httpbin.org as a test endpoint (echoes back your request)
String url = "https://httpbin.org/get?sensor=" + String(raw) + "&voltage=" + String(voltage, 2);
Serial.println("Sending data to cloud...");
digitalWrite(LED_SEND, HIGH);
http.begin(url);
int httpCode = http.GET();
if (httpCode > 0) {
Serial.print("HTTP Response: ");
Serial.println(httpCode);
if (httpCode == HTTP_CODE_OK) {
String payload = http.getString();
Serial.println("Server response received!");
// In real app, you'd parse the JSON response
}
} else {
Serial.print("HTTP Error: ");
Serial.println(http.errorToString(httpCode));
}
http.end();
digitalWrite(LED_SEND, LOW);
}Wiring Diagram
| Component | ESP32 Pin | Notes |
|---|---|---|
| Potentiometer | GPIO34 (ADC) | Simulates analog sensor |
| Status LED | GPIO4 | With 220Ω resistor |
| Built-in LED | GPIO2 | Shows WiFi status |
Understanding the Code
WiFi Connection States
Blinking LED (GPIO2): Connecting to WiFi
Solid LED (GPIO2): Connected
LED off: Disconnected
HTTP Request Flow
- Read sensor value from ADC
- Construct URL with query parameters
- Send HTTP GET request
- Check response code (200 = success)
- Blink LED to indicate transmission
Exercises
Exercise 1: Connection Resilience
What happens if you disconnect WiFi mid-transmission? Add error handling:
int retryCount = 0;
const int MAX_RETRIES = 3;
while (WiFi.status() != WL_CONNECTED && retryCount < MAX_RETRIES) {
WiFi.reconnect();
delay(1000);
retryCount++;
}Exercise 2: Data Buffering
If the network is unavailable, buffer sensor readings locally:
const int BUFFER_SIZE = 10;
float sensorBuffer[BUFFER_SIZE];
int bufferIndex = 0;
bool bufferFull = false;
// Add to loop:
if (WiFi.status() != WL_CONNECTED) {
// Store locally
sensorBuffer[bufferIndex] = voltage;
bufferIndex = (bufferIndex + 1) % BUFFER_SIZE;
if (bufferIndex == 0) bufferFull = true;
} else if (bufferFull) {
// Send buffered data first
// ...
}Exercise 3: Power Optimization
Calculate the power impact of different transmission intervals:
| Interval | Transmissions/hour | Relative Power |
|---|---|---|
| 1 second | 3600 | Very High |
| 5 seconds | 720 | High |
| 30 seconds | 120 | Medium |
| 5 minutes | 12 | Low |
What’s the trade-off between data freshness and battery life?
BLE Alternative
For low-power applications, consider Bluetooth Low Energy:
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
// BLE uses ~10x less power than WiFi for periodic sensor updates
// See LAB09 Chapter for full BLE implementationMQTT Protocol
For production IoT, MQTT is more efficient than HTTP:
#include <PubSubClient.h>
// MQTT uses persistent connections and smaller packets
// Ideal for high-frequency sensor data
// See LAB09 Chapter for MQTT implementationNext Steps
After completing this simulation:
- Level 3: Deploy on physical ESP32 with real WiFi
- LAB12: Implement streaming data pipeline
- LAB15: Add deep sleep for battery optimization
Wokwi’s simulated WiFi connects to a real network gateway, allowing your code to make actual HTTP/HTTPS requests to internet services. This is perfect for testing IoT workflows without hardware.