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

Files at this revision

API Documentation at this revision

Comitter:
simon
Date:
Mon Jan 10 18:10:05 2011 +0000
Commit message:
First simple example

Changed in this revision

EthernetNetIf.lib Show annotated file Show diff for this revision Revisions of this file
HTTPClient.lib Show annotated file Show diff for this revision Revisions of this file
Servo.lib Show annotated file Show diff for this revision Revisions of this file
WebTrafficMonitor.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /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
--- /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
--- /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
--- /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;            
+   }
+}
--- /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