Example of a simple website hits/signup traffic monitor to drive a display (e.g. servo or panel meter) and indicator (e.g. bell, solenoid) based on responses from a webserver
Dependencies: EthernetNetIf mbed Servo
Revision 0:3c13cc3b4e30, committed 2011-01-10
- Comitter:
- simon
- Date:
- Mon Jan 10 18:10:05 2011 +0000
- Commit message:
- First simple example
Changed in this revision
diff -r 000000000000 -r 3c13cc3b4e30 EthernetNetIf.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/EthernetNetIf.lib Mon Jan 10 18:10:05 2011 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/donatien/code/EthernetNetIf/#bc7df6da7589
diff -r 000000000000 -r 3c13cc3b4e30 HTTPClient.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HTTPClient.lib Mon Jan 10 18:10:05 2011 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/donatien/code/HTTPClient/#d0be6af2d1db
diff -r 000000000000 -r 3c13cc3b4e30 Servo.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Servo.lib Mon Jan 10 18:10:05 2011 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/simon/code/Servo/#36b69a7ced07
diff -r 000000000000 -r 3c13cc3b4e30 WebTrafficMonitor.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WebTrafficMonitor.cpp Mon Jan 10 18:10:05 2011 +0000 @@ -0,0 +1,70 @@ +// Example of a simple website hits/signup traffic monitor to drive +// a display (e.g. servo or panel meter) and indicator (e.g. bell, solenoid) +// based on responses from a webserver +// http://mbed.org + +#include "mbed.h" +#include "EthernetNetIf.h" +#include "HTTPClient.h" +#include "Servo.h" + +#define SIGNUPS_URL "http://url-returning-signups-int" // number of signups since last checked +#define HITS_URL "http://url-returning-hits-int" // number of hits/sec since last checked +#define HITS_MAX 1000 // scale max hits/sec + +EthernetNetIf eth; +HTTPClient http; + +DigitalOut bell(p12); +Servo meter(p21); + +// ding a bell (fire a digital out driving a solenoid) +void ding() { + bell = 1; + wait(0.25); + bell = 0; +} + +// fetch a integer from a url +int get_result(char *url) { + HTTPText txt; + HTTPResult r = http.get(url, &txt); + + if(r != HTTP_OK) { + printf("Error [%d] fetching url [%s]\n", r, url); + return -1; + } + + printf("Response [%s] from url [%s]\n", txt.gets(), url); + int v = 0; + if(sscanf(txt.gets(), "%d", &v) != 1) { + printf("Could not convert value to int\n"); + } + + return v; +} + +int main() { + printf("Setup the network...\n"); + EthernetErr ethErr = eth.setup(); + if (ethErr) { + error("Error %d in setup\n", ethErr); + } + printf("OK!\n"); + + while (1) { + int hits = get_result(HITS_URL); + int signups = get_result(SIGNUPS_URL); + + printf("hits = %d, signups = %d\n", hits, signups); + + // ding for every signup since last request + for(int i=0; i<signups; i++) { + ding(); + wait(0.5); + } + + // set the servo based on hits/sec + meter = (float)hits / (float)HITS_MAX; + } +}
diff -r 000000000000 -r 3c13cc3b4e30 mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Mon Jan 10 18:10:05 2011 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/9114680c05da