DONGHEON LEE
/
hw3_21300515
hw3_21300515
Fork of coap-example by
source/main.cpp@1:4d65f05c7d2f, 2018-01-02 (annotated)
- Committer:
- DongHeon
- Date:
- Tue Jan 02 12:36:15 2018 +0000
- Revision:
- 1:4d65f05c7d2f
- Parent:
- 0:0681e205d0e9
a
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
DongHeon | 1:4d65f05c7d2f | 1 | #include <string.h> |
Jan Jongboom |
0:0681e205d0e9 | 2 | #include "mbed.h" |
Jan Jongboom |
0:0681e205d0e9 | 3 | #include "easy-connect.h" |
DongHeon | 1:4d65f05c7d2f | 4 | #include "TCPSocket.h" |
DongHeon | 1:4d65f05c7d2f | 5 | #include "DHT.h" |
Jan Jongboom |
0:0681e205d0e9 | 6 | |
DongHeon | 1:4d65f05c7d2f | 7 | DHT dht22(A0,DHT22); |
Jan Jongboom |
0:0681e205d0e9 | 8 | |
DongHeon | 1:4d65f05c7d2f | 9 | #define SERVER_IP "192.168.0.8" |
DongHeon | 1:4d65f05c7d2f | 10 | #define SERVER_PORT 50000 |
Jan Jongboom |
0:0681e205d0e9 | 11 | |
DongHeon | 1:4d65f05c7d2f | 12 | Serial pc(USBTX, USBRX); |
Jan Jongboom |
0:0681e205d0e9 | 13 | |
Jan Jongboom |
0:0681e205d0e9 | 14 | |
Jan Jongboom |
0:0681e205d0e9 | 15 | |
DongHeon | 1:4d65f05c7d2f | 16 | int main(){ |
DongHeon | 1:4d65f05c7d2f | 17 | int error = 0; |
DongHeon | 1:4d65f05c7d2f | 18 | pc.baud(115200); |
DongHeon | 1:4d65f05c7d2f | 19 | pc.printf("\r\nConnecting...\r\n"); |
DongHeon | 1:4d65f05c7d2f | 20 | NetworkInterface * network = easy_connect(true); |
DongHeon | 1:4d65f05c7d2f | 21 | |
DongHeon | 1:4d65f05c7d2f | 22 | // couldn't find network. |
DongHeon | 1:4d65f05c7d2f | 23 | if(!network) |
DongHeon | 1:4d65f05c7d2f | 24 | { |
DongHeon | 1:4d65f05c7d2f | 25 | pc.printf("Error: Cannot connect to the network\r\n"); |
DongHeon | 1:4d65f05c7d2f | 26 | |
Jan Jongboom |
0:0681e205d0e9 | 27 | } |
DongHeon | 1:4d65f05c7d2f | 28 | //success |
DongHeon | 1:4d65f05c7d2f | 29 | pc.printf("Success\r\n\r\n"); |
DongHeon | 1:4d65f05c7d2f | 30 | pc.printf("MAC: %s\r\n", network->get_mac_address()); |
DongHeon | 1:4d65f05c7d2f | 31 | pc.printf("IP: %s\r\n", network->get_ip_address()); |
DongHeon | 1:4d65f05c7d2f | 32 | pc.printf("Netmask: %s\r\n", network->get_netmask()); |
DongHeon | 1:4d65f05c7d2f | 33 | pc.printf("Gateway: %s\r\n", network->get_gateway()); |
DongHeon | 1:4d65f05c7d2f | 34 | pc.printf("RSSI: %d\r\n\r\n", wifi.get_rssi()); |
DongHeon | 1:4d65f05c7d2f | 35 | |
DongHeon | 1:4d65f05c7d2f | 36 | TCPSocket socket; |
Jan Jongboom |
0:0681e205d0e9 | 37 | socket.open(network); |
DongHeon | 1:4d65f05c7d2f | 38 | socket.connect(SERVER_IP, SERVER_PORT); |
Jan Jongboom |
0:0681e205d0e9 | 39 | |
DongHeon | 1:4d65f05c7d2f | 40 | float cel = 0, humid = 0; |
DongHeon | 1:4d65f05c7d2f | 41 | int send_message, receive_message; |
DongHeon | 1:4d65f05c7d2f | 42 | while(1){ |
DongHeon | 1:4d65f05c7d2f | 43 | char rbuffer[64] ={}; |
DongHeon | 1:4d65f05c7d2f | 44 | pc.printf("Sending request to %s : %d ...\r\n", SERVER_IP, SERVER_PORT); |
DongHeon | 1:4d65f05c7d2f | 45 | // artik to nucleo |
DongHeon | 1:4d65f05c7d2f | 46 | receive_message = socket.recv(rbuffer, sizeof rbuffer); |
DongHeon | 1:4d65f05c7d2f | 47 | pc.printf("%s\r\n", rbuffer); |
DongHeon | 1:4d65f05c7d2f | 48 | if(!strcmp(rbuffer,"GET /DHT22\r\n")) |
DongHeon | 1:4d65f05c7d2f | 49 | { |
DongHeon | 1:4d65f05c7d2f | 50 | char sbuffer[64]={}; |
DongHeon | 1:4d65f05c7d2f | 51 | send_message = socket.send("OKAY\r", sizeof("OKAY\r")); |
DongHeon | 1:4d65f05c7d2f | 52 | // |
DongHeon | 1:4d65f05c7d2f | 53 | error = dht22.readData(); |
DongHeon | 1:4d65f05c7d2f | 54 | if (error == 0) { |
DongHeon | 1:4d65f05c7d2f | 55 | cel = dht22.ReadTemperature(CELCIUS); |
DongHeon | 1:4d65f05c7d2f | 56 | humid = dht22.ReadHumidity(); |
DongHeon | 1:4d65f05c7d2f | 57 | } |
DongHeon | 1:4d65f05c7d2f | 58 | else { |
DongHeon | 1:4d65f05c7d2f | 59 | printf("ERROR : %d\r\n", error); |
DongHeon | 1:4d65f05c7d2f | 60 | } |
DongHeon | 1:4d65f05c7d2f | 61 | sprintf(sbuffer, "TEMP = %.2f humid = %.2f!\r", cel, humid); |
DongHeon | 1:4d65f05c7d2f | 62 | send_message = socket.send(sbuffer, sizeof(sbuffer)); |
DongHeon | 1:4d65f05c7d2f | 63 | pc.printf(sbuffer); |
DongHeon | 1:4d65f05c7d2f | 64 | } |
DongHeon | 1:4d65f05c7d2f | 65 | else{ |
DongHeon | 1:4d65f05c7d2f | 66 | pc.printf("Unknown request\r"); |
DongHeon | 1:4d65f05c7d2f | 67 | send_message = socket.send("Unknown request\r", sizeof("Unknown request\r")); |
DongHeon | 1:4d65f05c7d2f | 68 | } |
DongHeon | 1:4d65f05c7d2f | 69 | } |
DongHeon | 1:4d65f05c7d2f | 70 | } |