Hallo, ich stehe gerade vor einem Problem. Ich nutze WLED auf einem ESP32, welcher mit einem SK6812 RGBW LED Streifen verbunden ist. WLED funktioniert darauf wunderbar. Nun möchte ich den LED Streifen bzw. die JSON API von WLED über einen anderen ESP32 ansteuern. Hintergrund: Ich möchte mir ein kleinen physisches Kontrollpanel bauen mit Knöpfen um Effekte in WLED zu aktivieren und bestimmte Szenen aufzurufen. WLED unterstützt auch eine HTTP API welche aber nicht die Funktionen beherrscht, die ich benötige. Also bleibt nur noch die Möglichkeit per POST Request die JSON API anzusteuern. WLED Wiki JSON API: https://github.com/Aircoookie/WLED/wiki/JSON-API Ich habe bereits folgenden Code geschrieben, welcher mit aber leider ein: "Error on HTTP Json request" ausspuckt... Mein Vorgehen: Der ESP32 meiner Steuereinheit sendet per Knopfdruck eine JSON POST an http://ipWLED/json/state Inhalt: Die Variable "on" am Anfang der JSON ⬇️ soll auf "true" gesetzt werden. Die JSON von WLED sieht dort so aus:
1 | {"on":false,"bri":128,"transition":7,"ps":-1,"pl":-1,"ccnf":{"min":1,"max":5,"time":12},"nl":{"on":false,"dur":60,"fade":true,"mode":1,"tbri":0,"rem":-1},"udpn":{"send":false,"recv":true},"lor":0,"mainseg":0,"seg":[{"id":0,"start":0,"stop":150,"len":150,"grp":1,"spc":0,"on":true,"bri":255,"col":[[255,0,1,0],[0,0,0,0],[0,0,0,0]],"fx":0,"sx":128,"ix":128,"pal":0,"sel":false,"rev":false,"mi":false}]} |
{"on":false, steht dabei dafür, dass die LEDs Ausgeschaltet sind. Diese will ich aber auf "true" gesetzt haben, damit die LEDs leuchten. Mein Code dazu:
1 | #include <WiFi.h> |
2 | #include <HTTPClient.h> |
3 | #include "OneButton.h" |
4 | |
5 | const char* ssid = "SSID"; |
6 | const char* password = "PW"; |
7 | |
8 | //Button setup |
9 | OneButton buttonSegment1(2, true); |
10 | |
11 | void setup() { |
12 | |
13 | Serial.begin(115200); |
14 | WiFi.begin(ssid, password); |
15 | |
16 | while (WiFi.status() != WL_CONNECTED) { |
17 | Serial.println("Connecting to WiFi.."); |
18 | } |
19 | |
20 | Serial.println("Connected to the WiFi network"); |
21 | |
22 | buttonSegment1.attachClick(buttonSegment1click); |
23 | } |
24 | |
25 | void loop() { |
26 | buttonSegment1.tick(); |
27 | } |
28 | |
29 | void buttonSegment1click() { |
30 | Serial.println("buttonSegment1 click."); |
31 | |
32 | HTTPClient http; |
33 | http.begin("http://wledIP/json/state"); |
34 | |
35 | http.addHeader("Content-Type", "applictaion/json"); |
36 | |
37 | int httpResponseCode = http.POST("{\"on\":\"true\"}"); // JSON Value "on" auf "true" umändern. |
38 | |
39 | //Antwort von WLED abwarten... |
40 | if (httpResponseCode > 0) { |
41 | String payload = http.getString(); |
42 | Serial.println("Statuscode: " + String(httpResponseCode)); |
43 | Serial.println(payload); |
44 | } |
45 | else { |
46 | Serial.println("Error on HTTP Json request"); |
47 | } |
48 | |
49 | |
50 | http.end(); |
51 | } |
Ich hoffe jemand von Euch schlauen Köpfen kann mir helfen. Ich denke es kann nur an der Formatierung der POST Request liegen. Dass http.POST("{\"on\":\"true\"}"); kommt mir merkwürdig vor... Vielen Dank für eure Hilfe und eine schönen Start ins Wochenende! Beste Grüße, Leo