Last active
December 21, 2015 02:39
-
-
Save grayal/6236550 to your computer and use it in GitHub Desktop.
electric imp code for remote
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Connected Remote | |
// | |
// code for a simple and fun remote - instructions for me get sent to my iPhone... wherever I am...! | |
// 3 simple buttons for 3 simple requests / instructions... | |
// * Cup of tea pelase! | |
// * Coffee time! | |
// * Home time! | |
// | |
// LED also fitted that flashes when a button is pressed | |
// | |
// side effect is a TMP36 which will monitor the temperature and log to cosm / xively | |
// | |
// code not final - could tidy up eg single fn with lambdas init for callbacks, needs power mgmt if battery powered | |
// Hardware connected at: | |
// Buttons on pins 2, 5, 9 | |
// Green LED on pin 7 | |
// Temperature sensor on pin 8 | |
// globals | |
local id = hardware.getimpeeid(); | |
local hwVoltage = hardware.voltage(); | |
local countDown = 0; | |
// comms | |
local output_temperature = OutputPort("LoungeTemperature", "number"); | |
local output_instruction = OutputPort("Instruction", "string"); | |
local output_instruction1 = OutputPort("Instruction1", "string"); | |
local output_instruction2 = OutputPort("Instruction2", "string"); | |
local output_instruction3 = OutputPort("Instruction3", "string"); | |
// Prowl | |
local my_apiKey = "add_here"; | |
local my_application = "Lounge Remote" | |
// http only in beta so hardcode urls and send to 3 outputs... sigh... ;-) | |
//"https://api.prowlapp.com/publicapi/add?apikey=add_here&application=Lounge%20Remote&event=Cup%20of%20tea%20please%21" | |
//"https://api.prowlapp.com/publicapi/add?apikey=add_here&application=Lounge%20Remote&event=Coffee%20time%21" | |
//"https://api.prowlapp.com/publicapi/add?apikey=add_here&application=Lounge%20Remote&event=Home%20time...%21" | |
/*function Push(_apiKey, _application, _event) | |
{ | |
local url = "https://api.prowlapp.com/publicapi/add?"; | |
local query = { apikey = _apiKey, application = _application, event = _event }; | |
url += http.urlencode(query); | |
server.log("Posting: " + url); | |
local result = http.post(url, {}, "").sendsync(); | |
server.log(result.statuscode + ": " + result.body); | |
}*/ | |
// Button fns | |
function readButtons() | |
{ | |
local btn1 = hardware.pin2.read(); | |
local btn2 = hardware.pin5.read(); | |
local btn3 = hardware.pin9.read(); | |
server.log(format("%d, %d, %d", btn1, btn2, btn3)); | |
} | |
function getMessage(btnId) | |
{ | |
if(btnId == 1) | |
{ | |
output_instruction1.set("T"); | |
return "Cup of tea please!"; | |
} | |
if(btnId == 2) | |
{ | |
output_instruction2.set("C"); | |
return "Coffee time!"; | |
} | |
else if(btnId == 3) | |
{ | |
output_instruction3.set("H"); | |
return "Home time...!" | |
} | |
} | |
function btnChanged(btnId, btnState) | |
{ | |
// if buttonState is 0, the button is pushed | |
if (btnState == 0) | |
{ | |
server.log(format("Button %d Pressed!", btnId)); | |
output_instruction.set(getMessage(btnId)); | |
//Push(my_apiKey, my_application, getMessage(btnId)); | |
if(countDown > 0) | |
{ | |
countDown = 10; | |
} | |
else | |
{ | |
countDown = 10; | |
blink(); | |
} | |
} | |
else | |
{ | |
server.log(format("Button %d Released!", btnId)); | |
} | |
} | |
function pin2changed() | |
{ | |
local btnState = hardware.pin2.read(); | |
btnChanged(1, btnState); | |
} | |
function pin5changed() | |
{ | |
local btnState = hardware.pin5.read(); | |
btnChanged(2, btnState); | |
} | |
function pin9changed() | |
{ | |
local btnState = hardware.pin9.read(); | |
btnChanged(3, btnState); | |
} | |
// LED fns | |
ledState <- 0; | |
function blink() | |
{ | |
if(countDown > 0) | |
{ | |
ledState = ledState?0:1; | |
hardware.pin7.write(ledState); | |
countDown--; | |
imp.wakeup(0.1, blink); | |
} | |
else | |
{ | |
hardware.pin7.write(1); | |
} | |
} | |
// temperature fns | |
// read temperature sensor | |
function readTemperature() | |
{ | |
local rawValue = hardware.pin8.read(); | |
local voltage = hwVoltage * rawValue / 65535.0; | |
// using TMP36: resolution is 10 mV / degree centigrade | |
//(500 mV offset) to make negative temperatures an option | |
local realTemp = (voltage - 0.5) * 100.0; | |
server.log(format("%3.1fC", realTemp)); | |
output_temperature.set(realTemp); | |
} | |
// main loop | |
function mainLoop() | |
{ | |
readTemperature(); | |
//readButtons(); | |
imp.wakeup(60.0, mainLoop); | |
} | |
server.log(id); | |
server.log("Configuring app: Remote..."); | |
// buttons | |
hardware.pin2.configure(DIGITAL_IN_PULLUP, pin2changed); | |
hardware.pin5.configure(DIGITAL_IN_PULLUP, pin5changed); | |
hardware.pin9.configure(DIGITAL_IN_PULLUP, pin9changed); | |
// leds | |
hardware.pin7.configure(DIGITAL_OUT); | |
// temperature sensor | |
hardware.pin8.configure(ANALOG_IN); | |
server.log(format("Running at %.2f V", hwVoltage)); | |
// and configure | |
imp.configure("Remote", [], [output_instruction, output_temperature, output_instruction1, output_instruction2, output_instruction3]); | |
server.log("Connected to Remote."); | |
// LEDs off | |
hardware.pin7.write(1); | |
// and go | |
mainLoop(); | |
// EOF |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment