Added the webserver and the wifi handling
This commit is contained in:
55
src/main.cpp
Normal file
55
src/main.cpp
Normal file
@@ -0,0 +1,55 @@
|
||||
#include "bme280.h"
|
||||
#include "dhcp_server.h"
|
||||
#include "hardware/i2c.h"
|
||||
#include "lwip/apps/httpd.h"
|
||||
#include "pico/cyw43_arch.h"
|
||||
#include "pico/stdlib.h"
|
||||
#include "webserver.h"
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
|
||||
void ap_init() {
|
||||
cyw43_arch_enable_ap_mode("SensorAP", "passwort123", CYW43_AUTH_WPA2_AES_PSK);
|
||||
|
||||
ip_addr_t gw{};
|
||||
ip_addr_t mask{};
|
||||
IP4_ADDR(&gw, 192, 168, 4, 1);
|
||||
IP4_ADDR(&mask, 255, 255, 255, 0);
|
||||
|
||||
static dhcp_server_t dhcp_server{};
|
||||
dhcp_server_init(&dhcp_server, &gw, &mask);
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
stdio_init_all();
|
||||
sleep_ms(3000);
|
||||
cyw43_arch_init();
|
||||
httpd_init();
|
||||
webserver_init();
|
||||
|
||||
while (true) {
|
||||
memset(saved_ssid, 0, sizeof(saved_ssid));
|
||||
memset(saved_password, 0, sizeof(saved_password));
|
||||
ap_init();
|
||||
|
||||
while (saved_ssid[0] == '\0') {
|
||||
cyw43_arch_poll();
|
||||
sleep_ms(100);
|
||||
}
|
||||
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))) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
while (true) {
|
||||
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 1);
|
||||
sleep_ms(200);
|
||||
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 0);
|
||||
sleep_ms(200);
|
||||
}
|
||||
}
|
||||
22
src/webserver.c
Normal file
22
src/webserver.c
Normal file
@@ -0,0 +1,22 @@
|
||||
#include "lwip/apps/httpd.h"
|
||||
#include "string.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);
|
||||
}
|
||||
if (strcmp(pcParam[i], "password") == 0) {
|
||||
strncpy(saved_password, pcValue[i], 64);
|
||||
}
|
||||
}
|
||||
return "/index.html";
|
||||
}
|
||||
|
||||
static const tCGI cgi_handlers[] = {{"/config", config_handler}};
|
||||
|
||||
void webserver_init(void) { http_set_cgi_handlers(cgi_handlers, 1); }
|
||||
17
src/webserver.h
Normal file
17
src/webserver.h
Normal file
@@ -0,0 +1,17 @@
|
||||
#ifndef WEBSERVER_H
|
||||
#define WEBSERVER_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern char saved_ssid[33];
|
||||
extern char saved_password[65];
|
||||
|
||||
void webserver_init(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user