Skip to content

Instantly share code, notes, and snippets.

View ElectricImpSampleCode's full-sized avatar

Electric Imp, Inc. ElectricImpSampleCode

View GitHub Profile
@ElectricImpSampleCode
ElectricImpSampleCode / net.info.device.nut
Last active April 19, 2024 13:50
Electric Imp imp API imp.net.info() example
local netData = imp.net.info();
if ("active" in netData) {
local type = netData.interface[netData.active].type;
// We have an active network connection - what type is it?
if (type == "cell") {
// The imp is on a cellular connection
local imei = netData.interface[netData.active].imei;
server.log("The imp has IMEI " + imei + " and is connected via cellular");
} else {
// FUNCTIONS
function onewireReset() {
// Configure UART for 1-Wire RESET timing
ow.configure(9600, 8, PARITY_NONE, 1, NO_CTSRTS);
ow.write(0xF0);
ow.flush();
local read = ow.read();
if (read == -1) {
// No UART data at all
server.log("No circuit connected to UART.");
@ElectricImpSampleCode
ElectricImpSampleCode / gnss.get-location.device.nut
Last active June 6, 2022 11:39
Electric Imp imp API GNSS example code: get the device’s location
// Hold a reference to the GNSS Session object
gnssSession <- null;
// Store device location
location <- { "lat": 0, "lon": 0 };
function isGnssEnabled() {
// Is GNSS ready to use?
if (gnssSession == null) return false;
try {
@ElectricImpSampleCode
ElectricImpSampleCode / iad.comms.agent.nut
Last active May 12, 2022 13:14
Effective Internet <-> Agent <-> Device communications with the Rocky and MessageManager libraries
/*
* Load the libraries that we need:
* Rocky to serve the API,
* MessageManager to handle the agent-device interaction
*/
#require "Rocky.agent.lib.nut:3.0.0"
#require "MessageManager.lib.nut:2.4.0"
/*
* Instantiate instances of Rocky and MessageManager
@ElectricImpSampleCode
ElectricImpSampleCode / UDP_Hub_secure.agent.nut
Last active April 22, 2022 10:00
impOS 42 UDP Local Networking Example
// ********** Imports **********
#require "Rocky.class.nut:2.0.2"
// ********** Web UI **********
const HTML_STRING = @"<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='UTF-8'>
<meta name='viewport' content='width=device-width, initial-scale=1.0'>
<title>UDP Demo</title>
@ElectricImpSampleCode
ElectricImpSampleCode / timer.class.nut
Last active June 2, 2021 08:29
imp API Cookbook recipe: repeating timers
class Timer {
static version = "1.0.0";
/*
* Private Properties
*/
_repeat = true;
_period = 0;
_timer = null;
@ElectricImpSampleCode
ElectricImpSampleCode / aysnc.wifican.device.nut
Last active May 20, 2021 13:46
Electric Imp imp API example code for imp.scanwifinetworks()
// Scan for local WLANs using imp.scanwifinetworks() asynchronously
try {
imp.scanwifinetworks(function(networks) {
if (networks.len() > 0) {
foreach (index, network in networks) {
server.log(format("%d. %s on channel %d", (index + 1), (network.ssid.len() > 0 ? network.ssid : "<Hidden>"), network.channel));
}
} else {
server.log("No nearby WiFi networks found");
}
@ElectricImpSampleCode
ElectricImpSampleCode / wifiscan.device.nut
Last active May 20, 2021 13:46
Electric Imp imp API imp.scanwifinetworks() example code
// Use imp.scanwifinetworks() in synchronous mode
local wlans = imp.scanwifinetworks();
foreach (wlan in wlans) {
local wlanData = format("WLAN '%s' is operating on channel %u (current RSSI: %i) ", wlan.ssid, wlan.channel, wlan.rssi);
wlanData = wlanData + "and is " + (wlan.open ? "open" : "closed");
server.log(wlanData);
}
@ElectricImpSampleCode
ElectricImpSampleCode / wakereason.example.nut
Last active March 18, 2021 14:50
A simple function which uses hardware.wakereason() to reveal why an imp restarted.
function logWokenReason() {
local s = "Device restarted. Reason: ";
local c = hardware.wakereason();
switch (c) {
case WAKEREASON_POWER_ON: // 0
s += "Cold boot";
break;
case WAKEREASON_TIMER: // 1
s += "Timer woke the imp";
break;
@ElectricImpSampleCode
ElectricImpSampleCode / uart.configure.device.nut
Last active March 18, 2021 14:48
Electric Imp imp API uart.configure() example 2
local serial = null;
// Set 'serial' according to imp type
switch (imp.info().type) {
case "imp001":
case "imp002":
serial = hardware.uart57;
break;
case "imp003":
serial = hardware.uartFG;