From 404895ec0460398132ab18900c1480bc28a46c30 Mon Sep 17 00:00:00 2001 From: David Kopf Date: Thu, 3 Nov 2011 14:58:33 -0400 Subject: [PATCH] Add streaming ajax page with csv text buffer --- apps/webserver-nano/httpd-cgi.c | 173 +++++++++++++++++- .../httpd-fs/makefsdata.ignore/ajax.js | 38 ++++ .../httpd-fs/makefsdata.ignore/ajax.shtml | 11 ++ .../httpd-fs/makefsdata.ignore/ajaxdata.shtml | 1 + apps/webserver-nano/httpd.h | 2 + 5 files changed, 217 insertions(+), 8 deletions(-) create mode 100644 apps/webserver-nano/httpd-fs/makefsdata.ignore/ajax.js create mode 100644 apps/webserver-nano/httpd-fs/makefsdata.ignore/ajax.shtml create mode 100644 apps/webserver-nano/httpd-fs/makefsdata.ignore/ajaxdata.shtml diff --git a/apps/webserver-nano/httpd-cgi.c b/apps/webserver-nano/httpd-cgi.c index c8684996c..26432eba3 100644 --- a/apps/webserver-nano/httpd-cgi.c +++ b/apps/webserver-nano/httpd-cgi.c @@ -42,6 +42,7 @@ */ #include +#include #include #include "contiki-net.h" @@ -57,6 +58,9 @@ #if RF230BB #define RADIOSTATS 1 #endif +#if CONTIKI_TARGET_REDBEE_ECONOTAG +#include "adc.h" +#endif #if WEBSERVER_CONF_CGI static struct httpd_cgi_call *calls = NULL; @@ -88,6 +92,9 @@ static const char rtes_name[] HTTPD_STRING_ATTR = "routes"; #if WEBSERVER_CONF_TICTACTOE static const char tictac_name[] HTTPD_STRING_ATTR = "tictac"; #endif +#if WEBSERVER_CONF_AJAX +static const char ajax_name[] HTTPD_STRING_ATTR = "ajaxdata"; +#endif #endif /*Process states for processes cgi*/ @@ -220,12 +227,17 @@ generate_header(void *arg) #define _MSS8 44 static const char httpd_cgi_headerm6[] HTTPD_STRING_ATTR = "|TicTacToe"; numprinted+=httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_headerm6); +#endif +#if WEBSERVER_CONF_AJAX +#define _MSS9 30 + static const char httpd_cgi_headerm7[] HTTPD_STRING_ATTR = "|Ajax"; + numprinted+=httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_headerm7); #endif static const char httpd_cgi_headerme[] HTTPD_STRING_ATTR = ""; numprinted+=httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_headerme); #endif /* WEBSERVER_CONF_MENU */ -#if UIP_RECEIVE_WINDOW < _MSS1+_MSS2+_MSS3_+MSS4_+MSS5_MSS6+_MSS7+_MSS8 +#if UIP_RECEIVE_WINDOW < _MSS1+_MSS2+_MSS3_+MSS4_+MSS5_MSS6+_MSS7+_MSS8+_MSS9 #warning ************************************************************ #warning UIP_RECEIVE_WINDOW not large enough for header cgi output. #warning Web pages will not render properly! @@ -550,6 +562,23 @@ generate_sensor_readings(void *arg) ADCSRA|=1<sout); +/*TODO:pick up time from ? parameter */ + timer_set(&t, 1*CLOCK_SECOND); + iter = 0; + + while(1) { + +#if CONTIKI_TARGET_SKY + SENSORS_ACTIVATE(sht11_sensor); + SENSORS_ACTIVATE(light_sensor); + numprinted = snprintf(buf, sizeof(buf), + "t(%d);h(%d);l1(%d);l2(%d);", + sht11_sensor.value(SHT11_SENSOR_TEMP), + sht11_sensor.value(SHT11_SENSOR_HUMIDITY), + light_sensor.value(LIGHT_SENSOR_PHOTOSYNTHETIC), + light_sensor.value(LIGHT_SENSOR_TOTAL_SOLAR)); + SENSORS_DEACTIVATE(sht11_sensor); + SENSORS_DEACTIVATE(light_sensor); +#elif CONTIKI_TARGET_MB851 + SENSORS_ACTIVATE(acc_sensor); + numprinted = snprintf(buf, sizeof(buf),"t(%d);ax(%d);ay(%d);az(%d);", + temperature_sensor.value(0), + acc_sensor.value(ACC_X_AXIS), + acc_sensor.value(ACC_Y_AXIS), + acc_sensor.value(ACC_Z_AXIS)); + SENSORS_DEACTIVATE(acc_sensor); + +#elif CONTIKI_TARGET_REDBEE_ECONOTAG +{ +//#include "adc.h" + uint8_t c; + adc_reading[8]=0; + adc_init(); + while (adc_reading[8]==0) adc_service(); + adc_disable(); + numprinted = snprintf(buf, sizeof(buf),"b(%u);adc(%u,%u,%u,%u,%u,%u,%u,%u);", + 1200*0xfff/adc_reading[8],adc_reading[0],adc_reading[1],adc_reading[2],adc_reading[3],adc_reading[4],adc_reading[5],adc_reading[6],adc_reading[7]); +} +#elif CONTIKI_TARGET_MINIMAL_NET +static uint16_t c0=0x3ff,c1=0x3ff,c2=0x3ff,c3=0x3ff,c4=0x3ff,c5=0x3ff,c6=0x3ff,c7=0x3ff; + numprinted = snprintf(buf, sizeof(buf), "t(%d);b(%u);v(%u);",273+(rand()&0x3f),3300-iter/10,iter); + numprinted += snprintf(buf+numprinted, sizeof(buf)-numprinted,"adc(%u,%u,%u,%u,%u,%u,%u,%u);",c0,c1,c2,c3,c4,c5,c6,c7); + c0+=(rand()&0xf)-8; + c1+=(rand()&0xf)-8; + c2+=(rand()&0xf)-7; + c3+=(rand()&0x1f)-15; + c4+=(rand()&0x3)-1; + c5+=(rand()&0xf)-8; + c6+=(rand()&0xf)-8; + c7+=(rand()&0xf)-8; +#else + numprinted = snprintf(buf, sizeof(buf), "v(%u);",iter); +#endif + + +#if CONTIKIMAC_CONF_COMPOWER +#include "sys/compower.h" +{ +//sl=compower_idle_activity.transmit/RTIMER_ARCH_SECOND; +//sl=compower_idle_activity.listen/RTIMER_ARCH_SECOND; +} +#endif + +#if RIMESTATS_CONF_ON + +#include "net/rime/rimestats.h" + numprinted += snprintf(buf+numprinted, sizeof(buf)-numprinted,"rime(%lu,%lu,%lu,%lu);", + rimestats.tx,rimestats.rx,rimestats.lltx-rimestats.tx,rimestats.llrx-rimestats.rx); +#endif + +#if ENERGEST_CONF_ON +{ +#if 1 + static unsigned long last_cpu, last_lpm, last_listen, last_transmit; + energest_flush(); + numprinted += snprintf(buf+numprinted, sizeof(buf)-numprinted, + "p(%lu,%lu,%lu,%lu);", + energest_type_time(ENERGEST_TYPE_CPU) - last_cpu, + energest_type_time(ENERGEST_TYPE_LPM) - last_lpm, + energest_type_time(ENERGEST_TYPE_TRANSMIT) - last_transmit, + energest_type_time(ENERGEST_TYPE_LISTEN) - last_listen); + last_cpu = energest_type_time(ENERGEST_TYPE_CPU); + last_lpm = energest_type_time(ENERGEST_TYPE_LPM); + last_transmit = energest_type_time(ENERGEST_TYPE_TRANSMIT); + last_listen = energest_type_time(ENERGEST_TYPE_LISTEN); +#endif +#if 1 + uint16_t cpp,txp,rxp; + energest_flush(); + cpp=((10000UL*energest_type_time(ENERGEST_TYPE_CPU))/RTIMER_ARCH_SECOND)/clock_seconds(); + txp=((10000UL*energest_type_time(ENERGEST_TYPE_TRANSMIT))/RTIMER_ARCH_SECOND)/clock_seconds(); + rxp=((10000UL*energest_type_time(ENERGEST_TYPE_LISTEN))/RTIMER_ARCH_SECOND)/clock_seconds(); + numprinted += snprintf(buf+numprinted, sizeof(buf)-numprinted,"ener(%u,%u,%u);",cpp,txp,rxp); +#endif +} +#endif /* ENERGEST_CONF_ON */ + + PSOCK_SEND_STR(&s->sout, buf); + timer_restart(&t); + PSOCK_WAIT_UNTIL(&s->sout, timer_expired(&t)); + iter++; +} + PSOCK_END(&s->sout); +} +#endif /* WEBSERVER_CONF_AJAX */ /*---------------------------------------------------------------------------*/ void httpd_cgi_add(struct httpd_cgi_call *c) @@ -889,6 +1040,9 @@ HTTPD_CGI_CALL(sensors, sensor_name, sensor_readings); #if WEBSERVER_CONF_TICTACTOE HTTPD_CGI_CALL( tictac, tictac_name, tictactoe ); #endif +#if WEBSERVER_CONF_AJAX +HTTPD_CGI_CALL( ajax, ajax_name, ajax_call ); +#endif void httpd_cgi_init(void) @@ -920,6 +1074,9 @@ httpd_cgi_init(void) #if WEBSERVER_CONF_TICTACTOE httpd_cgi_add( &tictac); #endif +#if WEBSERVER_CONF_AJAX + httpd_cgi_add( &ajax); +#endif } #endif /* WEBSERVER_CONF_CGI */ diff --git a/apps/webserver-nano/httpd-fs/makefsdata.ignore/ajax.js b/apps/webserver-nano/httpd-fs/makefsdata.ignore/ajax.js new file mode 100644 index 000000000..4abaf06be --- /dev/null +++ b/apps/webserver-nano/httpd-fs/makefsdata.ignore/ajax.js @@ -0,0 +1,38 @@ + + \ No newline at end of file diff --git a/apps/webserver-nano/httpd-fs/makefsdata.ignore/ajax.shtml b/apps/webserver-nano/httpd-fs/makefsdata.ignore/ajax.shtml new file mode 100644 index 000000000..08dc82b26 --- /dev/null +++ b/apps/webserver-nano/httpd-fs/makefsdata.ignore/ajax.shtml @@ -0,0 +1,11 @@ +%! header +%!: /ajax.js +
+
+
+

