Skip to content

Commit

Permalink
modularize network; add mqtt client
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffMboya committed Jan 13, 2025
1 parent 3a80447 commit 5809dba
Show file tree
Hide file tree
Showing 7 changed files with 405 additions and 183 deletions.
2 changes: 1 addition & 1 deletion embed-proplet/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ zephyr_library_sources(${WAMR_RUNTIME_LIB_SOURCE})

zephyr_library_app_memory(wamr_partition)

target_sources(app PRIVATE src/main.c src/wasm_handler.c)
target_sources(app PRIVATE src/main.c src/mqtt_client.c src/wifi_manager.c src/wasm_handler.c)

target_link_libraries(app PRIVATE wamr_lib)
13 changes: 6 additions & 7 deletions embed-proplet/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ CONFIG_NET_UDP=y

# Interface Configuration
CONFIG_NET_IF_MAX_IPV4_COUNT=2
CONFIG_NET_IF_MAX_IPV6_COUNT=2
# required if NET_IPV6 is set
# CONFIG_NET_IF_MAX_IPV6_COUNT=2
CONFIG_NET_L2_ETHERNET=y
CONFIG_NET_L2_WIFI_MGMT=y

Expand All @@ -32,6 +33,10 @@ CONFIG_NET_RX_STACK_SIZE=8192
CONFIG_NET_TX_STACK_SIZE=4096
CONFIG_NET_MAX_CONTEXTS=10

# MQTT Client
CONFIG_MQTT_LIB=y
CONFIG_MQTT_KEEPALIVE=10

# Logging
CONFIG_LOG=y
CONFIG_NET_LOG=y
Expand All @@ -56,12 +61,6 @@ CONFIG_THREAD_ANALYZER_AUTO_INTERVAL=5
CONFIG_THREAD_ANALYZER_LOG_LEVEL_DBG=y
CONFIG_THREAD_ANALYZER_ISR_STACK_USAGE=y

# Wi-Fi Stack Sizes
CONFIG_WIFI_ESP_AT_RX_STACK_SIZE=10240
CONFIG_WIFI_ESP_AT_WORKQ_STACK_SIZE=8192
CONFIG_WIFI_NM_WPA_SUPPLICANT_THREAD_STACK_SIZE=8192
CONFIG_WIFI_NM_WPA_SUPPLICANT_WQ_STACK_SIZE=8192

# Miscellaneous
CONFIG_MAIN_STACK_SIZE=8192
CONFIG_NET_PKT_RX_COUNT=16
Expand Down
221 changes: 46 additions & 175 deletions embed-proplet/src/main.c
Original file line number Diff line number Diff line change
@@ -1,182 +1,53 @@
#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>
#include <zephyr/net/wifi_mgmt.h>
#include <zephyr/net/dhcpv4_server.h>
#include "wifi_manager.h"
// #include "mqtt_client.h"

LOG_MODULE_REGISTER(MAIN);
LOG_MODULE_REGISTER(main);

#define MACSTR "%02X:%02X:%02X:%02X:%02X:%02X"
#define WIFI_SSID "YourSSID"
#define WIFI_PSK "YourPassword"
// #define PROPLET_ID "proplet-esp32s3"
// #define CHANNEL_ID "default_channel"

#define NET_EVENT_WIFI_MASK \
(NET_EVENT_WIFI_CONNECT_RESULT | NET_EVENT_WIFI_DISCONNECT_RESULT | \
NET_EVENT_WIFI_AP_ENABLE_RESULT | NET_EVENT_WIFI_AP_DISABLE_RESULT | \
NET_EVENT_WIFI_AP_STA_CONNECTED | NET_EVENT_WIFI_AP_STA_DISCONNECTED)

/* AP Mode Configuration */
#define WIFI_AP_SSID "ESP32S3-AP"
#define WIFI_AP_PSK ""
#define WIFI_AP_IP_ADDRESS "192.168.4.1"
#define WIFI_AP_NETMASK "255.255.255.0"

/* STA Mode Configuration */
#define WIFI_SSID "Octavifi"
#define WIFI_PSK "Unic0rn_2030"

static struct net_if *ap_iface;
static struct net_if *sta_iface;

static struct wifi_connect_req_params ap_config;
static struct wifi_connect_req_params sta_config;

static struct net_mgmt_event_callback cb;

