Skip to content

Commit

Permalink
Implementing Dashboard (#17)
Browse files Browse the repository at this point in the history
updating battery graph as new mqtt messages are received
hooked up power graph
closing mqtt connection when page is changed
hooked up server specs
added logo to the dashboard
added logo as icon
displaying graphs with times in function of epoch
  • Loading branch information
sarachehab authored Jun 12, 2024
1 parent d0a6431 commit 88652bd
Show file tree
Hide file tree
Showing 29 changed files with 553 additions and 179 deletions.
6 changes: 3 additions & 3 deletions esp32/lib/PidController/PidController.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
struct PidParams
{
PidParams(float ki_i, float kp_i, float kd_i, float tilt_setpoint,
float ki_o, float kp_o, float kd_o, float velocity_setpoint, float comp_coeff)
float ki_o, float kp_o, float kd_o, float velocity_setpoint, float rotation_setpoint)
: kp_i(kp_i), ki_i(ki_i), kd_i(kd_i), tilt_setpoint(tilt_setpoint),
kp_o(kp_o), ki_o(ki_o), kd_o(kd_o), velocity_setpoint(velocity_setpoint),
comp_coeff(comp_coeff) {}
rotation_setpoint(rotation_setpoint) {}

float kp_i;
float ki_i;
Expand All @@ -37,7 +37,7 @@ struct PidParams
float ki_o;
float kd_o;
float velocity_setpoint;
float comp_coeff;
float rotation_setpoint;
};

struct PidDirection
Expand Down
54 changes: 38 additions & 16 deletions esp32/lib/WifiSetup/MqttMessage.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#include "MqttMessage.h"

BatteryMessage::BatteryMessage(float batteryLevel)
: batteryLevel(batteryLevel) {}
// BatteryMessage Implementation
BatteryMessage::BatteryMessage(float batteryLevel, unsigned long timestamp)
: MqttMessage(timestamp), batteryLevel(batteryLevel) {}

void BatteryMessage::toJson(char *buffer, size_t bufferSize)
{
doc.clear();
doc["run_id"] = RunID;
doc["timestamp"] = millis();
doc["run_id"] = runId;
doc["timestamp"] = timestamp;
doc["battery"] = batteryLevel;
serializeJson(doc, buffer, bufferSize);
}
Expand All @@ -17,21 +18,36 @@ const char *BatteryMessage::getTopic()
return "esp32/battery";
}

MappingMessage::MappingMessage(float x, float y, float theta)
// PowerMessage Implementation
PowerMessage::PowerMessage(float powerLevel, unsigned long timestamp)
: MqttMessage(timestamp), powerLevel(powerLevel) {}

void PowerMessage::toJson(char *buffer, size_t bufferSize)
{
doc.clear();
doc["run_id"] = runId;
doc["timestamp"] = timestamp;
doc["power"] = powerLevel;
serializeJson(doc, buffer, bufferSize);
}

const char *PowerMessage::getTopic()
{
x_coordinate = x;
y_coordinate = y;
orientation = theta;
return "esp32/power";
}

// MappingMessage Implementation
MappingMessage::MappingMessage(float x, float y, float theta, unsigned long timestamp)
: MqttMessage(timestamp), x_coordinate(x), y_coordinate(y), orientation(theta) {}

void MappingMessage::toJson(char *buffer, size_t bufferSize)
{
doc.clear();
doc["run_id"] = RunID;
doc["timestamp"] = millis();
doc["run_id"] = runId;
doc["timestamp"] = timestamp;
doc["x"] = x_coordinate;
doc["y"] = y_coordinate;
doc["oritentation"] = orientation;
doc["orientation"] = orientation;
serializeJson(doc, buffer, bufferSize);
}

Expand All @@ -40,11 +56,15 @@ const char *MappingMessage::getTopic()
return "esp32/mapping";
}

StatusMessage::StatusMessage() {}
// StatusMessage Implementation
StatusMessage::StatusMessage(unsigned long timestamp)
: MqttMessage(timestamp) {}

void StatusMessage::toJson(char *buffer, size_t bufferSize)
{
doc["run_id"] = RunID;
doc.clear();
doc["run_id"] = runId;
doc["timestamp"] = timestamp;
doc["connection"] = 400;
serializeJson(doc, buffer, bufferSize);
}
Expand All @@ -54,14 +74,16 @@ const char *StatusMessage::getTopic()
return "esp32/status";
}

DebugMessage::DebugMessage(const char *message)
: message(message) {}
// DebugMessage Implementation
DebugMessage::DebugMessage(const char *message, unsigned long timestamp)
: MqttMessage(timestamp), message(message) {}

void DebugMessage::toJson(char *buffer, size_t bufferSize)
{
doc.clear();
doc["run_id"] = RunID;
doc["run_id"] = runId;
doc["bot_id"] = BotID;
doc["timestamp"] = timestamp;
doc["message"] = message;
serializeJson(doc, buffer, bufferSize);
}
Expand Down
23 changes: 19 additions & 4 deletions esp32/lib/WifiSetup/MqttMessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,23 @@

extern int RunID;
extern int BotID;

class MqttMessage
{
public:
MqttMessage(unsigned long timestamp) : timestamp(timestamp) {}
virtual void toJson(char *buffer, size_t bufferSize) = 0;
virtual const char *getTopic() = 0;

protected:
int runId = RunID;
unsigned long timestamp;
};