Sensors

+
+
+
+
+%! file-stats . diff --git a/apps/webserver-nano/httpd-fs/makefsdata.ignore/ajaxdata.shtml b/apps/webserver-nano/httpd-fs/makefsdata.ignore/ajaxdata.shtml new file mode 100644 index 000000000..010460220 --- /dev/null +++ b/apps/webserver-nano/httpd-fs/makefsdata.ignore/ajaxdata.shtml @@ -0,0 +1 @@ +%! ajaxdata diff --git a/apps/webserver-nano/httpd.h b/apps/webserver-nano/httpd.h index 9f093e305..5abfa0977 100644 --- a/apps/webserver-nano/httpd.h +++ b/apps/webserver-nano/httpd.h @@ -81,6 +81,7 @@ #define WEBSERVER_CONF_ROUTES 1 #define WEBSERVER_CONF_SENSORS 0 #define WEBSERVER_CONF_TICTACTOE 0 //Needs passquery of at least 10 chars +#define WEBSERVER_CONF_AJAX 0 //#define WEBSERVER_CONF_PASSQUERY 10 #if WEBSERVER_CONF_PASSQUERY extern char httpd_query[WEBSERVER_CONF_PASSQUERY]; @@ -126,6 +127,7 @@ extern char httpd_query[WEBSERVER_CONF_PASSQUERY]; #define WEBSERVER_CONF_ROUTES 1 #define WEBSERVER_CONF_SENSORS 1 #define WEBSERVER_CONF_TICTACTOE 1 //Needs passquery of at least 10 chars +#define WEBSERVER_CONF_AJAX 1 #define WEBSERVER_CONF_PASSQUERY 10 #if WEBSERVER_CONF_PASSQUERY extern char httpd_query[WEBSERVER_CONF_PASSQUERY];