I am the OP.
Note: I am not using open.se for tweeting anymore. I tweet using a .PHP that triggers a Maker Channel I created at IFTTT.com. Probably a better way to do it would be to leave IFTTT.com out and tweet straight from the .PHP, but I already have several IFTTT uses and they have proved to be very reliable/useful.
This is my current build using AGENT and DEVICE:
`
/*
DEVICE CODE
Tweeting Cat Door
*** Many Many thanks to the Electric Imp forum! ***
Special thanks to brendandawes, Hugo, odb, beardedinventor
Read switch on PIN7 and GND
Normally Open or Normally Closed can be adjusted in swEvent function
Blue LED PIN9 to V3V is ON if the code in running
Blinks off when door switch detected
*/
hardware.pin9.write(0); // LED ON, active LOW
local channelOutput = [ OutputPort(“Ch 1”, “string”)]; // String var that gets sent to PHP script
//Watchdog code below send Ping to Imp server every xx secs
//I added this because the Imp was not Tweeting all switch closures and
//power up/down seems to correct it. Assuming lost connection with Imp server?
function watchdog() {
imp.wakeup(600,watchdog); //xxx is seconds between pings
//server.log(“WatchDog ping…”); // this will echo “text” to debug window
}
watchdog();
//End of my watchdog code experiment
local stringForAgent;
local d = date(); // Date info not sent to twitter. Used only for planner debug
local min = d[“min”];
local hour = d[“hour”];
hour = hour -6; //adjust for central time
local sec = d[“sec”];
local state = hardware.pin7.read(); // put condition on the switch in var ‘state’
state = 0;
// function swEvent handles looking for action on the door switch
function swEvent() {
d = date(); // Date info not sent to twitter. Used only for planner debug
min = d[“min”];
hour = d[“hour”];
hour = hour -6; //adjust for central time
sec = d[“sec”];
state = hardware.pin7.read()
//server.log("State is "+state);
imp.sleep(5.0); // switch settle time of x.x seconds (long due you door swing back and forth)
// for some reason having settle time in the 'if' block below resulted in two executions
if (state == 1) { // "1" Tweets when switch opens (for a NC swtich). "0" Tweets when switch closes (for a NO swtich).
// Blink the LED Off to show a door swing detected
hardware.pin9.write(1); // LED OFF
//imp.sleep(0.5) // 500mS delay ((this line and next line)
//hardware.pin9.write(0); // LED back ON ((replaced with line below. Suggestion from Hugo May 5, 2013)
imp.wakeup(0.5, function() { hardware.pin9.write(0); } ); // turn LED on again after 500ms (Hugo's line)
server.log(" ---"); // just a space for formatting
server.log("0/FLAP DOWN. 1/FLAP UP: "+state+", Door Active at "+hour+":"+min+":"+sec); //echo to the debug planner window the generated RANDOM# and time. Used for debug.
// echo switch state (O or 1) and time to planner window for debug pane
// start new code from 'opd' in Imp forum
// this agent.send call, with the name "catFlap" will trigger the corresponding "device.on"
// call in the agent code. We send it a simple table with our string we created.
agent.send("catFlap", { "dateString" : stringForAgent });
imp.sleep(10.0); // switch settle time of x.x seconds (long due you door swing back and forth)
// End new code from odp...
}
} // End function swEvent
// Configure pin 9 as an open drain output with internal pull up
// Configure pin 7 as switch
hardware.pin9.configure(DIGITAL_OUT);
hardware.pin7.configure(DIGITAL_IN_PULLUP, swEvent);
server.log(“"); // just a space for formatting
server.log(“0/FLAP DOWN. 1/FLAP UP: “+state+”, Program run started at:”+hour+":"+min+":"+sec);
server.log("”); // just a space for formatting
`
`
/*
*
- AGENT CODE for @OurCatDoor tweeting cat door
- *** Many Many thanks to the Electric Imp forum! ***
- Special thanks to brendandawes, Hugo, odb, beardedinventor
-
*/
// call .PHP for Tweeting
pi_url <- “http://xxxxxx.com/xxxxxx.php”;
// function to send a table of keys and values as POST data
function pi_post(post_table)
{
local headers = {};
local req = http.post(pi_url, headers, http.urlencode(post_table));
local resp = req.sendsync();
if(resp.statuscode == 200)
{
server.log(“xxxxxx.php request = 200/OK/Successful”);
return true;
}
else
{
server.log("xxxxxx.php request error, status code = "+resp.statuscode);
return false;
}
}
// This is called on the agent when the device sends the “catFlap” event
device.on(“catFlap”, function(tableSentByDevice){
pi_post({“postVarName”: tableSentByDevice.dateString});
//pi_post({“postVarName”: tableSentByDevice.stringForAgent});
});
`