I’m currently trying to create an app that can communicate with my Philips HUE bridge through http.requests. The system uses the RESTful interface and JSON format. Basically, I was hoping someone could help me figure out how to properly communicate with my lights using Codea. As I am very new to both Codea and the HUE API, It has been a slightly daunting task to combine the two successfully. Ideally, I’d like to create a class that could handle the hue portion of my code. Is any of this even possible from within Codea?
I’ve tested many of the hue commands on my lights through the bridges debug tool, and its a blast to mess with. Now I just need to figure out how to make Codea do all the dirty work.
Mainly, I just need to know how to properly arrange the information required to send a command from my code.
-A command consists of a “Method”, a “URL”, and a “Body”.
-Tested/Working example of a command using method “PUT”:
The example above turns bulb #2 to “on” and sets color to “red”.
This all seemed pretty straight forward in codea, except for the “body”. These commands are always between “{}” symbols which seems to mess things up a bit, as Codea already uses them. Rightly, the same goes for the quotations. Is there a correct way to accomplish this? Any guidance would be greatly appreciated.
Here was something I was playing with… (Non-working)
--# Main
-- HUE API Test
-- the hue bridge uses the RESTful Interface
-- Commands and responses are JSON Format
-- Most commands include:
--Method --ex-- PUT
--URL --ex-- http://192.168.1.147/API/newdeveloper/lights/2/state
--Body --ex-- {"on":true}
-- Body is always inside "{}" which obviously doesnt work within my table,as they are already used.
-- Is there another way to do this? Maybe a way of storing the value in a variable?
function setup()
tbl={["method"]="PUT",["data"]="{"on":true}"}
end
function hueRequest()
http.request("http://192.168.1.147/API/newdeveloper/lights/2/state",httpResponse,httpError,tbl)
end
function httpResponse(data)
print(data)
end
function httpError(error)
print("Error:"..error)
end
function draw()
background(40, 40, 50)
strokeWidth(5)
end
function touched(touch)
hueRequest()
end
Hello @Circuit, this is a little late, but I have had some experience with using the Hue API to send requests to the lights to get some cool effects. The one thing that I believe is the roadblock your running into is that the request body would be a JSON table, and the data your sending is a Lua table which I do not believe would be implicitly converted into the proper format when sending as all JSON requests would come through normally as strings. For the body, you could try and replicate the JSON syntax yourself or find a library which can convert to JSON from Lua and vice versa. When I was first working with requests in Codea, I had similar issues with the body formatting. Let me know if that was any help, or if you could use a working code example, I could post that too a little later today when I have some more down time. Best of luck!
Actually, here is a working example. You may need to change the IP address, but this was successfully able to send a PUT request to my HUE lights. The actual error was that your string formatting above was invalid in Lua as using the same quotation symbol was actually exiting your string, so you should alternate using the ’ and " symbols when making that form of a request.
-- Philips Test
local URL = "http://192.168.1.147/api/newdeveloper/lights/2/state"
local error = function(data)
print("Error",data) end
local success = function(data)
print("Success",data) end
local request = {method = "PUT", data = '{"on":true}'}
-- Use this function to perform your initial setup
function setup()
http.request(URL,success,error,request)
end
@Beckett2000 Better late than never. I hadn’t even realized anyone had replied. Thanks so much for all the guidance! I was able to get some little things working prior to your assistance, but most examples I have found were fairly complex. This is exactly what I was hoping to see, something nice and simple. I suppose my next question would be… How do I insert a variable from my code into the string? Im guessing one of these JSON libs I grabbed would be a good place to start, but I’m still a little confused on their exact implementation.
I’m trying to figure out how to replace “newuser123” in the code below with a user id created by my program. I would also like to use different variables for pieces of the url, such as the IP address or light number. Is there a similarly simple method of accomplishing this? Thanks again for all your time.
local URL = "http://192.168.1.147/api/"
local error = function(data)
print("Error",data) end
local success = function(data)
print("Success",data) end
local request = {method = "POST", data = '{"devicetype":"test user","username":"newuser123"}'}
function setup()
userID = 2436448
ip = "192.168.1.5"
http.request(URL,success,error,request)
end
@dave1707 Thanks. It was pretty late when I posted and I was starting to go all cross eyed. I didn’t even notice I had done that. Funny that two even worked at all.