Use device.on(); alongside http.onrequest(httpHandler);

I’ve seen code that use both, but for some reason on my working code utilizing http.onrequest, any time I add a device.on() line I get No HTTP Handler from my Agent URL.

What am I missing?

I think this is bare minimum of what you need.

function requestHandler(request, response) { response.send(200, "Hello world!"); return; } http.onrequest(requestHandler);

It would help if you could post the actual code you are having problems with.

My code is kinda huge, but that’s basically what I’m doing. Below that, I’m trying to add a device.on(“state”, updateState); that references a function up in the HTML portion.

My request/response works great and all is well, until I add that device.on statement.

There are no issues with using both device.on and http.onrequest. Do you have some code you can share?

Why not:

HTML function:
function updateState(state) { $('#state').prepend('<span>'state'</span>'); }

`// agent code:
function httpHandler(request, response) {
try {
// if they passed a power parameter
if (“power” in request.query) {
// add the ajax header
response.header(“Access-Control-Allow-Origin”, “*”);

        // password variable
        local pw = null;
        // if they passed a password
        if ("pw" in request.query) {
            // grab the pw parameter
            pw = request.query["pw"];

        // if the password was wrong
        if (pw != PASSWORD) {
            // send back an angry message
            response.send(401, "UNAUTHORIZED");

        // grab the power parameter
        local powerState = request.query["power"].tointeger();
        if (powerState == 0 || powerState == 1 || powerState == 2 || powerState == 9) {
            // send it to the device
            device.send("power", powerState);

            // finally, send a response back to whoever made the request
            response.send(200, powerState == 1 ? "Daytime" : powerState == 2 ? "Night" : powerState == 9 ? "Timer" : "Off");
        } else {
                // if powerState isn't valid, send back an error message
                response.send(500, "Invalid power parameter. Please pass 1 or 2 or 0 or 9 and try again.");
    } else {
        // if power wasn't specified, send back the HTML page
        response.send(200, html);
catch (ex) {
    // if there was an error, send back a response with the error
    response.send(500, ex);


// run httpHandler whenever a request comes into the Agent URL

// receive power state from device
device.on(“state”, updateState);`

`lightLevel <- 0;

function requestHandler(request, response)
response.send(200, "Light level: " + lightLevel);

device.on(“lightlevel”, function(msg)
lightLevel = msg;

function light() { agent.send("lightlevel", hardware.lightlevel()); imp.wakeup(1, light); } light();

So is the problem that my function is not inside the device.on call? I can’t directly reference a function in the HTML?

My HTML is serving as a front end to pass settings TO the device as well, so the rest of that code I posted has to stay to deal with the HTML buttons.

@Trumpetrhapsody - looks like you’re missing the updateState function from your agent code.

Edit - sorry, you posted your reply while I was posting mine… Your HTML has no knowledge of your agent or device code. Flow looks like this:

  1. Device Code sends information to agent with agent.send
  2. Agent catches the message, and stores the information
  3. Agent’s onrequest function returns the stored information when requested
  4. Webpage makes AJAX requests to agent’s URL with javascript, and uses the returned values to update the webpage.

Ah ok that’s the breakdown then.

I was trying to get it where if someone was sitting with the agent HTML open in a browser, the device could trigger a div or element to change. Sounds like it will have to be a bit more manual, or have the agent pull the status from the device.


Take a look at this thread, which is also active at the moment. It has some links to PubNub resources, which will take care of a lot of it for you :slight_smile: