Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function Counter() {
- this.clear();
- }
- /// Clear the counters back to zero
- Counter.prototype.clear = function() {
- this.totals = {
- count : 0, // Overall count
- year : new Uint32Array(12), // each month of the year (0..11)
- week : new Uint32Array(7), // each day of the week (0..6)
- month : new Uint32Array(31), // each day of the month (0..31)
- day : new Uint32Array(24), // each hour (0..23)
- };
- this.historyPeriod = 60*60*1000; // 1 hour in milliseconds
- this.history = new Uint32Array(96); // last 96 hours
- this.historyTime = 0;
- this.lastUpdate = new Date.now();
- };
- Counter.prototype.inc = function(n) {
- if (n===undefined) n=1;
- var t = this.totals;
- var d = new Date();
- // Totals by time period
- t.count+=n;
- if (d.getMinutes()==0 && t.day[d.getHours()]>50){
- t.day[d.getHours()]=0;
- }
- if (d.getHours()==0 && d.getMinutes()==0 && t.month[d.getDate()-1]>50){
- t.month[d.getDate()-1]=0;
- }
- t.year[d.getMonth()]+=n;
- t.week[d.getDay()]+=n;
- t.month[d.getDate()-1]+=n;
- t.day[d.getHours()]+=n;
- print("Day:"+t.day);
- print("Month:"+t.month);
- pulseHour=t.day[d.getHours()];
- pulseDay=t.month[d.getDate()-1];
- // Rolling history
- this.historyTime += d.getTime()-this.lastUpdate;
- this.lastUpdate = d.getTime();
- var steps = Math.floor(this.historyTime / this.historyPeriod);
- if (steps>=0) {
- this.historyTime -= steps*this.historyPeriod;
- var h = this.history;
- h.set(new Uint32Array(h.buffer, 4*steps), 0);
- h.fill(0, h.length-steps);
- h[h.length-1]+=n;
- }
- };
- E.setTimeZone(8);
- var c = new Counter();
- var bootUpTime = Date.now();
- var pulseTotal = 0;
- var power = 0;
- var lastPowerUpdate = Date.now();
- var cTime = Date.now();
- var pulseHour = 0;
- var pulseDay = 0;
- var LEDStatus = 0; // 0 = LED Status off 1 = LED Status on
- var txpower = 0; //-20, -16, -12, -8, -4, 0, and 4
- var lightReading=0;
- //NRF.setSecurity({display : 1, mitm : 1, bond : 1, passkey : '123456'});
- NRF.setTxPower(txpower); //-20, -16, -12, -8, -4, 0, and 4
- // Update BLE advertising
- function update() {
- var a = new ArrayBuffer(4);
- var dataV = new DataView(a);
- var mydate = new Date();
- var temp = E.getTemperature();
- var batt = E.getBattery();
- var dataJSON = {b:batt,t:temp,c:1};
- var timeDiff = (Date.now() - cTime);
- //Number of pulses per wh - found or set on the meter.
- var ppwh = 1; //1000 pulses/kwh = 1 pulse per wh
- //Calculate power
- var totalkWh = pulseTotal/1000;
- if (pulseHour>0) {
- power = Math.floor(((60*60*1000) / timeDiff)/ppwh);
- }else{
- power = 0;
- }
- cTime=Date.now();
- print(mydate.toString());
- print("Temperature:"+temp);
- print("Battery:"+batt);
- print("Current power use:"+power);
- print("Total power use:"+totalkWh);
- print("Pulses this hour:"+pulseHour);
- print("Pulses this day:"+pulseDay);
- print("LED status:"+LEDStatus);
- print("TX Power:"+txpower);
- print("Power on time:"+Math.round(Date.now()-bootUpTime)/1000);
- NRF.setTxPower(txpower); //-20, -16, -12, -8, -4, 0, and 4
- NRF.setAdvertising([{
- 0x2a03 : [String(power)],
- 0x2a04 : [String(pulseHour)],
- 0x2a05 : [String(pulseDay)]}],
- {interval: 600,
- showName:false
- }
- // name: "PM \xE2\x9A\xA1"} // default is 375 - save a bit of power
- );
- power = 0;
- }
- function onInit() {
- var sDate = new Date();
- //check if Meter LED is stuck in the ON position
- var onCount = 0;
- var pulledUp = true;
- D1.write(0);
- setInterval(function() { // every 10 seconds
- if (!pulledUp) pinMode(D2,"input_pullup");
- if (D2.read()) { // LED is off - all ok
- onCount = 0;
- pulledUp = true;
- print("Power meter LED is not stuck");
- } else {
- onCount++;
- if (onCount>6) { // >60 secs on
- pulledUp = false;
- print(getPinMode(D2));
- print("Power meter LED is STUCK in the ON position for "+onCount*10+" seconds");
- }
- }
- if (!pulledUp) pinMode(D2,"input_pulldown");
- }, 10000);
- clearWatch();
- pinMode(D2,"input_pullup");
- setWatch(function(e) {
- if (!pulledUp) return;
- c.inc(1);
- pulseTotal++;
- update();
- if (LEDStatus==1){
- digitalPulse(LED1,1,1); // show activity
- }
- }, D2, { repeat:true, edge:"falling" });
- update();
- setWatch(function(e) {
- print("Button pressed!");
- if (LEDStatus == 0) {
- LEDStatus = 1;
- txpower = 4;
- // NRF.setSecurity({display : 0, mitm : 0, bond : 0, passkey : '910222'});
- }else{
- LEDStatus = 0;
- txpower = 0;
- // NRF.setSecurity({display : 1, mitm : 1, bond : 1, passkey : '910222'});
- }
- print("LED status:"+LEDStatus);
- var isLong = (e.time-e.lastTime)>5;
- if (isLong) {
- print("Long press detected!");
- print("Long press duration:"+(e.time-e.lastTime));
- print("Rebooting now!");
- setTimeout(E.reboot(), 3000);
- }
- }, BTN, {edge:"falling", debounce:50, repeat:true});
- setInterval(function() {
- var mDate = new Date();
- var uptime = (Date.now()-bootUpTime)/1000;
- power = 0;
- c.inc(0);
- if (mDate.getMinutes()==0) {
- update();
- }
- if (mDate.getHours()==0 && mDate.getMinutes()==0 && uptime > 120 ) {
- E.reboot();
- }
- NRF.setAdvertising([{
- 0x1809 : [E.getTemperature()],
- 0x180f : [Puck.getBatteryPercentage()],
- 0x2a03 : [String(power)]},
- {
- 0x2a04 : [String(pulseHour)],
- 0x2a05 : [String(pulseDay)]}],
- {interval:2000}
- );
- print("Battery and temp data sent");
- print("Battery level %:"+Puck.getBatteryPercentage());
- print("Battery level volts:"+NRF.getBattery());
- print("Light reading:"+Puck.light());
- print("LED status:"+LEDStatus);
- print(getPinMode(D2));
- print("Power on time:"+Math.round(Date.now()-bootUpTime)/1000);
- }, 30000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement