
CoAP example application for mbed OS 5. I refer F411-mbed-os-example-simpleHTTPClient code
Fork of coap-example by
source/main.cpp@1:cca24408264b, 2018-01-02 (annotated)
- Committer:
- fbdp1202
- Date:
- Tue Jan 02 07:54:50 2018 +0000
- Revision:
- 1:cca24408264b
- Parent:
- 0:0681e205d0e9
reference in easy-connect and DHT library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
fbdp1202 | 1:cca24408264b | 1 | #include <string.h> |
Jan Jongboom |
0:0681e205d0e9 | 2 | #include "mbed.h" |
Jan Jongboom |
0:0681e205d0e9 | 3 | #include "easy-connect.h" |
fbdp1202 | 1:cca24408264b | 4 | #include "TCPSocket.h" |
fbdp1202 | 1:cca24408264b | 5 | #include "DHT.h" |
Jan Jongboom |
0:0681e205d0e9 | 6 | |
fbdp1202 | 1:cca24408264b | 7 | DHT sensor(D7,DHT22); |
Jan Jongboom |
0:0681e205d0e9 | 8 | |
fbdp1202 | 1:cca24408264b | 9 | #define SERVER_IP "192.168.0.6" |
fbdp1202 | 1:cca24408264b | 10 | #define SERVER_PORT 50000 |
fbdp1202 | 1:cca24408264b | 11 | |
fbdp1202 | 1:cca24408264b | 12 | Serial pc(USBTX, USBRX); |
Jan Jongboom |
0:0681e205d0e9 | 13 | |
fbdp1202 | 1:cca24408264b | 14 | int main(){ |
fbdp1202 | 1:cca24408264b | 15 | int error = 0; |
fbdp1202 | 1:cca24408264b | 16 | pc.baud(115200); |
fbdp1202 | 1:cca24408264b | 17 | pc.printf("\r\n Simple HTTP example over ESP8266\r\n\r\n"); |
Jan Jongboom |
0:0681e205d0e9 | 18 | |
fbdp1202 | 1:cca24408264b | 19 | pc.printf("\r\nConnecting...\r\n"); |
fbdp1202 | 1:cca24408264b | 20 | NetworkInterface *network = easy_connect(true); |
fbdp1202 | 1:cca24408264b | 21 | while(!network){ |
fbdp1202 | 1:cca24408264b | 22 | pc.printf("Error: Cannot connect to the network\r\n"); |
fbdp1202 | 1:cca24408264b | 23 | wait(1); |
fbdp1202 | 1:cca24408264b | 24 | network = easy_connect(true); |
fbdp1202 | 1:cca24408264b | 25 | } |
fbdp1202 | 1:cca24408264b | 26 | |
fbdp1202 | 1:cca24408264b | 27 | pc.printf("Success\r\n\r\n"); |
fbdp1202 | 1:cca24408264b | 28 | pc.printf("MAC: %s\r\n", network->get_mac_address()); |
fbdp1202 | 1:cca24408264b | 29 | pc.printf("IP: %s\r\n", network->get_ip_address()); |
fbdp1202 | 1:cca24408264b | 30 | pc.printf("Netmask: %s\r\n", network->get_netmask()); |
fbdp1202 | 1:cca24408264b | 31 | pc.printf("Gateway: %s\r\n", network->get_gateway()); |
fbdp1202 | 1:cca24408264b | 32 | pc.printf("RSSI: %d\r\n\r\n", wifi.get_rssi()); |
Jan Jongboom |
0:0681e205d0e9 | 33 | |
fbdp1202 | 1:cca24408264b | 34 | pc.printf("\r\nDone\r\n"); |
Jan Jongboom |
0:0681e205d0e9 | 35 | |
fbdp1202 | 1:cca24408264b | 36 | TCPSocket socket; |
Jan Jongboom |
0:0681e205d0e9 | 37 | |
fbdp1202 | 1:cca24408264b | 38 | socket.open(network); |
fbdp1202 | 1:cca24408264b | 39 | socket.connect(SERVER_IP, SERVER_PORT); |
Jan Jongboom |
0:0681e205d0e9 | 40 | |
fbdp1202 | 1:cca24408264b | 41 | pc.printf("Sending request to %s : %d ...\r\n", SERVER_IP, SERVER_PORT); |
Jan Jongboom |
0:0681e205d0e9 | 42 | |
fbdp1202 | 1:cca24408264b | 43 | float c = 0, h = 0; |
fbdp1202 | 1:cca24408264b | 44 | int scount, rcount; |
fbdp1202 | 1:cca24408264b | 45 | while(1){ |
fbdp1202 | 1:cca24408264b | 46 | char rbuffer[64]; |
fbdp1202 | 1:cca24408264b | 47 | rcount = socket.recv(rbuffer, sizeof rbuffer); |
fbdp1202 | 1:cca24408264b | 48 | // pc.printf("%s\r\n", rbuffer); |
fbdp1202 | 1:cca24408264b | 49 | if(!strcmp(rbuffer, "GET /DHT22\r\n")){ |
fbdp1202 | 1:cca24408264b | 50 | pc.printf("Command Request") |
fbdp1202 | 1:cca24408264b | 51 | scount = socket.send("OK!", sizeof("OK!")); |
fbdp1202 | 1:cca24408264b | 52 | error = sensor.readData(); |
fbdp1202 | 1:cca24408264b | 53 | if (error == 0) { |
fbdp1202 | 1:cca24408264b | 54 | c = sensor.ReadTemperature(CELCIUS); |
fbdp1202 | 1:cca24408264b | 55 | h = sensor.ReadHumidity(); |
fbdp1202 | 1:cca24408264b | 56 | } |
fbdp1202 | 1:cca24408264b | 57 | else if(error == 6){ |
fbdp1202 | 1:cca24408264b | 58 | printf("CheckSum Error.\r\n"); |
fbdp1202 | 1:cca24408264b | 59 | } |
fbdp1202 | 1:cca24408264b | 60 | char sbuffer[64]; |
fbdp1202 | 1:cca24408264b | 61 | sprintf(sbuffer, "{temp: \"%.1f\", humid: \"%.1f\"}\n", c, h); |
fbdp1202 | 1:cca24408264b | 62 | scount = socket.send(sbuffer, sizeof(sbuffer)); |
fbdp1202 | 1:cca24408264b | 63 | pc.printf(sbuffer); |
fbdp1202 | 1:cca24408264b | 64 | } |
fbdp1202 | 1:cca24408264b | 65 | else{ |
fbdp1202 | 1:cca24408264b | 66 | pc.printf("Unknown request\r\n"); |
fbdp1202 | 1:cca24408264b | 67 | scount = socket.send("Unknown request\r\n", sizeof("Unknown request\r\n")); |
fbdp1202 | 1:cca24408264b | 68 | } |
fbdp1202 | 1:cca24408264b | 69 | } |
Jan Jongboom |
0:0681e205d0e9 | 70 | } |