-
-
Save fokcuk/ab3171add0c45707eb526cb26bcf398c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function onInit() { | |
NRF.setTxPower(4); | |
E.enableWatchdog(120); | |
} | |
var f = require("Storage"); | |
if (getTime() < Number(f.readJSON("my_date"))) { | |
setTime(Number(f.readJSON("my_date"))+5); | |
} | |
var watthours = 0; | |
var bootUpTime = Date.now(); | |
var lastpulse = Date.now(); | |
var pulsetime = 0; | |
var onCount = 0; | |
var pulledUp = true; | |
var watts = 0; | |
E.setTimeZone(8); | |
var wattsfromfile = Number(f.readJSON("watt_h")); | |
if (wattsfromfile > watthours) { | |
watthours = wattsfromfile; | |
} else { | |
f.writeJSON("watt_h",watthours); | |
} | |
function update() { | |
if (Date.now() - lastpulse > 31) { | |
watts = 0; | |
} | |
NRF.setAdvertising([ | |
0x0e,0xff,0xa0,0x83, // id | |
watthours,watthours>>8,watthours>>16,watthours>>24, // total usage | |
watts,watts>>8, // current usage | |
Puck.getBatteryPercentage(), // battery % | |
Number((NRF.getBattery()).toFixed(1))*10, // battery voltage | |
Math.random()*256|0, // random packet id | |
0x00,0x00 // not used | |
],{ | |
name: "PM", | |
interval: 1000 | |
}); | |
} | |
setInterval(function() { | |
f.writeJSON("my_date",getTime()); | |
update(); | |
}, 30000); | |
function rate() { | |
pulsetime = Date.now() - lastpulse; | |
watts = 3600000 / pulsetime; | |
watts = Number(watts.toFixed(0)); | |
lastpulse = Date.now(); | |
} | |
function wattReset() { | |
var d = new Date(); | |
if (d.getHours()==0 && d.getMinutes()==0 && watthours>100 ){ | |
watthours = 0; | |
} | |
} | |
function debugme() { | |
var mydate = new Date(); | |
print(mydate.toString()); | |
print("Temperature:"+E.getTemperature()); | |
print("Battery:"+E.getBattery()); | |
print("Current power use:"+watts); | |
print("Total power use:"+watthours); | |
print("Power on time:"+Math.round(Date.now()-bootUpTime)/1000); | |
print("Light reading:"+Puck.light()); | |
print("Wh from file:"+Number(f.read("watt_h"))); | |
print(process.memory()); | |
print(getPinMode(D2)); | |
} | |
D1.write(0); | |
setInterval(function() { | |
if (!pulledUp) pinMode(D2,"input_pullup"); | |
if (D2.read()) { | |
onCount = 0; | |
pulledUp = true; | |
print("Power meter LED is not stuck"); | |
} else { | |
onCount++; | |
if (onCount>6) { | |
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; | |
wattReset(); | |
watthours++; | |
f.writeJSON("watt_h",watthours); | |
rate(); | |
update(); | |
digitalPulse(LED1,1,1); | |
}, D2, { repeat:true, edge:"falling" }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment