I’m trying to send data to a web browser. What I don’t understand is how to incorporate the header? I managed to get the header of the site I want to send data to. What do I need from here? This is the header info:
Why does this piece of code in the agent doesn’t work?
function incoming(pinState){ server.log("incoming message from the device! "+pinState); local postUrl = "http://yyy.xxx.com/imp.html"; local header ={"Content-Type": "application/x-www-form-urlencoded"}; http.post(postUrl,header, "some_stuff_to_post"); local res = req.sendsync(); } device.on("ledState", incoming);
Well, I have a radio button and I want to make sure that the buttons’ state is synced to the output pin. In other words, when I press the button in the browser, I want to see the state of the digital out pin (1 or 0). So far I managed to send the state with ‘.read()’ to the agent, now I want to pass this on and display it in the site.
I checked the http.jsonencode(object) and to all fairness it doesn’t really help. I clearly remember that about two years ago I could easily display data coming from the imp. There was even example code for that. Why is it so damn hard and why isn’t there better documentation???
A html file is static, you can’t change anything in it, and it does not have headers.
You either have to have some kind of system like php to receive the post form the agent and process it, or do it the other way around with javascript where it reads json (can be something else too, but I find json easy), and then sets the buttons according to what is received.
the way I have done this is to use a technique called long-polling. I don’t know how much street cred this has but it has worked. (I am a beginner)
The web page submits a GET request upon loading and the agent takes this but does not respond right away, it stores the response.
When the device has an action it notifies the agent which then sends a response to the GET request.
Every time the web page gets a response to a GET, it immediately opens another GET request. This way there is always a request open and responses are immediate.
With some browsers this can create a ‘constantly loading’ situation but when I use Chrome everything appears to run smoothly.
I don’t know much about Javascript to be able to do it myself and weirdly there isn’t any example. I tried the pubnub way and it doesn’t work. I guess I should just give my original idea up.