Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ozzieg/6668896 to your computer and use it in GitHub Desktop.
Save ozzieg/6668896 to your computer and use it in GitHub Desktop.
// Tutorial 3: T1 & T2 combined Electric imp April inputs & outputs from JQM web app
//Imp & Agent are written in squirrel-lang.org They should have a .NUT extension.
//but I've used .js so the editor will use colour highlighting.
//T3 April Digital Inputs & Outputs
const ON=1
const OFF=0
// Array that holds the state of each pin
pinState <- [ 0, 0, 0, 0, 0, 0];
// Pins array channel 1 is channelPin[0] in array
local Pins = [ hardware.pin1, hardware.pin2, hardware.pin5, hardware.pin7, hardware.pin8, hardware.pin9 ];
// Register imp
imp.configure("T3: April Digital IN_OUT",[],[]);
server.log("T3: April Digital IN_OUT - v1.2");
// Event handler for state changes
function pinEvent()
{
// Idle for 50ms to allow switch to settle
imp.sleep(0.05);
// Read each switch
for(local _pin=0; _pin<2; _pin++)
{
// Get switch state
local state = Pins[_pin].read();
// State changed?
if(state != pinState[_pin])
{
// Update pin sate
pinState[_pin] = state;
}
}
// send pin state to Agent
agent.send("impValues",{pin1=pinState[0],pin2=pinState[1],rssi=imp.rssi(),vdd=hardware.voltage(),bssid=imp.getbssid(),mac=imp.getmacaddress()});
}
agent.on("setOutputs", function(agentMSG) {
if("pin5" in agentMSG){
server.log("pin5:"+agentMSG.pin5);
Pins[2].write(agentMSG.pin5=="ON" ? ON : OFF);
} else {
server.log("pin7:"+agentMSG.pin7);
Pins[3].write(agentMSG.pin7=="ON" ? ON : OFF);
}
});
// Configure input pins with internal pull-up
Pins[0].configure(DIGITAL_IN_PULLUP, pinEvent);
Pins[1].configure(DIGITAL_IN_PULLUP, pinEvent);
// Configure output pins
Pins[2].configure(DIGITAL_OUT);
Pins[2].write(OFF);
Pins[3].configure(DIGITAL_OUT);
Pins[3].write(OFF);
// Get pin state on bootup
pinEvent();
//T3 April Digital Inputs & Outputs
server.log("Agent: start INOUT v1.0");
_pin1 <- "";
_pin2 <- "";
_tmp <- "";
_rh <- "";
_rssi <-"";
_vdd <-"";
_bssid <-"";
_mac <-"";
function sendIMPvalues(request,res){
//res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Origin", "http://industrialinternet.co.uk");
server.log("reqMethod:"+request.method);
if(request.method=="GET"){
local vars = {
"pin1": ""+_pin1+"",
"pin2": ""+_pin2+"",
"rssi": ""+_rssi+"",
"vdd": ""+_vdd+""
"bssid": ""+_bssid+"",
"mac": ""+_mac+""
}
local jvars = http.jsonencode(vars);
res.send(200,jvars);
}
if(request.method=="OPTIONS"){
res.header("Access-Control-Allow-Methods", "POST, GET, OPTIONS");
res.header("Access-Control-Allow-Headers", "origin, x-csrftoken, content-type, accept");
res.send(200,"OK.OPTIONS");
}
if(request.method=="POST"){
local impAction = http.jsondecode(request.body);
if(("pin5" in impAction) || ("pin7" in impAction)){
device.send("setOutputs", impAction);
res.header("AgentResponce", "Pin_Request_OK");
} else {
res.header("AgentResponce", "Pin_Request_Error");
}
res.header("Access-Control-Expose-Headers", "AgentResponce");
res.send(200,"OK.POST");
}
}
// When HTTP request comes in handel with sendIMPvalues
http.onrequest(sendIMPvalues);
// Process msg from imp
device.on("impValues",function(iv){
server.log("agent: impValues:"+iv.pin1+":"+iv.pin2+":"+iv.rssi+":"+iv.vdd+":"+iv.bssid+":"+iv.mac);
_pin1 = iv.pin1;
_pin2 = iv.pin2;
_rssi = iv.rssi;
_vdd = iv.vdd;
_bssid = iv.bssid;
_mac = iv.mac;
});
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<link rel="apple-touch-startup-image" media="(device-width: 320px)" href="cc_startup.png" />
<link rel="apple-touch-icon" href="cc_screen_icon.png" />
<title>T3: April Digital In & Out</title>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.css" />
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.js"></script>
<script>
var polling = false;
var agentActions = "";
$( function() {
var externalURL ="";
if (localStorage["agentURL"]) {
externalURL=localStorage["agentURL"];
}
var pollRate ="1000";
if (localStorage["pollRate"]) {
pollRate=localStorage["pollRate"];
}
$("[data-role=slider]").bind( "change", function(event, ui) {
//alert("e:"+event+" ui: "+ this.id);
//console.log(this.id,$(this).slider().val());
var slider_value = $(this).slider().val();
//console.log(this.id,slider_value);
impSetValue(this.id,slider_value);
});
function postToIMP(){
if(polling == true){
console.log("delay post @2");
$(function(){
setTimeout(postToIMP, 100);
});
} else {
polling = true;
$.ajax({
type: "POST",
url: "https://agent.electricimp.com/"+externalURL,
data: agentActions,
contentType: "application/json; charset=utf-8",
dataType: "json",
complete: function(jqXHR, textStatus){
_header= jqXHR.getResponseHeader("AgentResponce");
console.log("textStatus: "+jqXHR.responseText+" AgentResponce:"+_header);
polling = false;
}
});
}
}
// data: "value="+_slider_value, //get some data back to the screen if needed
function impSetValue(_pin,_slider_value){
var jobj = {_pin : _slider_value }
var jobj = { _pin : _slider_value };
var obj = {};
pinID = ""+_pin+"";
obj[pinID] = _slider_value;
agentActions = JSON.stringify(obj);
console.log("agentActions:"+ agentActions);
if(polling == true){
console.log("delay post @1");
//var _delay = setTimeout((function(){postToIMP}),100);
$(function(){
setTimeout(postToIMP, 100);
});
} else {
postToIMP();
}
}
function poll(){
if(polling == true) return;
polling = true;
$.ajax({
type: "get",
url: "https://agent.electricimp.com/"+externalURL,
dataType: "json",
success: function(agentMsg) {
//console.log("agent-msg:"+agentMsg.pin1);
updateToggle("#pin1",agentMsg.pin1);
updateToggle("#pin2",agentMsg.pin2);
$("#rssi").html(agentMsg.rssi+" dBm");
$("#vdd").html(agentMsg.vdd+" vdc");
$("#bssid").html(agentMsg.bssid);
$("#mac").html(agentMsg.mac);
polling = false;
},
error: function(err) {
console.log("err"+ err.status)
}
});
}
// Set the rate to poll your imp
setInterval(function(){ poll(); }, pollRate);
function updateToggle(did,_value)
{
var _switch = $(did);
if( _value == "1"){
_switch[0].selectedIndex = 1;
_switch.slider("refresh");
} else {
_switch[0].selectedIndex = 0;
_switch.slider("refresh");
}
}
});
</script>
<style>
@media only screen and (min-width: 321px){
#c1 {width: 390px !important; margin:0 !important; position: relative !important;}
#h1 {width: 420px !important; margin:0 !important; position: relative !important;}
}
.ui-li {list-style-position:inside !important;}
.ui-li h3 {margin:0 0 0 135px; padding:0; width: 50%; }
.ui-li p { padding-top: 20px; border:0px solid blue;}
p.dev { padding-top: 10px; border:0px solid blue;}
p.msg { padding: 0px 0 0 115px; overflow: visible; white-space: pre-wrap; 0}
.ui-li p span{ padding-left: 81px;}
.ui-li-thumb {height:80px !important; left: 140px !important; right : auto; padding:0; opacity:0.6;}
label {display: block; float:left; width:100px; text-align:left; padding-top: 12px; xborder:1px solid red; }
</style>
</head>
<body>
<div data-role="page" id="aprilDigitalOUT"><!-- Home -->
<div data-theme="a" data-role="header">
<h3 style="margin-left: 38px; text-align:left"><span style="font-size:0.86em; padding:0 14px 0 0; color:#909090;">T3: April Digital In & Out</span></h3>
<a href="options.html" data-role="button" data-inline="false" data-icon="gear" data-iconpos="notext" data-iconshadow="false" data-rel="dialog">Agent External URL</a>
</div>
<div data-role="content" id="DIN">
<div data-role="fieldcontain" style="width:320px"><!-- Toggle switches -->
<label for="pin1">pin1 In:</label>
<select id="pin1" data-role="slider">
<option value="off">Off</option>
<option value="on">On</option>
</select>
</div>
<div data-role="fieldcontain" style="width:320px" style="font-size:4.0em;">
<label for="pin2">pin2 In:</label>
<select id="pin2" data-role="slider" >
<option value="off">Off</option>
<option value="on">On</option>
</select>
</div>
<div data-role="fieldcontain" style="width:320px" style="font-size:4.0em;">
<label for="pin5">pin5 Out:</label>
<select id="pin5" data-role="slider" >
<option value="OFF">Off</option>
<option value="ON">On</option>
</select>
</div>
<div data-role="fieldcontain" style="width:320px" style="font-size:4.0em;">
<label for="pin7">pin7 Out:</label>
<select id="pin7" data-role="slider" >
<option value="OFF">Off</option>
<option value="ON">On</option>
</select>
</div>
<ul data-role="listview" data-inset="false" id="statusView" data-theme="d" data-divider-theme="d">
<li style="margin:-3px 0 0 -75px; background-color:#F4F5F6; border-top:0px solid #D8D8D8 !important;">
<p class="dev"><strong>rssi&nbsp;&nbsp;</strong><span style="font-weight: bold; font-size:1.2em;" id="rssi"></span></p>
<p style="padding: 0px 0 0 130px; overflow: visible; white-space: pre-wrap;"></p>
<img src="images/wifi_4.png" id="i3" style="padding-left: 4px;" />
</li>
<li style="margin:-3px 0 0 -75px; background-color:#F4F5F6; border-top:0px solid #D8D8D8 !important;">
<p class="dev"><strong>vdd&nbsp;&nbsp;</strong><span style="font-weight: bold; font-size:1.2em;" id="vdd"></span></p>
<p style="padding: 0px 0 0 130px; overflow: visible; white-space: pre-wrap;">&nbsp;</p>
<img src="images/battery_4.png" style="padding-left: 4px;" />
</li>
<li style="margin:-3px 0 0 -75px; background-color:#F4F5F6; border-top:0px solid #D8D8D8 !important;">
<p class="dev"><strong>bssid</strong><span style="font-weight: bold; font-size:1.2em;" id="bssid"></span></p>
<p style="padding: 0px 0 0 130px; overflow: visible; white-space: pre-wrap;"></p>
<img src="images/gen.png" style="padding-left: 4px;" />
</li>
<li style="margin:-3px 0 0 -75px; background-color:#F4F5F6; border-top:0px solid #D8D8D8 !important;">
<p class="dev"><strong>mac&nbsp;&nbsp;</strong><span style="font-weight: bold; font-size:1.2em;" id="mac"></span></p>
<p style="padding: 0px 0 0 130px; overflow: visible; white-space: pre-wrap;">&nbsp;</p>
<img src="images/gen.png" style="padding-left: 4px;" />
</li>
</ul>
</div> <!-- Eof content -->
</div> <!-- Eof JQM page -->
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment