tag:blogger.com,1999:blog-9996776896602917762024-03-13T15:42:36.397-07:00Like ProgrammingCarthttp://www.blogger.com/profile/16125921576345147186noreply@blogger.comBlogger1125tag:blogger.com,1999:blog-999677689660291776.post-30168553753376880882014-12-15T22:49:00.002-08:002014-12-15T22:49:51.842-08:00My IoT<script>
//https://thingspeak.com/channels
var Write_API_Key = "PO699JNJX2EA1NHM";
var Read_API_Key = "PO699JNJX2EA1NHM";
var CHANNEL_ID = "20825";
</script>
<style>
.Out{
background-color: #FFF;
border: 1px solid #000;
}
table {
background-color: #FFF;
border: 2px solid #000;
}
#Read{
display:none;
cursor:pointer;
font-weight:bold;
font-size:15px;
text-align:center;
}
#Write{
display:none;
cursor:pointer;
font-weight:bold;
font-size:15px;
text-align:center;
}
</style>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
var bgd ='background-color';
var bgd_on = '#6F0';
var bgd_off = '#FFF';
var bgd_req = '#FF0';
var writedata =0;
var ReadAPI = "http://api.thingspeak.com/channels/"+CHANNEL_ID+"/feed/last.json?key="+Read_API_Key;
var WriteAPI = "http://api.thingspeak.com/update?key="+Write_API_Key;
$(function(){
ReadCycle();
$("#WriteVal").text("0");
$(".wait").css( "display", "none" );
$("#Write").css( "display", "block" );
$(".waitr").css( "display", "none" );
$("#Read").css( "display", "block" );
$(".Out").click(function() {
writedata =0;
$(".Output").each(function(index) {
var bin=Math.pow(2, index);
writedata += $(this).prop("checked")*bin;
});
$("#WriteVal").text(writedata);
SetReq(writedata);
});
$("#Write").click(function() {
$("#Write").css( "display", "none" );
$(".wait").css( "display", "block" );
$(".Output").each(function(index) {
$(this).prop("disabled",true);
});
$(".response" ).html('<pre>...wait...</pre>');
var writeval = $("#WriteVal").text();
$.getJSON(WriteAPI+"&field2="+writeval,{format: "json"}).done(function( data ) {
if(data==0)alert("Try Again");
$(".response" ).html('<pre>'+JSON.stringify(data)+'</pre>');
$(".Output").each(function(index) {
$(this).prop("disabled",false);
});
$(".wait").css( "display", "none" );
$("#Write").css( "display", "block" );
});
});
$("#Read").click(function() {
ReadField();
});
function SetAct(value) {
$(".Out").each(function(index) {
var bin=Math.pow(2, index);
(((value&bin)==bin)?$(this).css(bgd, bgd_on):$(this).css(bgd, bgd_off));
});
}
function SetReq(value) {
$(".Out").each(function(index) {
var bin=Math.pow(2, index);
(((value&bin)==bin)?$(this).css(bgd, bgd_req):$(this).css(bgd, bgd_off));
});
}
function SetCheck(value) {
$(".Output").each(function(index) {
var bin=Math.pow(2, index);
(((value&bin)==bin)?$(this).prop("checked",true):$(this).prop("checked",false));
});
$("#WriteVal").text(value);
}
function ReadField() {
$(".response" ).text("...wait...");
$.getJSON(ReadAPI,{format: "json"}).done(function( data ) {
$(".response" ).html('<pre>'+JSON.stringify(data)+'</pre>');
var read1=data.field1;
var read2=data.field2;
$(".Req").text("Request Value = " + read2);
$(".Act").text("Actual Value = " + read1);
$(".RCrun").text("");
if( read1 != null && read2 != null){
SetAct(read1);
SetCheck(read1);
return;
}
if(read1 == null && read2 != null){
SetReq(read2);
SetCheck(read2);
return;
}
});
}
function ReadCycle(){
$(".RCrun").text(" RUN....");
ReadField();
setTimeout(ReadCycle, 15000); //cycle 15s
}
});
</script>
<b>PLC Remote Control via the Internet of Things (IoT)</b><br/>
YELLOW = Request , GREEN = Actual<br/>
<table border="2" cellspacing="4" cellpadding="6">
<tr>
<th scope="col" class="Out"><input type="checkbox" class="Output"><label>0</label></th>
<th scope="col" class="Out"><input type="checkbox" class="Output"><label>1</label></th>
<th scope="col" class="Out"><input type="checkbox" class="Output"><label>2</label></th>
<th scope="col" class="Out"><input type="checkbox" class="Output"><label>3</label></th>
<th class="Out" scope="col"><div align="center">
<input type="button" id="Read" value=" R e a d ">
<span class="waitr">...wait...</span></div></th>
</tr>
<tr>
<th colspan="2" scope="col">Write</th>
<th colspan="2" id="WriteVal" scope="col">x</th>
<th scope="col"><div align="center">
<input type="button" id="Write" value="Write">
<span class="wait">...wait...</span></div></th>
</tr>
</table>
<br/>
<b>Read Cycle:</b><span class="RCrun"></span><br/>
<span class="Req">Request Value = </span><br/>
<span class="Act">Actual Value = </span><br/>
<b>Response:</b><br/>
<span class="response">...wait...</span>
Carthttp://www.blogger.com/profile/16125921576345147186noreply@blogger.com0