updated the webserver to properly use post
This commit is contained in:
@@ -31,7 +31,6 @@ add_custom_command(
|
||||
add_executable(sensor-pico
|
||||
src/main.cpp
|
||||
src/webserver.c
|
||||
src/fsdata.c
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
#ifndef _LWIPOPTS_H
|
||||
#define _LWIPOPTS_H
|
||||
|
||||
#define HTTPD_FSDATA_FILE "src/fsdata.c"
|
||||
|
||||
#define NO_SYS 1
|
||||
#define LWIP_SOCKET 0
|
||||
#define LWIP_NETCONN 0
|
||||
@@ -18,6 +20,7 @@
|
||||
#define LWIP_HTTPD 1
|
||||
#define LWIP_HTTPD_CGI 1
|
||||
#define LWIP_HTTPD_SSI 1
|
||||
#define LWIP_HTTPD_SUPPORT_POST 1
|
||||
|
||||
#define LWIP_NETIF_HOSTNAME 1
|
||||
#define LWIP_NETIF_STATUS_CALLBACK 1
|
||||
|
||||
33
src/main.cpp
33
src/main.cpp
@@ -4,11 +4,11 @@
|
||||
#include "lwip/apps/httpd.h"
|
||||
#include "pico/cyw43_arch.h"
|
||||
#include "pico/stdlib.h"
|
||||
#include "webserver.h"
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
|
||||
void ap_init() {
|
||||
/* void ap_init() {
|
||||
cyw43_arch_enable_ap_mode("SensorAP", "passwort123", CYW43_AUTH_WPA2_AES_PSK);
|
||||
|
||||
ip_addr_t gw{};
|
||||
@@ -18,38 +18,41 @@ void ap_init() {
|
||||
|
||||
static dhcp_server_t dhcp_server{};
|
||||
dhcp_server_init(&dhcp_server, &gw, &mask);
|
||||
}
|
||||
} */
|
||||
|
||||
int main() {
|
||||
|
||||
int ret{};
|
||||
stdio_init_all();
|
||||
sleep_ms(3000);
|
||||
cyw43_arch_init();
|
||||
httpd_init();
|
||||
webserver_init();
|
||||
|
||||
while (true) {
|
||||
memset(saved_ssid, 0, sizeof(saved_ssid));
|
||||
/* memset(saved_ssid, 0, sizeof(saved_ssid));
|
||||
memset(saved_password, 0, sizeof(saved_password));
|
||||
ap_init();
|
||||
ap_init(); */
|
||||
printf("Return Code: %d\n", ret);
|
||||
|
||||
while (saved_ssid[0] == '\0') {
|
||||
/* while (saved_ssid[0] == '\0') {
|
||||
cyw43_arch_poll();
|
||||
sleep_ms(100);
|
||||
}
|
||||
cyw43_arch_disable_ap_mode();
|
||||
} */
|
||||
// cyw43_arch_disable_ap_mode();
|
||||
cyw43_arch_enable_sta_mode();
|
||||
|
||||
if (!(cyw43_arch_wifi_connect_timeout_ms(saved_ssid, saved_password,
|
||||
CYW43_AUTH_WPA2_AES_PSK, 20000))) {
|
||||
ret = cyw43_arch_wifi_connect_timeout_ms("HainerErnst-IoT",
|
||||
"vpUaR68xLZzXanS7",
|
||||
CYW43_AUTH_WPA2_MIXED_PSK, 10000);
|
||||
if (ret == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
while (true) {
|
||||
cyw43_arch_poll();
|
||||
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 1);
|
||||
sleep_ms(200);
|
||||
sleep_ms(100);
|
||||
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 0);
|
||||
sleep_ms(200);
|
||||
sleep_ms(100);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,22 +1,69 @@
|
||||
#include "lwip/apps/httpd.h"
|
||||
#include "string.h"
|
||||
#include <stdint.h>
|
||||
|
||||
char saved_ssid[33];
|
||||
char saved_password[65];
|
||||
|
||||
static const char *config_handler(int iIndex, int iNumParams, char *pcParam[],
|
||||
char *pcValue[]) {
|
||||
for (int i = 0; i < iNumParams; i++) {
|
||||
if (strcmp(pcParam[i], "ssid") == 0) {
|
||||
strncpy(saved_ssid, pcValue[i], 32);
|
||||
/* static const char *config_handler(int iIndex, int iNumParams, char
|
||||
*pcParam[], char *pcValue[]) { for (int i = 0; i < iNumParams; i++) { if
|
||||
(strcmp(pcParam[i], "ssid") == 0) { strncpy(saved_ssid, pcValue[i], 32);
|
||||
}
|
||||
if (strcmp(pcParam[i], "password") == 0) {
|
||||
strncpy(saved_password, pcValue[i], 64);
|
||||
}
|
||||
}
|
||||
return "/index.html";
|
||||
} */
|
||||
|
||||
err_t httpd_post_begin(void *connection, const char *uri,
|
||||
const char *http_request, u16_t http_request_len,
|
||||
int content_len, char *response_uri,
|
||||
u16_t response_uri_len, u8_t *post_auto_wnd) {
|
||||
if (strcmp(uri, "/config") == 0) {
|
||||
strncpy(response_uri, "/index.html", response_uri_len);
|
||||
*post_auto_wnd = 1;
|
||||
return ERR_OK;
|
||||
}
|
||||
return ERR_VAL;
|
||||
}
|
||||
|
||||
static const tCGI cgi_handlers[] = {{"/config", config_handler}};
|
||||
static char post_buffer[128];
|
||||
static uint16_t post_buffer_len = 0;
|
||||
|
||||
void webserver_init(void) { http_set_cgi_handlers(cgi_handlers, 1); }
|
||||
err_t httpd_post_receive_data(void *connection, struct pbuf *p) {
|
||||
post_buffer_len =
|
||||
pbuf_copy_partial(p, post_buffer, sizeof(post_buffer) - 1, 0);
|
||||
post_buffer[post_buffer_len] = '\0';
|
||||
pbuf_free(p);
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
void httpd_post_finished(void *connection, char *response_uri,
|
||||
u16_t response_uri_len) {
|
||||
strncpy(response_uri, "/index.html", response_uri_len);
|
||||
|
||||
char *pos = strstr(post_buffer, "ssid=");
|
||||
pos = pos + 5;
|
||||
char *end = strchr(pos, '&');
|
||||
int len = end - pos;
|
||||
if (len > 32) {
|
||||
len = 32;
|
||||
}
|
||||
strncpy(saved_ssid, pos, len);
|
||||
|
||||
char *pos2 = strstr(post_buffer, "password=") + 9;
|
||||
char *end2 = strchr(pos2, '\0');
|
||||
int len2 = end2 - pos2;
|
||||
if (len2 > 64) {
|
||||
len2 = 64;
|
||||
}
|
||||
strncpy(saved_password, pos2, len2);
|
||||
|
||||
saved_ssid[len] = '\0';
|
||||
saved_password[len] = '\0';
|
||||
}
|
||||
|
||||
// static const tCGI cgi_handlers[] = {{"/config", config_handler}};
|
||||
|
||||
// void webserver_init(void) { http_set_cgi_handlers(cgi_handlers, 1); }
|
||||
|
||||
@@ -8,8 +8,6 @@ extern "C" {
|
||||
extern char saved_ssid[33];
|
||||
extern char saved_password[65];
|
||||
|
||||
void webserver_init(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user