Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integration of open paygo time based fonctionality #131

Merged
merged 5 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions firmware/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ board = bluepill_f103c8_128k

upload_protocol = serial
monitor_speed = 115200
#upload_port = COM4 # To upload the code uncomment this line and put the port corresponding to your module on you PC

# Disable some warning flags for libraries
build_flags =
Expand Down
2 changes: 1 addition & 1 deletion firmware/src/credit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "relay.hpp"

float billing = 0.0;
unsigned int tariff = 70;
unsigned int tariff = 1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you explain why this is needed?

float creditt = 0.0;
byte fault = 0;

Expand Down
4 changes: 3 additions & 1 deletion firmware/src/helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ void urgeent() {
credit_formular();
}

STS_keypad();
if (Mode_select != 2) {
STS_keypad();
}
warn_now = millis();
if (c_chek == 1) {
if (warn_now - warntime > 1500) {
Expand Down
27 changes: 20 additions & 7 deletions firmware/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,32 +156,43 @@ void setup() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.println("No Configuration! ");
while (Mode_select == 0) // wait for mode configuration
while (Mode_select == 0) // wait for configuration mode
{
STS_keypad();
delay(20);
}
break;

case 1:
case 1: // Module in STS Mode
printf("Welcome to the STS Energy-based\n");
MyTim->attachInterrupt(urgeent);
MyTim->resume();
break;

case 2:
case 2: // Module in OpenPaygo Energy-based
/*OpenPayGo Token initializing code; */
printf("Welcome to the OPAYGO Device\n");
printf("Welcome to the OpenPaygo Energy-based\n");
printf(
"We're waiting for the * character to start recording the key "
"presses.\n(Press the '#' key to see the device activation "
"status)\n\n");
LoadActivationVariables(); // We load the activation variableS
LoadActivationVariables(); // We load the activation variables
break;

case 3: // Module in OpenPaygo Time-based
/*OpenPaygo Time-based initializing code; */
printf("Welcome to the OpenPaygo Time-based\n");
printf(
"We're waiting for the * character to start recording the key "
"presses.\n(Press the '#' key to see the device activation "
"status)\n\n");
LoadActivationVariables(); // We load the activation variables
break;
}
}

