From c4ad8eeb30954a9c74bd34c217a95a742b1880db Mon Sep 17 00:00:00 2001 From: Kanishka Gunawardana Date: Tue, 23 Jan 2024 22:32:17 +0530 Subject: [PATCH 1/4] led charging --- .../impax-buddy-firmware/include/util.h | 31 +++--- .../impax-buddy-firmware/src/define.h | 2 +- .../impax-buddy-firmware/src/main.cpp | 10 +- .../impax-buddy-firmware/src/utils/util.cpp | 94 ++++++++++++++++++- 4 files changed, 119 insertions(+), 18 deletions(-) diff --git a/code/buddy-firmware/impax-buddy-firmware/include/util.h b/code/buddy-firmware/impax-buddy-firmware/include/util.h index 26781270..8cb5dc51 100644 --- a/code/buddy-firmware/impax-buddy-firmware/include/util.h +++ b/code/buddy-firmware/impax-buddy-firmware/include/util.h @@ -7,32 +7,41 @@ // Define LED pins and blink interval #define LED_BLINK_DELAY 100 +#define LED_FADE_DELAY 2 #define LED_BATTERY_LOW 5 #define LED_ON 2 #define LED_ON_PIN 2 #define LED_WIFI_PIN 0 +#define LED_CHARGIN 7 #define LED_BLINK 1 #define LED_OFF 0 #define MOVING_AVERAGE_SIZE 6 -#define VOLTAGE_UPPER_LIMIT 4.19 -#define VOLTAGE_LOWER_LIMIT 3.50 +#define VOLTAGE_UPPER_LIMIT 4.05 +#define VOLTAGE_LOWER_LIMIT 3.4 // Define battery-related constants #define BATTERY_READ 33 const float Vref = 3.3; +// Define a global variable to store the previous percentage value +static int prevPercentage = 0; +#define CHARGIN_STATE 200 +#define BATTERY_LIMIT 10 // Function declarations -void initLED(); // Initialize LED pins -void onLedOn(); // Turn on the ON LED -void offLedOn(); // Turn off the ON LED -void onLedWifi(); // Turn on the WiFi LED -void offLedWifi(); // Turn off the WiFi LED -void blinkLedWifi(); // Blink the WiFi LED -void blinkLedOn(); // Blink the ON LED -int getBatteryStatus(); // Get battery status based on voltage -float getBatteryVoltage(); // Get the voltage of the battery +void initLED(); // Initialize LED pins +void onLedOn(); // Turn on the ON LED +void offLedOn(); // Turn off the ON LED +void onLedWifi(); // Turn on the WiFi LED +void offLedWifi(); // Turn off the WiFi LED +void blinkLedWifi(); // Blink the WiFi LED +void blinkLedOn(); // Blink the ON LED +int getBatteryStatus(); // Get battery status based on voltage +float getBatteryVoltage(float *readings); // Get the voltage of the battery +float getBatteryVoltage(); // Get the voltage of the battery void batteryInit(); void led(int); +bool batteryIsCharging(float *readings, int size); // Is the battery charging +void fadeLedOn(); int wakeup_reason(); diff --git a/code/buddy-firmware/impax-buddy-firmware/src/define.h b/code/buddy-firmware/impax-buddy-firmware/src/define.h index a8546d7f..06c897bc 100644 --- a/code/buddy-firmware/impax-buddy-firmware/src/define.h +++ b/code/buddy-firmware/impax-buddy-firmware/src/define.h @@ -49,7 +49,7 @@ const int mqtt_port = 1883; unsigned long batteryStatusTimer = 0; unsigned long measuringTimer = 0; -#define BATTERY_LIMIT 30 + String CA_cert = "-----BEGIN CERTIFICATE-----\n" diff --git a/code/buddy-firmware/impax-buddy-firmware/src/main.cpp b/code/buddy-firmware/impax-buddy-firmware/src/main.cpp index 435e48fb..f15a3814 100644 --- a/code/buddy-firmware/impax-buddy-firmware/src/main.cpp +++ b/code/buddy-firmware/impax-buddy-firmware/src/main.cpp @@ -118,9 +118,6 @@ void process() int batteryStatus = getBatteryStatus(); buddyMQTT.publish(buddyMQTT.topics.BATTERY.c_str(), batteryStatus); - float vol = getBatteryVoltage(); - buddyMQTT.publish(buddyMQTT.topics.TEST.c_str(), vol); - if (batteryStatus < BATTERY_LIMIT) { ledStatus = LED_BATTERY_LOW; @@ -130,6 +127,11 @@ void process() ledStatus = LED_ON; } + if (batteryStatus == CHARGIN_STATE) + { + ledStatus = LED_CHARGIN; + } + batteryStatusTimer = millis(); } @@ -143,6 +145,8 @@ void process() void buddyInit() { // leds + ledStatus = LED_OFF; + led(ledStatus); ledStatus = LED_BLINK; led(ledStatus); diff --git a/code/buddy-firmware/impax-buddy-firmware/src/utils/util.cpp b/code/buddy-firmware/impax-buddy-firmware/src/utils/util.cpp index b981a4e0..d9b1b6aa 100644 --- a/code/buddy-firmware/impax-buddy-firmware/src/utils/util.cpp +++ b/code/buddy-firmware/impax-buddy-firmware/src/utils/util.cpp @@ -33,11 +33,21 @@ int getBatteryStatus() { float voltage = getBatteryVoltage(); - int percentage = 2808.3808 * pow(voltage, 4) - 43560.9157 * pow(voltage, 3) + 252848.5888 * pow(voltage, 2) - 650767.4615 * voltage + 626532.5703; if (voltage > VOLTAGE_UPPER_LIMIT) - percentage = 100; + return 100; else if (voltage <= VOLTAGE_LOWER_LIMIT) - percentage = 0; + return 0; + + int percentage = 2808.3808 * pow(voltage, 4) - 43560.9157 * pow(voltage, 3) + 252848.5888 * pow(voltage, 2) - 650767.4615 * voltage + 626532.5703; + + // Check if the percentage is increasing + bool isIncreasingPercentage = (percentage > prevPercentage); + + if (isIncreasingPercentage) + return CHARGIN_STATE; + + // Update the previous percentage value + prevPercentage = percentage; return percentage; } @@ -51,6 +61,57 @@ void batteryInit() } } +bool batteryIsCharging(float *readings, int size) +{ + for (int i = 1; i < size; i++) + { + if (readings[i] > readings[i - 1]) + { + return false; // If any element is less than or equal to the previous one, the array is not strictly increasing + } + } + return true; // If all elements are strictly increasing +} + +float getBatteryVoltage(float *readings) +{ + static int index = 0; + static float sum = 0; + + // Read the raw value + int rawValue = analogRead(BATTERY_READ); + + // Convert the raw value to voltage (assuming a voltage divider) + float voltage = rawValue * (Vref / 4095.0); // Adjust 3.3 to your actual reference voltage + + // Assuming a voltage divider with equal resistors, adjust the divisor accordingly + voltage = voltage * 2.0; + + // Update the sum with the new reading and subtract the oldest reading + sum = sum - readings[index] + voltage; + + // Store the new reading in the array + readings[index] = voltage; + + // Move to the next index + index = (index + 1) % MOVING_AVERAGE_SIZE; + + // Calculate the moving average + float movingAverage = sum / MOVING_AVERAGE_SIZE; + + // Serial.print(voltage); + // Serial.print(" - "); + + // for (int i = 0; i < MOVING_AVERAGE_SIZE; i++) + // { + // Serial.print(readings[i]); + // Serial.print(", "); + // } + // Serial.println(movingAverage); + + return movingAverage; +} + float getBatteryVoltage() { static float readings[MOVING_AVERAGE_SIZE]; @@ -104,6 +165,28 @@ void blinkLedWifi() } } +void fadeLedOn() +{ + static unsigned long lastTime = 0; + static int brightness = 0; + static int fadeDirection = 1; // 1 for increasing brightness, -1 for decreasing + + if (millis() - lastTime > LED_FADE_DELAY) + { + lastTime = millis(); + + brightness += fadeDirection; + + // Change fade direction when reaching brightness limits + if (brightness <= 0 || brightness >= 255) + { + fadeDirection = -fadeDirection; + } + + analogWrite(LED_ON_PIN, brightness); + } +} + void blinkLedOn() { static unsigned long lastTime = 0; @@ -139,6 +222,11 @@ void led(int LED_STATE) blinkLedOn(); onLedWifi(); } + else if (LED_STATE == LED_CHARGIN) + { + fadeLedOn(); + onLedWifi(); + } } int wakeup_reason() From dec9926a93d23545f68cffff4cc49447a071455f Mon Sep 17 00:00:00 2001 From: Kanishka Gunawardana Date: Wed, 24 Jan 2024 00:17:18 +0530 Subject: [PATCH 2/4] led fix --- code/buddy-firmware/impax-buddy-firmware/src/main.cpp | 11 +++++------ .../impax-buddy-firmware/src/utils/util.cpp | 5 ++--- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/code/buddy-firmware/impax-buddy-firmware/src/main.cpp b/code/buddy-firmware/impax-buddy-firmware/src/main.cpp index f15a3814..0d32f4e5 100644 --- a/code/buddy-firmware/impax-buddy-firmware/src/main.cpp +++ b/code/buddy-firmware/impax-buddy-firmware/src/main.cpp @@ -118,7 +118,11 @@ void process() int batteryStatus = getBatteryStatus(); buddyMQTT.publish(buddyMQTT.topics.BATTERY.c_str(), batteryStatus); - if (batteryStatus < BATTERY_LIMIT) + if (batteryStatus == CHARGIN_STATE) + { + ledStatus = LED_CHARGIN; + } + else if (batteryStatus < BATTERY_LIMIT) { ledStatus = LED_BATTERY_LOW; } @@ -127,11 +131,6 @@ void process() ledStatus = LED_ON; } - if (batteryStatus == CHARGIN_STATE) - { - ledStatus = LED_CHARGIN; - } - batteryStatusTimer = millis(); } diff --git a/code/buddy-firmware/impax-buddy-firmware/src/utils/util.cpp b/code/buddy-firmware/impax-buddy-firmware/src/utils/util.cpp index d9b1b6aa..bf0a08dd 100644 --- a/code/buddy-firmware/impax-buddy-firmware/src/utils/util.cpp +++ b/code/buddy-firmware/impax-buddy-firmware/src/utils/util.cpp @@ -42,13 +42,12 @@ int getBatteryStatus() // Check if the percentage is increasing bool isIncreasingPercentage = (percentage > prevPercentage); + // Update the previous percentage value + prevPercentage = percentage; if (isIncreasingPercentage) return CHARGIN_STATE; - // Update the previous percentage value - prevPercentage = percentage; - return percentage; } From 49fd383d30b2083be56f9eafc517b19839c36378 Mon Sep 17 00:00:00 2001 From: Kanishka Gunawardana Date: Thu, 25 Jan 2024 07:30:34 +0530 Subject: [PATCH 3/4] Update Wi-Fi and MQTT configurations --- .../impax-buddy-firmware/src/define.h | 26 +++++++++---------- code/hub-firmware/led-battery/led..txt | 11 ++++++++ .../mqtt-broker-config/ca_authentication.txt | 24 +++++++++++++++++ 3 files changed, 47 insertions(+), 14 deletions(-) create mode 100644 code/hub-firmware/led-battery/led..txt diff --git a/code/buddy-firmware/impax-buddy-firmware/src/define.h b/code/buddy-firmware/impax-buddy-firmware/src/define.h index 06c897bc..cd529f6b 100644 --- a/code/buddy-firmware/impax-buddy-firmware/src/define.h +++ b/code/buddy-firmware/impax-buddy-firmware/src/define.h @@ -23,25 +23,25 @@ int ledStatus; // String ssid = ""; // String password = ""; -// String ssid = "Dialog 4G 629"; -// String password = "189FFF07"; -String ssid = "impax"; -String password = "impax12345678"; +String ssid = "Dialog 4G 629"; +String password = "189FFF07"; +// String ssid = "impax"; +// String password = "impax12345678"; // {Dialog 4G 629,189FFF07,emqx,public} // {WIFI_SSID,WIFI_PASSWORD,MQTT_USERNAME,MQTT_PASSWORD} // {impax,impax12345678,impax,impax} // MQTT Broker -const char *mqtt_broker = "192.168.4.1"; -String mqtt_username = "impax"; -String mqtt_password = "impax"; -const int mqtt_port = 1883; - -// const char *mqtt_broker = "broker.emqx.io"; -// String mqtt_username = "emqx"; -// String mqtt_password = "public"; +// const char *mqtt_broker = "192.168.4.1"; +// String mqtt_username = "impax"; +// String mqtt_password = "impax"; // const int mqtt_port = 1883; +const char *mqtt_broker = "broker.emqx.io"; +String mqtt_username = "emqx"; +String mqtt_password = "public"; +const int mqtt_port = 1883; + #define BATTER_STATUS_DELAY 10000 #define MEASURE_DELAY 50 #define CLK_SPEED 5 @@ -49,8 +49,6 @@ const int mqtt_port = 1883; unsigned long batteryStatusTimer = 0; unsigned long measuringTimer = 0; - - String CA_cert = "-----BEGIN CERTIFICATE-----\n" "MIIDJzCCAg+gAwIBAgIUPKKmid6OdQ5kxFZQxDI0tTTMOwgwDQYJKoZIhvcNAQEL\n" diff --git a/code/hub-firmware/led-battery/led..txt b/code/hub-firmware/led-battery/led..txt new file mode 100644 index 00000000..f364966d --- /dev/null +++ b/code/hub-firmware/led-battery/led..txt @@ -0,0 +1,11 @@ +sudo systemctl daemon-reload + +sudo systemctl enable led +sudo systemctl start led + +sudo systemctl status led + +sudo systemctl restart hubService + +sudo nano /etc/systemd/system/hubService.service + diff --git a/code/hub-firmware/mqtt-broker-config/ca_authentication.txt b/code/hub-firmware/mqtt-broker-config/ca_authentication.txt index 8b169a0d..9c9c5434 100644 --- a/code/hub-firmware/mqtt-broker-config/ca_authentication.txt +++ b/code/hub-firmware/mqtt-broker-config/ca_authentication.txt @@ -30,4 +30,28 @@ keyfile /etc/mosquitto/certs/server.key certfile /etc/mosquitto/certs/server.crt tls_version tlsv1.2 +-----BEGIN CERTIFICATE----- +MIIECTCCAvGgAwIBAgIUGEzPoz/1G3v0vKcmPSoTCv0YGC4wDQYJKoZIhvcNAQEL +BQAwgZMxCzAJBgNVBAYTAkxLMRMwEQYDVQQIDApTaHJvcHNoaXJlMRMwEQYDVQQH +DApJcm9uYnJpZGdlMREwDwYDVQQKDAhDQW1hc3RlcjENMAsGA1UECwwEVEVTVDEU +MBIGA1UEAwwLcmFzcGJlcnJ5cGkxIjAgBgkqhki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KHPYrWypCDOgDSz4Pmi6STbJltQbFRIWtUjQiJsItBOlfanKdwgnbUfwRJR3/Yj +hnrgltMV/x5Xa1zhrZ6mXBm3jDSVaFf8yiMp+8o= +-----END CERTIFICATE----- From cb531c316c4d291f98266dd5798af264e422f4fc Mon Sep 17 00:00:00 2001 From: Kanishka Gunawardana Date: Thu, 25 Jan 2024 20:05:31 +0530 Subject: [PATCH 4/4] config --- .../impax-buddy-firmware/src/define.h | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/code/buddy-firmware/impax-buddy-firmware/src/define.h b/code/buddy-firmware/impax-buddy-firmware/src/define.h index cd529f6b..5abc9d24 100644 --- a/code/buddy-firmware/impax-buddy-firmware/src/define.h +++ b/code/buddy-firmware/impax-buddy-firmware/src/define.h @@ -23,25 +23,25 @@ int ledStatus; // String ssid = ""; // String password = ""; -String ssid = "Dialog 4G 629"; -String password = "189FFF07"; -// String ssid = "impax"; -// String password = "impax12345678"; +// String ssid = "Dialog 4G 629"; +// String password = "189FFF07"; +String ssid = "impax"; +String password = "impax12345678"; // {Dialog 4G 629,189FFF07,emqx,public} // {WIFI_SSID,WIFI_PASSWORD,MQTT_USERNAME,MQTT_PASSWORD} // {impax,impax12345678,impax,impax} // MQTT Broker -// const char *mqtt_broker = "192.168.4.1"; -// String mqtt_username = "impax"; -// String mqtt_password = "impax"; -// const int mqtt_port = 1883; - -const char *mqtt_broker = "broker.emqx.io"; -String mqtt_username = "emqx"; -String mqtt_password = "public"; +const char *mqtt_broker = "192.168.4.1"; +String mqtt_username = "impax"; +String mqtt_password = "impax"; const int mqtt_port = 1883; +// const char *mqtt_broker = "broker.emqx.io"; +// String mqtt_username = "emqx"; +// String mqtt_password = "public"; +// const int mqtt_port = 1883; + #define BATTER_STATUS_DELAY 10000 #define MEASURE_DELAY 50 #define CLK_SPEED 5