Skip to content

Instantly share code, notes, and snippets.

View ElectricImpSampleCode's full-sized avatar

Electric Imp, Inc. ElectricImpSampleCode

View GitHub Profile
@ElectricImpSampleCode
ElectricImpSampleCode / pi_logger.py
Last active August 29, 2015 14:15
Raspberry Pi Python code for unattended logging
#!/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 / Logger.class.nut
Last active August 29, 2015 14:15
Squirrel class for logging via UART
class Logger {
_serial = null;
constructor (impUART) {
if (impUART != null) {
_serial = impUART;
_serial.configure(115200, 8, PARITY_NONE, 1, NO_RX);
}
}
@ElectricImpSampleCode
ElectricImpSampleCode / pwmServo.device.nut
Last active August 29, 2015 14:15
Example code to control a servo motor connected to an imp. It makes use of the imp’s PWM_OUT GPIO pin setting.
// 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 August 29, 2015 14:15
Example code to blink an LED connected to an imp. It makes use of the imp’s DIGITAL_OUT GPIO pin setting.
// 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 / digitalin-led.device.nut
Last active August 29, 2015 14:15
Example code to sample a button connected to an imp. It makes use of the imp’s DIGITAL_IN GPIO pin setting.
// 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 / analoginPotentio.device.nut
Last active August 29, 2015 14:15
Example code to sample a potentiometer connected to an imp. It makes use of the imp’s ANALOG_IN GPIO pin setting.
// 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 / analogTherm.device.nut
Last active August 29, 2015 14:15
Example code to sample a thermistor connected to an imp. It makes use of the imp’s ANALOG_IN GPIO pin setting.
// 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 / loop.device.nut
Last active August 29, 2015 14:16
An example of a simple main program loop written in an impOS-friendly way.
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 August 29, 2015 14:16
Code examples for agent.send() troubleshooting guide.
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 August 29, 2015 14:18
Code for the London Bus Time Tracker learning card.
// 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;