Skip to content

Commit

Permalink
Added persistent storage! Every KWH the counter is stored.
Browse files Browse the repository at this point in the history
Will change to a regular interval later on to reduce flash memory wear.
  • Loading branch information
DJFliX committed Feb 10, 2016
1 parent 55b0f7a commit 46814f7
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 12 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ Done:
- [X] Show (& calc) total consumption
- [X] Use interrupts to get button input + pulses
- [X] Use button input to switch between frames
- [X] Persistent storage of KWh counter

Future wishes:
- [ ] Persistant storage of KWh counter
- [ ] Add NTP time to sketch
- [ ] Implement timezone offset, apply to current time
- [ ] Keep track of total consumption for the day (and display this)
Expand All @@ -27,12 +27,14 @@ Future wishes:
- [ ] Multi-SSID
- [ ] Store measurements while there is no Wi-Fi connection
- [ ] Work stand-alone (without Wi-Fi connection) without switching to Config mode
- [ ] Reduce Flash memory wear by writing periodically instead of on every KWh increment

##Resources used
* [SparkFun Eagle Libraries](https://github.com/sparkfun/SparkFun-Eagle-Libraries) by [SparkFun](https://github.com/sparkfun)
* [TCRT5000 Eagle Library](https://github.com/mohamedpsx/Eagle-Libraries) by [mohamedpsx](https://github.com/mohamedpsx)

* [esp8266-oled-ssd1306](https://github.com/squix78/esp8266-oled-ssd1306) by [squix78](https://github.com/squix78))
* [EEPROMWriteAnything](http://playground.arduino.cc/Code/EEPROMWriteAnything) by the Arduino Community

* [AirSensor Energy Monitor tutorial](http://www.airsensor.co.uk/component/zoo/item/energy-monitor.html)
* [nongnu.org dtostrf](http://www.nongnu.org/avr-libc/user-manual/group__avr__stdlib.html#ga060c998e77fb5fc0d3168b3ce8771d42)
Expand Down
24 changes: 24 additions & 0 deletions code/oakpowerboard/eepromanything.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <EEPROM.h>
#include <Arduino.h> // for type definitions

template <class T> int EEPROM_writeAnything(int ee, const T& value)
{
const byte* p = (const byte*)(const void*)&value;
unsigned int i;
EEPROM.begin(sizeof(value));
for (i = 0; i < sizeof(value); i++)
EEPROM.write(ee++, *p++);
EEPROM.end();
return i;
}

template <class T> int EEPROM_readAnything(int ee, T& value)
{
byte* p = (byte*)(void*)&value;
unsigned int i;
EEPROM.begin(sizeof(value));
for (i = 0; i < sizeof(value); i++)
*p++ = EEPROM.read(ee++);
EEPROM.end();
return i;
}
40 changes: 29 additions & 11 deletions code/oakpowerboard/oakpowerboard.ino
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,33 @@
#include "font/roboto_thin_20.h"
#include "images.h"

#include <EEPROM.h>

#include "eepromanything.h"

#define PULSES_PER_KWH 375
#define WH_PER_PULSE (1000.0F / PULSES_PER_KWH)

struct config_t
{
int state;
float consumption_total;
int pulse_count_current_kwh;
} storage;

#define BTN_1 9
#define BTN_2 8
#define BTN_3 7
#define BTN_4 6
#define PULSE_PIN 5
#define DEBUG_LED 10

int pulse_count_current_kwh;
#define EEPROM_READ_ADDR 0

float consumption_total = 56712.0f;
char consumption_total_str[10] = "56712.00";
char consumption_total_str[10] = "00000.00";

float consumption_current = 0.0F;
char consumption_current_str[6] = "00000";
char consumption_current_str[6] = "Wait";

long time_since_last_pulse = millis();

Expand Down Expand Up @@ -80,6 +90,15 @@ void trigger_pulse() {
}

void setup() {
EEPROM_readAnything(EEPROM_READ_ADDR, storage);
if(storage.state != 0xF7) {
storage.state = 0xF7;
storage.consumption_total = 56712.0f;
storage.pulse_count_current_kwh = 0;
EEPROM_writeAnything(EEPROM_READ_ADDR, storage);
}
dtostrf(storage.consumption_total, 6, 2, consumption_total_str);

ui.setTargetFPS(30);
ui.setActiveSymbole(activeSymbole);
ui.setInactiveSymbole(inactiveSymbole);
Expand Down Expand Up @@ -115,8 +134,6 @@ void setup() {
attachInterrupt(digitalPinToInterrupt(BTN_4), toggle_b4, FALLING);
}

bool btn_state = false;

void loop() {
int remainingTimeBudget = ui.update();
long deadline = millis() + remainingTimeBudget;
Expand Down Expand Up @@ -144,13 +161,14 @@ void loop() {
}

if(pulse_triggered == true) {
pulse_count_current_kwh++;
if(pulse_count_current_kwh == PULSES_PER_KWH) {
consumption_total++;
pulse_count_current_kwh = 0;
storage.pulse_count_current_kwh++;
if(storage.pulse_count_current_kwh == PULSES_PER_KWH) {
storage.consumption_total++;
storage.pulse_count_current_kwh = 0;
EEPROM_writeAnything(EEPROM_READ_ADDR, storage);
}
consumption_current = get_consumption_from_time_since_last_pulse(time_since_last_pulse);
dtostrf(consumption_total + ((((float)pulse_count_current_kwh) * WH_PER_PULSE) / 1000.0F), 6, 2, consumption_total_str);
dtostrf(storage.consumption_total + ((((float)storage.pulse_count_current_kwh) * WH_PER_PULSE) / 1000.0F), 6, 2, consumption_total_str);
dtostrf(consumption_current, 4, 0, consumption_current_str);
time_since_last_pulse = millis();
pulse_triggered = false;
Expand Down

0 comments on commit 46814f7

Please sign in to comment.