diff --git a/components/HTTPServe/CMakeLists.txt b/components/HTTPServe/CMakeLists.txt new file mode 100644 index 0000000..afede0e --- /dev/null +++ b/components/HTTPServe/CMakeLists.txt @@ -0,0 +1,3 @@ +idf_component_register(SRCS "HTTPServe.c" + INCLUDE_DIRS "include" + REQUIRES esp_http_server) diff --git a/components/HTTPServe/HTTPServe.c b/components/HTTPServe/HTTPServe.c new file mode 100644 index 0000000..d3011c4 --- /dev/null +++ b/components/HTTPServe/HTTPServe.c @@ -0,0 +1,73 @@ +#include +#include "esp_err.h" +#include "esp_log.h" +#include "esp_http_server.h" +#include "HTTPServe.h" + +esp_err_t start_server(HTTP_serve_config_t config){ + httpd_config_t serv_config = HTTPD_DEFAULT_CONFIG(); + serv_config.uri_match_fn = httpd_uri_match_wildcard; + httpd_handle_t server = NULL; + + ESP_LOGI("SERV", "addr %i", (&config)->mountpoint); + + httpd_uri_t uri_get = { + .uri = config.getUri, + .method = HTTP_GET, + .handler = get_handler, + .user_ctx = &config, + }; + httpd_uri_t uri_post = { + .uri = config.postUri, + .method = HTTP_POST, + .handler = post_handler, + .user_ctx = NULL, + }; + + esp_err_t status = httpd_start(&server, &serv_config); + if(status == ESP_OK) { + httpd_register_uri_handler(server, &uri_get); + httpd_register_uri_handler(server, &uri_post); + } + + return status; +} + +esp_err_t get_handler(httpd_req_t *req){ + char* prefix = ((HTTP_serve_config_t*)req->user_ctx)->mountpoint; + const char* uri = req->uri; + bool inRoot = !strcmp(uri,"/"); + char filename[strlen(uri) + strlen(prefix) + 1 + (inRoot?10:0)]; + strcpy(filename, prefix); + strcat(filename, uri); + if(inRoot) + strcat(filename, "index.html"); + const char err404[] = "404 page not found"; + + ESP_LOGI("HTTP","req : %s %s", prefix, filename); + FILE* f; + if(!(f = fopen(filename,"r"))){ + httpd_resp_send(req, err404, HTTPD_RESP_USE_STRLEN); + return ESP_FAIL; + } + + if(strstr(uri, ".js") != NULL) + httpd_resp_set_type(req, "application/javascript"); + char chunk[1024]; + size_t chunksize; + do { + chunksize = fread(chunk, 1, sizeof(chunk), f); + if (httpd_resp_send_chunk(req, chunk, chunksize) != ESP_OK) { + fclose(f); + return ESP_FAIL; + } + } while (chunksize != 0); + + httpd_resp_send_chunk(req, NULL, 0); + fclose(f); + return ESP_OK; +} + +esp_err_t post_handler(httpd_req_t *req){ + return ESP_OK; +} diff --git a/components/HTTPServe/include/HTTPServe.h b/components/HTTPServe/include/HTTPServe.h new file mode 100644 index 0000000..0812a3d --- /dev/null +++ b/components/HTTPServe/include/HTTPServe.h @@ -0,0 +1,13 @@ +#include "esp_http_server.h" + +struct HTTP_serve_config { + char mountpoint[32]; + char getUri[32]; + char postUri[32]; +}; +typedef struct HTTP_serve_config HTTP_serve_config_t; + +esp_err_t get_handler(httpd_req_t *req); +esp_err_t post_handler(httpd_req_t *req); + +esp_err_t start_server(HTTP_serve_config_t config); diff --git a/http/assets/index.css b/http/assets/index.css new file mode 100644 index 0000000..19ceff8 --- /dev/null +++ b/http/assets/index.css @@ -0,0 +1 @@ +:root{font-family:-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen,Ubuntu,Cantarell,Open Sans,Helvetica Neue,sans-serif}main.svelte-1fm71xa{text-align:center;padding:1em;margin:0 auto}h1.svelte-1fm71xa{color:#ff3e00;text-transform:uppercase;font-size:4rem;font-weight:100;line-height:1.1;margin:2rem auto;max-width:14rem}@media (min-width: 480px){h1.svelte-1fm71xa{max-width:none}} diff --git a/http/assets/index.js b/http/assets/index.js new file mode 100644 index 0000000..7c59c3c --- /dev/null +++ b/http/assets/index.js @@ -0,0 +1 @@ +import{S as _,i as y,s as x,e as d,a as v,b as u,n as f,d as m,o as S,c as A,f as L,g,h,m as I,t as M,j as O,k as P}from"./vendor.js";const k=function(){const n=document.createElement("link").relList;if(n&&n.supports&&n.supports("modulepreload"))return;for(const e of document.querySelectorAll('link[rel="modulepreload"]'))s(e);new MutationObserver(e=>{for(const t of e)if(t.type==="childList")for(const a of t.addedNodes)a.tagName==="LINK"&&a.rel==="modulepreload"&&s(a)}).observe(document,{childList:!0,subtree:!0});function o(e){const t={};return e.integrity&&(t.integrity=e.integrity),e.referrerpolicy&&(t.referrerPolicy=e.referrerpolicy),e.crossorigin==="use-credentials"?t.credentials="include":e.crossorigin==="anonymous"?t.credentials="omit":t.credentials="same-origin",t}function s(e){if(e.ep)return;e.ep=!0;const t=o(e);fetch(e.href,t)}};k();function N(i){let n,o,s;return{c(){n=d("canvas"),o=v(),s=d("p"),s.textContent=`${C} ppm`},m(e,t){u(e,n,t),i[3](n),u(e,o,t),u(e,s,t)},p:f,i:f,o:f,d(e){e&&m(n),i[3](null),e&&m(o),e&&m(s)}}}let C=7500;function q(i,n,o){let{min:s=0}=n,{max:e=15e3}=n,t;S(()=>{const r=t.getContext("2d");let l=t.width,b=t.height,p=l/5,w=(C-s)/e*Math.PI;r.arc(l/2,b,l/2-p,-Math.PI,-Math.PI+w,!1),r.lineWidth=p;let c=r.createLinearGradient(0,0,l,0);c.addColorStop("0","green"),c.addColorStop("0.5","yellow"),c.addColorStop("1.0","red"),r.strokeStyle=c,r.stroke()});function a(r){A[r?"unshift":"push"](()=>{t=r,o(0,t)})}return i.$$set=r=>{"min"in r&&o(1,s=r.min),"max"in r&&o(2,e=r.max)},[t,s,e,a]}class E extends _{constructor(n){super();y(this,n,q,N,x,{min:1,max:2})}}function j(i){let n,o,s,e,t;return e=new E({}),{c(){n=d("main"),o=d("h1"),o.textContent="AAA",s=v(),L(e.$$.fragment),g(o,"class","svelte-1fm71xa"),g(n,"class","svelte-1fm71xa")},m(a,r){u(a,n,r),h(n,o),h(n,s),I(e,n,null),t=!0},p:f,i(a){t||(M(e.$$.fragment,a),t=!0)},o(a){O(e.$$.fragment,a),t=!1},d(a){a&&m(n),P(e)}}}class B extends _{constructor(n){super();y(this,n,null,j,x,{})}}new B({target:document.getElementById("app")}); diff --git a/http/assets/vendor.js b/http/assets/vendor.js new file mode 100644 index 0000000..829114c --- /dev/null +++ b/http/assets/vendor.js @@ -0,0 +1 @@ +function S(){}function C(t){return t()}function j(){return Object.create(null)}function m(t){t.forEach(C)}function O(t){return typeof t=="function"}function L(t,n){return t!=t?n==n:t!==n||t&&typeof t=="object"||typeof t=="function"}function M(t){return Object.keys(t).length===0}function Q(t,n){t.appendChild(n)}function R(t,n,e){t.insertBefore(n,e||null)}function z(t){t.parentNode.removeChild(t)}function U(t){return document.createElement(t)}function B(t){return document.createTextNode(t)}function V(){return B(" ")}function W(t,n,e){e==null?t.removeAttribute(n):t.getAttribute(n)!==e&&t.setAttribute(n,e)}function F(t){return Array.from(t.childNodes)}let l;function f(t){l=t}function P(){if(!l)throw new Error("Function called outside component initialization");return l}function X(t){P().$$.on_mount.push(t)}const a=[],v=[],h=[],A=[],T=Promise.resolve();let y=!1;function q(){y||(y=!0,T.then(N))}function x(t){h.push(t)}const $=new Set;let d=0;function N(){const t=l;do{for(;d{_.delete(t),o&&(e&&t.d(1),o())}),t.o(n)}}function Z(t){t&&t.c()}function I(t,n,e,o){const{fragment:u,on_mount:p,on_destroy:i,after_update:g}=t.$$;u&&u.m(n,e),o||x(()=>{const s=p.map(C).filter(O);i?i.push(...s):m(s),t.$$.on_mount=[]}),g.forEach(x)}function J(t,n){const e=t.$$;e.fragment!==null&&(m(e.on_destroy),e.fragment&&e.fragment.d(n),e.on_destroy=e.fragment=null,e.ctx=[])}function K(t,n){t.$$.dirty[0]===-1&&(a.push(t),q(),t.$$.dirty.fill(0)),t.$$.dirty[n/31|0]|=1<{const k=E.length?E[0]:w;return r.ctx&&u(r.ctx[c],r.ctx[c]=k)&&(!r.skip_bound&&r.bound[c]&&r.bound[c](k),b&&K(t,c)),w}):[],r.update(),b=!0,m(r.before_update),r.fragment=o?o(r.ctx):!1,n.target){if(n.hydrate){const c=F(n.target);r.fragment&&r.fragment.l(c),c.forEach(z)}else r.fragment&&r.fragment.c();n.intro&&H(t.$$.fragment),I(t,n.target,n.anchor,n.customElement),N()}f(s)}class nt{$destroy(){J(this,1),this.$destroy=S}$on(n,e){const o=this.$$.callbacks[n]||(this.$$.callbacks[n]=[]);return o.push(e),()=>{const u=o.indexOf(e);u!==-1&&o.splice(u,1)}}$set(n){this.$$set&&!M(n)&&(this.$$.skip_bound=!0,this.$$set(n),this.$$.skip_bound=!1)}}export{nt as S,V as a,R as b,v as c,z as d,U as e,Z as f,W as g,Q as h,tt as i,Y as j,J as k,I as m,S as n,X as o,L as s,H as t}; diff --git a/http/favicon.ico b/http/favicon.ico new file mode 100644 index 0000000..fb9d98e Binary files /dev/null and b/http/favicon.ico differ diff --git a/http/index.html b/http/index.html new file mode 100644 index 0000000..991a6b1 --- /dev/null +++ b/http/index.html @@ -0,0 +1,16 @@ + + + + + + + CO2 Sense + + + + + +
+ + + diff --git a/main/CO2_Sense.c b/main/CO2_Sense.c index 42d7774..efc726f 100644 --- a/main/CO2_Sense.c +++ b/main/CO2_Sense.c @@ -14,6 +14,8 @@ #include "APlib.h" #include "HTTPServe.h" +#include + void app_main(void) { esp_err_t ret = nvs_flash_init(); @@ -46,4 +48,8 @@ void app_main(void) }; ESP_ERROR_CHECK(start_server(serv_config)); + + while(1){ + vTaskDelay(100 / portTICK_PERIOD_MS); + } } diff --git a/sdkconfig b/sdkconfig index 75f1878..c335edc 100644 --- a/sdkconfig +++ b/sdkconfig @@ -859,13 +859,14 @@ CONFIG_HEAP_TRACING_OFF=y # CONFIG_LOG_DEFAULT_LEVEL_NONE is not set # CONFIG_LOG_DEFAULT_LEVEL_ERROR is not set # CONFIG_LOG_DEFAULT_LEVEL_WARN is not set -# CONFIG_LOG_DEFAULT_LEVEL_INFO is not set -CONFIG_LOG_DEFAULT_LEVEL_DEBUG=y +CONFIG_LOG_DEFAULT_LEVEL_INFO=y +# CONFIG_LOG_DEFAULT_LEVEL_DEBUG is not set # CONFIG_LOG_DEFAULT_LEVEL_VERBOSE is not set -CONFIG_LOG_DEFAULT_LEVEL=4 +CONFIG_LOG_DEFAULT_LEVEL=3 CONFIG_LOG_MAXIMUM_EQUALS_DEFAULT=y +# CONFIG_LOG_MAXIMUM_LEVEL_DEBUG is not set # CONFIG_LOG_MAXIMUM_LEVEL_VERBOSE is not set -CONFIG_LOG_MAXIMUM_LEVEL=4 +CONFIG_LOG_MAXIMUM_LEVEL=3 CONFIG_LOG_COLORS=y CONFIG_LOG_TIMESTAMP_SOURCE_RTOS=y # CONFIG_LOG_TIMESTAMP_SOURCE_SYSTEM is not set