From 57d6edfc95689ee8491cba953df3ba5d6e154f21 Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 30 Aug 2023 17:31:51 +0200 Subject: [PATCH 1/5] Restore state of `Choice-between-energy-based-and-time-based` --- firmware/src/helpers.hpp | 4 +- firmware/src/main.cpp | 38 ++++++++-- firmware/src/opaygo_functions.hpp | 112 +++++++++++++++++++++++++----- firmware/src/sts_token.hpp | 2 +- firmware/src/token_management.hpp | 8 ++- 5 files changed, 137 insertions(+), 27 deletions(-) diff --git a/firmware/src/helpers.hpp b/firmware/src/helpers.hpp index 4251033e..bed68634 100644 --- a/firmware/src/helpers.hpp +++ b/firmware/src/helpers.hpp @@ -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) { diff --git a/firmware/src/main.cpp b/firmware/src/main.cpp index a037266f..dadeb0e1 100644 --- a/firmware/src/main.cpp +++ b/firmware/src/main.cpp @@ -163,25 +163,31 @@ void setup() { } 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; */ + break; } } void loop() { - if (Mode_select == 1) { + if (Mode_select == 1) { // Module in STS Mode mesure(); if ((mains_input_value > 50)) { credit_reminder(); @@ -196,7 +202,7 @@ void loop() { } } - if (Mode_select == 2) { + if (Mode_select == 2) { // Module in OpenPaygo Energy-based // We wait for a token to be entered InputToken = WaitForTokenEntry(); // We get the activation value from the token @@ -214,4 +220,24 @@ void loop() { UpdateDeviceStatusFromTokenValue(Output.Value, Output.Count); } + + if (Mode_select == 3) { // Module in OpenPaygo Time-based + /*Code for OpenPaygo Time-based*/ + /* // We wait for a token to be entered + InputToken = WaitForTokenEntry(); + // We get the activation value from the token + + Output = GetDataFromToken( + InputToken, &TokenCount, &UsedTokens, StartingCode, + SECRET_KEY); // We get the activation value from the token + + printf("\n(Token entered: %llu)", InputToken); + printf("\n(Activation Value from Token: %d)", + Output.Value); // Activation Value found in the token + printf("\n(Count: %d)", Output.Count); // Count found in the token + printf("\n(Max Count: %d)", TokenCount); // Count found in the token + printf("\n(Used Tokens: %d)\n", UsedTokens); // Count found in the token + + UpdateDeviceStatusFromTokenValue(Output.Value, Output.Count);*/ + } } diff --git a/firmware/src/opaygo_functions.hpp b/firmware/src/opaygo_functions.hpp index c9983133..1a15c905 100644 --- a/firmware/src/opaygo_functions.hpp +++ b/firmware/src/opaygo_functions.hpp @@ -57,12 +57,27 @@ 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() { @@ -70,12 +85,22 @@ void StoreActivationVariables() { 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) { @@ -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(); @@ -223,9 +270,17 @@ void UpdateDeviceStatusFromTokenValue(int TokenValue, int ActivationCount) { } else { if (ActivationCount % 2) { PAYGEnabled = true; - SetTime(TokenValue); + if (Mode_select == 2) { + SetCreditt(TokenValue); + } else { + SetTime(TokenValue); + } } else { - AddTime(TokenValue); + if (Mode_select == 2) { + AddCreditt(TokenValue); + } else { + AddTime(TokenValue); + } } BlinkGreenLED( 2, @@ -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() { @@ -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) { // + 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 { + printf("\nTime Left: %" PRIu32 "seconds", + ActiveUntil - GetTimeInSeconds()); + } + #endif } else { BlinkRedLED(1, BLINK_PERIOD); diff --git a/firmware/src/sts_token.hpp b/firmware/src/sts_token.hpp index 5dc00c98..84ecb1cd 100644 --- a/firmware/src/sts_token.hpp +++ b/firmware/src/sts_token.hpp @@ -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; } diff --git a/firmware/src/token_management.hpp b/firmware/src/token_management.hpp index 0d26a33b..913c83be 100644 --- a/firmware/src/token_management.hpp +++ b/firmware/src/token_management.hpp @@ -158,11 +158,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 } } } From c3aac026530445c742118c6a5ea116f33666b3e6 Mon Sep 17 00:00:00 2001 From: Peguy-WANDA Date: Mon, 4 Sep 2023 17:04:20 +0100 Subject: [PATCH 2/5] upload_port --- firmware/platformio.ini | 1 + 1 file changed, 1 insertion(+) diff --git a/firmware/platformio.ini b/firmware/platformio.ini index c097c2cc..b68b4a64 100644 --- a/firmware/platformio.ini +++ b/firmware/platformio.ini @@ -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 = From 2e5de5e85e90914793f6ca2e5cc0ed1ac4478628 Mon Sep 17 00:00:00 2001 From: Peguy-WANDA Date: Wed, 27 Sep 2023 08:53:16 +0100 Subject: [PATCH 3/5] Integration of OpenPAYGO time-based functionnality --- firmware/.gitignore | 5 +++++ firmware/src/credit.hpp | 4 ++-- firmware/src/main.cpp | 36 +++++++++---------------------- firmware/src/opaygo_functions.hpp | 12 +++++------ firmware/src/token_management.hpp | 1 - 5 files changed, 23 insertions(+), 35 deletions(-) create mode 100644 firmware/.gitignore diff --git a/firmware/.gitignore b/firmware/.gitignore new file mode 100644 index 00000000..89cc49cb --- /dev/null +++ b/firmware/.gitignore @@ -0,0 +1,5 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch diff --git a/firmware/src/credit.hpp b/firmware/src/credit.hpp index 921843f5..6e9c9865 100644 --- a/firmware/src/credit.hpp +++ b/firmware/src/credit.hpp @@ -11,7 +11,7 @@ #include "relay.hpp" float billing = 0.0; -unsigned int tariff = 70; +unsigned int tariff = 1; float creditt = 0.0; byte fault = 0; @@ -19,7 +19,7 @@ void credit_formular() { if (true_power > 0) { ENERGY = ENERGY + ((true_power) / (3600000)); // energy per KWH(energy gotten per sec) - billing = (true_power * tariff) / 3600000; // cost per sec + billing = (true_power * tariff) / 3600000; // cost per sec if (creditt > billing) { creditt = (creditt - billing); } diff --git a/firmware/src/main.cpp b/firmware/src/main.cpp index dadeb0e1..4fcc8379 100644 --- a/firmware/src/main.cpp +++ b/firmware/src/main.cpp @@ -156,7 +156,7 @@ 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); @@ -174,14 +174,17 @@ void setup() { 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"); + "presses.\n(Press the '#' key to see the device activation status)\n\n"); 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; } } @@ -202,7 +205,7 @@ void loop() { } } - if (Mode_select == 2) { // Module in OpenPaygo Energy-based + 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 @@ -219,25 +222,6 @@ void loop() { printf("\n(Used Tokens: %d)\n", UsedTokens); // Count found in the token UpdateDeviceStatusFromTokenValue(Output.Value, Output.Count); - } - - if (Mode_select == 3) { // Module in OpenPaygo Time-based - /*Code for OpenPaygo Time-based*/ - /* // We wait for a token to be entered - InputToken = WaitForTokenEntry(); - // We get the activation value from the token - - Output = GetDataFromToken( - InputToken, &TokenCount, &UsedTokens, StartingCode, - SECRET_KEY); // We get the activation value from the token - - printf("\n(Token entered: %llu)", InputToken); - printf("\n(Activation Value from Token: %d)", - Output.Value); // Activation Value found in the token - printf("\n(Count: %d)", Output.Count); // Count found in the token - printf("\n(Max Count: %d)", TokenCount); // Count found in the token - printf("\n(Used Tokens: %d)\n", UsedTokens); // Count found in the token - - UpdateDeviceStatusFromTokenValue(Output.Value, Output.Count);*/ - } + } } + diff --git a/firmware/src/opaygo_functions.hpp b/firmware/src/opaygo_functions.hpp index 1a15c905..930efaf3 100644 --- a/firmware/src/opaygo_functions.hpp +++ b/firmware/src/opaygo_functions.hpp @@ -270,15 +270,15 @@ void UpdateDeviceStatusFromTokenValue(int TokenValue, int ActivationCount) { } else { if (ActivationCount % 2) { PAYGEnabled = true; - if (Mode_select == 2) { + if (Mode_select == 2) {// Module in OpenPaygo Energy-based SetCreditt(TokenValue); - } else { + } else { // Module in OpenPaygo Time-based SetTime(TokenValue); } } else { - if (Mode_select == 2) { + if (Mode_select == 2) {// Module in OpenPaygo Energy-based AddCreditt(TokenValue); - } else { + } else {// Module in OpenPaygo Time-based AddTime(TokenValue); } } @@ -332,7 +332,7 @@ uint64_t WaitForTokenEntry() { if (IsActive()) { BlinkGreenLED(1, BLINK_PERIOD); #ifdef DEBUG - if (Mode_select == 2) { // + if (Mode_select == 2) { // Module in OpenPaygo Energy-based mesure(); if ((mains_input_value > 50)) { credit_reminder(); @@ -343,7 +343,7 @@ uint64_t WaitForTokenEntry() { } urgeent(); printf("\nEnergy Left: %f KWH", ENERGY); - } else { + } else { // Module in OpenPaygo Time-based printf("\nTime Left: %" PRIu32 "seconds", ActiveUntil - GetTimeInSeconds()); } diff --git a/firmware/src/token_management.hpp b/firmware/src/token_management.hpp index 913c83be..98feae09 100644 --- a/firmware/src/token_management.hpp +++ b/firmware/src/token_management.hpp @@ -14,7 +14,6 @@ #include "sts_token.hpp" char customKey; -bool is_STSmode = false; // by default on OpenPAYGO Token // keypad const byte ROWS = 4; From 65b2a06bd44ab6aa3598b045f55faab9a81ed680 Mon Sep 17 00:00:00 2001 From: Peguy-WANDA Date: Wed, 27 Sep 2023 09:17:22 +0100 Subject: [PATCH 4/5] format document --- firmware/src/credit.hpp | 2 +- firmware/src/main.cpp | 13 ++++++++----- firmware/src/opaygo_functions.hpp | 10 +++++----- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/firmware/src/credit.hpp b/firmware/src/credit.hpp index 6e9c9865..fbe395c3 100644 --- a/firmware/src/credit.hpp +++ b/firmware/src/credit.hpp @@ -19,7 +19,7 @@ void credit_formular() { if (true_power > 0) { ENERGY = ENERGY + ((true_power) / (3600000)); // energy per KWH(energy gotten per sec) - billing = (true_power * tariff) / 3600000; // cost per sec + billing = (true_power * tariff) / 3600000; // cost per sec if (creditt > billing) { creditt = (creditt - billing); } diff --git a/firmware/src/main.cpp b/firmware/src/main.cpp index 4fcc8379..840296ad 100644 --- a/firmware/src/main.cpp +++ b/firmware/src/main.cpp @@ -174,7 +174,8 @@ void setup() { 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"); + "presses.\n(Press the '#' key to see the device activation " + "status)\n\n"); LoadActivationVariables(); // We load the activation variables break; @@ -183,7 +184,8 @@ void setup() { 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"); + "presses.\n(Press the '#' key to see the device activation " + "status)\n\n"); LoadActivationVariables(); // We load the activation variables break; } @@ -205,7 +207,9 @@ void loop() { } } - if (Mode_select == 2 || Mode_select == 3) { // 2 = Module in OpenPaygo Energy-based; 3 = Module in OpenPaygo Time-based + 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 @@ -222,6 +226,5 @@ void loop() { printf("\n(Used Tokens: %d)\n", UsedTokens); // Count found in the token UpdateDeviceStatusFromTokenValue(Output.Value, Output.Count); - } + } } - diff --git a/firmware/src/opaygo_functions.hpp b/firmware/src/opaygo_functions.hpp index 930efaf3..5d3529fc 100644 --- a/firmware/src/opaygo_functions.hpp +++ b/firmware/src/opaygo_functions.hpp @@ -270,15 +270,15 @@ void UpdateDeviceStatusFromTokenValue(int TokenValue, int ActivationCount) { } else { if (ActivationCount % 2) { PAYGEnabled = true; - if (Mode_select == 2) {// Module in OpenPaygo Energy-based + if (Mode_select == 2) { // Module in OpenPaygo Energy-based SetCreditt(TokenValue); - } else { // Module in OpenPaygo Time-based + } else { // Module in OpenPaygo Time-based SetTime(TokenValue); } } else { - if (Mode_select == 2) {// Module in OpenPaygo Energy-based + if (Mode_select == 2) { // Module in OpenPaygo Energy-based AddCreditt(TokenValue); - } else {// Module in OpenPaygo Time-based + } else { // Module in OpenPaygo Time-based AddTime(TokenValue); } } @@ -343,7 +343,7 @@ uint64_t WaitForTokenEntry() { } urgeent(); printf("\nEnergy Left: %f KWH", ENERGY); - } else { // Module in OpenPaygo Time-based + } else { // Module in OpenPaygo Time-based printf("\nTime Left: %" PRIu32 "seconds", ActiveUntil - GetTimeInSeconds()); } From 5822728bc9c4c968124452a81c56c18040a0d88d Mon Sep 17 00:00:00 2001 From: Peguy-WANDA Date: Wed, 18 Oct 2023 12:31:51 +0100 Subject: [PATCH 5/5] deleting .gitignore --- firmware/.gitignore | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 firmware/.gitignore diff --git a/firmware/.gitignore b/firmware/.gitignore deleted file mode 100644 index 89cc49cb..00000000 --- a/firmware/.gitignore +++ /dev/null @@ -1,5 +0,0 @@ -.pio -.vscode/.browse.c_cpp.db* -.vscode/c_cpp_properties.json -.vscode/launch.json -.vscode/ipch