class BatteryMessage : public MqttMessage
{
public:
BatteryMessage(float batteryLevel);
BatteryMessage(float batteryLevel, unsigned long timestamp);
void toJson(char *buffer, size_t bufferSize) override;
const char *getTopic() override;

Expand All @@ -26,10 +29,22 @@ class BatteryMessage : public MqttMessage
StaticJsonDocument<200> doc;
};

class PowerMessage : public MqttMessage
{
public:
PowerMessage(float powerLevel, unsigned long timestamp);
void toJson(char *buffer, size_t bufferSize) override;
const char *getTopic() override;

private:
float powerLevel;
StaticJsonDocument<200> doc;
};

class MappingMessage : public MqttMessage
{
public:
MappingMessage(float x, float y, float orientation);
MappingMessage(float x, float y, float theta, unsigned long timestamp);
void toJson(char *buffer, size_t bufferSize) override;
const char *getTopic() override;

Expand All @@ -43,7 +58,7 @@ class MappingMessage : public MqttMessage
class StatusMessage : public MqttMessage
{
public:
StatusMessage();
StatusMessage(unsigned long timestamp);
void toJson(char *buffer, size_t bufferSize) override;
const char *getTopic() override;

Expand All @@ -54,7 +69,7 @@ class StatusMessage : public MqttMessage
class DebugMessage : public MqttMessage
{
public:
DebugMessage(const char *message);
DebugMessage(const char *message, unsigned long timestamp);
void toJson(char *buffer, size_t bufferSize) override;
const char *getTopic() override;

Expand Down
47 changes: 36 additions & 11 deletions esp32/lib/WifiSetup/MqttSetup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
float x = 0;
float y = 0;

MqttSetup::MqttSetup(const char *server, int port)
: server(server), port(port), client(espClient)
MqttSetup::MqttSetup(const char *server, int port, NTPClient &timeClient)
: server(server), port(port), client(espClient), timeClient(timeClient)
{
client.setServer(server, port);
String tmp_id = "Bot-" + String(BotID);
client_id = new char[tmp_id.length() + 1];
strcpy(client_id, tmp_id.c_str());
}

void MqttSetup::connect(unsigned long timeout)
Expand All @@ -15,7 +18,7 @@ void MqttSetup::connect(unsigned long timeout)
while (!client.connected() && start < timeout)
{
Serial.print("Attempting MQTT connection...");
if (client.connect("ESP32Client"))
if (client.connect(client_id))
{
client.setKeepAlive(60); // Set keep-alive to 60 seconds
Serial.println("connected");
Expand All @@ -32,7 +35,7 @@ void MqttSetup::connect(unsigned long timeout)
}

// Send a status message to update run number on server
StatusMessage statusMessage;
StatusMessage statusMessage(getEpochTime());
publishMessage(statusMessage);
}

Expand All @@ -45,27 +48,42 @@ void MqttSetup::loop()
connect();
}

// Send a new message every 2 seconds
int current_time = millis();
if (current_time - lastMsgSent > delayMsgSent)

// Update power status and publish message every 2 seconds
if (current_time - lastBatteryMessageSent > delayBatterySent)
{
lastMsgSent = millis();
lastBatteryMessageSent = millis();

// Update battery status and publish message
batteryLevel -= 7;
if (batteryLevel < 0)
{
batteryLevel = 100;
}
BatteryMessage batteryMessage(batteryLevel);
BatteryMessage batteryMessage(batteryLevel, getEpochTime());
publishMessage(batteryMessage);
}

// Update power status and publish message every second
if (current_time - lastPowerMessageSent > delayPowerSent)
{
lastPowerMessageSent = millis();

// Update battery status and publish message
powerLevel -= 123;
if (powerLevel < 0)
{
powerLevel = 1000;
}
PowerMessage powerMessage(powerLevel, getEpochTime());
publishMessage(powerMessage);

// TODO: Update mapping status and publish message
// MappingMessage mappingMessage(x, y, orientation * PI / 180);
// publishMessage(mappingMessage);
// Update power status and publish message every second
}

client.loop();
timeClient.update();
}

void MqttSetup::setCallback(MQTT_CALLBACK_SIGNATURE)
Expand All @@ -89,3 +107,10 @@ const char *MqttSetup::getServer() const
{
return server;
}

unsigned long MqttSetup::getEpochTime()
{
timeClient.update();
unsigned long epochTime = timeClient.getEpochTime();
return epochTime;
}
19 changes: 16 additions & 3 deletions esp32/lib/WifiSetup/MqttSetup.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

#include <PubSubClient.h>
#include <WiFiClient.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <Arduino.h>
#include "MqttMessage.h"

extern int RunID;
extern int BotID;
class MqttSetup
{
public:
MqttSetup(const char *server, int port);
MqttSetup(const char *server, int port, NTPClient &timeClient);
void connect(unsigned long timeout = ULONG_MAX);

/**
Expand All @@ -28,13 +31,23 @@ class MqttSetup
void pingServer() const;
const char *getServer() const;

unsigned long getEpochTime();

private:
const char *server;
int port;
WiFiClient espClient;
PubSubClient client;

int lastMsgSent = 0;
int delayMsgSent = 2000;
NTPClient &timeClient;

int lastBatteryMessageSent = 0;
int delayBatterySent = 2000;
int batteryLevel = 100;

int lastPowerMessageSent = 0;
int delayPowerSent = 1000;
int powerLevel = 1000;

char *client_id;
};
Loading

0 comments on commit 88652bd

Please sign in to comment.