Skip to content

Commit

Permalink
Add missing sub/pub methods
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffMboya committed Jan 15, 2025
1 parent 9b68c5e commit ef76450
Show file tree
Hide file tree
Showing 4 changed files with 378 additions and 28 deletions.
2 changes: 2 additions & 0 deletions embed-proplet/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ CONFIG_NET_STATISTICS_PERIODIC_OUTPUT=n
# CONFIG_THREAD_ANALYZER_ISR_STACK_USAGE=y

# Miscellaneous
CONFIG_BASE64=y
CONFIG_FILE_SYSTEM=y
CONFIG_MAIN_STACK_SIZE=8192
CONFIG_NET_PKT_RX_COUNT=16
CONFIG_NET_PKT_TX_COUNT=16
Expand Down
24 changes: 14 additions & 10 deletions embed-proplet/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,49 +10,53 @@ LOG_MODULE_REGISTER(main);
#define PROPLET_ID "proplet1"
#define CHANNEL_ID "channel1"

const char *channel_id = CHANNEL_ID;

void main(void)
{
LOG_INF("Starting Proplet...");

/* Initialize Wi-Fi */
wifi_manager_init();
if (wifi_manager_connect(WIFI_SSID, WIFI_PSK) != 0) {
LOG_ERR("Wi-Fi connection failed");
return;
}

/* Wait for Wi-Fi connection */
if (!wifi_manager_wait_for_connection(K_SECONDS(60))) {
LOG_ERR("Wi-Fi connection timed out");
return;
}

LOG_INF("Wi-Fi connected, proceeding with MQTT initialization");

/* Initialize and connect MQTT client */
while (mqtt_client_init_and_connect() != 0) {
while (mqtt_client_connect() != 0) {
LOG_ERR("MQTT client initialization failed. Retrying...");
k_sleep(K_SECONDS(5));
}

LOG_INF("MQTT connected successfully.");

/* Publish discovery announcement */
if (mqtt_client_discovery_announce(PROPLET_ID, CHANNEL_ID) != 0) {
if (publish_discovery(PROPLET_ID, CHANNEL_ID) != 0) {
LOG_ERR("Discovery announcement failed");
return;
}

/* Subscribe to topics */
if (mqtt_client_subscribe(CHANNEL_ID) != 0) {
if (subscribe(CHANNEL_ID) != 0) {
LOG_ERR("Topic subscription failed");
return;
}

LOG_INF("Proplet ready");

/* Main loop for MQTT processing */
while (1) {
mqtt_client_process();
k_sleep(K_MSEC(100)); // Adjusted for better responsiveness

if (mqtt_connected) {
publish_alive_message(CHANNEL_ID);
} else {
LOG_WRN("MQTT client is not connected");
}

k_sleep(K_SECONDS(30));
}
}
Loading

0 comments on commit ef76450

Please sign in to comment.