void loop() {
if (Mode_select == 1) {
if (Mode_select == 1) { // Module in STS Mode
mesure();
if ((mains_input_value > 50)) {
credit_reminder();
Expand All @@ -196,7 +207,9 @@ void loop() {
}
}

if (Mode_select == 2) {
if (Mode_select == 2 ||
Mode_select == 3) { // 2 = Module in OpenPaygo Energy-based; 3 = Module
// in OpenPaygo Time-based
// We wait for a token to be entered
InputToken = WaitForTokenEntry();
// We get the activation value from the token
Expand Down
112 changes: 95 additions & 17 deletions firmware/src/opaygo_functions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,25 +57,50 @@ void LoadActivationVariables() {
// UsedTokens if needed)
UsedTokens =
mem.readInt(UsedTokens_eeprom_location); // We load UsedTokens if needed
PAYGEnabled = mem.read(
switch (mem.read(PAYGEnabled_eeprom_location)) {
case 0:
PAYGEnabled = false;
break;
case 1:
PAYGEnabled = true;
break;
}

mem.read(
PAYGEnabled_eeprom_location); // We load PAYGEnabled //Verify this syntax
ActiveUntil =
mem.readLong(ActiveUntil_eeprom_location); // We load ActiveUntil
TokenEntryLockedUntil = mem.readLong(
TokenEntryLockedUntil_eeprom_location); // We load TokenEntryLockedUntil
if (Mode_select == 2) {
creditt =
mem.readLong(credit_eeprom_location); // We load creditt in case of
// OpenPaygo Energy-based
} else {
ActiveUntil =
mem.readLong(ActiveUntil_eeprom_location); // We load ActiveUntil
}
}

void StoreActivationVariables() {
mem.writeInt(TokenCount_eeprom_location,
TokenCount); // We store TokenCount (& UsedTokens if needed)
mem.writeInt(UsedTokens_eeprom_location,
UsedTokens); // We store UsedTokens if needed
mem.write(PAYGEnabled_eeprom_location,
PAYGEnabled); // We store PAYGEnabled //Verify this syntax
mem.writeLong(ActiveUntil_eeprom_location,
ActiveUntil); // We store ActiveUntil
if (PAYGEnabled) {
mem.writeInt(PAYGEnabled_eeprom_location, 1); // We store PAYGEnabled
} else {
mem.writeInt(PAYGEnabled_eeprom_location, 0); // We store PAYGEnabled
}

mem.writeLong(TokenEntryLockedUntil_eeprom_location,
TokenEntryLockedUntil); // We store TokenEntryLockedUntil
if (Mode_select == 2) {
mem.writeLong(
credit_eeprom_location,
creditt); // We Store creditt in case of OpenPaygo Energy-based
} else {
mem.writeLong(ActiveUntil_eeprom_location,
ActiveUntil); // We store ActiveUntil
}
}

void storeTimeStampEEPROM(uint32_t timeStampInSeconds) {
Expand Down Expand Up @@ -202,7 +227,29 @@ void SetTime(int ActivationValue) {
// seconds for to compare to our RTC time)
}

void AddCreditt(int ActivationValue) {
mem.writeLong(credit_eeprom_location, creditt); // write present credit
creditt = mem.readLong(credit_eeprom_location); // fetch previous credit //
// add new if any to old credit //
creditt += ActivationValue;
mem.writeLong(credit_eeprom_location, creditt);
get_credit = 1;
}

void SetCreditt(int ActivationValue) {
creditt = ActivationValue;
mem.writeLong(credit_eeprom_location, creditt); // write present credit
creditt = mem.readLong(credit_eeprom_location); // fetch previous credit //
// add new if any to old credit //
creditt = ActivationValue;
mem.writeLong(credit_eeprom_location, creditt);
get_credit = 1;
}

void UpdateDeviceStatusFromTokenValue(int TokenValue, int ActivationCount) {
// TokenValue in case of OpenPaygo Energy-based is consider directly as a
// credit to add or set and in case of OpenPaygo Time-based it is consider as
// a number of day to add or to set
if (TokenValue == -1) {
InvalidTokenCount++;
UpdateInvalidTokenWaitingPeriod();
Expand All @@ -223,9 +270,17 @@ void UpdateDeviceStatusFromTokenValue(int TokenValue, int ActivationCount) {
} else {
if (ActivationCount % 2) {
PAYGEnabled = true;
SetTime(TokenValue);
if (Mode_select == 2) { // Module in OpenPaygo Energy-based
SetCreditt(TokenValue);
} else { // Module in OpenPaygo Time-based
SetTime(TokenValue);
}
} else {
AddTime(TokenValue);
if (Mode_select == 2) { // Module in OpenPaygo Energy-based
AddCreditt(TokenValue);
} else { // Module in OpenPaygo Time-based
AddTime(TokenValue);
}
}
BlinkGreenLED(
2,
Expand All @@ -237,14 +292,23 @@ void UpdateDeviceStatusFromTokenValue(int TokenValue, int ActivationCount) {

bool IsActive() {
if (PAYGEnabled) {
if (ActiveUntil > GetTimeInSeconds()) {
return true;
} else {
return false;
if (Mode_select == 2) {
if (creditt < 1 || fault == 1) {
return false;
} else {
return true;
}
}

if (Mode_select == 3) {
if (ActiveUntil > GetTimeInSeconds()) {
return true;
} else {
return false;
}
}
} else {
return true;
}
return true;
}

uint64_t WaitForTokenEntry() {
Expand All @@ -268,8 +332,22 @@ uint64_t WaitForTokenEntry() {
if (IsActive()) {
BlinkGreenLED(1, BLINK_PERIOD);
#ifdef DEBUG
printf("\nTime Left: %" PRIu32 "seconds",
ActiveUntil - GetTimeInSeconds());
if (Mode_select == 2) { // Module in OpenPaygo Energy-based
mesure();
if ((mains_input_value > 50)) {
credit_reminder();
}
if ((mains_input_value < 50)) {
digitalWrite(red_led, LOW);
digitalWrite(green_led, LOW);
}
urgeent();
printf("\nEnergy Left: %f KWH", ENERGY);
} else { // Module in OpenPaygo Time-based
printf("\nTime Left: %" PRIu32 "seconds",
ActiveUntil - GetTimeInSeconds());
}

#endif
} else {
BlinkRedLED(1, BLINK_PERIOD);
Expand Down
2 changes: 1 addition & 1 deletion firmware/src/sts_token.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ void STStoken_decode() {
mem.writeLong(credit_eeprom_location, creditt); // write present credit
creditt =
mem.readLong(credit_eeprom_location); // fetch previous credit //
creditt = creditt + topup; // add new if any to old credit //
creditt = creditt + topup; // add new if any to old credit //
mem.writeLong(credit_eeprom_location, creditt);
get_credit = 1;
}
Expand Down
9 changes: 6 additions & 3 deletions firmware/src/token_management.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#include "sts_token.hpp"

char customKey;
bool is_STSmode = false; // by default on OpenPAYGO Token

// keypad
const byte ROWS = 4;
Expand Down Expand Up @@ -158,11 +157,15 @@ void STS_keypad() {
dt = 0;
}
if (sts_data == "112") {
Mode_select = 1;
Mode_select = 1; // For STS Mode (Energy-based)
}

if (sts_data == "122") {
Mode_select = 2;
Mode_select = 2; // For OpenPaygo Energy-based
}

if (sts_data == "132") {
Mode_select = 3; // For OpenPaygo Time-based
}
}
}
Expand Down