Skip to content

Commit

Permalink
Add v2
Browse files Browse the repository at this point in the history
  • Loading branch information
amadeuspzs committed Sep 18, 2020
1 parent b84c7c4 commit e1a0652
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ Hardware/software based on https://openenergymonitor.org/
5. 2 x 10k resistors
6. 10 uF capacitor

## Flowchart
## v1

![v1 flow](esp8266-smart-meter.png)
Simple timer-based circuit that publishes latest readings every 5 seconds.

![v1 flow](esp8266-smart-meter-v1.png)

## v2

Timer-based circuit that publishes average of all readings every 5 seconds.

Note this skews towards higher power readings as these interrupt at a higher frequency and will be represented more often in the accumulator.

![v1 flow](esp8266-smart-meter-v2.png)
File renamed without changes
Binary file added esp8266-smart-meter-v2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 17 additions & 2 deletions smart-meter/smart-meter.ino
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ unsigned long currentTime, lastMessage, timeElapsed;
// power and energy
double power, elapsedkWh, T;

// For more accurate power average
double powerAccumulator, powerAverage;
long powerAccumulatorCount;

void setup()
{
Serial.begin(115200);
Expand Down Expand Up @@ -79,7 +83,15 @@ void loop()
currentTime = millis();
timeElapsed = currentTime - lastMessage;

if (timeElapsed >= 5000) { // every 5 seconds
if (timeElapsed >= 5000) { // every 5 seconds

// calculate average power reading since last publication
// note this will skew for higher powers as there will be more counts
// at a higher power level
powerAverage = powerAccumulator / powerAccumulatorCount;
powerAccumulator = 0.0;
powerAccumulatorCount = 0;

// connect to MQTT server
if (!client.connected()) {
Serial.print("Attempting MQTT connection...");
Expand All @@ -95,7 +107,7 @@ void loop()
} // end if client.connected
Serial.println("Publishing to MQTT...");
// publish new reading
client.publish(power_topic, String(power/1000.0).c_str(), true);
client.publish(power_topic, String(powerAverage/1000.0).c_str(), true);
client.publish(energy_topic, String(elapsedkWh).c_str(), true);
lastMessage = millis();
} else {
Expand All @@ -120,4 +132,7 @@ ICACHE_RAM_ATTR void onPulse()

//Find kwh elapsed (kWh)
elapsedkWh = (1.0*pulseCount/(ppwh*1000)); //multiply by 1000 to convert pulses per wh to kwh

powerAccumulator += power;
powerAccumulatorCount += 1;
}

0 comments on commit e1a0652

Please sign in to comment.