Example project for the yeswecancoap library

Dependencies:   LM75B RGB-fun mbed yeswecancoap

Fork of UDPSocket_HelloWorld by mbed official

Files at this revision

API Documentation at this revision

Comitter:
sillevl
Date:
Wed Oct 21 17:16:27 2015 +0000
Parent:
10:1992d8ec32d2
Commit message:
Yes We Can Coap example

Changed in this revision

EthernetInterface.lib Show diff for this revision Revisions of this file
LM75B.lib Show annotated file Show diff for this revision Revisions of this file
RGB-fun.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-rtos.lib Show diff for this revision Revisions of this file
yeswecancoap.lib Show annotated file Show diff for this revision Revisions of this file
--- a/EthernetInterface.lib	Wed May 14 15:32:24 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://mbed.org/users/mbed_official/code/EthernetInterface/#097a9996f8d5
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LM75B.lib	Wed Oct 21 17:16:27 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/neilt6/code/LM75B/#7ac462ba84ac
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/RGB-fun.lib	Wed Oct 21 17:16:27 2015 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/sillevl/code/RGB-fun/#edc6e64bfc65
--- a/main.cpp	Wed May 14 15:32:24 2014 +0000
+++ b/main.cpp	Wed Oct 21 17:16:27 2015 +0000
@@ -1,28 +1,56 @@
 #include "mbed.h"
-#include "EthernetInterface.h"
+#include "coap.h"
+#include "LM75B.h"
+#include "RGB.h"
+#include <string> 
+
+LM75B sensor(p28,p27);
+RGB led(p23,p24,p25);
  
+void get_hello(Request* req, Response* res)
+{
+     res->setContent("Hello world\r\n", 13);
+}
+
+void get_temp(Request* req, Response* res)
+{
+    if (sensor.open()) {
+        char temp[20];
+        int count = sprintf(temp, "%.3f", (float)sensor);
+        res->setContent(temp, count);
+    } else {
+        res->setCode(SERVICE_UNAVAILABLE);
+    }
+}
+
+void get_led(Request* req, Response* res)
+{
+    char data[20];
+    Color* color = led.getColor();
+    int count = sprintf(data, "#%06d", color->getHex());
+    res->setContent(data, count);
+}
+
+void post_led(Request* req, Response* res)
+{
+    Color color((int) std::strtol(req->getContent(), 0, 16));
+    led.setColor(&color);
+    res->setCode(CHANGED);
+}
+
+            
 int main() {
-    EthernetInterface eth;
-    eth.init(); //Use DHCP
-    eth.connect();
-    
-    UDPSocket sock;
-    sock.init();
-    
-    Endpoint nist;
-    nist.set_address("utcnist.colorado.edu", 37);
+    Server server;
     
-    char out_buffer[] = "plop"; // Does not matter
-    sock.sendTo(nist, out_buffer, sizeof(out_buffer));
-    
-    char in_buffer[4];
-    int n = sock.receiveFrom(nist, in_buffer, sizeof(in_buffer));
+    server.enableBroadcast();
     
-    unsigned int timeRes = ntohl( *((unsigned int*)in_buffer));
-    printf("Received %d bytes from server %s on port %d: %u seconds since 1/01/1900 00:00 GMT\n", n, nist.get_address(), nist.get_port(), timeRes);
+    server.add("/hello",   &get_hello, GET);
+    server.add("/temp", &get_temp, GET);
+    server.add("/led", &get_led, GET);
+    server.add("/led", &post_led, POST);
     
-    sock.close();
-    
-    eth.disconnect();
-    while(1) {}
+    while(1)
+    {
+        server.waitForRequest();  
+    };
 }
--- a/mbed-rtos.lib	Wed May 14 15:32:24 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://mbed.org/users/mbed_official/code/mbed-rtos/#5dfe422a963d
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/yeswecancoap.lib	Wed Oct 21 17:16:27 2015 +0000
@@ -0,0 +1,1 @@
+https://developer.mbed.org/users/sillevl/code/yeswecancoap/#22849ab35dc2