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

User avatar
By Monki
#20724 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)
conn:send("PUT /api/username/lights/1/state HTTP/1.1"
.."Connection: keep-alive"
.."Content-Length: 12\r\n"
.."Content-Type: application/json"

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:
User avatar
By lethe
#20742 Line endings for HTTP are supposed to be CRLF, so unless LUA somehow adds this for you, there must be a "\r\n" at the end of each line, plus an extra "\r\n" between the header & the body (where you put "\n\n").
User avatar
By Monki
#20750 YES!

Thank you both so much! I finally got it to work with this (after more head scratching) -

Code: Select allconn=net.createConnection(net.TCP, 0)
    conn:on("receive", function(conn, payload) print(payload) end )
    conn:send("PUT /api/username/lights/4/state\r\n"
           .." HTTP/1.1\r\n"
                .."Connection: close\r\n"
            .."Accept: */*\r\n"
           .."Content-Type: text/plain;charset=UTF-8\r\n"
           .."Content-Length: 13\r\n\r\n"

The fact that i didn't understand the importance of Content-Length and the need for \r\n\r\n to state it was time for the body didn't help me haha.
This is great... now i can work on the 433 receiver and 'ifs' and 'elses' :D