IOT_GRZYBKI
/
server2
code for gateway node connecting by wifi with backend and by radio with sensor nodes
main.cpp@1:605fa4405e4f, 2019-01-25 (annotated)
- Committer:
- thewiztory
- Date:
- Fri Jan 25 02:10:25 2019 +0000
- Revision:
- 1:605fa4405e4f
- Parent:
- 0:544925185af9
- Child:
- 2:73331442c00e
adam
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Ka_myk | 0:544925185af9 | 1 | #include "mbed.h" |
Ka_myk | 0:544925185af9 | 2 | #include "coapServer.h" |
Ka_myk | 0:544925185af9 | 3 | #include "radioWrapper.h" |
Ka_myk | 0:544925185af9 | 4 | #include <string> |
Ka_myk | 0:544925185af9 | 5 | #include "dbg.h" |
Ka_myk | 0:544925185af9 | 6 | #include "msg.h" |
Ka_myk | 0:544925185af9 | 7 | #include "itoa.h" |
Ka_myk | 0:544925185af9 | 8 | |
Ka_myk | 0:544925185af9 | 9 | int channel = 4; |
Ka_myk | 0:544925185af9 | 10 | |
Ka_myk | 0:544925185af9 | 11 | unsigned long long txAdress = 0xABCDEF08; |
Ka_myk | 0:544925185af9 | 12 | unsigned long long rxAdress[] = {0xABCDEF11, 0xABCDEF12, 0xABCDEF13}; |
Ka_myk | 0:544925185af9 | 13 | |
Ka_myk | 0:544925185af9 | 14 | |
Ka_myk | 0:544925185af9 | 15 | Serial pc(USBTX, USBRX); // tx, rx |
Ka_myk | 0:544925185af9 | 16 | InterruptIn button(USER_BUTTON); |
Ka_myk | 0:544925185af9 | 17 | bool start = false; |
Ka_myk | 0:544925185af9 | 18 | |
Ka_myk | 0:544925185af9 | 19 | void send_packet() { |
Ka_myk | 0:544925185af9 | 20 | start = true; |
Ka_myk | 0:544925185af9 | 21 | } |
Ka_myk | 0:544925185af9 | 22 | |
Ka_myk | 0:544925185af9 | 23 | int main() { |
Ka_myk | 0:544925185af9 | 24 | pc.baud(115200); |
thewiztory | 1:605fa4405e4f | 25 | std::string uris[] = {"/dist", "/temp", "/hum", "/moist", "/conf/dist", "/conf/temp", "/conf/hum", "/conf/moist"}; |
thewiztory | 1:605fa4405e4f | 26 | CoapServer coapServer = CoapServer(32, 1000, uris, 8, channel, txAdress, rxAdress); |
Ka_myk | 0:544925185af9 | 27 | button.rise(&send_packet); |
Ka_myk | 0:544925185af9 | 28 | uint8_t buffer[32]; |
Ka_myk | 0:544925185af9 | 29 | CoapServer::METHOD method; |
Ka_myk | 0:544925185af9 | 30 | int uriNum = -1; |
Ka_myk | 0:544925185af9 | 31 | pc.printf("Server initialized \r\n"); |
Ka_myk | 0:544925185af9 | 32 | int pipe = -1; |
Ka_myk | 0:544925185af9 | 33 | while (1) { |
Ka_myk | 0:544925185af9 | 34 | while (start) { |
Ka_myk | 0:544925185af9 | 35 | int ret = coapServer.listen(buffer, 32, &method, &uriNum, &pipe); |
Ka_myk | 0:544925185af9 | 36 | pc.printf("Code : %d \r\n", ret); |
Ka_myk | 0:544925185af9 | 37 | if (ret >= 0) { |
Ka_myk | 0:544925185af9 | 38 | pc.printf("Recieved: %s \r\n", buffer); |
thewiztory | 1:605fa4405e4f | 39 | pc.printf("*****Pipe: %d", pipe); |
Ka_myk | 0:544925185af9 | 40 | int resLen = 0; |
Ka_myk | 0:544925185af9 | 41 | if (method == CoapServer::POST) { |
Ka_myk | 0:544925185af9 | 42 | resLen = coapServer.respond(uriNum, NULL, 0, CoapPDU::COAP_CREATED, pipe); |
Ka_myk | 0:544925185af9 | 43 | int data = 0; |
Ka_myk | 0:544925185af9 | 44 | uint8_t sensor = 1; |
Ka_myk | 0:544925185af9 | 45 | Msg::deconstruct_data_msg(&sensor, &data, buffer, ret); |
thewiztory | 1:605fa4405e4f | 46 | switch (uriNum % 4) { |
Ka_myk | 0:544925185af9 | 47 | case 0: |
Ka_myk | 0:544925185af9 | 48 | DBG("dist %d", data); |
Ka_myk | 0:544925185af9 | 49 | break; |
Ka_myk | 0:544925185af9 | 50 | case 1: |
Ka_myk | 0:544925185af9 | 51 | DBG("TEMP %d", data); |
Ka_myk | 0:544925185af9 | 52 | break; |
Ka_myk | 0:544925185af9 | 53 | case 2: |
Ka_myk | 0:544925185af9 | 54 | DBG("HUM %d", data); |
Ka_myk | 0:544925185af9 | 55 | break; |
thewiztory | 1:605fa4405e4f | 56 | case 3: |
thewiztory | 1:605fa4405e4f | 57 | DBG("MOIST %d", data); |
thewiztory | 1:605fa4405e4f | 58 | break; |
Ka_myk | 0:544925185af9 | 59 | } |
Ka_myk | 0:544925185af9 | 60 | } else if (method == CoapServer::GET) { |
thewiztory | 1:605fa4405e4f | 61 | uint8_t response[10]; |
thewiztory | 1:605fa4405e4f | 62 | switch (uriNum % 4) { |
Ka_myk | 0:544925185af9 | 63 | case 0: |
thewiztory | 1:605fa4405e4f | 64 | Msg::construct_data_msg(0, 5, response, 10); |
thewiztory | 1:605fa4405e4f | 65 | //itoa(10, (char*)response, 10); |
Ka_myk | 0:544925185af9 | 66 | break; |
Ka_myk | 0:544925185af9 | 67 | case 1: |
thewiztory | 1:605fa4405e4f | 68 | Msg::construct_data_msg(0, 5, response, 10); |
Ka_myk | 0:544925185af9 | 69 | break; |
Ka_myk | 0:544925185af9 | 70 | case 2: |
thewiztory | 1:605fa4405e4f | 71 | Msg::construct_data_msg(0, 13, response, 10); |
thewiztory | 1:605fa4405e4f | 72 | break; |
thewiztory | 1:605fa4405e4f | 73 | case 3: |
thewiztory | 1:605fa4405e4f | 74 | Msg::construct_data_msg(0, 8, response, 10); |
Ka_myk | 0:544925185af9 | 75 | break; |
Ka_myk | 0:544925185af9 | 76 | } |
thewiztory | 1:605fa4405e4f | 77 | pc.printf("*****response:\r\n"); |
thewiztory | 1:605fa4405e4f | 78 | for (int i = 0; i < 10; i++) { |
thewiztory | 1:605fa4405e4f | 79 | pc.printf("%d ", response[i]); |
thewiztory | 1:605fa4405e4f | 80 | } |
thewiztory | 1:605fa4405e4f | 81 | pc.printf("\r\n"); |
thewiztory | 1:605fa4405e4f | 82 | resLen = coapServer.respond(uriNum, response, 10, CoapPDU::COAP_CONTENT, pipe); |
Ka_myk | 0:544925185af9 | 83 | } |
Ka_myk | 0:544925185af9 | 84 | DBG("RESPONDED WITH %d", resLen); |
Ka_myk | 0:544925185af9 | 85 | } |
Ka_myk | 0:544925185af9 | 86 | } |
Ka_myk | 0:544925185af9 | 87 | } |
Ka_myk | 0:544925185af9 | 88 | } |