#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
#include <IRremoteESP8266.h>
#include <IRsend.h>
// WiFi credentials
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// Server address
const char* serverAddress = "http://your_server_address/api"; // Replace with your server's address
// IR LED pin
const int irLedPin = D4;
IRsend irsend(irLedPin);
void sendIRCode(unsigned long code, int bits) {
irsend.sendRaw(&code, bits, 38);
delay(50);
}
void setup() {
Serial.begin(115200);
// ... (Connect to WiFi)
irsend.begin();
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String response;
// 1. Get Device List (Example)
http.begin(serverAddress + "/devices"); // Example API endpoint
int httpCode = http.GET();
if (httpCode == HTTP_CODE_OK) {
response = http.getString();
Serial.println("Devices: " + response);
// Parse JSON response (using ArduinoJson)
DynamicJsonDocument doc(1024); // Adjust size as needed
DeserializationError error = deserializeJson(doc, response);
if (!error) {
// Example: Print device names
for (JsonVariant device : doc.as<JsonArray>()) {
String deviceName = device["name"];
Serial.println("Device: " + deviceName);
// You could display these on a web page, etc.
}
}
}
http.end();
// 2. Get Commands for a Device (Example)
http.begin(serverAddress + "/commands?device_id=1"); // Example: Get commands for device with ID 1
httpCode = http.GET();
if (httpCode == HTTP_CODE_OK) {
response = http.getString();
Serial.println("Commands: " + response);
DynamicJsonDocument doc(2048); // Adjust size
DeserializationError error = deserializeJson(doc, response);
if (!error) {
// Example: Iterate through commands and send IR when a button is pressed on web UI
for (JsonVariant command : doc.as<JsonArray>()) {
String commandName = command["name"];
unsigned long irCode = command["ir_code"];
int bits = command["bits"];
// In a real application, you would link the commandName to a button on your web interface.
// When that button is pressed, you would call sendIRCode(irCode, bits);
Serial.println("Command: " + commandName);
Serial.println("Code: ");
Serial.println(irCode, HEX);
Serial.println("Bits: ");
Serial.println(bits);
}
}
}
http.end();
}
delay(5000); // Check every 5 seconds
}