IoT - Kubus / Mbed 2 deprecated Kubus

Dependencies:   mbed nRF24L01P

Files at this revision

API Documentation at this revision

Comitter:
Bartosz Stebel
Date:
Thu Jan 19 13:19:55 2017 +0100
Parent:
58:72a8e61929c6
Child:
61:9e4e08aae8d0
Commit message:
finally fix wifi

Changed in this revision

master.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/master.cpp	Thu Jan 19 11:43:12 2017 +0100
+++ b/master.cpp	Thu Jan 19 13:19:55 2017 +0100
@@ -28,16 +28,21 @@
     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) );
 
-    frm = 1;
+    frm = 0;
     while (1) {
         if(radio.readable(NRF24L01P_PIPE_P1)){
+            frm = 1;
             int rx_bytes = radio.read(NRF24L01P_PIPE_P1, rxData, sizeof(rxData));
-            CoapPDU pdu;
+            uint8_t pdu_buffer[100] = {0};
+            CoapPDU pdu(pdu_buffer+1, 99, 0);
             pdu.setVersion(1);
             pdu.setType(CoapPDU::COAP_NON_CONFIRMABLE);
             pdu.setCode(CoapPDU::COAP_POST);
-            pdu.setToken((uint8_t*)"\3\2\1\1",4);
-            pdu.setMessageID(0x0005);
+            //pdu.setToken((uint8_t*)"\3\2\1\1",4);
+            pdu.setToken(NULL,0);   // *maybe* can be empty, see rfc7252 section 5.3.1
+            static uint16_t msgid = 0;  //TODO endian?
+            pdu.setMessageID(msgid++);  //will overflow, that is okay (rfc again)
+
             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());
@@ -51,28 +56,40 @@
             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];
+            char uribuf[64] = {0};
+            char pbuf[32] = {0};
+            int psize = 0;
             switch(d.type) {
                 case PIR1:
                     strcpy(uribuf, "pir1");
+                    psize = sprintf(pbuf, "%d", d.value.i);
                     break;
                 case PIR2:
                     strcpy(uribuf, "pir2");
+                    psize = sprintf(pbuf, "%d", d.value.i);
                     break;
                 case DISTANCE:
                     strcpy(uribuf, "distance");
+                    psize = sprintf(pbuf, "%f", d.value.f);
                     break;
                 case SOUND:
                     strcpy(uribuf, "sound");
+                    psize = sprintf(pbuf, "%f", d.value.f);
                     break;
             }
             pdu.setURI(uribuf,strlen(uribuf));
 
-            char pbuf[32];
-            int psize = sprintf(pbuf, "%ld", d.value.i);
             pdu.setPayload((uint8_t*)pbuf, psize);
             frm = 0;
-            wifi.write(pdu.getPDUPointer(), pdu.getPDULength(), 0, 0);
+            //wait(0.1);
+            //wifi.write((uint8_t*)test, sizeof(test), 0, 0);
+            //uint8_t hdr[3] = {0x7e, 0, pdu.getPDULength()};
+            //wifi.write(hdr, 3, 0, 0);
+            wait(0.01);
+            wifi.write(pdu_buffer, pdu.getPDULength()+1, 0, 0);
+            //wait(0.1);
+            //uint8_t end = 0xce;
+            //wifi.write(&end, 1, 0, 0);
             wait(0.1);
             frm = 1;
             //wifi.printf("\n");