%lu\n", os_random());
- httpdSend(connData, buff, -1);
+ // the "success" field is after the data,
+ // so if readout fails, success can be set to false.
- if (--rns->count_remain == 0) {
- break;
+ if (strcmp(token, "values") == 0) {
+
+ if (st->done_count == 0) {
+ dbg("Delay to simulate readout...");
+ // 1000 ms delay
+ for(int i=0;i<1000;i++) {
+ os_delay_us(1000);
}
}
- // We are done.
- if (rns->count_remain == 0) {
- free(rns);
- *arg = NULL; // mark as already freed
- return HTTPD_CGI_DONE;
+ // TODO wait for data to be received
+ // on error, terminate and proceed to the "success" field.
+
+ u32 chunk = MIN(10, st->total_count - st->done_count); // chunks of 10
+
+ // chunk of data...
+ for (u32 i = 0; i < chunk; i++, st->done_count++) {
+ // preceding comma if not the first number
+ if (st->done_count > 0) {
+ httpdSend(connData, ", ", 2);
+ }
+
+ // one number
+ os_sprintf(buff20, "%lu", os_random());
+ httpdSend(connData, buff20, -1);
}
- return HTTPD_CGI_MORE;
+ // wait for more in this substitution
+ if (st->done_count < st->total_count)
+ return HTTPD_CGI_MORE; // more numbers to come
+
+ } else if (strcmp(token, "success") == 0) {
+ // success status
+ httpdSend(connData, (st->success ? "true" : "false"), -1);
}
return HTTPD_CGI_DONE;
}
+
+
+
+// Example of multi-pass generation of a html file
+
+
/*
// better to put it in the fs...
@@ -121,8 +149,8 @@ int FLASH_FN cgiRandomNumbers(HttpdConnData *connData) {
httpdSend(connData, ""
""
""
- " Generated page."
- " "
+ " Generated page."
+ " "
""
""
""
diff --git a/user/cgi.h b/user/cgi.h
index a610216..0f7cdd3 100644
--- a/user/cgi.h
+++ b/user/cgi.h
@@ -4,6 +4,6 @@
#include
#include "httpd.h"
-int tplMultipart(HttpdConnData *connData, char *token, void **arg);
+int tplReadSamplesJSON(HttpdConnData *connData, char *token, void **arg);
#endif
diff --git a/user/routes.c b/user/routes.c
new file mode 100644
index 0000000..e6a94f0
--- /dev/null
+++ b/user/routes.c
@@ -0,0 +1,99 @@
+#include "routes.h"
+
+#include
+#include
+
+#include "httpdespfs.h"
+#include "cgiwifi.h"
+//#include "cgiflash.h"
+//#include "auth.h"
+//#include "cgiwebsocket.h"
+
+// user files
+#include "cgi.h"
+#include "page_home.h"
+#include "sampling.h"
+
+#define WIFI_PROTECT 0
+
+#if WIFI_PROTECT
+static int FLASH_FN myPassFn(HttpdConnData *connData, int no, char *user, int userLen, char *pass, int passLen);
+#endif
+
+
+/**
+ * This is the main url->function dispatching data struct.
+ *
+ * In short, it's a struct with various URLs plus their handlers. The handlers can
+ * be 'standard' CGI functions you wrote, or 'special' CGIs requiring an argument.
+ * They can also be auth-functions. An asterisk will match any url starting with
+ * everything before the asterisks; "*" matches everything. The list will be
+ * handled top-down, so make sure to put more specific rules above the more
+ * general ones. Authorization things (like authBasic) act as a 'barrier' and
+ * should be placed above the URLs they protect.
+ */
+HttpdBuiltInUrl builtInUrls[] = {
+ ROUTE_CGI_ARG("*", cgiRedirectApClientToHostname, "esp8266.nonet"), // redirect func for the captive portal
+
+ ROUTE_TPL_FILE("/", tplHome, "/pages/home.tpl.html"),
+
+ ROUTE_CGI("/acquire.cgi", cgiReadSamples),
+
+
+ // API for measurements
+ ROUTE_TPL_FILE("/meas/samples.json", tplReadSamplesJSON, "/json/samples.tpl.json"),
+
+
+#if WIFI_PROTECT
+ ROUTE_AUTH("/wifi/*", myPassFn),
+#endif
+
+ ROUTE_REDIRECT("/wifi/", "/wifi"),
+ ROUTE_TPL_FILE("/wifi", tplWlan, "/pages/wifi.tpl.html"),
+
+ ROUTE_CGI("/wifi/scan.cgi", cgiWiFiScan),
+ ROUTE_CGI("/wifi/connect.cgi", cgiWiFiConnect),
+ ROUTE_CGI("/wifi/connstatus.cgi", cgiWiFiConnStatus),
+ ROUTE_CGI("/wifi/setmode.cgi", cgiWiFiSetMode),
+
+ ROUTE_FS("*"), //Catch-all cgi function for the filesystem NOTE: unsafe, lets user read templates.
+
+ ROUTE_END()
+};
+
+
+#if WIFI_PROTECT
+/**
+ * @brief BasicAuth name/password checking function.
+ *
+ * It's invoked by the authBasic() built-in route handler
+ * until it returns 0. Each time it populates the provided
+ * name and password buffer.
+ *
+ * @param connData : connection context
+ * @param no : user number (incremented each time it's called)
+ * @param user : user buffer
+ * @param userLen : user buffer size
+ * @param pass : password buffer
+ * @param passLen : password buffer size
+ * @return 0 to end, 1 if more users are available.
+ */
+static int FLASH_FN myPassFn(HttpdConnData *connData, int no, char *user, int userLen, char *pass, int passLen)
+{
+ (void)connData;
+ (void)userLen;
+ (void)passLen;
+
+ if (no == 0) {
+ os_strcpy(user, "admin");
+ os_strcpy(pass, "s3cr3t");
+ return 1;
+//Add more users this way. Check against incrementing no for each user added.
+// } else if (no==1) {
+// os_strcpy(user, "user1");
+// os_strcpy(pass, "something");
+// return 1;
+ }
+ return 0;
+}
+#endif
diff --git a/user/routes.h b/user/routes.h
new file mode 100644
index 0000000..220c57c
--- /dev/null
+++ b/user/routes.h
@@ -0,0 +1,8 @@
+#ifndef ROUTES_H
+#define ROUTES_H
+
+#include
+
+extern HttpdBuiltInUrl builtInUrls[];
+
+#endif // ROUTES_H
diff --git a/user/user_main.c b/user/user_main.c
index e530164..e87b25a 100644
--- a/user/user_main.c
+++ b/user/user_main.c
@@ -1,4 +1,6 @@
/*
+ * Based on an example project for ESP-HTTPD.
+ *
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* Jeroen Domburg wrote this file. As long as you retain
@@ -10,33 +12,25 @@
// library headers
#include
#include "httpd.h"
-#include "httpdespfs.h"
-#include "cgiwifi.h"
-#include "cgiflash.h"
-#include "auth.h"
#include "espfs.h"
-#include "captdns.h"
#include "webpages-espfs.h"
-#include "cgiwebsocket.h"
+#include "captdns.h"
-// user files
-#include "cgi.h"
#include "serial.h"
#include "io.h"
#include "datalink.h"
-#include "uart_driver.h"
#include "uptime.h"
+#include "routes.h"
-#include "page_home.h"
-#include "sampling.h"
+extern HttpdBuiltInUrl builtInUrls[];
-#include "sbmp.h"
-
-static ETSTimer prHeapTimer;
+static ETSTimer prSecondTimer;
/** Timer called each second */
-static void ICACHE_FLASH_ATTR prHeapTimerCb(void *arg)
+static void ICACHE_FLASH_ATTR prSecondTimerCb(void *arg)
{
+ (void)arg;
+
static u8 cnt = 0;
static u32 last = 0;
@@ -54,41 +48,6 @@ static void ICACHE_FLASH_ATTR prHeapTimerCb(void *arg)
}
-/**
- * @brief BasicAuth name/password checking function.
- *
- * It's invoked by the authBasic() built-in route handler
- * until it returns 0. Each time it populates the provided
- * name and password buffer.
- *
- * @param connData : connection context
- * @param no : user number (incremented each time it's called)
- * @param user : user buffer
- * @param userLen : user buffer size
- * @param pass : password buffer
- * @param passLen : password buffer size
- * @return 0 to end, 1 if more users are available.
- */
-int FLASH_FN myPassFn(HttpdConnData *connData, int no, char *user, int userLen, char *pass, int passLen)
-{
- (void)connData;
- (void)userLen;
- (void)passLen;
-
- if (no == 0) {
- os_strcpy(user, "admin");
- os_strcpy(pass, "s3cr3t");
- return 1;
-//Add more users this way. Check against incrementing no for each user added.
-// } else if (no==1) {
-// os_strcpy(user, "user1");
-// os_strcpy(pass, "something");
-// return 1;
- }
- return 0;
-}
-
-
// Some stuff for alternative ESPFS storage methods
#ifdef ESPFS_POS
CgiUploadFlashDef uploadParams = {
@@ -99,6 +58,7 @@ CgiUploadFlashDef uploadParams = {
};
#define INCLUDE_FLASH_FNS
#endif
+
#ifdef OTA_FLASH_SIZE_K
CgiUploadFlashDef uploadParams = {
.type = CGIFLASH_TYPE_FW,
@@ -111,56 +71,6 @@ CgiUploadFlashDef uploadParams = {
#endif
-/**
- * This is the main url->function dispatching data struct.
- *
- * In short, it's a struct with various URLs plus their handlers. The handlers can
- * be 'standard' CGI functions you wrote, or 'special' CGIs requiring an argument.
- * They can also be auth-functions. An asterisk will match any url starting with
- * everything before the asterisks; "*" matches everything. The list will be
- * handled top-down, so make sure to put more specific rules above the more
- * general ones. Authorization things (like authBasic) act as a 'barrier' and
- * should be placed above the URLs they protect.
- */
-
-
-static HttpdBuiltInUrl builtInUrls[] = {
- ROUTE_CGI_ARG("*", cgiRedirectApClientToHostname, "esp8266.nonet"), // redirect func for the captive portal
-
- ROUTE_TPL_FILE("/", tplHome, "/pages/home.tpl"),
-
- ROUTE_CGI("/acquire.cgi", cgiReadSamples),
-
- ROUTE_TPL_FILE("/multipart", tplMultipart, "/multipart.tpl"),
-
-//Enable the line below to protect the WiFi configuration with an username/password combo.
-// ROUTE_AUTH("/wifi/*", myPassFn),
-
- ROUTE_REDIRECT("/wifi/", "/wifi"),
-
- ROUTE_TPL_FILE("/wifi", tplWlan, "/pages/wifi.tpl"),
-
- ROUTE_CGI("/wifi/scan.cgi", cgiWiFiScan),
- ROUTE_CGI("/wifi/connect.cgi", cgiWiFiConnect),
- ROUTE_CGI("/wifi/connstatus.cgi", cgiWiFiConnStatus),
- ROUTE_CGI("/wifi/setmode.cgi", cgiWiFiSetMode),
-
- ROUTE_FS("*"), //Catch-all cgi function for the filesystem
-
- ROUTE_END()
-};
-
-
-//static ETSTimer prTestTimer;
-
-//static void ICACHE_FLASH_ATTR test_timer_task(void *arg) {
-// const char * t = "Test\r\n";
-// UART_WriteBuffer(0, (uint8_t*)t, strlen(t), 1000);
-//}
-
-#define STR_HELPER(x) #x
-#define STR(x) STR_HELPER(x)
-
/**
* Main routine. Initialize stdout, the I/O, filesystem and the webserver and we're done.
*/
@@ -170,10 +80,10 @@ void user_init(void)
serialInit();
uptime_timer_init();
- banner("\n*** ESP8266 starting, "
- "HTTPD v."HTTPDVER", "
- "SBMP v."SBMP_VER", "
- "IoT SDK v." STR(ESP_SDK_VERSION)" ***\n");
+ banner("*** ESP8266 starting ***");
+ info("HTTPD v."HTTPDVER", SBMP v."SBMP_VER", IoT SDK v."STR(ESP_SDK_VERSION));
+ printf(LOG_EOL);
+
// reset button etc
ioInit();
@@ -196,15 +106,11 @@ void user_init(void)
httpdInit(builtInUrls, 80);
- info("\nReady\n");
-
- os_timer_disarm(&prHeapTimer);
- os_timer_setfn(&prHeapTimer, prHeapTimerCb, NULL);
- os_timer_arm(&prHeapTimer, 1000, 1);
-}
+ printf(LOG_EOL);
+ info("Ready");
+ printf(LOG_EOL);
-
-void user_rf_pre_init()
-{
- //Not needed, but some SDK versions want this defined.
+ os_timer_disarm(&prSecondTimer);
+ os_timer_setfn(&prSecondTimer, prSecondTimerCb, NULL);
+ os_timer_arm(&prSecondTimer, 1000, 1);
}