code for gateway node connecting by wifi with backend and by radio with sensor nodes

Dependencies:   mbed coapRadio

main.cpp

Committer:
Ka_myk
Date:
2019-01-24
Revision:
0:544925185af9
Child:
1:605fa4405e4f

File content as of revision 0:544925185af9:

#include "mbed.h"
#include "coapServer.h"
#include "radioWrapper.h"
#include <string>
#include "dbg.h"
#include "msg.h"
#include "itoa.h"

int channel = 4;

unsigned long long txAdress = 0xABCDEF08;
unsigned long long rxAdress[] = {0xABCDEF11, 0xABCDEF12, 0xABCDEF13};


Serial pc(USBTX, USBRX); // tx, rx
InterruptIn button(USER_BUTTON);
bool start = false;

void send_packet() {
    start = true;
}

int main() {
    pc.baud(115200);
    std::string uris[] = {"/dist", "/temp", "/hum", "/conf/dist", "/conf/temp", "/conf/hum"};
    CoapServer coapServer = CoapServer(32, 1000, uris, 6, channel, txAdress, rxAdress);
    button.rise(&send_packet);
    uint8_t buffer[32];
    CoapServer::METHOD method;
    int uriNum = -1;
    pc.printf("Server initialized \r\n");
    int pipe = -1;
    while (1) {
        while (start) {
            int ret = coapServer.listen(buffer, 32, &method, &uriNum, &pipe);
            pc.printf("Code    : %d \r\n", ret);
            if (ret >= 0) {
                pc.printf("Recieved: %s \r\n", buffer);
                int resLen = 0;
                if (method == CoapServer::POST) {
                    resLen = coapServer.respond(uriNum, NULL, 0, CoapPDU::COAP_CREATED, pipe);
                    int data = 0;
                    uint8_t sensor = 1;
                    Msg::deconstruct_data_msg(&sensor, &data, buffer, ret);
                    switch (uriNum % 3) {
                        case 0:
                        DBG("dist %d", data);
                            break;
                        case 1:
                        DBG("TEMP %d", data);
                            break;
                        case 2:
                        DBG("HUM %d", data);
                            break;
                    }
                } else if (method == CoapServer::GET) {
                    uint8_t response[32];
                    switch (uriNum % 3) {
                        case 0:
                            itoa(10, (char*)response, 10);
                            break;
                        case 1:
                            itoa(20, (char*)response, 10);
                            break;
                        case 2:
                            itoa(30, (char*)response, 10);
                            break;
                    }

                    resLen = coapServer.respond(uriNum, response, std::strlen((char*)response), CoapPDU::COAP_CONTENT, pipe);
                }
                DBG("RESPONDED WITH %d", resLen);
            }
        }
    }
}