Spaces:
Running on Zero
Running on Zero
Download esp32_example/main.cpp from Onyxl/esp32-ai-proxy: direct link, hf CLI and curl.
- Browser
- Download file 9.93 kB
-
https://huggingface.co/spaces/Onyxl/esp32-ai-proxy/resolve/main/esp32_example/main.cpp
- Command line
-
hf download hf://spaces/Onyxl/esp32-ai-proxy/esp32_example/main.cpp
-
curl -L -o main.cpp https://huggingface.co/spaces/Onyxl/esp32-ai-proxy/resolve/main/esp32_example/main.cpp
9.93 kB
| const char* WIFI_SSID = "YOUR_WIFI_SSID"; | |
| const char* WIFI_PASS = "YOUR_WIFI_PASSWORD"; | |
| const char* SPACE_HOST = "onyxl-esp32-ai-proxy.hf.space"; | |
| const char* ROOT_CA = \ | |
| "-----BEGIN CERTIFICATE-----\n" \ | |
| "MIIFazCCA1OgAwIBAgIRAIIQz7DSQONZRGPgu2OCiwAwDQYJKoZIhvcNAQELBQAw\n" \ | |
| "TzELMAkGA1UEBhMCVVMxKTAnBgNVBAoTIEludGVybmV0IFNlY3VyaXR5IFJlc2Vh\n" \ | |
| "cmNoIEdyb3VwMRUwEwYDVQQDEwxJU1JHIFJvb3QgWDEwHhcNMTUwNjA0MTEwNDM4\n" \ | |
| "WhcNMzUwNjA0MTEwNDM4WjBPMQswCQYDVQQGEwJVUzEpMCcGA1UEChMgSW50ZXJu\n" \ | |
| "ZXQgU2VjdXJpdHkgUmVzZWFyY2ggR3JvdXAxFTATBgNVBAMTDElTUkcgUm9vdCBY\n" \ | |
| "MTCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoCggIBAK3oJHP0FDfzm54rVygc\n" \ | |
| "h77ct984kIxuPOZXoHj3dcKi/vVqbvYATyjb3miGbESTtrFj/RQSa78f0uoxmyF+\n" \ | |
| "0TM8ukj13Xnfs7j/EvEhmkvBioZxaUpmZmyPfjxwv60pIgbz5MDmgK7iS4+3mX6\n" \ | |
| "UA5/TR5dKYmDJdGKo0CnKCt0NN0DFHfGgFCW1B1LDs7CS1ahPgFAo9dSO6L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b1dJh4zC\n" \ | |
| "N3U1XeJU1kjn/Mwz9f3k1s0X9b2+1k2L7Q3f5O6i1j2j3d4+5e7f8g9h0i1j2k\n" \ | |
| "3l4m5n6o7p8q9r0s1t2u3v4w5x6y7z8A9B0C1D2E3F4G5H6I7J8K9L0M1N2O3\n" \ | |
| "P4Q5R6S7T8U9V0W1X2Y3Z4A5B6C7D8E9F0G1H2I3J4K5L6M7N8O9P0Q1R2S3T\n" \ | |
| "4U5V6W7X8Y9Z0a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0u1v2w3x\n" \ | |
| "4y5z\n" \ | |
| "-----END CERTIFICATE-----\n"; | |
| String history = ""; | |
| void connectWiFi() { | |
| WiFi.begin(WIFI_SSID, WIFI_PASS); | |
| Serial.print("Connecting to WiFi"); | |
| while (WiFi.status() != WL_CONNECTED) { | |
| delay(500); | |
| Serial.print("."); | |
| } | |
| Serial.println("\nWiFi connected: " + WiFi.localIP().toString()); | |
| } | |
| String generateUUID() { | |
| String uuid = ""; | |
| for (int i = 0; i < 8; i++) uuid += String(random(0, 16), HEX); | |
| uuid += "-"; | |
| for (int i = 0; i < 4; i++) uuid += String(random(0, 16), HEX); | |
| uuid += "-4"; | |
| for (int i = 0; i < 3; i++) uuid += String(random(0, 16), HEX); | |
| uuid += "-"; | |
| String variants = "89ab"; | |
| uuid += variants[random(0, 4)]; | |
| for (int i = 0; i < 3; i++) uuid += String(random(0, 16), HEX); | |
| uuid += "-"; | |
| for (int i = 0; i < 12; i++) uuid += String(random(0, 16), HEX); | |
| return uuid; | |
| } | |
| String postJSON(String path, String body) { | |
| WiFiClientSecure secure; | |
| secure.setCACert(ROOT_CA); | |
| HTTPClient https; | |
| String url = "https://" + String(SPACE_HOST) + path; | |
| https.begin(secure, url); | |
| https.addHeader("Content-Type", "application/json"); | |
| int code = https.POST(body); | |
| String response = ""; | |
| if (code > 0) { | |
| response = https.getString(); | |
| } else { | |
| Serial.println("POST error: " + https.errorToString(code)); | |
| } | |
| https.end(); | |
| return response; | |
| } | |
| String askAI(String message, String context) { | |
| String sessionHash = generateUUID(); | |
| if (context.length() > 0) { | |
| message = "[Device context: " + context + "] " + message; | |
| } | |
| String historyVal = history; | |
| if (historyVal.length() == 0) historyVal = "[]"; | |
| StaticJsonDocument<512> payload; | |
| JsonArray data = payload.createNestedArray("data"); | |
| data.add(message); | |
| data.add(historyVal); | |
| data.add("Qwen/Qwen2.5-7B-Instruct"); | |
| payload["fn_index"] = 1; | |
| payload["session_hash"] = sessionHash; | |
| String payloadStr; | |
| serializeJson(payload, payloadStr); | |
| Serial.println("Submitting request..."); | |
| String joinResp = postJSON("/gradio_api/queue/join", payloadStr); | |
| if (joinResp.length() == 0) { | |
| return "{\"error\": \"Failed to join queue\"}"; | |
| } | |
| Serial.println("Waiting for response..."); | |
| String resultUrl = "/gradio_api/queue/data?session_hash=" + sessionHash; | |
| WiFiClientSecure secure; | |
| secure.setCACert(ROOT_CA); | |
| HTTPClient https; | |
| String url = "https://" + String(SPACE_HOST) + resultUrl; | |
| https.begin(secure, url); | |
| https.setTimeout(30000); | |
| int httpCode = https.GET(); | |
| if (httpCode != 200) { | |
| https.end(); | |
| return "{\"error\": \"SSE request failed: " + String(httpCode) + "\"}"; | |
| } | |
| String result = ""; | |
| String line = ""; | |
| unsigned long start = millis(); | |
| while (millis() - start < 30000) { | |
| if (https.connected()) { | |
| if (https.available()) { | |
| char c = https.read(); | |
| line += c; | |
| if (c == '\n') { | |
| if (line.startsWith("data: ")) { | |
| String data = line.substring(6); | |
| data.trim(); | |
| if (data == "[DONE]") break; | |
| StaticJsonDocument<2048> event; | |
| DeserializationError err = deserializeJson(event, data); | |
| if (!err && event.containsKey("output")) { | |
| JsonObject output = event["output"]; | |
| if (output.containsKey("data")) { | |
| JsonArray outData = output["data"].as<JsonArray>(); | |
| if (outData.size() > 0) { | |
| result = outData[0].as<String>(); | |
| } | |
| } | |
| } | |
| } | |
| line = ""; | |
| } | |
| } | |
| } else { | |
| break; | |
| } | |
| } | |
| https.end(); | |
| if (result.length() == 0) { | |
| return "{\"error\": \"No response received\"}"; | |
| } | |
| StaticJsonDocument<1024> respDoc; | |
| DeserializationError err = deserializeJson(respDoc, result); | |
| if (!err) { | |
| if (respDoc.containsKey("history")) { | |
| history = respDoc["history"].as<String>(); | |
| } | |
| } | |
| return result; | |
| } | |
| void handleToolCall(JsonDocument& toolCall) { | |
| String name = toolCall["name"].as<String>(); | |
| JsonObject args = toolCall["arguments"]; | |
| Serial.println("Executing tool: " + name); | |
| if (name == "set_led") { | |
| int pin = args["pin"] | 2; | |
| String state = args["state"] | "off"; | |
| pinMode(pin, OUTPUT); | |
| digitalWrite(pin, state == "on" ? HIGH : LOW); | |
| Serial.println("LED pin " + String(pin) + " set to " + state); | |
| } else if (name == "read_sensor") { | |
| int pin = args["pin"] | 34; | |
| int value = analogRead(pin); | |
| Serial.println("Sensor pin " + String(pin) + " = " + String(value)); | |
| } else if (name == "set_motor") { | |
| int pin = args["pin"] | 5; | |
| int speed = args["speed"] | 0; | |
| ledcSetup(0, 1000, 8); | |
| ledcAttachPin(pin, 0); | |
| ledcWrite(0, speed); | |
| Serial.println("Motor pin " + String(pin) + " speed = " + String(speed)); | |
| } else if (name == "digital_write") { | |
| int pin = args["pin"] | 2; | |
| int value = args["value"] | 0; | |
| pinMode(pin, OUTPUT); | |
| digitalWrite(pin, value); | |
| Serial.println("Pin " + String(pin) + " written " + String(value)); | |
| } else if (name == "digital_read") { | |
| int pin = args["pin"] | 4; | |
| pinMode(pin, INPUT); | |
| int value = digitalRead(pin); | |
| Serial.println("Pin " + String(pin) + " read = " + String(value)); | |
| } else if (name == "analog_write") { | |
| int pin = args["pin"] | 5; | |
| int value = args["value"] | 0; | |
| ledcSetup(0, 1000, 8); | |
| ledcAttachPin(pin, 0); | |
| ledcWrite(0, value); | |
| Serial.println("Pin " + String(pin) + " PWM = " + String(value)); | |
| } else if (name == "beep") { | |
| int pin = args["pin"] | 8; | |
| int duration = args["duration_ms"] | 200; | |
| pinMode(pin, OUTPUT); | |
| digitalWrite(pin, HIGH); | |
| delay(duration); | |
| digitalWrite(pin, LOW); | |
| Serial.println("Beep on pin " + String(pin) + " for " + String(duration) + "ms"); | |
| } | |
| } | |
| void processResponse(String response) { | |
| StaticJsonDocument<1024> doc; | |
| DeserializationError err = deserializeJson(doc, response); | |
| if (err) { | |
| Serial.println("Response: " + response); | |
| return; | |
| } | |
| if (doc.containsKey("response")) { | |
| Serial.println("AI: " + doc["response"].as<String>()); | |
| } | |
| if (doc.containsKey("tool_calls")) { | |
| JsonArray toolCalls = doc["tool_calls"].as<JsonArray>(); | |
| for (JsonVariant tc : toolCalls) { | |
| handleToolCall(tc); | |
| } | |
| String toolSummary = "Tool executed successfully"; | |
| String resultResponse = askAI(toolSummary, "tool_result"); | |
| processResponse(resultResponse); | |
| } | |
| } | |
| void setup() { | |
| Serial.begin(115200); | |
| delay(1000); | |
| randomSeed(analogRead(0)); | |
| Serial.println("\n=== ESP32 AI Proxy Client ==="); | |
| connectWiFi(); | |
| String response = askAI("Hello, I am an ESP32-S3. What can you help me with?", ""); | |
| processResponse(response); | |
| } | |
| void loop() { | |
| if (Serial.available()) { | |
| String input = Serial.readStringUntil('\n'); | |
| input.trim(); | |
| if (input.length() > 0) { | |
| Serial.println("\nYou: " + input); | |
| String sensorData = "Temperature: 25C, Humidity: 60%"; | |
| String response = askAI(input, sensorData); | |
| processResponse(response); | |
| } | |
| } | |
| delay(100); | |
| } | |