IoT - Kubus / Mbed 2 deprecated Kubus

Dependencies:   mbed nRF24L01P

Revision:
52:917b3f31cad3
Parent:
39:381993764b40
Child:
53:b2c949470f4e
diff -r 381993764b40 -r 917b3f31cad3 master.cpp
--- a/master.cpp	Tue Jan 17 12:32:12 2017 +0100
+++ b/master.cpp	Tue Jan 17 12:47:55 2017 +0100
@@ -27,17 +27,48 @@
     pc.printf( "nRF24L01+ RX0 Address   : 0x%010llX\r\n", radio.getRxAddress(NRF24L01P_PIPE_P0) );
     pc.printf( "nRF24L01+ RX1 Address   : 0x%010llX\r\n", radio.getRxAddress(NRF24L01P_PIPE_P1) );
 
-    CoapPDU *pdu = new CoapPDU();
-    pdu->setType(CoapPDU::COAP_NON_CONFIRMABLE);
-    pdu->setCode(CoapPDU::COAP_POST);
-    pdu->setURI((char*)"pir1",4);
 
     while (1) {
         if(radio.readable(NRF24L01P_PIPE_P1)){
             int rx_bytes = radio.read(NRF24L01P_PIPE_P1, rxData, sizeof(rxData));
+            CoapPDU pdu;
+            pdu.setType(CoapPDU::COAP_NON_CONFIRMABLE);
+            pdu.setCode(CoapPDU::COAP_POST);
             std::string output = str_hex(rxData, rx_bytes); 
             wifi.printf("received: %s\r\n", output.c_str());
             pc.printf("received: %s\r\n", output.c_str());
+            Data d;
+            d.deserialize(std::string(rxData, 32));
+            std::stringstream ss;
+            
+            for (int i = 0; i < rx_bytes; ++i) {
+                ss << std::hex << rxData[i] / 16 << rxData[i] % 16; // ugly!
+            }
+            pc.printf("received: %s|id %d \r\n", ss.str().c_str(), d.type);
+            //wifi.printf("received: %s, pkt:", ss.str().c_str());
+
+            char uribuf[64];
+            switch(d.type) {
+                case PIR1:
+                    strcpy(uribuf, "pir1");
+                    break;
+                case PIR2:
+                    strcpy(uribuf, "pir2");
+                    break;
+                case DISTANCE:
+                    strcpy(uribuf, "distance");
+                    break;
+                case SOUND:
+                    strcpy(uribuf, "sound");
+                    break;
+            }
+            pdu.setURI(uribuf,strlen(uribuf));
+
+            char pbuf[32];
+            int psize = sprintf(pbuf, "%ld", d.value.i);
+            pdu.setPayload((uint8_t*)pbuf, psize);
+            wifi.write(pdu.getPDUPointer(), pdu.getPDULength(), 0, 0);
+            //wifi.printf("\n");
         }
     }
 }