static void wifi_event_handler(struct net_mgmt_event_callback *cb, uint32_t mgmt_event,
struct net_if *iface)
void main(void)
{
switch (mgmt_event) {
case NET_EVENT_WIFI_CONNECT_RESULT: {
LOG_INF("Connected to %s", WIFI_SSID);
break;
}
case NET_EVENT_WIFI_DISCONNECT_RESULT: {
LOG_INF("Disconnected from %s", WIFI_SSID);
break;
}
case NET_EVENT_WIFI_AP_ENABLE_RESULT: {
LOG_INF("AP Mode is enabled. Waiting for station to connect");
break;
}
case NET_EVENT_WIFI_AP_DISABLE_RESULT: {
LOG_INF("AP Mode is disabled.");
break;
}
case NET_EVENT_WIFI_AP_STA_CONNECTED: {
struct wifi_ap_sta_info *sta_info = (struct wifi_ap_sta_info *)cb->info;

LOG_INF("station: " MACSTR " joined ", sta_info->mac[0], sta_info->mac[1],
sta_info->mac[2], sta_info->mac[3], sta_info->mac[4], sta_info->mac[5]);
break;
}
case NET_EVENT_WIFI_AP_STA_DISCONNECTED: {
struct wifi_ap_sta_info *sta_info = (struct wifi_ap_sta_info *)cb->info;

LOG_INF("station: " MACSTR " leave ", sta_info->mac[0], sta_info->mac[1],
sta_info->mac[2], sta_info->mac[3], sta_info->mac[4], sta_info->mac[5]);
break;
}
default:
break;
}
LOG_INF("Proplet starting...");

/* Initialize Wi-Fi */
wifi_manager_init();

/* Connect to Wi-Fi */
int ret = wifi_manager_connect(WIFI_SSID, WIFI_PSK);
if (ret != 0) {
LOG_ERR("Failed to connect to Wi-Fi, ret=%d", ret);
return;
}

// /* Initialize and connect MQTT client */
// ret = mqtt_client_init_and_connect();
// if (ret != 0) {
// LOG_ERR("Failed to initialize MQTT client, exiting");
// return;
// }

// /* Announce discovery */
// ret = mqtt_client_discovery_announce(PROPLET_ID, CHANNEL_ID);
// if (ret != 0) {
// LOG_ERR("Failed to publish discovery announcement, exiting");
// return;
// }

// /* Subscribe to topics */
// ret = mqtt_client_subscribe(CHANNEL_ID);
// if (ret != 0) {
// LOG_ERR("Failed to subscribe to topics, exiting");
// return;
// }

// /* Main loop for processing MQTT events */
// while (1) {
// mqtt_client_process(); /* Process MQTT events */
// k_sleep(K_SECONDS(5)); /* Sleep for a while */
// }
}

static void enable_dhcpv4_server(void)
{
static struct in_addr addr;
static struct in_addr netmaskAddr;

if (net_addr_pton(AF_INET, WIFI_AP_IP_ADDRESS, &addr)) {
LOG_ERR("Invalid address: %s", WIFI_AP_IP_ADDRESS);
return;
}

if (net_addr_pton(AF_INET, WIFI_AP_NETMASK, &netmaskAddr)) {
LOG_ERR("Invalid netmask: %s", WIFI_AP_NETMASK);
return;
}

net_if_ipv4_set_gw(ap_iface, &addr);

if (net_if_ipv4_addr_add(ap_iface, &addr, NET_ADDR_MANUAL, 0) == NULL) {
LOG_ERR("unable to set IP address for AP interface");
}

if (!net_if_ipv4_set_netmask_by_addr(ap_iface, &addr, &netmaskAddr)) {
LOG_ERR("Unable to set netmask for AP interface: %s", WIFI_AP_NETMASK);
}

addr.s4_addr[3] += 10;
if (net_dhcpv4_server_start(ap_iface, &addr) != 0) {
LOG_ERR("DHCP server is not started for desired IP");
return;
}

LOG_INF("DHCPv4 server started...\n");
}

static int enable_ap_mode(void)
{
if (!ap_iface) {
LOG_INF("AP: is not initialized");
return -EIO;
}

LOG_INF("Turning on AP Mode");
ap_config.ssid = (const uint8_t *)WIFI_AP_SSID;
ap_config.ssid_length = strlen(WIFI_AP_SSID);
ap_config.psk = (const uint8_t *)WIFI_AP_PSK;
ap_config.psk_length = strlen(WIFI_AP_PSK);
ap_config.channel = WIFI_CHANNEL_ANY;
ap_config.band = WIFI_FREQ_BAND_2_4_GHZ;

if (strlen(WIFI_AP_PSK) == 0) {
ap_config.security = WIFI_SECURITY_TYPE_NONE;
} else {

ap_config.security = WIFI_SECURITY_TYPE_PSK;
}

enable_dhcpv4_server();

int ret = net_mgmt(NET_REQUEST_WIFI_AP_ENABLE, ap_iface, &ap_config,
sizeof(struct wifi_connect_req_params));
if (ret) {
LOG_ERR("NET_REQUEST_WIFI_AP_ENABLE failed, err: %d", ret);
}

return ret;
}

static int connect_to_wifi(void)
{
if (!sta_iface) {
LOG_INF("STA: interface no initialized");
return -EIO;
}

sta_config.ssid = (const uint8_t *)WIFI_SSID;
sta_config.ssid_length = strlen(WIFI_SSID);
sta_config.psk = (const uint8_t *)WIFI_PSK;
sta_config.psk_length = strlen(WIFI_PSK);
sta_config.security = WIFI_SECURITY_TYPE_PSK;
sta_config.channel = WIFI_CHANNEL_ANY;
sta_config.band = WIFI_FREQ_BAND_2_4_GHZ;

LOG_INF("Connecting to SSID: %s\n", sta_config.ssid);

int ret = net_mgmt(NET_REQUEST_WIFI_CONNECT, sta_iface, &sta_config,
sizeof(struct wifi_connect_req_params));
if (ret) {
LOG_ERR("Unable to Connect to (%s)", WIFI_SSID);
}

return ret;
}

int main(void)
{
k_sleep(K_SECONDS(5));

net_mgmt_init_event_callback(&cb, wifi_event_handler, NET_EVENT_WIFI_MASK);
net_mgmt_add_event_callback(&cb);

/* Get AP interface in AP-STA mode. */
ap_iface = net_if_get_wifi_sap();

/* Get STA interface in AP-STA mode. */
sta_iface = net_if_get_wifi_sta();

// enable_ap_mode();
connect_to_wifi();

return 0;
}
Loading

0 comments on commit 5809dba

Please sign in to comment.