IOT_GRZYBKI / Mbed 2 deprecated WIFI_BOARD

Dependencies:   mbed cantcoap WIFI_BOARD

Dependents:   WIFI_BOARD

Revision:
0:73e49785c686
Child:
1:1b58077320a3
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Fri Jan 18 15:58:10 2019 +0000
@@ -0,0 +1,63 @@
+#include "mbed.h"
+#include "cantcoap.h"
+
+Serial pc(SERIAL_TX, SERIAL_RX);
+Serial wifi(PA_11, PA_12);
+DigitalOut send(PC_8);
+
+#define PCDBG(...) if(1){ pc.printf(__VA_ARGS__); pc.printf("\r\n");}
+
+CoapPDU* read() {
+    uint8_t buff[1023];
+    int ret = wifi.read(buff, 1023, NULL);
+    CoapPDU *recvPDU = new CoapPDU(buff,ret,1023);
+    if(recvPDU->validate()) {
+        //recvPDU.printHuman();
+        // do your work
+        return recvPDU;
+    }
+    return NULL;
+}
+
+int sendd(uint8_t* buff, int len) {
+    //TODO callback for response
+    int ret = wifi.write(buff, len, NULL);
+    send.write(1);
+    wait(0.2);
+    send.write(0);
+    return ret;
+}
+
+int main() {
+    pc.baud(115200);
+    wifi.baud(115200);
+    CoapPDU* pdu = new CoapPDU();
+    uint16_t messageID = 0;
+    int ret;
+    while(1) {
+        ++messageID;
+        pdu->setType(CoapPDU::COAP_CONFIRMABLE);
+        pdu->setCode(CoapPDU::COAP_GET);
+        pdu->setToken((uint8_t*)"\3\2\1\0",4);
+        pdu->setMessageID(messageID);
+        pdu->setURI((char*)"test",4);
+    
+        PCDBG("Sending packet");
+        // send packet 
+        ret = sendd(pdu->getPDUPointer(), pdu->getPDULength());
+        PCDBG("Packet send");
+        
+        // get response
+        CoapPDU* res = read();
+        PCDBG("Response read");
+        CoapPDU::Code code = res->getCode();
+        uint8_t len = res->getPayloadLength();
+        // getPayloadPointerCopy to get allocated copy of payload
+        uint8_t* payload = res->getPayloadPointer();
+        //char status[10];
+        //if(code == COAP_VALID) status = "OK"; else status = "UNKNOWN";
+        PCDBG("%s, %s", code, payload);
+        delete res;
+        wait(1);
+    }
+}
\ No newline at end of file