Skip to content

Instantly share code, notes, and snippets.

@electricimp
electricimp / datapersist.agent.nut
Last active August 29, 2015 14:08
Data Persistence: preserving data via the agent
// Start of agent
// Load the data from persistent storage
settings <- server.load()
// It the load is null, it tells us this is the first time the agent is running
if (settings == null)
{
@electricimp
electricimp / datapersist.device.nut
Last active August 29, 2015 14:08
Data Persistence: preserving device data via the nv table
// On wake from deep sleep or cold boot, Squirrel starts here
// Check if nv table exists and if it contains the key 'count'
if (!("nv" in getroottable() && "count" in nv))
{
// Either the nv table hasn't been instantiated or it has but lacks a 'count' slot
// So create them. If nv table exists, this code just adds 'count' to it. If nv
// doesn't exist yet, this code will automatically create it when it adds 'count'
@electricimp
electricimp / regexp2.search.agent.nut
Last active August 29, 2015 14:08
Example of using regexp2.search
// Define the regular expression:
// Look for digits one or more times (\d+)
// Look for spaces
// Look for any case character one or more times ([a-zA-Z]+)
// Look for the first punctuation mark (\p)
local expression = regexp2(@"\d+ [a-zA-Z]+\p")
local compareString = "stuff 123 Test."
@electricimp
electricimp / regexp2.capture.agent.nut
Last active August 29, 2015 14:08
Example of using regexp2.capture
// Define the regular expression:
// Look for digits one or more times (\d+)
// Look for spaces
// Look for any case character one or more times ([a-zA-Z]+)
// Look for the first punctuation mark (\p)
local expression = regexp2(@"(\d+) ([a-zA-Z]+)(\p)")
local compareString = "stuff 123 Test."
// Generate an array of matches
local results = expression.capture(compareString)
@electricimp
electricimp / regexp2.match.agent.nut
Created October 23, 2014 15:31
Check whether a string matches a regular expression
// Define the regular expression:
// Look for digits one or more times: \d+
local expression = regexp2(@"\d+")
local compareString = "91939312"
if (expression.match(compareString))
{
// Do we have a match?
// The KeenIO class can be found at:
// https://github.com/electricimp/reference/blob/master/webservices/keenio/keenio.agent.nut
class KeenIO { ... }
const PROJECT_ID = ""; // Your Project ID (from Keen Dashboard)
const API_KEY = ""; // Your API Write Key (from Keen Dashboard)
// Create the KeenIO Client:
keen <- KeenIO(PROJECT_ID, API_KEY)
class TestDigitalOut(ElectricImpTestCase):
def test_digital_out_can_write_high(self):
value = 1
mbedPin = mbedrpc.DigitalIn(self.mbedrpc, "p10")
self.loadAndRunSquirrelCode("""
hardware.pin5.configure(DIGITAL_OUT);
hardware.pin5.write(%d);
""" % value
assert_that(mbedPin.read(), is_(value))
@electricimp
electricimp / uart.configure.device.nut
Created September 10, 2014 15:59
Check for imp003 when setting up UART
function getImpType()
{
// Determines which kind of imp is in use, returning
// an integer value according to type: eg. imp001 returns 1
if (imp.environment() == ENVIRONMENT_CARD)
{
return 1
}
else
@electricimp
electricimp / spi.configure.device.nut
Created September 10, 2014 15:55
Check for imp003 when setting up SPI
function getImpType()
{
// Determines which kind of imp is in use, returning
// an integer value according to type: eg. imp001 returns 1
if (imp.environment() == ENVIRONMENT_CARD)
{
return 1
}
else
@electricimp
electricimp / i2c.device.nut
Last active August 29, 2015 14:06
Check for imp003 when setting up I2C
function getImpType()
{
// Determines which kind of imp is in use, returning
// an integer value according to type: eg. imp001 returns 1
if (imp.environment() == ENVIRONMENT_CARD)
{
return 1
}
else