Skip to content

Instantly share code, notes, and snippets.

@metadaddy
Created May 4, 2015
Embed
What would you like to do?
Press a button on the Electric Imp to upsert a record in Salesforce
#require "Salesforce.class.nut:1.0.0"
force <- Salesforce("CLIENT_ID_GOES_HERE",
"CLIENT_SECRET_GOES_HERE");
USERNAME <- "user@example.com";
PASSWORD <- "p455w0rd";
SECURITY_TOKEN <- "s3cr3t";
force.login(USERNAME, PASSWORD, SECURITY_TOKEN, function(err, data) {
if (err != null) {
server.error(err);
return;
}
if(data.result == false) {
server.error("Could not login");
return;
}
server.log("Logged in as "+USERNAME);
// -----------------------------------------------------------------------------
// Button event handler
device.on("button1", function(data) {
server.log("button1: "+data.state);
force.request("post", "sobjects/Button__c/Device_ID__c/"+data.device_id+"?_HttpMethod=PATCH",
http.jsonencode({
State__c = (data.state != 0) // Turn integer into a boolean
}),
function(err, response) {
if (err) {
server.error("ERROR: " + http.jsonencode(err))
return
}
// Response will be record id on creation, empty on update
server.log("RESPONSE: " + http.jsonencode(response));
});
});
});
// -----------------------------------------------------------------------------
// PIN mux
btn1 <- hardware.pin1;
// -----------------------------------------------------------------------------
// Handle the state change for button 1
function btn1_change() {
imp.sleep(0.01); // Debounce
// Send the device ID and whether the button is up (1) or down (0)
agent.send("button1", {
device_id = hardware.getdeviceid(),
state = btn1.read()
});
}
// -----------------------------------------------------------------------------
imp.enableblinkup(true);
// Configure button 1 as an input and set an event handler
btn1.configure(DIGITAL_IN_PULLDOWN, btn1_change);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment