Processor now working as an mqtt broker
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
File diff suppressed because one or more lines are too long
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -58,6 +58,17 @@ static void event_handler_cb_vote_results_obj8(lv_event_t *e) {
|
||||
}
|
||||
}
|
||||
|
||||
static void event_handler_cb_ibvs_processor_obj10(lv_event_t *e) {
|
||||
lv_event_code_t event = lv_event_get_code(e);
|
||||
if (event == LV_EVENT_VALUE_CHANGED) {
|
||||
lv_obj_t *ta = lv_event_get_target(e);
|
||||
if (tick_value_change_obj != ta) {
|
||||
const char *value = lv_textarea_get_text(ta);
|
||||
set_var_ui_ip(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void create_screen_start() {
|
||||
lv_obj_t *obj = lv_obj_create(0);
|
||||
objects.start = obj;
|
||||
@@ -599,7 +610,7 @@ void create_screen_ibvs_processor() {
|
||||
lv_obj_t *parent_obj = obj;
|
||||
{
|
||||
lv_obj_t *obj = lv_spinner_create(parent_obj);
|
||||
lv_obj_set_pos(obj, 120, 120);
|
||||
lv_obj_set_pos(obj, 211, 120);
|
||||
lv_obj_set_size(obj, 80, 80);
|
||||
lv_spinner_set_anim_params(obj, 1000, 60);
|
||||
}
|
||||
@@ -615,12 +626,46 @@ void create_screen_ibvs_processor() {
|
||||
lv_obj_set_style_text_align(obj, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN | LV_STATE_DEFAULT);
|
||||
lv_obj_set_style_text_font(obj, &lv_font_montserrat_30, LV_PART_MAIN | LV_STATE_DEFAULT);
|
||||
}
|
||||
{
|
||||
lv_obj_t *obj = lv_textarea_create(parent_obj);
|
||||
objects.obj10 = obj;
|
||||
lv_obj_set_pos(obj, 10, 180);
|
||||
lv_obj_set_size(obj, 180, 50);
|
||||
lv_textarea_set_max_length(obj, 128);
|
||||
lv_textarea_set_one_line(obj, false);
|
||||
lv_textarea_set_password_mode(obj, false);
|
||||
lv_obj_add_event_cb(obj, event_handler_cb_ibvs_processor_obj10, LV_EVENT_ALL, 0);
|
||||
lv_obj_set_style_text_align(obj, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN | LV_STATE_DEFAULT);
|
||||
lv_obj_set_style_text_font(obj, &lv_font_montserrat_24, LV_PART_MAIN | LV_STATE_DEFAULT);
|
||||
}
|
||||
{
|
||||
lv_obj_t *obj = lv_textarea_create(parent_obj);
|
||||
lv_obj_set_pos(obj, 10, 136);
|
||||
lv_obj_set_size(obj, 150, 44);
|
||||
lv_textarea_set_max_length(obj, 128);
|
||||
lv_textarea_set_text(obj, "MQTT IP:");
|
||||
lv_textarea_set_one_line(obj, false);
|
||||
lv_textarea_set_password_mode(obj, false);
|
||||
lv_obj_set_style_bg_opa(obj, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
|
||||
lv_obj_set_style_border_opa(obj, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
|
||||
lv_obj_set_style_text_font(obj, &lv_font_montserrat_22, LV_PART_MAIN | LV_STATE_DEFAULT);
|
||||
}
|
||||
}
|
||||
|
||||
tick_screen_ibvs_processor();
|
||||
}
|
||||
|
||||
void tick_screen_ibvs_processor() {
|
||||
{
|
||||
const char *new_val = get_var_ui_ip();
|
||||
const char *cur_val = lv_textarea_get_text(objects.obj10);
|
||||
uint32_t max_length = lv_textarea_get_max_length(objects.obj10);
|
||||
if (strncmp(new_val, cur_val, max_length) != 0) {
|
||||
tick_value_change_obj = objects.obj10;
|
||||
lv_textarea_set_text(objects.obj10, new_val);
|
||||
tick_value_change_obj = NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -35,6 +35,7 @@ typedef struct _objects_t {
|
||||
lv_obj_t *obj7;
|
||||
lv_obj_t *obj8;
|
||||
lv_obj_t *obj9;
|
||||
lv_obj_t *obj10;
|
||||
} objects_t;
|
||||
|
||||
extern objects_t objects;
|
||||
|
||||
@@ -23,7 +23,8 @@ enum FlowGlobalVariables {
|
||||
FLOW_GLOBAL_VARIABLE_UI_ITEM3 = 5,
|
||||
FLOW_GLOBAL_VARIABLE_UI_ITEM4 = 6,
|
||||
FLOW_GLOBAL_VARIABLE_UI_ITEM5 = 7,
|
||||
FLOW_GLOBAL_VARIABLE_UI_WINNER = 8
|
||||
FLOW_GLOBAL_VARIABLE_UI_WINNER = 8,
|
||||
FLOW_GLOBAL_VARIABLE_UI_IP = 9
|
||||
};
|
||||
|
||||
// Native global variables
|
||||
@@ -46,6 +47,8 @@ extern const char *get_var_ui_item5();
|
||||
extern void set_var_ui_item5(const char *value);
|
||||
extern const char *get_var_ui_winner();
|
||||
extern void set_var_ui_winner(const char *value);
|
||||
extern const char *get_var_ui_ip();
|
||||
extern void set_var_ui_ip(const char *value);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
||||
@@ -28,6 +28,7 @@ struct LongText {
|
||||
|
||||
extern bool screenNeedsUpdate;
|
||||
extern char current_screen;
|
||||
extern int ibvs_mode;
|
||||
|
||||
// Funcion declarations
|
||||
void serial_config();
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
89
src/main.cpp
89
src/main.cpp
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user