Skip to content

Commit

Permalink
Add mDNS functionality
Browse files Browse the repository at this point in the history
Closes #9
  • Loading branch information
krzmaz committed Nov 13, 2022
1 parent d388ad4 commit 85f11d5
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 74 deletions.
10 changes: 9 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,17 @@ execute_process(COMMAND
)
file(RENAME fsdata.c my_fsdata.c)


# TODO fix after https://github.com/georgerobotics/cyw43-driver/pull/39 gets into the SDK
set(BROKEN_FILE ${PICO_CYW43_DRIVER_PATH}/src/cyw43_lwip.c)
file(READ ${BROKEN_FILE} FILE_CONTENTS)
string(REPLACE "LWIP_MDNS_RESPONDER" "LWIP_1_MDNS_RESPONDER" FILE_CONTENTS "${FILE_CONTENTS}")
file(WRITE ${BROKEN_FILE} "${FILE_CONTENTS}")

add_executable(${PROGRAM_NAME}
main.cpp
double_reset_detector.cpp
access_point.cpp
network_utils.cpp
http_server.cpp
)
target_compile_definitions(${PROGRAM_NAME} PRIVATE
Expand All @@ -35,6 +42,7 @@ target_link_libraries(${PROGRAM_NAME}
cyw43_driver_base
pico_cyw43_arch_lwip_threadsafe_background
pico_lwip_http
pico_lwip_mdns
pico_stdlib
hardware_adc
)
Expand Down
39 changes: 0 additions & 39 deletions src/access_point.cpp

This file was deleted.

7 changes: 0 additions & 7 deletions src/access_point.h

This file was deleted.

4 changes: 4 additions & 0 deletions src/lwipopts.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@

#define LWIP_AUTOIP 1

#define LWIP_IGMP 1
#define LWIP_MDNS_RESPONDER 1
#define LWIP_NUM_NETIF_CLIENT_DATA (LWIP_MDNS_RESPONDER)

#define LWIP_HTTPD 1
#define LWIP_HTTPD_SSI 1
// don't include the tag comment - less work for the CPU, but may be harder to debug
Expand Down
30 changes: 3 additions & 27 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,14 @@

#include "double_reset_detector.h"
#include "http_server.h"
#include "access_point.h"
#include "network_utils.h"

#include "lwipopts.h"

namespace double_reset_detector {
extern bool double_reset_detected;
}

int connect_to_wifi() {
cyw43_arch_enable_sta_mode();
// this seems to be the best be can do using the predefined `cyw43_pm_value` macro:
// cyw43_wifi_pm(&cyw43_state, CYW43_PERFORMANCE_PM);
// however it doesn't use the `CYW43_NO_POWERSAVE_MODE` value, so we do this instead:
cyw43_wifi_pm(&cyw43_state, cyw43_pm_value(CYW43_NO_POWERSAVE_MODE, 20, 1, 1, 1));

printf("Connecting to WiFi...\n");
if (cyw43_arch_wifi_connect_timeout_ms(WIFI_SSID, WIFI_PASSWORD, CYW43_AUTH_WPA2_AES_PSK, 30000)) {
printf("failed to connect.\n");
return ERR_CONN;
} else {
printf("Connected.\n");

auto ip_addr = cyw43_state.netif[CYW43_ITF_STA].ip_addr.addr;
printf("IP Address: %lu.%lu.%lu.%lu\n", ip_addr & 0xFF, (ip_addr >> 8) & 0xFF, (ip_addr >> 16) & 0xFF, ip_addr >> 24);
}
return ERR_OK;
}

void setup() {
if (double_reset_detector::double_reset_detected) {
printf("Double reset detected!\n");
access_point::setup_access_point();
network_utils::setup_access_point();
http_server::start_http_server();
while (true) {
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 1);
Expand All @@ -59,7 +35,7 @@ int main() {
// turn on LED to distinguish from BOOTSEL mode
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 1);
setup();
if (connect_to_wifi() != ERR_OK) {
if (network_utils::connect_to_wifi() != ERR_OK) {
return 1;
}
http_server::start_http_server();
Expand Down
88 changes: 88 additions & 0 deletions src/network_utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#include "network_utils.h"

extern "C" {
#include <cyw43.h>
}
#include <lwip/autoip.h>
#include <lwip/apps/mdns.h>
#include <lwip/err.h>
#include <pico/cyw43_arch.h>

namespace network_utils {

err_t setup_mdns(struct netif *netif){
const auto hostname = "pico-w-webserver";
mdns_resp_init();
return mdns_resp_add_netif(netif, hostname);
}

err_t connect_to_wifi() {
cyw43_arch_enable_sta_mode();
// this seems to be the best be can do using the predefined `cyw43_pm_value` macro:
// cyw43_wifi_pm(&cyw43_state, CYW43_PERFORMANCE_PM);
// however it doesn't use the `CYW43_NO_POWERSAVE_MODE` value, so we do this instead:
cyw43_wifi_pm(&cyw43_state, cyw43_pm_value(CYW43_NO_POWERSAVE_MODE, 20, 1, 1, 1));
auto& netif = cyw43_state.netif[CYW43_ITF_STA];
const auto err = setup_mdns(&netif);
if (err != ERR_OK) {
return err;
}

printf("Connecting to WiFi...\n");
if (cyw43_arch_wifi_connect_timeout_ms(WIFI_SSID, WIFI_PASSWORD, CYW43_AUTH_WPA2_AES_PSK, 30000)) {
printf("failed to connect.\n");
return ERR_CONN;
} else {
printf("Connected.\n");

auto ip_addr = netif.ip_addr.addr;
printf("IP Address: %lu.%lu.%lu.%lu\n", ip_addr & 0xFF, (ip_addr >> 8) & 0xFF, (ip_addr >> 16) & 0xFF, ip_addr >> 24);
}
return ERR_OK;
}

err_t stop_access_point(){
auto& netif = cyw43_state.netif[CYW43_ITF_AP];
auto err = autoip_stop(&netif);
if (err != ERR_OK){
return err;
}
err = mdns_resp_remove_netif(&netif);
if (err != ERR_OK){
return err;
}
return ERR_OK;
}

err_t setup_access_point() {
if (!cyw43_is_initialized(&cyw43_state)){
if (cyw43_arch_init()) {
printf("failed to initialise\n");
return ERR_ABRT;
}
}

const char *ap_name = "picow_webserver";
const char *password = NULL;

cyw43_arch_enable_ap_mode(ap_name, password, CYW43_AUTH_WPA2_AES_PSK);
// sleep mostly to let serial monitor catch the ip log. probably to be removed once mDNS is set up
sleep_ms(2000);

auto& netif = cyw43_state.netif[CYW43_ITF_AP];
const auto err = setup_mdns(&netif);
if (err != ERR_OK) {
return err;
}

autoip_start(&netif);
auto link_local_ip_addr = netif_autoip_data(&netif)->llipaddr.addr;
printf("IP Address: %lu.%lu.%lu.%lu\n",
link_local_ip_addr & 0xFF,
(link_local_ip_addr >> 8) & 0xFF,
(link_local_ip_addr >> 16) & 0xFF,
link_local_ip_addr >> 24);
return ERR_OK;
}

} // namespace network_utils
11 changes: 11 additions & 0 deletions src/network_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#include <lwip/err.h>

namespace network_utils {
err_t connect_to_wifi();

err_t stop_access_point();

err_t setup_access_point();
} // namespace network_utils

0 comments on commit 85f11d5

Please sign in to comment.