sen5x Module in eigenes Component verschoben
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,14 @@
|
|||||||
|
idf_component_register(
|
||||||
|
SRCS
|
||||||
|
"sen5x_i2c.c"
|
||||||
|
"sensirion_common.c"
|
||||||
|
"sensirion_i2c.c"
|
||||||
|
"sensirion_i2c_hal.c"
|
||||||
|
"i2cdev.c"
|
||||||
|
INCLUDE_DIRS
|
||||||
|
"."
|
||||||
|
REQUIRES
|
||||||
|
driver
|
||||||
|
freertos
|
||||||
|
log
|
||||||
|
)
|
||||||
@@ -2,12 +2,6 @@ idf_component_register(
|
|||||||
SRCS
|
SRCS
|
||||||
"main.c"
|
"main.c"
|
||||||
"lv_fs_port.c"
|
"lv_fs_port.c"
|
||||||
"i2cdev.c"
|
|
||||||
"sensirion_common.c"
|
|
||||||
"sensirion_i2c.c"
|
|
||||||
"sensirion_i2c_hal.c"
|
|
||||||
"sen5x_i2c.c"
|
|
||||||
"sen5x_app.c"
|
|
||||||
"wifi_mgr.c"
|
"wifi_mgr.c"
|
||||||
"http_portal.c"
|
"http_portal.c"
|
||||||
"storage.c"
|
"storage.c"
|
||||||
@@ -15,6 +9,7 @@ idf_component_register(
|
|||||||
"net_mdns.c"
|
"net_mdns.c"
|
||||||
"net_influx.c"
|
"net_influx.c"
|
||||||
"sys_status.c"
|
"sys_status.c"
|
||||||
|
"sen5x_app.c"
|
||||||
INCLUDE_DIRS
|
INCLUDE_DIRS
|
||||||
"."
|
"."
|
||||||
REQUIRES
|
REQUIRES
|
||||||
|
|||||||
@@ -160,7 +160,7 @@ bool net_mdns_find_service_url(const char* service, const char* proto,
|
|||||||
}
|
}
|
||||||
if (!txt_ok) continue;
|
if (!txt_ok) continue;
|
||||||
|
|
||||||
if (r->instance_name && strncmp(r->instance_name, "influxdb2-", 9) != 0) {
|
if (r->instance_name && strncmp(r->instance_name, "myapp-", 5) != 0) {
|
||||||
continue; // falsche Instanz, weitersuchen
|
continue; // falsche Instanz, weitersuchen
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -360,7 +360,7 @@ static void mdns_discover_task(void *arg) {
|
|||||||
const TickType_t kDelay = pdMS_TO_TICKS(60000); // 60 s
|
const TickType_t kDelay = pdMS_TO_TICKS(60000); // 60 s
|
||||||
for (;;) {
|
for (;;) {
|
||||||
// Erst offizieller Service, dann Fallback auf HTTP:8086
|
// Erst offizieller Service, dann Fallback auf HTTP:8086
|
||||||
esp_err_t err = net_mdns_autodiscover_and_store("_influxdb2", "_tcp", -1,
|
esp_err_t err = net_mdns_autodiscover_and_store("_myapp", "_tcp", -1,
|
||||||
NULL, NULL, 2000);
|
NULL, NULL, 2000);
|
||||||
if (err != ESP_OK) {
|
if (err != ESP_OK) {
|
||||||
err = net_mdns_autodiscover_and_store("_http", "_tcp", 8086,
|
err = net_mdns_autodiscover_and_store("_http", "_tcp", 8086,
|
||||||
|
|||||||
-238
@@ -1,238 +0,0 @@
|
|||||||
// File: src/http_portal.c
|
|
||||||
#include <string.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
#include "freertos/FreeRTOS.h"
|
|
||||||
#include "freertos/event_groups.h"
|
|
||||||
|
|
||||||
#include "esp_log.h"
|
|
||||||
#include "esp_wifi.h"
|
|
||||||
#include "esp_http_server.h"
|
|
||||||
|
|
||||||
#include "http_portal.h"
|
|
||||||
#include "storage.h"
|
|
||||||
|
|
||||||
// Provided by WiFi Manager (defined in wifi_mgr.c)
|
|
||||||
bool wifi_mgr_load_sta_creds(char **out_ssid, char **out_pass);
|
|
||||||
void wifi_mgr_save_sta_creds_and_reconnect(const char* ssid, const char* pass);
|
|
||||||
|
|
||||||
// NEW: Save device properties (implemented in wifi_mgr.c)
|
|
||||||
void wifi_mgr_save_device_info(const char* devicename, const char* token, const char* bucket, const char* org);
|
|
||||||
|
|
||||||
static char* html_escape(const char* s) {
|
|
||||||
if (!s) return strdup("");
|
|
||||||
size_t len = 0;
|
|
||||||
for (const char* p = s; *p; ++p) {
|
|
||||||
switch(*p){ case '&': len+=5; break; case '<': case '>': len+=4; break; case '"': case '\'': len+=6; break; default: len+=1; }
|
|
||||||
}
|
|
||||||
char* out = (char*)malloc(len+1); if(!out) return NULL;
|
|
||||||
char* o = out;
|
|
||||||
for (const char* p = s; *p; ++p) {
|
|
||||||
switch(*p){
|
|
||||||
case '&': memcpy(o,"&",5); o+=5; break;
|
|
||||||
case '<': memcpy(o,"<",4); o+=4; break;
|
|
||||||
case '>': memcpy(o,">",4); o+=4; break;
|
|
||||||
case '"': memcpy(o,""",6);o+=6; break;
|
|
||||||
case '\'':memcpy(o,"'",5); o+=5; break;
|
|
||||||
default: *o++ = *p;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*o = '\0';
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
|
|
||||||
static const char *TAG = "portal_http";
|
|
||||||
|
|
||||||
static httpd_handle_t s_http = NULL;
|
|
||||||
static EventGroupHandle_t s_wifi_events = NULL;
|
|
||||||
static EventBits_t s_BIT_GOTIP = 0;
|
|
||||||
static EventBits_t s_BIT_FAIL = 0;
|
|
||||||
static char s_ap_ssid[33] = {0};
|
|
||||||
|
|
||||||
// ---------- HTML (template with placeholders) ----------
|
|
||||||
static const char form_html_tpl[] =
|
|
||||||
"<!doctype html><html><head><meta charset='utf-8'/>"
|
|
||||||
"<meta name='viewport' content='width=device-width,initial-scale=1'/>"
|
|
||||||
"<title>Wi-Fi Setup</title>"
|
|
||||||
"<style>body{font-family:system-ui,Arial;margin:2rem;line-height:1.4}"
|
|
||||||
"label{display:block;margin-top:1rem}input,select{padding:.5rem;width:100%%;max-width:28rem}"
|
|
||||||
"button{margin-top:1rem;padding:.6rem 1rem;border:0;border-radius:.6rem;background:#0b6cff;color:#fff;}"
|
|
||||||
".card{max-width:32rem;padding:1rem 1.25rem;border:1px solid #e5e5e5;border-radius:1rem;box-shadow:0 2px 10px rgba(0,0,0,.04)}"
|
|
||||||
".row{display:flex;gap:.5rem;align-items:center;max-width:28rem}"
|
|
||||||
".muted{color:#666;font-size:.9em}</style>"
|
|
||||||
"</head><body><div class='card'>"
|
|
||||||
"<h1>Wi-Fi Configuration</h1>"
|
|
||||||
"<p>Connected to <b>%s</b>. Choose your Wi-Fi network or enter it manually:</p>"
|
|
||||||
"<div class='row'><select id='nets'></select><button id='refresh' type='button'>Refresh</button></div>"
|
|
||||||
"<form method='POST' action='/save'>"
|
|
||||||
"<label>SSID*<input id='ssid' name='ssid' required></label>"
|
|
||||||
"<label>Password*<input name='pass' type='password' required></label>"
|
|
||||||
"<label>Device Name*<input id='dev' name='dev' required value='%s' placeholder=''></label>"
|
|
||||||
"<label>Organization (optional)<input id='org' name='org' placeholder='' value='%s'></label>"
|
|
||||||
"<label>Bucket (optional)<input id='bucket' name='bucket' placeholder='' value='%s'></label>"
|
|
||||||
"<label>Token<input id='token' name='token' placeholder=''></label>"
|
|
||||||
"<button type='submit'>Save & Connect</button>"
|
|
||||||
"</form>"
|
|
||||||
"<p class='muted'>After saving, the device will connect to your Wi-Fi network. "
|
|
||||||
"The access point will automatically shut down shortly afterward.</p>"
|
|
||||||
"</div>"
|
|
||||||
"<script>"
|
|
||||||
"async function load(){try{"
|
|
||||||
" let r=await fetch('/scan'); if(!r.ok) throw 0; let a=await r.json();"
|
|
||||||
" let s=document.getElementById('nets'); s.innerHTML='';"
|
|
||||||
" a.sort((x,y)=>y.rssi-x.rssi);"
|
|
||||||
" a.forEach(n=>{let o=document.createElement('option');"
|
|
||||||
" o.value=n.ssid; o.textContent=n.ssid+' ('+n.rssi+' dBm)'; s.appendChild(o);});"
|
|
||||||
" if(a.length && !document.getElementById('ssid').value){"
|
|
||||||
" document.getElementById('ssid').value=a[0].ssid;}"
|
|
||||||
"}catch(e){console.log(e);}}"
|
|
||||||
"document.getElementById('refresh').onclick=load;"
|
|
||||||
"document.getElementById('nets').onchange=function(){document.getElementById('ssid').value=this.value;};"
|
|
||||||
"load();"
|
|
||||||
"</script>"
|
|
||||||
"</body></html>";
|
|
||||||
|
|
||||||
static const char saved_html[] =
|
|
||||||
"<!doctype html><html><head><meta charset='utf-8'/>"
|
|
||||||
"<meta name='viewport' content='width=device-width,initial-scale=1'/>"
|
|
||||||
"<title>Saved</title></head><body>"
|
|
||||||
"<h1>✔️ Saved</h1>"
|
|
||||||
"<p>The credentials have been saved. The device is now connecting to your Wi-Fi…</p>"
|
|
||||||
"<script>setTimeout(()=>location.href='/status',1500)</script>"
|
|
||||||
"</body></html>";
|
|
||||||
|
|
||||||
static const char status_tpl[] =
|
|
||||||
"<!doctype html><html><head><meta charset='utf-8'/>"
|
|
||||||
"<meta name='viewport' content='width=device-width,initial-scale=1'/>"
|
|
||||||
"<title>Status</title></head><body>"
|
|
||||||
"<h1>Connection Status</h1>"
|
|
||||||
"<p>%s</p>"
|
|
||||||
"<p><a href='/'>Back</a></p>"
|
|
||||||
"</body></html>";
|
|
||||||
|
|
||||||
// ---------- simple URL decoder ----------
|
|
||||||
static int hexval(char c){ if(c>='0'&&c<='9')return c-'0'; if(c>='a'&&c<='f')return 10+c-'a'; if(c>='A'&&c<='F')return 10+c-'A'; return -1; }
|
|
||||||
static void urldecode(char* s){
|
|
||||||
char* o = s;
|
|
||||||
for(char* i=s; *i; ++i){
|
|
||||||
if(*i=='+'){ *o++=' '; }
|
|
||||||
else if(*i=='%' && hexval(i[1])>=0 && hexval(i[2])>=0){ *o++ = (char)((hexval(i[1])<<4)|hexval(i[2])); i+=2; }
|
|
||||||
else { *o++=*i; }
|
|
||||||
}
|
|
||||||
*o='\0';
|
|
||||||
}
|
|
||||||
|
|
||||||
// ---------- HTTP Handlers ----------
|
|
||||||
static esp_err_t root_get(httpd_req_t *req) {
|
|
||||||
char *org = NULL, *bucket = NULL, *token = NULL;
|
|
||||||
if (!storage_get_effective_influx(&org, &bucket, &token)) {
|
|
||||||
org = strdup("");
|
|
||||||
bucket = strdup("");
|
|
||||||
token = strdup("");
|
|
||||||
if (!org || !bucket || !token) {
|
|
||||||
if (org) free(org);
|
|
||||||
if (bucket) free(bucket);
|
|
||||||
if (token) free(token);
|
|
||||||
return httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "oom");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
char *dev_raw = NULL;
|
|
||||||
if (!storage_get_effective_device_name(&dev_raw)) {
|
|
||||||
dev_raw = strdup("");
|
|
||||||
if (!dev_raw) {
|
|
||||||
free(org); free(bucket); free(token);
|
|
||||||
return httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "oom");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
char *dev_esc = html_escape(dev_raw);
|
|
||||||
char *org_esc = html_escape(org ? org : "");
|
|
||||||
char *bucket_esc = html_escape(bucket ? bucket : "");
|
|
||||||
if (!dev_esc || !org_esc || !bucket_esc) {
|
|
||||||
if (dev_esc) free(dev_esc);
|
|
||||||
if (org_esc) free(org_esc);
|
|
||||||
if (bucket_esc) free(bucket_esc);
|
|
||||||
free(org); free(bucket); free(token);
|
|
||||||
free(dev_raw);
|
|
||||||
return httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "oom");
|
|
||||||
}
|
|
||||||
|
|
||||||
int need = snprintf(NULL, 0, form_html_tpl,
|
|
||||||
s_ap_ssid,
|
|
||||||
dev_esc,
|
|
||||||
org_esc,
|
|
||||||
bucket_esc);
|
|
||||||
if (need < 0) {
|
|
||||||
free(org); free(bucket); free(token);
|
|
||||||
free(dev_raw);
|
|
||||||
free(dev_esc); free(org_esc); free(bucket_esc);
|
|
||||||
return httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "format error");
|
|
||||||
}
|
|
||||||
|
|
||||||
char *page = (char*)malloc((size_t)need + 1);
|
|
||||||
if (!page) {
|
|
||||||
free(org); free(bucket); free(token);
|
|
||||||
free(dev_raw);
|
|
||||||
free(dev_esc); free(org_esc); free(bucket_esc);
|
|
||||||
return httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "oom");
|
|
||||||
}
|
|
||||||
|
|
||||||
snprintf(page, (size_t)need + 1, form_html_tpl,
|
|
||||||
s_ap_ssid,
|
|
||||||
dev_esc,
|
|
||||||
org_esc,
|
|
||||||
bucket_esc);
|
|
||||||
|
|
||||||
httpd_resp_set_type(req, "text/html; charset=utf-8");
|
|
||||||
esp_err_t r = httpd_resp_send(req, page, HTTPD_RESP_USE_STRLEN);
|
|
||||||
|
|
||||||
free(page);
|
|
||||||
free(org); free(bucket); free(token);
|
|
||||||
free(dev_raw);
|
|
||||||
free(dev_esc); free(org_esc); free(bucket_esc);
|
|
||||||
|
|
||||||
return r;
|
|
||||||
}
|
|
||||||
|
|
||||||
static esp_err_t captive_ok(httpd_req_t *req) {
|
|
||||||
EventBits_t bits = xEventGroupGetBits(s_wifi_events);
|
|
||||||
if (bits & s_BIT_GOTIP) {
|
|
||||||
httpd_resp_set_status(req, "204 No Content");
|
|
||||||
httpd_resp_set_type(req, "text/plain");
|
|
||||||
return httpd_resp_send(req, NULL, 0);
|
|
||||||
} else {
|
|
||||||
httpd_resp_set_type(req, "text/html; charset=utf-8");
|
|
||||||
const char msg[] = "<html><body>OK</body></html>";
|
|
||||||
return httpd_resp_send(req, msg, HTTPD_RESP_USE_STRLEN);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static esp_err_t status_get(httpd_req_t *req) {
|
|
||||||
EventBits_t bits = xEventGroupGetBits(s_wifi_events);
|
|
||||||
const char *msg = (bits & s_BIT_GOTIP) ? "✅ Connected to external Wi-Fi."
|
|
||||||
: (bits & s_BIT_FAIL) ? "❌ Connection failed. Please check your credentials."
|
|
||||||
: "⏳ Connecting...";
|
|
||||||
|
|
||||||
int need = snprintf(NULL, 0, status_tpl, msg);
|
|
||||||
if (need < 0) return httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "format error");
|
|
||||||
|
|
||||||
char *page = (char*)malloc((size_t)need + 1);
|
|
||||||
if (!page) return httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "oom");
|
|
||||||
|
|
||||||
snprintf(page, (size_t)need + 1, status_tpl, msg);
|
|
||||||
httpd_resp_set_type(req, "text/html; charset=utf-8");
|
|
||||||
esp_err_t r = httpd_resp_send(req, page, HTTPD_RESP_USE_STRLEN);
|
|
||||||
free(page);
|
|
||||||
return r;
|
|
||||||
}
|
|
||||||
|
|
||||||
static esp_err_t favicon_get(httpd_req_t *req) {
|
|
||||||
httpd_resp_set_status(req, "204 No Content");
|
|
||||||
httpd_resp_set_type(req, "image/x-icon");
|
|
||||||
return httpd_resp_send(req, NULL, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
// The rest of the logic (save_post, scan_get, start_http, portal_http_start, portal_http_stop)
|
|
||||||
// remains IDENTICAL to your provided code above — only text/UI changes were made.
|
|
||||||
Reference in New Issue
Block a user