Skip to content

Instantly share code, notes, and snippets.

Avatar

Electric Imp, Inc. ElectricImpSampleCode

View GitHub Profile
@ElectricImpSampleCode
ElectricImpSampleCode / Logger.class.nut
Last active Aug 29, 2015
Squirrel class for logging via UART
View Logger.class.nut
class Logger {
_serial = null;
constructor (impUART) {
if (impUART != null) {
_serial = impUART;
_serial.configure(115200, 8, PARITY_NONE, 1, NO_RX);
}
}
@ElectricImpSampleCode
ElectricImpSampleCode / pi_logger.py
Last active Aug 29, 2015
Raspberry Pi Python code for unattended logging
View pi_logger.py
#!/usr/bin/env python
import serial
s = serial.Serial("/dev/ttyAMA0", baudrate=115200, timeout=3.0)
message = ""
while True:
logChar = s.read()
if logChar:
@ElectricImpSampleCode
ElectricImpSampleCode / digitalin-led.device.nut
Last active Aug 29, 2015
Example code to sample a button connected to an imp. It makes use of the imp’s DIGITAL_IN GPIO pin setting.
View digitalin-led.device.nut
// Alias the GPIO pin as 'button'
button <- hardware.pin1;
function buttonPress() {
local state = button.read();
if (state == 1) {
// The button is released
server.log("Release");
} else {
// The button is pressed
@ElectricImpSampleCode
ElectricImpSampleCode / pwmServo.device.nut
Last active Aug 29, 2015
Example code to control a servo motor connected to an imp. It makes use of the imp’s PWM_OUT GPIO pin setting.
View pwmServo.device.nut
// These constants may be different for your servo
const SERVO_MIN = 0.03;
const SERVO_MAX = 0.1;
// Create global variable for the pin to which the servo is connected
// then configure the pin for PWM
servo <- hardware.pin7;
servo.configure(PWM_OUT, 0.02, SERVO_MIN);
// Define a function to control the servo.
@ElectricImpSampleCode
ElectricImpSampleCode / digitalout-led.device.nut
Last active Aug 29, 2015
Example code to blink an LED connected to an imp. It makes use of the imp’s DIGITAL_OUT GPIO pin setting.
View digitalout-led.device.nut
// Assign pin9 to a global variable
led <- hardware.pin9;
// Configure LED pin for DIGITAL_OUTPUT, and set initial state to off
led.configure(DIGITAL_OUT, 0);
// Assign a global variable to track current state of LED pin
state <- 0;
// Function to blink LED
@ElectricImpSampleCode
ElectricImpSampleCode / analogTherm.device.nut
Last active Aug 29, 2015
Example code to sample a thermistor connected to an imp. It makes use of the imp’s ANALOG_IN GPIO pin setting.
View analogTherm.device.nut
// Assign hardware.pin5 to a global variable, therm
therm <- hardware.pin5;
// Configure pin5/therm for analog input
therm.configure(ANALOG_IN);
// These constants are particular to the thermistor we're using.
// Check your datasheet for what values you should be using
const B_THERM = 3977.0;
const T0_THERM = 298.15;
@ElectricImpSampleCode
ElectricImpSampleCode / analoginPotentio.device.nut
Last active Aug 29, 2015
Example code to sample a potentiometer connected to an imp. It makes use of the imp’s ANALOG_IN GPIO pin setting.
View analoginPotentio.device.nut
// Alias pin 2 as pot
pot <- hardware.pin2;
// Configure the pin for analog input
pot.configure(ANALOG_IN);
// Define a function to poll the pin every 0.1 seconds
function poll() {
// Read the pin and log its value
server.log(pot.read());
@ElectricImpSampleCode
ElectricImpSampleCode / loop.device.nut
Last active Aug 29, 2015
An example of a simple main program loop written in an impOS-friendly way.
View loop.device.nut
function loop() {
// Get an integer reading from the imp's light sensor and relay it via UART
uart57.write("Current light level is: " + hardware.lightlevel() + "\r\n");
// Set the imp to check again in two seconds' time
imp.wakeup(2.0, loop);
}
// Start of program
// Initiate main loop
@ElectricImpSampleCode
ElectricImpSampleCode / ack.device.nut
Last active Aug 29, 2015
Code examples for agent.send() troubleshooting guide.
View ack.device.nut
server.settimeoutpolicy(SUSPEND_ON_ERROR, WAIT_FOR_ACK, 10);
// Program does some work...
foreach (count, blob in buffer) {
// Every send will not return until the server has ACK’d
// Assume no timeout for this example
local outcome = agent.send(“process.data”, blob);
server.log(“Blob %u sent to server”, count + 1);
}
@ElectricImpSampleCode
ElectricImpSampleCode / bus.agent.nut
Last active Aug 29, 2015
Code for the London Bus Time Tracker learning card.
View bus.agent.nut
// Agent Code
class TFLData {
_apikey = null;
_appID = null;
_stops = null;
_url = null;
_listTimes = 4;
constructor (apiKey = null, appID = null, busStops = null, listTimes = 4) {
_apikey = apiKey;