As the title says... Chat on...

User avatar
By TerryE
#20756 You can also use tricks like
Code: Select all    -- . . .
    local postData = '{"on":true}\r\n'
    local hostIP = "192.168.1.71"
    conn:send(table.concat ({
        "PUT /api/username/lights/4/state",
        "HTTP/1.1","Host: " .. hostIP,
        "Connection: close","Accept: text/plain",
        "Content-Type: text/plain;charset=UTF-8",
        "Content-Length: " .. #postData, '', postData
     } ,"\r\n")
   -- . . .

Note the extra empty field to generate the double CRLF.
User avatar
By Monki
#20906 Interesting... That looks neater thanks.
Assigning the posted data to a variable makes more sense too, as I will later be adding instructions to the string like brightness and hue.

When you say extra field, do you mean the '', part of this? -

"Content-Length: " .. #postData, '', postData

I like that #postData automatically gives the correct content length too! This will come in handy when I add more to the mix so I don't have to count the characters :D

Thanks again for your help guys.
User avatar
By G4UFS
#21285
Monki wrote:Hi all,

I hate to ask, as i appreciate there's already a massive amount of info available on the internet, but i've hit a dead end!

Basically, i'm trying to get my ESP8266 to turn a Philips hue light on with a PUT command.
I know nothing about code, but i can manipulate other peoples examples and have so far managed with my Pi and Arduino to find and change code i've found for my own use (like Frankenstein code) ... My end goal is to create a 433mhz wireless motion sensor to turn my lights on.
I've already made this happen with my Pi, 433 receiver and wireless PIR... but now i want to try with an arduino and ESP :D

This is the main part of what i have managed to bodge together-

conn:on("connection", function(conn, payload)
print('\nConnected')
conn:send("PUT /api/username/lights/1/state HTTP/1.1"
.."Host: xxx.xxx.xxx.xxx"
.."Connection: keep-alive"
.."Content-Length: 12\r\n"
.."Content-Type: application/json"
.."\n\n"
.."{\"on\":true}")
end)

The Philips Hue API expects {"on":true} to be part of the body, as this is the command that will turn the lights on.
The problem i have is that i receive following error -
[{"error":{"type":5,"address":"/lights/1/state","description":"invalid/missing parameters in body"}}]

To me, this is a kind of a good thing! My bodged together n00b code works! Except for the fact that i cannot for the life of me figure out how to send the {"on":true} statement as the body :(
I spent 5 hours last night going in circles and eventually gave in today and just had to ask for help...

Any pointers would be greatly appreciated! :oops:



Hi,

You might want to consider the option I chose for this although my intention was to turn the Hue lights off after a period of inactivity in the room, rather than on when motion was detected but the principal is the same.

It's a little off topic but since you mentioned the RPi, I used my alarm system's PIR to trigger my Pi and then wrote a Python script to detect if the lights were on and then turn them off 5 minutes after the last PIR trigger. It works well and cuts down even further on the electricity bill of a family of two children who are not yet aware of how energy has to be paid for :)

Regards
Dave
User avatar
By Monki
#21701 Nice! How on earth did you get the Pi to recognize the alarms PIR? :o
I had to get 2x generic PIRs with SC2262 encoding so I could use the RCSwitch library already available. I do have 2x Friedland PIRs but I don't know what encoding they use :cry: so I was unable to use them.

Currently, with the use of an arduino and some "millis()" trickery, i've been able to get the lights to turn off after X seconds/minutes from being activated. It works brilliantly! I also now have a cheap LDR hooked up, so the whole motion thing only turns lights on when it's dark enough. I'll post the code up tomrrow when I'm at my computer...It's messy but does the job.
It's such a nice feeling when you finally figure this stuff out and your project finally works isn't it :D