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

Dev #63

Merged
merged 5 commits into from
Jan 25, 2024
Merged

Dev #63

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
31 changes: 20 additions & 11 deletions code/buddy-firmware/impax-buddy-firmware/include/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
2 changes: 0 additions & 2 deletions code/buddy-firmware/impax-buddy-firmware/src/define.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ const int mqtt_port = 1883;
unsigned long batteryStatusTimer = 0;
unsigned long measuringTimer = 0;

#define BATTERY_LIMIT 30

String CA_cert =
"-----BEGIN CERTIFICATE-----\n"
"MIIDJzCCAg+gAwIBAgIUPKKmid6OdQ5kxFZQxDI0tTTMOwgwDQYJKoZIhvcNAQEL\n"
Expand Down
11 changes: 7 additions & 4 deletions code/buddy-firmware/impax-buddy-firmware/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,11 @@ 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)
if (batteryStatus == CHARGIN_STATE)
{
ledStatus = LED_CHARGIN;
}
else if (batteryStatus < BATTERY_LIMIT)
{
ledStatus = LED_BATTERY_LOW;
}
Expand All @@ -143,6 +144,8 @@ void process()
void buddyInit()
{
// leds
ledStatus = LED_OFF;
led(ledStatus);
ledStatus = LED_BLINK;
led(ledStatus);

Expand Down
93 changes: 90 additions & 3 deletions code/buddy-firmware/impax-buddy-firmware/src/utils/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,20 @@ 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);
// Update the previous percentage value
prevPercentage = percentage;

if (isIncreasingPercentage)
return CHARGIN_STATE;

return percentage;
}
Expand All @@ -51,6 +60,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];
Expand Down Expand Up @@ -104,6 +164,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;
Expand Down Expand Up @@ -139,6 +221,11 @@ void led(int LED_STATE)
blinkLedOn();
onLedWifi();
}
else if (LED_STATE == LED_CHARGIN)
{
fadeLedOn();
onLedWifi();
}
}

int wakeup_reason()
Expand Down
11 changes: 11 additions & 0 deletions code/hub-firmware/led-battery/led..txt
Original file line number Diff line number Diff line change
@@ -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

24 changes: 24 additions & 0 deletions code/hub-firmware/mqtt-broker-config/ca_authentication.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
MBIGA1UEAwwLcmFzcGJlcnJ5cGkxIjAgBgkqhkiG9w0BCQEWE3N0ZXZlQHRlc3Rl
bWFpbC5jb20wHhcNMjQwMTIyMjIwNDMxWhcNMjkwMTIxMjIwNDMxWjCBkzELMAkG
A1UEBhMCTEsxEzARBgNVBAgMClNocm9wc2hpcmUxEzARBgNVBAcMCklyb25icmlk
Z2UxETAPBgNVBAoMCENBbWFzdGVyMQ0wCwYDVQQLDARURVNUMRQwEgYDVQQDDAty
YXNwYmVycnlwaTEiMCAGCSqGSIb3DQEJARYTc3RldmVAdGVzdGVtYWlsLmNvbTCC
ASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAMMxR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