Ah, progress… I knew the move from planner would kill my “Tweeting Cat Door” project. I am, admittedly; an Imp novice and would have never gotten the project going without the help of brendandawes and Hugo. (thread here: http://forums.electricimp.com/discussion/comment/1262)
I’m lost. I was hoping to find a Twitter example in the forums after the Planner RIP date, but no luck. I openly throw myself on the mercy and criticism of the forum for help. ;(
Here is what the project did: 1) The Imp detects a reed switch when a cat opens the door flap. 2) A PHP script communicates with the open.se API to send a Tweet and update a usage graph. All the glory of the project is documented here: http://whiskeytangohotel.com/ourcatdoor
Of, course, my Imp still reads the door switch fine. My planner code used a ‘noodle’ to call a PHP script running on a Raspberry PI at my house. Below is a planner screen shot, my PHP code, and a cut down version of my Imp code.
I am looking for insight (or a flat out solution) on how to move this to the new Imp dev platform. As always, thanks for the help.
`/*
Tweeting Cat Door
Dec 2012. Rev Aug 8, 2013
*** Many Many thanks to the Electric Imp forum! ***
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 for 0.5 secs when door switch dectected
*/
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 cost with Imp server?
function watchdog() {
imp.wakeup(300,watchdog); //xxx is seconds between pings
//server.log(“WD ping”); // this will echo “text” to debug window
}
watchdog();
//End of my watchdog code experiment
// function swEvent handles looking for action on the door switch
function swEvent() {
local d = date(); // Date info not sent to twitter. Used only for planner debug
local min = d[“min”];
local hour = d[“hour”];
local sec = d[“sec”];
local state = hardware.pin7.read();
if (state == 1) { // "1" Tweets when switch opens (for a NC swtich). "0" Tweets when switch closes (for a NO swtich).
// Select Random Msg
local r = math.rand()%80; // .rand(xx) should r== + 1. this generate random number (min val is 0. max val is %xxx in Decimal)
if (r == 2) {
channelOutput[0].set("Tweet random message #2");
}
if (r == 1) {
channelOutput[0].set("Tweet random message #1");
}
if (r == 0) {
channelOutput[0].set("Tweet random message #0");
}
// end Random Msg selecton
// 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.show("r="+r+" @ "+hour+":"+min+":"+sec); //echo to the Imp Planner Blue BOX the generated RANDOM# and time. Used for debug.
server.log("r="+r); //echo to the debug planner window the generated RANDOM# and time. Used for debug.
} // end if state ==0 (or state ==1 depending on NO or NC switch type)
imp.sleep(10.0); // switch settle time of x.x seconds (long due you door swing back and forth)
server.log(state); // echo switch state (O or 1) to planner window for debug pane
} // 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_OD_PULLUP); //((replaced with line below. Suggestion from Hugo May5, 2013)
hardware.pin9.configure(DIGITAL_OUT); // (Hugo’s line)
hardware.pin7.configure(DIGITAL_IN_PULLUP, swEvent);
imp.configure( “TCD w-Blink”, [], channelOutput);`
PHP Script running on the RaspPI:
`<?php
$feed_id = “open.se FEEDID# here”;
$api_key = “my personal open.se API key here”;
if(isset($_POST[‘value’])) {
$data = array(“feed_id” => $feed_id, “value” => $_POST[‘value’]);
$data_string = json_encode($data);
$ch = curl_init(‘http://api.sen.se/events/?sense_key=’.$api_key);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, “POST”);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
‘Content-Type: application/json’,
'Content-Length: ’ . strlen($data_string))
);
$result = curl_exec($ch);
Print $data_string;
}
?>`