Skip to content

Instantly share code, notes, and snippets.

Avatar

Electric Imp, Inc. ElectricImpSampleCode

View GitHub Profile
@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,
@ElectricImpSampleCode
ElectricImpSampleCode / gnss.get-location.device.nut
Last active Jan 5, 2021
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 enableGNSS() {
// Not got a GNSS Session? Establish one
if (gnssSession == null) {
// Set up a session
@ElectricImpSampleCode
ElectricImpSampleCode / gnss.get-nmea.device.nut
Created Jan 5, 2021
Electric Imp imp API GNSS example code: get NMEA sentences
View gnss.get-nmea.device.nut
// Get GSV (satellites in view) NMEA sentences from modem
// NOTE #1 See https://gpsd.gitlab.io/gpsd/NMEA.html#_nmea_standard_sentences
// for sentence details
// NOTE #2 Assumes you have an existing configured and enabled gnss-session
// instance, as per https://developer.electricimp.com/api/gnss-session/enable
// Callback to process read sentences
function processSentences(result) {
if (result.status == 0) {
// No error, so just output each received sentence,
@ElectricImpSampleCode
ElectricImpSampleCode / factory.activation.dut.agent.nut
Last active Nov 23, 2020
Factory Activation Example Code 2.1.1
View factory.activation.dut.agent.nut
// ------------------------------------------------------------------------------
// File: factory.activation.dut.agent.nut
// Version: 2.1.2
//
// Copyright 2020 Twilio
//
// SPDX-License-Identifier: MIT
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal