Skip to content

Instantly share code, notes, and snippets.

Avatar

Electric Imp, Inc. ElectricImpSampleCode

View GitHub Profile
@ElectricImpSampleCode
ElectricImpSampleCode / gnss.get-location.device.nut
Last active Jun 6, 2022
Electric Imp imp API GNSS example code: get the device’s location
View gnss.get-location.device.nut
// 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
Effective Internet <-> Agent <-> Device communications with the Rocky and MessageManager libraries
View iad.comms.agent.nut
/*
* 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 Apr 22, 2022
impOS 42 UDP Local Networking Example
View UDP_Hub_secure.agent.nut
// ********** 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 Jun 2, 2021
imp API Cookbook recipe: repeating timers
View timer.class.nut
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
Electric Imp imp API example code for imp.scanwifinetworks()
View aysnc.wifican.device.nut
// 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
Electric Imp imp API imp.scanwifinetworks() example code
View wifiscan.device.nut
// 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 Mar 18, 2021
A simple function which uses hardware.wakereason() to reveal why an imp restarted.
View wakereason.example.nut
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 Mar 18, 2021
Electric Imp imp API uart.configure() example 2
View uart.configure.device.nut
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;
View 1-wire.multiple.device.nut
// 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 / ALawFixedFrequencyDAC.agent.nut
Last active Feb 19, 2021
Electric Imp imp API fixedfrequencydac.addbuffer() example code
View ALawFixedFrequencyDAC.agent.nut
// AGENT CODE
const AMPLITUDE = 0x3FFF;
const NUMBER_POINTS = 1024;
const CLIP_VALUE = 32635;
const INCR_X = 0.78531475;
local streaming = false;
local logTable = [
1,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,