Advertisement
Guest User

puck.js Smart Meter

a guest
Aug 31st, 2021
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.88 KB | None | 0 0
  1. function Counter() {
  2. this.clear();
  3. }
  4. /// Clear the counters back to zero
  5. Counter.prototype.clear = function() {
  6. this.totals = {
  7. count : 0, // Overall count
  8. year : new Uint32Array(12), // each month of the year (0..11)
  9. week : new Uint32Array(7), // each day of the week (0..6)
  10. month : new Uint32Array(31), // each day of the month (0..31)
  11. day : new Uint32Array(24), // each hour (0..23)
  12. };
  13. this.historyPeriod = 60*60*1000; // 1 hour in milliseconds
  14. this.history = new Uint32Array(96); // last 96 hours
  15. this.historyTime = 0;
  16. this.lastUpdate = new Date.now();
  17. };
  18.  
  19. Counter.prototype.inc = function(n) {
  20. if (n===undefined) n=1;
  21.  
  22. var t = this.totals;
  23. var d = new Date();
  24. // Totals by time period
  25. t.count+=n;
  26. if (d.getMinutes()==0 && t.day[d.getHours()]>50){
  27. t.day[d.getHours()]=0;
  28. }
  29. if (d.getHours()==0 && d.getMinutes()==0 && t.month[d.getDate()-1]>50){
  30. t.month[d.getDate()-1]=0;
  31. }
  32.  
  33. t.year[d.getMonth()]+=n;
  34. t.week[d.getDay()]+=n;
  35. t.month[d.getDate()-1]+=n;
  36. t.day[d.getHours()]+=n;
  37. print("Day:"+t.day);
  38. print("Month:"+t.month);
  39.  
  40. pulseHour=t.day[d.getHours()];
  41. pulseDay=t.month[d.getDate()-1];
  42.  
  43. // Rolling history
  44. this.historyTime += d.getTime()-this.lastUpdate;
  45. this.lastUpdate = d.getTime();
  46.  
  47. var steps = Math.floor(this.historyTime / this.historyPeriod);
  48.  
  49. if (steps>=0) {
  50. this.historyTime -= steps*this.historyPeriod;
  51. var h = this.history;
  52.  
  53. h.set(new Uint32Array(h.buffer, 4*steps), 0);
  54. h.fill(0, h.length-steps);
  55. h[h.length-1]+=n;
  56. }
  57. };
  58.  
  59. E.setTimeZone(8);
  60. var c = new Counter();
  61. var bootUpTime = Date.now();
  62. var pulseTotal = 0;
  63. var power = 0;
  64. var lastPowerUpdate = Date.now();
  65. var cTime = Date.now();
  66. var pulseHour = 0;
  67. var pulseDay = 0;
  68. var LEDStatus = 0; // 0 = LED Status off 1 = LED Status on
  69. var txpower = 0; //-20, -16, -12, -8, -4, 0, and 4
  70. var lightReading=0;
  71.  
  72. //NRF.setSecurity({display : 1, mitm : 1, bond : 1, passkey : '123456'});
  73. NRF.setTxPower(txpower); //-20, -16, -12, -8, -4, 0, and 4
  74.  
  75. // Update BLE advertising
  76. function update() {
  77. var a = new ArrayBuffer(4);
  78. var dataV = new DataView(a);
  79. var mydate = new Date();
  80. var temp = E.getTemperature();
  81. var batt = E.getBattery();
  82. var dataJSON = {b:batt,t:temp,c:1};
  83. var timeDiff = (Date.now() - cTime);
  84. //Number of pulses per wh - found or set on the meter.
  85. var ppwh = 1; //1000 pulses/kwh = 1 pulse per wh
  86. //Calculate power
  87.  
  88. var totalkWh = pulseTotal/1000;
  89. if (pulseHour>0) {
  90. power = Math.floor(((60*60*1000) / timeDiff)/ppwh);
  91. }else{
  92. power = 0;
  93. }
  94. cTime=Date.now();
  95.  
  96. print(mydate.toString());
  97.  
  98. print("Temperature:"+temp);
  99. print("Battery:"+batt);
  100. print("Current power use:"+power);
  101. print("Total power use:"+totalkWh);
  102. print("Pulses this hour:"+pulseHour);
  103. print("Pulses this day:"+pulseDay);
  104. print("LED status:"+LEDStatus);
  105. print("TX Power:"+txpower);
  106. print("Power on time:"+Math.round(Date.now()-bootUpTime)/1000);
  107.  
  108. NRF.setTxPower(txpower); //-20, -16, -12, -8, -4, 0, and 4
  109. NRF.setAdvertising([{
  110. 0x2a03 : [String(power)],
  111. 0x2a04 : [String(pulseHour)],
  112. 0x2a05 : [String(pulseDay)]}],
  113. {interval: 600,
  114. showName:false
  115. }
  116. // name: "PM \xE2\x9A\xA1"} // default is 375 - save a bit of power
  117. );
  118. power = 0;
  119. }
  120.  
  121. function onInit() {
  122. var sDate = new Date();
  123. //check if Meter LED is stuck in the ON position
  124. var onCount = 0;
  125. var pulledUp = true;
  126. D1.write(0);
  127. setInterval(function() { // every 10 seconds
  128. if (!pulledUp) pinMode(D2,"input_pullup");
  129. if (D2.read()) { // LED is off - all ok
  130. onCount = 0;
  131. pulledUp = true;
  132. print("Power meter LED is not stuck");
  133. } else {
  134. onCount++;
  135. if (onCount>6) { // >60 secs on
  136. pulledUp = false;
  137. print(getPinMode(D2));
  138. print("Power meter LED is STUCK in the ON position for "+onCount*10+" seconds");
  139. }
  140. }
  141. if (!pulledUp) pinMode(D2,"input_pulldown");
  142. }, 10000);
  143.  
  144.  
  145. clearWatch();
  146.  
  147.  
  148. pinMode(D2,"input_pullup");
  149. setWatch(function(e) {
  150. if (!pulledUp) return;
  151.  
  152. c.inc(1);
  153. pulseTotal++;
  154. update();
  155. if (LEDStatus==1){
  156. digitalPulse(LED1,1,1); // show activity
  157. }
  158. }, D2, { repeat:true, edge:"falling" });
  159. update();
  160.  
  161. setWatch(function(e) {
  162. print("Button pressed!");
  163. if (LEDStatus == 0) {
  164. LEDStatus = 1;
  165. txpower = 4;
  166. // NRF.setSecurity({display : 0, mitm : 0, bond : 0, passkey : '910222'});
  167.  
  168. }else{
  169. LEDStatus = 0;
  170. txpower = 0;
  171. // NRF.setSecurity({display : 1, mitm : 1, bond : 1, passkey : '910222'});
  172. }
  173. print("LED status:"+LEDStatus);
  174.  
  175. var isLong = (e.time-e.lastTime)>5;
  176.  
  177. if (isLong) {
  178. print("Long press detected!");
  179. print("Long press duration:"+(e.time-e.lastTime));
  180. print("Rebooting now!");
  181. setTimeout(E.reboot(), 3000);
  182.  
  183. }
  184. }, BTN, {edge:"falling", debounce:50, repeat:true});
  185.  
  186. setInterval(function() {
  187. var mDate = new Date();
  188. var uptime = (Date.now()-bootUpTime)/1000;
  189. power = 0;
  190.  
  191. c.inc(0);
  192.  
  193. if (mDate.getMinutes()==0) {
  194. update();
  195. }
  196. if (mDate.getHours()==0 && mDate.getMinutes()==0 && uptime > 120 ) {
  197. E.reboot();
  198. }
  199.  
  200. NRF.setAdvertising([{
  201. 0x1809 : [E.getTemperature()],
  202. 0x180f : [Puck.getBatteryPercentage()],
  203. 0x2a03 : [String(power)]},
  204. {
  205. 0x2a04 : [String(pulseHour)],
  206. 0x2a05 : [String(pulseDay)]}],
  207. {interval:2000}
  208. );
  209. print("Battery and temp data sent");
  210. print("Battery level %:"+Puck.getBatteryPercentage());
  211. print("Battery level volts:"+NRF.getBattery());
  212. print("Light reading:"+Puck.light());
  213. print("LED status:"+LEDStatus);
  214. print(getPinMode(D2));
  215. print("Power on time:"+Math.round(Date.now()-bootUpTime)/1000);
  216. }, 30000);
  217.  
  218. }
  219.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement