Processor now working as an mqtt broker

This commit is contained in:
2025-11-02 20:26:47 +00:00
parent 4b4b816a8c
commit 17fd36ab4c
18 changed files with 37269 additions and 35522 deletions

View File

@@ -22,11 +22,13 @@ extern "C" void action_act_butt_5(lv_event_t *e) {
}
extern "C" void action_act_badge(lv_event_t *e) {
current_screen = 'B';
screenNeedsUpdate = true;
ibvs_mode = IBVS_BADGE;
current_screen = 'B';
screenNeedsUpdate = true;
}
extern "C" void action_act_processor(lv_event_t *e) {
current_screen = 'H';
screenNeedsUpdate = true;
ibvs_mode = IBVS_PROCESSOR;
current_screen = 'H';
screenNeedsUpdate = true;
}

View File

@@ -17,21 +17,27 @@ int x, y, z;
#define SCREEN_HEIGHT 320
// MQTT settings
const char* mqtt_server = "10.164.67.154";
const char* mqtt_server_ip = "10.164.67.154";
const int mqtt_port = 1883;
//const char* mqtt_user = "badge_device";
//const char* mqtt_password = "letmein";
PicoMQTT::Client mqtt_client(mqtt_server);
PicoMQTT::Client mqtt_client(mqtt_server_ip);
PicoMQTT::Server mqtt_server;
// Global variables
int start_screen = 0;
int ibvs_m = IBVS_BADGE;
int ibvs_mode = 0;
int ping_number = 0;
unsigned long last_publish_time = 0;
bool ibvs_mode_done = false;
bool screenNeedsUpdate = true;
bool configDone = false;
bool winnerDone = false;
char current_screen = 'A';
char winner = '0';
char Badge_ID[9];
String topic_test = "voting/ready";
String message = "Testing da thing #";
LongText question = {"To be or not to be ?", 4, 40};
ShortText orgev = {" ATO", 4, 4};
ShortText voter = {" Bob", 190, 4};
@@ -66,6 +72,7 @@ void setup() {
// Print the IP address
Serial.print("My ip: ");
Serial.println(WiFi.localIP());
set_var_ui_ip(WiFi.localIP().toString().c_str());
// Create Badge ID from MAC address
String mac = WiFi.macAddress();
mac.replace(":", ""); // Remove all colons
@@ -101,25 +108,41 @@ void setup() {
// Function to draw the GUI (text, buttons and sliders)
ui_init();
mqtt_subs();
xTaskCreate(screen_manager, "Manage_screen", 8192, NULL, 1, NULL);
xTaskCreate(mqtt_manager, "Manage_mqtt", 8192, NULL, 1, NULL);
}
void loop() {
if (!configDone) {
serial_config();
configDone = true;
if (ibvs_mode == IBVS_BADGE) {
if (!configDone) {
Serial.println("Entering badge mode...");
serial_config();
configDone = true;
}
if (winner != '0' && !winnerDone) {
Winner_Screen();
winnerDone = true;
if (winner != '0' && !winnerDone) {
Winner_Screen();
winnerDone = true;
}
}
if (ibvs_mode == IBVS_PROCESSOR) {
if (!configDone) {
Serial.println("Entering processor mode...");
configDone = true;
}
if (millis() - last_publish_time >= 60000) {
message = "MQTT server ping #" + String(ping_number++);
Serial.printf("Publishing message in topic '%s': %s\r\n", topic_test.c_str(), message.c_str());
mqtt_server.publish(topic_test, message);
last_publish_time = millis();
}
}
vTaskDelay(10 / portTICK_PERIOD_MS);
}
@@ -228,6 +251,7 @@ void serial_config() {
void mqtt_subs() {
if (ibvs_mode == IBVS_BADGE) {
mqtt_client.subscribe("voting/#", [](const char *topic, const char *payload) {
Serial.printf("Received message in topic '%s': %s\r\n", topic, payload);
@@ -275,6 +299,13 @@ void mqtt_subs() {
}
});
}
if (ibvs_mode == IBVS_PROCESSOR) {
mqtt_server.subscribe("#", [](const char * topic, const char * payload) {
// payload might be binary, but PicoMQTT guarantees that it's zero-terminated
Serial.printf("Received message in topic '%s': %s\r\n", topic, payload);
});
}
}
void mqtt_send_result(char result) {
@@ -331,13 +362,13 @@ void touchscreen_read(lv_indev_t * indev, lv_indev_data_t * data) {
data->point.y = y;
// Print Touchscreen info about X, Y and Pressure (Z) on the Serial Monitor
Serial.print("X = ");
/* Serial.print("X = ");
Serial.print(x);
Serial.print(" | Y = ");
Serial.print(y);
Serial.print(" | Pressure = ");
Serial.print(z);
Serial.println();
Serial.println(); */
}
else {
data->state = LV_INDEV_STATE_RELEASED;
@@ -407,7 +438,31 @@ void screen_manager(void *pvParameters) {
void mqtt_manager(void *pvParameters) {
while (1) {
mqtt_client.loop();
vTaskDelay(5 / portTICK_PERIOD_MS);
if (ibvs_mode == IBVS_BADGE) {
if (!ibvs_mode_done){
mqtt_client.begin();
mqtt_subs();
ibvs_mode_done = true;
Serial.println("mqtt_subs() done once in badge mode"); // Debug
}
mqtt_client.loop();
vTaskDelay(5 / portTICK_PERIOD_MS);
}
else if (ibvs_mode == IBVS_PROCESSOR) {
if (!ibvs_mode_done){
mqtt_server.begin();
mqtt_subs();
ibvs_mode_done = true;
Serial.println("mqtt_subs() done once in processor mode"); // Debug
}
mqtt_server.loop();
vTaskDelay(5 / portTICK_PERIOD_MS);
}
else {
vTaskDelay(5 / portTICK_PERIOD_MS);
}
}
}

View File

@@ -12,6 +12,7 @@ std::string ui_item3;
std::string ui_item4;
std::string ui_item5;
std::string ui_winner;
std::string ui_ip;
extern "C" const char *get_var_ui_question() {
return ui_question.c_str();
@@ -85,3 +86,11 @@ extern "C" void set_var_ui_winner(const char *value) {
ui_winner = value;
Serial.println("Found the winner");
}
extern "C" const char *get_var_ui_ip() {
return ui_ip.c_str();
}
extern "C" void set_var_ui_ip(const char *value) {
ui_ip = value;
}