Change led example to read status

Hi All,
I just got my IMP running and tried the LED example (agent device). That worked fine.
However, I want to just read pin9 on event check bit 9. I am having trouble gettiing
my code to work correctly. Any hints would be appreciated.

Steve Faris

Post your code?

I probably need to read a bit more but here is what I have so far.
Also, once I get this working, all I need to do is login with my Android and
it will say Door Open or Door Closed. I need this to prove to my customer
that we can implement a simple idea quickly and reliable.
Thanks for responsing.

Steve
Agent
`// Log the URLs we need
server.log("check: " + http.agenturl() + “?check”);
function requestHandler(request, response) {
try {
// check if the user sent check as a query parameter
//if (“check” in request.query) {

  // if they did, and check=1.. set our variable to 1
  //if (request.query.check ) {
    // convert the check query parameter to an integer
   local test = request.query.check;

    // send "check" message to device, and send checkState as the data
    device.send("check"); 
    
  //}

// }
// send a response back saying everything was OK.
response.send(200, “OK 200”);
} catch (ex) {
response.send(500, "Internal Server Error: " + ex);
}
}

// register the HTTP handler
http.onrequest(requestHandler);`

Device:

`// create a global variabled called check,
// and assign pin9 to it
check <- hardware.pin9;

// configure check to be a digital input
check.configure(DIGITAL_IN_PULLUP);

// function to read sensor check
local state = check.read();
server.log("value " + state);

function checkbit(state) {

if (state == 1)
{
// The door is open
server.log("Door Open " + state);
} else
{
// The door is closed
//server.log(“Door Closed” + state);
}
}

// register a handler for “check” messages from the agent
agent.on(“check”, checkbit);`

Hugo,
I think my problem is decoding the single bit. I will try reg expression and
PULLUP and PULLDOWN to test.

I think I need regexpr to decode a single bit9. Is there a suggested way to do that?

Not quite; you are doing a synchronous call - ie you want to, within the HTTP handler, query the device state. You can do it that way, or you could just have the agent know the current state all the time (ie it gets updated any time the door state changes) so that it can always reply immediately.

Thanks for comment. I want to go with the lowest power mode. I think I would
put the device to sleep, then wake it up when a login from a phone is seen. Then put it back to sleep.

Also, the code below is giving me problems in the device code. When I set PULLUP I should get 1 and PULLDOWN should give me a 0. That works, however the if statement in the device code does not work. Can you comment?

Sorry I am still getting to know the language.

// Log the URLs we need
server.log("check: " + http.agenturl() + “?check”);
function requestHandler(request, response) {
try {
// convert the check query parameter to an integer
local chkdoor = request.query.check;

    // send "check" message to device, and send check as the data
    device.send("check", chkdoor); 
    
  //}

// }
// send a response back saying everything was OK.
response.send(200, “OK 200”);
} catch (ex) {
response.send(500, "Internal Server Error: " + ex);
}
}

// register the HTTP handler
http.onrequest(requestHandler);

Device
// create a global variabled called test,
// and assign pin9 to it
//#require"Button.class.nut:1.0.0" Does not work
test <- hardware.pin9;
// PULLUP =1 and PULLDOWN =0
test.configure(DIGITAL_IN_PULLDOWN);

// function to read sensor check
function chkdoor(check)
{
local state = test.read();
server.log("Value state " + state);
if (state = “1”)
{
// The door is open
server.log("Door Open " + state);
} else if(state = “0”)
{
// The door is closed
server.log(“Door Closed” + state);
} else {
server.log("Door State Unknown " + state)
}
}

// register a handler for “check” messages from the agent
agent.on(“check”, chkdoor);

I found that you must use == and no quotes in if statements.

It works now.

The require statement I have does not work. Is there something wrong.
#require"Button.class.nut:1.0.0"

@sjfaris, you need a space between the #require and the first quote mark

I tried it with the space but I still get a warning but no error message.
#require “Button.class.nut:1.0.0”

What’s your warning message?

FWIW, I’m using the Button class in a project and it’s working for me.

#require "Button.class.nut:1.0.0"

and then

local select = Button(hardware.pin7, DIGITAL_IN_PULLUP, 1, null, selectRelease)

@sjfaris - are you developing on a windows machine (and is the error you’re receiving “Unexpected character after library specifier”?

We identified an issue earlier this week where line endings from Windows machines are occasionally misinterpreted as unexpected characters. We’ve created a fix for the bug, and it should be going out sometime tomorrow (I’ll update this thread when it’s live).

Once the fix is live, you should stop seeing the issue :slight_smile:

Warning “There is/are errors in your code, please fix them and check again”. It also puts an x on the require statement.
Also, I am using Chrome and Windows Vista.
Thank you!!

If you mouse over the ‘X’ it should give you a bit more information.

But it does sound like you are running into that issue, so I’ll let you know once the fix goes live!

@sjfaris - we deployed the fix this afternoon, and the issue you were seeing should be resolved. If you trying to Build and Run again, you shouldn’t see the error anymore.

Let me know if the fix worked - it’s always nice to have external / community verification of these things :slight_smile:

@beardedinventor, Thanks it worked fine.