Skip to content

Instantly share code, notes, and snippets.

@electricimp
electricimp / latency.agent.nut
Last active December 18, 2015 15:58
Sample code for demonstrating device/agent communication with an Electric Imp (and round trip latency).
function startTime(time)
{
// Send the device a 'pong' message immediately
device.send("pong", time);
}
// When we get a 'ping' message from the device, call start_time()
device.on("ping", startTime);
@electricimp
electricimp / isconnected.agent.nut
Last active December 18, 2015 23:39
HTTP handler to demonstrate device.isconnected(). When you browse to the agent URL you will either see a green background (if the agent is connected to the device) or a red background (when the agent is disconnected from the device).
connectedString <- "disconnected";
if (device.isconnected())
{
connectedString = "connected";
}
server.log("The device is " + connectedString);
@electricimp
electricimp / onconnection.agent.nut
Last active December 19, 2015 00:29
This code demonstrates the connection flow of an agent and its device: (1) device connected to agent, (2) device online, (3) device sleeps, (4) device disconnected from agent.
device.onconnect(function() {server.log("Device connected to agent");});
device.ondisconnect(function() {server.log("Device disconnected from agent");});
@electricimp
electricimp / getimpeeid.device.nut
Created June 26, 2013 17:06
Logs the impee's unique ID
server.log("Impee's unique id: " + hardware.getimpeeid());
@electricimp
electricimp / pulse.device.nut
Last active December 19, 2015 00:29
This example demonstrates how to use hardware.micros() to capture the length of a pulse on a digital input (ie - how long the pin stayed high)
pulseStart <- 0;
function state_change()
{
if (hardware.pin1.read())
{
pulseStart = hardware.micros();
}
else
{
@electricimp
electricimp / ADCtoVoltage.device.nut
Last active December 19, 2015 00:49
Code to convert ADC readings to actual Voltage.
function getPin9Voltage()
{
// Returns value in volts, between 0.0 and 3.3
local voltage = hardware.voltage();
local reading = hardware.pin9.read();
return (reading / 65535.0) * voltage;
}
function sample()
@electricimp
electricimp / wakereason.device.nut
Last active December 19, 2015 00:49
Code snippet showing typical switch statement for hardware.wakereason(). In this example we're just logging the reason to the debug window, but we *could* be performing various tasks based on how the imp was woken up.
function logDeviceOnline()
{
local reasonString = "Unknown"
switch(hardware.wakereason())
{
case WAKEREASON_POWER_ON:
reasonString = "The power was turned on"
break
case WAKEREASON_SW_RESET:
@electricimp
electricimp / agenturl.agent.nut
Created June 27, 2013 18:04
This is a useful snippet to have at the top of your agent code to output the agent URL whenever the agent starts.
server.log(http.agenturl()); // output the agent url to the log console.
@electricimp
electricimp / base64decode.agent.nut
Created June 27, 2013 18:23
Logs a secret message that we've base64 encoded
// logs a secret message that we've base64 encoded
server.log(http.base64decode("SW1wdGFzdGljLCBtYXRlIQ=="));
@electricimp
electricimp / basicauth.agent.nut
Created June 27, 2013 18:41
Code snippet demonstrating how to do basic auth with an electric imp
function getBasicAuthHeader(user, password) {
local headers = {
"Authorization" : "Basic " + http.base64encode(user + ":" + password)
};
return headers;
}