Created
May 4, 2015 23:17
-
-
Save metadaddy/b610f5c72d666e39c1b9 to your computer and use it in GitHub Desktop.
Press a button on the Electric Imp to upsert a record in Salesforce
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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)); | |
}); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ----------------------------------------------------------------------------- | |
// 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