Simple IoT demo over Ethernet using the legacy network libraries

Dependencies:   EthernetNetIf mbed DNSResolver

Files at this revision

API Documentation at this revision

Comitter:
emilmont
Date:
Thu Feb 23 11:12:00 2012 +0000
Commit message:

Changed in this revision

DNSResolver.lib Show annotated file Show diff for this revision Revisions of this file
EthernetNetIf.lib Show annotated file Show diff for this revision Revisions of this file
Websocket.lib Show annotated file Show diff for this revision Revisions of this file
Wifly.lib Show annotated file Show diff for this revision Revisions of this file
main.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/DNSResolver.lib	Thu Feb 23 11:12:00 2012 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/hlipka/code/DNSResolver/#097d4993dd1e
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/EthernetNetIf.lib	Thu Feb 23 11:12:00 2012 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/donatien/code/EthernetNetIf/#bc7df6da7589
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Websocket.lib	Thu Feb 23 11:12:00 2012 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/samux/code/Websocket/#f4aac491ea26
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Wifly.lib	Thu Feb 23 11:12:00 2012 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/samux/code/Wifly/#56f3c4da1ea8
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Feb 23 11:12:00 2012 +0000
@@ -0,0 +1,86 @@
+#include "mbed.h"
+#include "Websocket.h"
+#define MEAN_VALUE_MIC 32500
+
+Serial pc(USBTX, USBRX);
+Timer tmr;
+
+Websocket ws("ws://sockets.mbed.org/ws/sensors/wo");
+
+AnalogIn light_pin(p19);
+AnalogIn mic_pin(p18);
+
+DigitalOut led1(LED1);
+DigitalOut led2(LED2);
+DigitalOut led3(LED3);
+DigitalOut led4(LED4);
+void notify(int a, int b, int c) {
+    led1 = a;
+    led2 = b;
+    led3 = c;
+}
+
+int main() {
+    notify(1, 0, 0);
+    
+    //unsigned short light;
+    char json_str[100];
+    int press;
+    double temp;
+    int light;
+    unsigned short mic_mean;
+    unsigned short mic = 0;
+
+    char recv[128];
+    while (1) {
+
+        while (!ws.connect()) {
+            pc.printf("cannot connect websocket, retrying in one second\r\n");
+            wait(1.0);
+        }
+        notify(0, 0, 1);
+
+        pc.printf("Connected\r\n");
+
+        tmr.start();
+        while (1) {
+
+            if (tmr.read() > 0.5) {
+
+                //pressure
+                //press = scp1000.readPressure();
+                press = 10;
+
+                //temperature
+                //temp = scp1000.readTemperature();
+                temp = 10.1;
+
+                //light
+                light = light_pin.read_u16()/480;
+
+                //microphone
+                mic_mean = 0;
+                for (double i = 0; i < 200; i++) {
+                    mic = mic_pin.read_u16();
+                    mic = (mic > MEAN_VALUE_MIC) ? mic : MEAN_VALUE_MIC + (MEAN_VALUE_MIC - mic);
+                    mic_mean = (mic_mean*i + mic)/(i+1);
+                }
+                pc.putc('!');
+                
+                sprintf(json_str, "{\"id\":\"eth_env\",\"p\":\"%d\",\"t\":\"%d\",\"l\":\"%d\",\"m\":\"%d\"}", (int)press, (int)temp, (int)(140 - light), mic_mean);
+                ws.send(json_str);
+                
+                led4 = !led4;
+                
+                if (ws.read(recv)) {
+                    pc.printf(".");
+                    pc.printf("recv: %s\r\n", recv);
+                }
+
+                tmr.start();
+            }
+
+            Net::poll();
+        }
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Thu Feb 23 11:12:00 2012 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/63bcd7ba4912