Skip to content

Commit

Permalink
Implement Access Point in setup mode
Browse files Browse the repository at this point in the history
Closes #6
  • Loading branch information
krzmaz committed Nov 13, 2022
1 parent 6f4222f commit d388ad4
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 21 deletions.
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ file(RENAME fsdata.c my_fsdata.c)
add_executable(${PROGRAM_NAME}
main.cpp
double_reset_detector.cpp
access_point.cpp
http_server.cpp
)
target_compile_definitions(${PROGRAM_NAME} PRIVATE
WIFI_SSID=\"${WIFI_SSID}\"
Expand Down
39 changes: 39 additions & 0 deletions src/access_point.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include "access_point.h"

#include <cyw43.h>
#include <lwip/autoip.h>
#include <pico/cyw43_arch.h>

namespace access_point {

int stop_access_point(){
autoip_stop(&cyw43_state.netif[CYW43_ITF_AP]);
return 0;
}

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

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);

autoip_start(&cyw43_state.netif[CYW43_ITF_AP]);
auto link_local_ip_addr = netif_autoip_data(&cyw43_state.netif[CYW43_ITF_AP])->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 0;
}

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

namespace access_point {
int stop_access_point();

int setup_access_point();
} // namespace access_point
6 changes: 6 additions & 0 deletions src/double_reset_detector.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#pragma once
namespace double_reset_detector {

extern bool double_reset_detected;

} // namespace double_reset_detector
11 changes: 9 additions & 2 deletions src/ssi.h → src/http_server.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#pragma once

#include "hardware/adc.h"
#include "lwip/apps/httpd.h"
#include "pico/cyw43_arch.h"

#include "lwipopts.h"

namespace http_server {

// max length of the tags defaults to be 8 chars
// LWIP_HTTPD_MAX_TAG_NAME_LEN
const char * __not_in_flash("httpd") ssi_example_tags[] = {
Expand Down Expand Up @@ -54,3 +54,10 @@ void ssi_init() {
ssi_example_tags, LWIP_ARRAYSIZE(ssi_example_tags)
);
}

void start_http_server() {
httpd_init();
ssi_init();
printf("Http server initialized.\n");
}
} // namespace http_server
4 changes: 4 additions & 0 deletions src/http_server.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#pragma once
namespace http_server {
void start_http_server();
} // namespace http_server
2 changes: 2 additions & 0 deletions src/lwipopts.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
#define LWIP_STATS_DISPLAY 1
#endif

#define LWIP_AUTOIP 1

#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
34 changes: 15 additions & 19 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
#include <string.h>
#include <stdlib.h>

#include "lwip/apps/httpd.h"
#include "lwip/err.h"
#include "pico/stdlib.h"
#include "pico/cyw43_arch.h"
#include <lwip/err.h>
#include <pico/stdlib.h>
#include <pico/cyw43_arch.h>

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

#include "lwipopts.h"
#include "ssi.h"

namespace double_reset_detector {
extern bool double_reset_detected;
}
Expand All @@ -26,28 +29,17 @@ int connect_to_wifi() {
} else {
printf("Connected.\n");

extern cyw43_t cyw43_state;
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 run_server() {
if (connect_to_wifi() != ERR_OK){
return;
}
httpd_init();
ssi_init();
printf("Http server initialized.\n");
// infinite loop for now
for (;;) {}
}

void setup() {
if (double_reset_detector::double_reset_detected) {
printf("Double reset detected!\n");
// just blink the LED for now
access_point::setup_access_point();
http_server::start_http_server();
while (true) {
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 1);
sleep_ms(500);
Expand All @@ -67,5 +59,9 @@ int main() {
// turn on LED to distinguish from BOOTSEL mode
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 1);
setup();
run_server();
if (connect_to_wifi() != ERR_OK) {
return 1;
}
http_server::start_http_server();
while (true) {};
}

0 comments on commit d388ad4

Please sign in to comment.