Example project for the yeswecancoap library

Dependencies:   LM75B RGB-fun mbed yeswecancoap

Fork of UDPSocket_HelloWorld by mbed official

Revision:
11:3f372e554d90
Parent:
7:dedf5dde9798
diff -r 1992d8ec32d2 -r 3f372e554d90 main.cpp
--- 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();  
+    };
 }