Skip to content

Commit

Permalink
Fix Wi-Fi struct and function usage errors
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffMboya committed Jan 10, 2025
1 parent 1ec341a commit 9ca633c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
6 changes: 6 additions & 0 deletions embed-proplet/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@ CONFIG_MAIN_STACK_SIZE=4096
CONFIG_HEAP_MEM_POOL_SIZE=16384

CONFIG_WIFI=y
CONFIG_WIFI_ESP32=y
CONFIG_WIFI_CREDENTIALS_STATIC=y
CONFIG_WIFI_CREDENTIALS_STATIC_SSID="Nakuja"
CONFIG_WIFI_CREDENTIALS_STATIC_PASSWORD="12345678"
CONFIG_NET_L2_WIFI_MGMT=y
CONFIG_NET_MGMT=y
CONFIG_NET_MGMT_EVENT=y
CONFIG_NET_MGMT_EVENT_INFO=y


CONFIG_NET_IPV4=y
CONFIG_NET_IPV6=n
Expand Down
24 changes: 18 additions & 6 deletions embed-proplet/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
#include <net/mqtt.h>
#include <net/socket.h>
#include <net/net_config.h>
#include <net/wifi.h>
#include <json.h>
#include <zephyr/random/random.h>
#include "wasm_handler.h"
#include <zephyr/net/wifi.h>
#include <zephyr/net/socket.h>
#include <zephyr/net/net_ip.h>
#include <zephyr/net/net_mgmt.h>
#include <zephyr/net/wifi_mgmt.h>

#define MQTT_BROKER_HOSTNAME "broker.supermq.example"
#define MQTT_BROKER_PORT 1883
Expand All @@ -25,8 +29,8 @@ static int configure_broker(void)
broker->sin_family = AF_INET;
broker->sin_port = htons(MQTT_BROKER_PORT);

int ret = inet_pton(AF_INET, MQTT_BROKER_HOSTNAME, &broker->sin_addr);
if (ret != 1) {
int ret = net_addr_pton(AF_INET, MQTT_BROKER_HOSTNAME, &broker->sin_addr);
if (ret != 0) {
printk("Failed to configure broker address.\n");
return -EINVAL;
}
Expand Down Expand Up @@ -119,15 +123,23 @@ static int wifi_connect(void)
return -ENODEV;
}

struct wifi_connect_req_params params = {
.ssid = CONFIG_WIFI_CREDENTIALS_STATIC_SSID,
.ssid_length = strlen(CONFIG_WIFI_CREDENTIALS_STATIC_SSID),
.psk = CONFIG_WIFI_CREDENTIALS_STATIC_PASSWORD,
.psk_length = strlen(CONFIG_WIFI_CREDENTIALS_STATIC_PASSWORD),
.channel = WIFI_CHANNEL_ANY,
.security = WIFI_SECURITY_TYPE_PSK,
};

printk("Connecting to Wi-Fi...\n");
int ret = net_wifi_connect(iface);
int ret = net_mgmt(NET_REQUEST_WIFI_CONNECT, iface, &params, sizeof(params));
if (ret) {
printk("Wi-Fi connection failed: %d\n", ret);
return ret;
}

k_sleep(K_SECONDS(5));

printk("Wi-Fi connected successfully.\n");
return 0;
}

Expand Down

0 comments on commit 9ca633c

Please sign in to comment.