Toggle navigation
Bootsnipp
Bootstrap
For
CSS Frameworks
Bootstrap
Foundation
Semantic UI
Materialize
Pure
Bulma
References
CSS Reference
Tools
Community
Page Builder
Form Builder
Button Builder
Icon Search
Dan's Tools
Diff / Merge
Color Picker
Keyword Tool
Web Fonts
.htaccess Generator
Favicon Generator
Site Speed Test
Snippets
Featured
Tags
By Bootstrap Version
4.1.1
4.0.0
3.3.0
3.2.0
3.1.0
3.0.3
3.0.1
3.0.0
2.3.2
Register
Login
"decode frame"
Bootstrap 3.3.0 Snippet by
PeterElsys
3.3.0
jQuery
Preview
HTML
JS
View Full Screen
Fork
Fork this
3.2K
 
0 Fav
Post to Facebook
Tweet this
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css"> <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script> <script src="//code.jquery.com/jquery-1.11.1.min.js"></script> <!------ Include the above in your HEAD tag ----------> <div class="container"> <div class="row"> <div class="col-md-4"> <input class="form-control input-md" type="text" id="inputData" name="inputData" value="123"> </div> </div> <div class="row"> <div class="col-md-4"> <a href="#" id="convert" class="btn btn-sm btn-info"><span class="glyphicon glyphicon-refresh"></span> Convert</a> </div> </div> <div class="row"> <div class="col-md-4"> <textarea class="form-control" id="result" name="textarea" rows="10"></textarea> </div> </div> </div>
/* ______ _ _______ _______ | ____| | / ____\ \ / / ____| | |__ | | | (___ \ \_/ / (___ | __| | | \___ \ \ / \___ \ | |____| |____ ____) | | | ____) | |______|______|_____/ |_| |_____/ ELSYS simple payload decoder. Use it as it is or remove the bugs :) www.elsys.se peter@elsys.se */ const TYPE_TEMP =0x01; //temp 2 bytes -3276.8°C -->3276.7°C const TYPE_RH =0x02; //Humidity 1 byte 0-100% const TYPE_ACC =0x03; //acceleration 3 bytes X,Y,Z -128 --> 127 +/-63=1G const TYPE_LIGHT =0x04; //Light 2 bytes 0-->65535 Lux const TYPE_MOTION =0x05; //No of motion 1 byte 0-255 const TYPE_CO2 =0x06; //Co2 2 bytes 0-65535 ppm const TYPE_VDD =0x07; //VDD 2byte 0-65535mV const TYPE_ANALOG1 =0x08; //VDD 2byte 0-65535mV const TYPE_GPS =0x09; //3bytes lat 3bytes long binary const TYPE_PULSE1 =0x0A; //2bytes relative pulse count function bin16dec(bin) { var num=bin&0xFFFF; if (0x8000 & num) num = - (0x010000 - num); return num; } function bin8dec(bin) { var num=bin&0xFF; if (0x80 & num) num = - (0x0100 - num); return num; } function hexToBytes(hex) { for (var bytes = [], c = 0; c < hex.length; c += 2) bytes.push(parseInt(hex.substr(c, 2), 16)); return bytes; } function DecodeElsysPayload(data){ var obj = new Object(); for(i=0;i<data.length;i++){ console.log(data[i]); switch(data[i]){ case TYPE_TEMP: //Temperature var temp=(data[i+1]<<8)|(data[i+2]); temp=bin16dec(temp); obj.temperature=temp; i+=2; break case TYPE_RH: //Humidity var rh=(data[i+1]); obj.humidity=rh; i+=1; break case TYPE_ACC: //Acceleration obj.x=bin8dec(data[i+1]); obj.y=bin8dec(data[i+2]); obj.z=bin8dec(data[i+3]); i+=3; break case TYPE_LIGHT: //Light var light=(data[i+1]<<8)|(data[i+2]); obj.light=light; i+=2; break case TYPE_MOTION: //Motion sensor(PIR) var motion=(data[i+1]); obj.motion=motion; i+=1; break case TYPE_CO2: //CO2 var co2=(data[i+1]<<8)|(data[i+2]); obj.co2=co2; i+=2; break case TYPE_VDD: //Battery level var vdd=(data[i+1]<<8)|(data[i+2]); obj.vdd=vdd; i+=2; break case TYPE_ANALOG1: //Analog input 1 var analog1=(data[i+1]<<8)|(data[i+2]); obj.analog1=analog1; i+=2; break case TYPE_GPS: //gps obj.lat=(data[i+1]<<16)|(data[i+2]<<8)|(data[i+3]); obj.long=(data[i+4]<<16)|(data[i+5]<<8)|(data[i+6]); i+=6; break case TYPE_PULSE1: //Pulse input 1 var pulse1=(data[i+1]<<8)|(data[i+2]); obj.pulse1=pulse1; i+=2; break } } return obj; } $(function() { $("#convert").click(function() { console.log("click"); var res=DecodeElsysPayload(hexToBytes($('#inputData').val())); var json=JSON.stringify(res,null,4); $('#result').html(json); }); });
Related:
See More
Template
Paper Dashboard Pro
Questions / Comments:
Post
Posting Guidelines
Formatting
- Now
×
Close
Donate
BTC: 12JxYMYi6Vt3mx3hcmP3B2oyFiCSF3FhYT
ETH: 0xCD715b2E3549c54A40e6ecAaFeB82138148a6c76