Skip to content

Instantly share code, notes, and snippets.

@voodootikigod
Created January 24, 2013 20:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save voodootikigod/4627143 to your computer and use it in GitHub Desktop.
Save voodootikigod/4627143 to your computer and use it in GitHub Desktop.
The Squirrel code to create an Electric Imp driven micro wifi analyzer. Presents connected state, signal RSSI value, roundtrip latency, and voltage. Great for remote signal detection and analysis.
// Code below this line is for your node.
// Drive 3.3v LCD Screen using a Sparkfun SerLCD backpack from an Electric IMP
hardware.uart12.configure(9600, 8, PARITY_NONE, 1, NO_RX);
CharactersInLCD <- 16;
ConnectedState <- false;
LastLatency <- 0;
function BuildMessage() {
local signal = ""+imp.rssi();
while (signal.len() < 4) {
signal = " "+signal;
}
local lat = ""+LastLatency+"ms";
while (lat.len() < 6) {
lat = lat+" ";
}
local voltage = ""+hardware.voltage()+"v";
while (voltage.len() < 8) {
voltage = " "+voltage;
}
local msg = (ConnectedState ? "ALIVE" : "DEAD ")+" I "+signal+"dBm" +
lat+" I"+voltage;
return msg;
}
class Led extends InputPort {
name = "led";
function set(prevTime) {
local now = hardware.millis();
ConnectedState = true;
LastLatency = (now - prevTime);
}
}
local ticktock = OutputPort("ticktock");
//Backlight brightness values from 128 to 157
//128 - Backlight off
//140 - 40% on
//150 - 73% on
//157 - 100% on
function LCDBacklight(Brightness)
{
if((Brightness>127)&&(Brightness<158))
{
hardware.uart12.write(124);
hardware.uart12.write(Brightness);
}
}
//Sets the SerLCD module to the correct LCD Type.
//3 - 20 Characters wide
//4 - 16 Characters wide
//5 - 4 Lines
//6 - 2 Lines
function SerLCDTypeSetup(LCDType)
{
if((LCDType>3)||(LCDType<4)||(LCDType<5)||(LCDType<6))
{
hardware.uart12.write(124);
hardware.uart12.write(LCDType);
}
}
//Sets the splash screen.
function LCDSetSplash(Message)
{
LCDClear();
hardware.uart12.write(Message);
imp.sleep( 1 );
hardware.uart12.write(0x7C);
hardware.uart12.write(0x0A);
imp.sleep( 1 );
}
//Splash screen on/off toggle
function LCDToggleSplash()
{
hardware.uart12.write(0x7C);
hardware.uart12.write(0x09);
}
function LCDClear()
{
hardware.uart12.write(254);
hardware.uart12.write(0x01);
hardware.uart12.write(254);
hardware.uart12.write(128);
LCDBoxCursorOff();
}
function LCDMoveCursorRight()
{
hardware.uart12.write(254);
hardware.uart12.write(0x14);
}
function LCDMoveCursorLeft()
{
hardware.uart12.write(254);
hardware.uart12.write(0x10);
}
function LCDScrollRight()
{
hardware.uart12.write(254);
hardware.uart12.write(0x1c);
}
function LCDScrollLeft()
{
hardware.uart12.write(254);
hardware.uart12.write(0x18);
}
function LCDVisualDisplayOn()
{
hardware.uart12.write(254);
hardware.uart12.write(0x0C);
}
function LCDVisualDisplayOff()
{
hardware.uart12.write(254);
hardware.uart12.write(0x08);
}
function LCDUnderlineCursorOn()
{
hardware.uart12.write(254);
hardware.uart12.write(0x0E);
}
function LCDUnderlineCursorOff()
{
hardware.uart12.write(254);
hardware.uart12.write(0x0C);
}
function LCDBoxCursorOn()
{
hardware.uart12.write(254);
hardware.uart12.write(0x0D);
}
function LCDBoxCursorOff()
{
hardware.uart12.write(254);
hardware.uart12.write(0x0C);
}
// Position is zero based
function LCDSetCursorPosition(Line,Position)
{
local LineBaseAddress = 0;
if(CharactersInLCD==16)
{
if(Line==2)
{
LineBaseAddress = 64;
}
if(Line==3)
{
LineBaseAddress = 16;
}
if(Line==4)
{
LineBaseAddress = 80;
}
}
if(CharactersInLCD==20)
{
if(Line==2)
{
LineBaseAddress = 64;
}
if(Line==3)
{
LineBaseAddress = 20;
}
if(Line==4)
{
LineBaseAddress = 84;
}
}
hardware.uart12.write(254);
hardware.uart12.write(LineBaseAddress + Position + 128);
}
function LCDWriteMessage(LCDMessage)
{
hardware.uart12.write(LCDMessage);
}
function ping() {
ConnectedState = false;
ticktock.set(hardware.millis());
}
//Main Execution loop
function loop()
{
// On every loop, display current status
LCDClear();
LCDWriteMessage(BuildMessage());
// The sent up next status attempt ping
imp.wakeup(0.5, ping);
// Queue next loop execution
imp.wakeup(1.0, loop);
}
//Setup Activities - only executed once
imp.configure("WiFlea", [Led()], [ticktock], {});
// Initial clear and set
LCDClear();
LCDWriteMessage(BuildMessage());
//Pause 2 Seconds before starting the main loop - adjust to taste
imp.wakeup(1.0, loop);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment