All I want to do is make a reed switch open and close

All I want to do is make a reed switch press and release ! it works in the log why not in the agent?

Are you getting the log from the device or agent? Some code would help a lot to understand what you are doing.

`// Alias the GPIO pin as 'button'
 local pin1Value =;  
button <- hardware.pin1;
function buttonPress() 
    local state =;
      if (state == 1) 
        // The button is released
        // create table and send to agent
  local data = { "pin1": pin1Value ,} 
  agent.send("senddata", data);

    }   else
    { // The button is released
        // create table and send to agent
  local data = { "pin1": pin1Value ,} 
  agent.send("senddata", data);  

// Configure the button to call buttonPress() when the pin's state changes
button.configure(DIGITAL_IN_PULLUP, buttonPress);

sorry I am not sure how to rap the code.
log is from the device.

2015-02-12 23:26:28 UTC+10 [Agent] { “”: “”, “Name”: “Door”, “Open”: “Numder” }
2015-02-12 23:26:28 UTC+10 [Device] Release
2015-02-12 23:26:31 UTC+10 [Agent] { “”: “”, “Name”: “Door”, “Open”: “Numder” }
2015-02-12 23:26:31 UTC+10 [Device] Press

All I want to change is the open to close. as it is doing relese to close

here is the agent

`device.on("senddata", function (data)
  // Set URL to your web service
  local url = ""
  local state = { "Name": "Door", "Open": "Numder", "": "" }
  local data = { "Name": "Door", "Close": "Numder", "": "" }
  local headers = { "Content-Type": "application/json" };
  // encode data and log
  local body = http.jsonencode(state);
  // send data to your web service, headers, body).sendsync();

In device code all you really need to do is send data as a variable (as in no need to define a “pin1” unless you plan to add other parameters to data object):
local data = pin1Value; agent.send("senddata", data);

then on agent side you need to actually use the data received… at the moment you are not doing anything with “data” in function(data)… so if your use this code for your “device.on” function it might work:
device.on("senddata", function (data) { if (data) { server.log("Release"); // do something else in agent code to handle release logic } else { server.log("Press"); // do something else in agent code to handle presslogic } }

You should now see in server log…
– timestamp-- [Device] Release
– timestamp-- [Agent] Release

As to what you are also trying to do in agent code I am not sure.

hi gerriko.Thanks for the help thats what i was just working on when you sent your post. It does not work only gives release in agent.

Buy the way I have the worst luck in the world I have been working on this for a year and eveytime i get close somthing stuffs up I am so over it

In your “device.on” function, add this so you can see exactly what’s being sent:


peter i cant get that to work either
I think its time i gave up!!! Things are just never going to work for me

If you are only getting release in agent that may mean your device code not quite right… this might work for you:

function buttonPress() { local state =; agent.send("senddata", state); if (state) server.log("Release"); else server.log("Press"); }

Also in agent code you can do a little debugging by checking what you are actually receiving by simply adding the following:

device.on(“senddata”, function (data)
server.log("This is what I have received from device: " + data);


@joshreid, looking at your code, when the button is pressed, it sends pin1Value to the agent, but this variable is only set at the start of the program - it is not updated when the button is pressed or released. So your agent will only ever get one value.

Second, in the agent code, you receive the info from the agent in data but then overwrite it with a table (the line after local state = . . . ) so your agent can’t act accurately on the button press.

So, in the device code make these lines:

local data = { "pin1": pin1Value ,}


local data = { "pin1": state ,}

and in the agent code, get rid of this line:

local data = { "Name": "Door", "Close": "Numder", "": "" }

and change this line:

local body = http.jsonencode(state);


local body = http.jsonencode(data);