Skip to content

Instantly share code, notes, and snippets.

@brendandawes
Created August 31, 2012 18:02
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brendandawes/3556615 to your computer and use it in GitHub Desktop.
Save brendandawes/3556615 to your computer and use it in GitHub Desktop.
Electric Imp example for a button to trigger a tweet - needs http://open.sen.se
<?php
$feed_id = "your feed id";
$api_key = "your api key";
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);
}
?>
// Simple button to Twitter
// Button is on pin1 and GND
local channelOutput = [ OutputPort("Ch 1", "string")];
// Event handler for state changes
function swEvent() {
local d = date();
local min = d["min"];
local hour = d["hour"];
local sec = d["sec"];
local state = hardware.pin1.read();
if (state == 0) {
channelOutput[0].set(hour+":"+min+":"+sec+" This is a tweet via an Electric Imp");
server.show("Tweet sent");
}
imp.sleep(0.5);
server.log(state);
}
hardware.pin1.configure(DIGITAL_IN_PULLUP, swEvent);
imp.configure("Simple Switch To Twitter", [], channelOutput);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment