Example project for the yeswecancoap library

Dependencies:   LM75B RGB-fun mbed yeswecancoap

Fork of UDPSocket_HelloWorld by mbed official

Committer:
sillevl
Date:
Wed Oct 21 17:16:27 2015 +0000
Revision:
11:3f372e554d90
Parent:
7:dedf5dde9798
Yes We Can Coap example

Who changed what in which revision?

UserRevisionLine numberNew contents of line
donatien 1:8e1d4987eb90 1 #include "mbed.h"
sillevl 11:3f372e554d90 2 #include "coap.h"
sillevl 11:3f372e554d90 3 #include "LM75B.h"
sillevl 11:3f372e554d90 4 #include "RGB.h"
sillevl 11:3f372e554d90 5 #include <string>
sillevl 11:3f372e554d90 6
sillevl 11:3f372e554d90 7 LM75B sensor(p28,p27);
sillevl 11:3f372e554d90 8 RGB led(p23,p24,p25);
emilmont 6:25aad2d88749 9
sillevl 11:3f372e554d90 10 void get_hello(Request* req, Response* res)
sillevl 11:3f372e554d90 11 {
sillevl 11:3f372e554d90 12 res->setContent("Hello world\r\n", 13);
sillevl 11:3f372e554d90 13 }
sillevl 11:3f372e554d90 14
sillevl 11:3f372e554d90 15 void get_temp(Request* req, Response* res)
sillevl 11:3f372e554d90 16 {
sillevl 11:3f372e554d90 17 if (sensor.open()) {
sillevl 11:3f372e554d90 18 char temp[20];
sillevl 11:3f372e554d90 19 int count = sprintf(temp, "%.3f", (float)sensor);
sillevl 11:3f372e554d90 20 res->setContent(temp, count);
sillevl 11:3f372e554d90 21 } else {
sillevl 11:3f372e554d90 22 res->setCode(SERVICE_UNAVAILABLE);
sillevl 11:3f372e554d90 23 }
sillevl 11:3f372e554d90 24 }
sillevl 11:3f372e554d90 25
sillevl 11:3f372e554d90 26 void get_led(Request* req, Response* res)
sillevl 11:3f372e554d90 27 {
sillevl 11:3f372e554d90 28 char data[20];
sillevl 11:3f372e554d90 29 Color* color = led.getColor();
sillevl 11:3f372e554d90 30 int count = sprintf(data, "#%06d", color->getHex());
sillevl 11:3f372e554d90 31 res->setContent(data, count);
sillevl 11:3f372e554d90 32 }
sillevl 11:3f372e554d90 33
sillevl 11:3f372e554d90 34 void post_led(Request* req, Response* res)
sillevl 11:3f372e554d90 35 {
sillevl 11:3f372e554d90 36 Color color((int) std::strtol(req->getContent(), 0, 16));
sillevl 11:3f372e554d90 37 led.setColor(&color);
sillevl 11:3f372e554d90 38 res->setCode(CHANGED);
sillevl 11:3f372e554d90 39 }
sillevl 11:3f372e554d90 40
sillevl 11:3f372e554d90 41
emilmont 6:25aad2d88749 42 int main() {
sillevl 11:3f372e554d90 43 Server server;
emilmont 7:dedf5dde9798 44
sillevl 11:3f372e554d90 45 server.enableBroadcast();
donatien 1:8e1d4987eb90 46
sillevl 11:3f372e554d90 47 server.add("/hello", &get_hello, GET);
sillevl 11:3f372e554d90 48 server.add("/temp", &get_temp, GET);
sillevl 11:3f372e554d90 49 server.add("/led", &get_led, GET);
sillevl 11:3f372e554d90 50 server.add("/led", &post_led, POST);
emilmont 6:25aad2d88749 51
sillevl 11:3f372e554d90 52 while(1)
sillevl 11:3f372e554d90 53 {
sillevl 11:3f372e554d90 54 server.waitForRequest();
sillevl 11:3f372e554d90 55 };
donatien 1:8e1d4987eb90 56 }