WLED JSON API
Hi folks !!!
I am trying to create behaviour with max and the huge WLED collection of fx and colour palettes !! :-). WLED that has a JSON API: https://kno.wled.ge/interfaces/json-api/. This API can be used to control a lot of parameters of a WLED device over wifi (an ESP32 + a led strip). Could someone help me to realize the link between max and this API to make POST requests ton the weed API ? I've tried different things... maxurl, udpsend, but I am too much unexperienced in max and javascript or python to understand what would be the best and efficient method to do that !
Maybe the most straightforward would be to use node.js with the [node.script] object. There's plenty of tutorials about how to connect a web API to Max using node.js, and some tutorials about using JSON API with node.js.
Thanks TFL ! I would have hope that there were a lot of examples about this method... but there is so few.... or I didn't look enough...
I found this ... could it be relevant for what I want to achieve ? : https://github.com/ShiftLimits/wled-client
Yes! The node version should really help, as it is exactly what you want I guess.
As for Max-node communication, check for examples here and there and, as usual, read the doc!
Thanks TFL I'm taking an attentive look at what you sent me !
Well It's been very instructive and I have the wled script running in Max ! but what should be the next step ? My first raw of questions : In the script there's is this line: xhr.open("POST", "http://" + ip + "/json", true); I assume that in this line "http://" should be the address of the API, and " + ip +" the IP address of the device, but what is "/json" ? Second raw of questions : I don't understand how to send post requests now... here is my patch and the script to access the json API
// Define the function to send a command to WLED
function sendWLEDCommand(ip, data) {
var xhr = new XMLHttpRequest();
xhr.open("POST", "http://" + ip + "/json", true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.send(JSON.stringify(data));
}
// Functions that can be called from Max
function changePalette(ip, paletteID) {
sendWLEDCommand(ip, {"seg": [{"pal": paletteID}]});
}
function changeEffect(ip, effectID) {
sendWLEDCommand(ip, {"seg": [{"fx": effectID}]});
}
function changeSpeed(ip, speed) {
sendWLEDCommand(ip, {"seg": [{"sx": speed}]});
}
function changeIntensity(ip, intensity) {
sendWLEDCommand(ip, {"seg": [{"ix": intensity}]});
}
So apparently you ended up not using the WLED client! Maybe that's simpler, after all.
In your example:
'http://' is the used communication protocol.
'ip' indeed need to be the ip address of the WLED device
'/json' completes the URL so that the WLED server knows that the request uses the JSON API.
The actual request lies in 'JSON.stringify(data)'.
By looking at the code, to send a request from Max, just send a message formated relatively to the declared functions.
For example, sending the message 'changeEffect 192.168.0.12 myEffectId' to the [node.script] should change the effect to myEffectId (I have no idea what these effect ids are supposed to be like) to the device which has "192.168.0.12" as IP address.
Maybe you should simplify the code to prevent you from sending the ip for every request and saving it in a variable with a dedicated function.
Here is what they look like : https://kno.wled.ge/features/palettes/ . I've tried to send that to node script as a message 'changeEffect 192.168.0.12 50' here, 50 is the ID of the "forest" palette...
I've tried a lot of things but I am unable to get device running the request
When something doesn't work as expected, make sure everything work as it should for every step, even the most obvious ones. Using message box to see what messages you are actually sending can be helpful in that regard (as well as the "Event Probe" feature available in the Debug menu).
Replace '$i1' by '$1'. The '$i1' syntax is for expr objects, for which you need to specify the input data type (hence the 'i'). In messages, you just want to refer to an argument by its index number ($1, $2, $3... up to $9).
Also, for next times, please use the "Copy compressed" feature to share your patch instead of upoading the file.
Thanks TFL ! corrections made ! and thanks for the "copy compressed" tip ! Actually my code does not work more or less... Obviously, there is something I am not catching ... I learned that there were missing objects or missing Idontknowwhats in my code, called handlers, that can be used to achieve what I want, and that I have to write Idontkowwhere in the node.script code. I have prepositioned those objects in my js code, with comments for I'm taking time this afternoon with a friend of mine, who is a coding teacher, to try to untangle that. I will make a post tonight... For the moment there is my js code