Wi-Fi Implementation: ESP32 using REST API for Cloud Services
To implement Wi-Fi communication on an ESP32, using REST API to send real-time sensor data to cloud services like ThingSpeak, AWS, or Firebase.
Benefits
- Fast data transfer
- Easy REST API integration
- Real-time monitoring via cloud
Required Hardware
- ESP32 Development Board
- Wi-Fi Network
- Cloud Service (e.g., ThingSpeak, Firebase, AWS)
- (Optional) Sensors (e.g., DHT11, Temperature, Humidity)
Steps
- Set Up Cloud Services
Create ThingSpeak or Firebase account and obtain API keys/URLs.
- Install Required Libraries
Install WiFi.h, HTTPClient.h, and optionally ArduinoJson via Arduino Library Manager.
- Configure Wi-Fi and API Keys
1const char* ssid = "your_wifi_ssid"; 2const char* password = "your_wifi_password"; 3const String writeAPIKey = "your_thingspeak_write_api_key"; 4const String firebaseURL = "https://your-project-id.firebaseio.com/";
- Wi-Fi Connection Setup
1#include <WiFi.h> 2#include <HTTPClient.h> 3 4void setup() { 5 WiFi.begin(ssid, password); 6 while (WiFi.status() != WL_CONNECTED) delay(1000); 7}
- Send Data to ThingSpeak
1void loop() { 2 if (WiFi.status() == WL_CONNECTED) { 3 HTTPClient http; 4 float sensorData = 25.5; 5 String url = "http://api.thingspeak.com/update?api_key=" + writeAPIKey + "&field1=" + String(sensorData); 6 http.begin(url); 7 int httpResponseCode = http.GET(); 8 http.end(); 9 delay(20000); 10 } 11}
- Send Data to Firebase (Optional)
1void sendToFirebase(float sensorData) { 2 HTTPClient http; 3 String jsonData = "{"temperature": " + String(sensorData) + "}"; 4 http.begin(firebaseURL); 5 http.addHeader("Content-Type", "application/json"); 6 int httpResponseCode = http.POST(jsonData); 7 http.end(); 8}
- Monitor Data on Cloud
Use ThingSpeak graphs or Firebase Console → Realtime Database to view data.