CoAP example application for mbed OS 5

Dependencies:   DHT

Fork of coap-example by sandbox

Committer:
Dongho
Date:
Tue Jan 02 21:57:20 2018 +0000
Revision:
1:113541750e81
Parent:
0:0681e205d0e9
ok

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Jan Jongboom 0:0681e205d0e9 1 /*
Dongho 1:113541750e81 2 #include <string>
Dongho 1:113541750e81 3 #include "mbed.h"
Dongho 1:113541750e81 4 #include "easy-connect.h"
Dongho 1:113541750e81 5 #include "TCPSocket.h"
Dongho 1:113541750e81 6 #include "DHT.h"
Dongho 1:113541750e81 7
Dongho 1:113541750e81 8 #define SERVER_IP "192.168.0.14"
Dongho 1:113541750e81 9 #define SERVER_PORT 50000
Dongho 1:113541750e81 10
Dongho 1:113541750e81 11 Serial esp(D8, D2); // computer to mbed boardSerial esp(D1, D0);
Dongho 1:113541750e81 12 Serial pc(USBTX, USBRX);
Dongho 1:113541750e81 13
Dongho 1:113541750e81 14 DHT sensor(D6, AM2302);
Dongho 1:113541750e81 15
Dongho 1:113541750e81 16 int main() {
Dongho 1:113541750e81 17 esp.baud(115200);
Dongho 1:113541750e81 18 pc.baud(115200);
Dongho 1:113541750e81 19 pc.printf("\r\n Simple TCP example over ESP8266\r\n\r\n");
Dongho 1:113541750e81 20
Dongho 1:113541750e81 21 pc.printf("\r\nConnecting...\r\n");
Dongho 1:113541750e81 22
Dongho 1:113541750e81 23 NetworkInterface *network = easy_connect(true);
Dongho 1:113541750e81 24 if (!network) {
Dongho 1:113541750e81 25 pc.printf("Error: Cannot connect to the network\r\n");
Dongho 1:113541750e81 26 return -1;
Dongho 1:113541750e81 27 }
Dongho 1:113541750e81 28
Dongho 1:113541750e81 29 pc.printf("Success\r\n\r\n");
Dongho 1:113541750e81 30 pc.printf("MAC: %s\r\n", network->get_mac_address());
Dongho 1:113541750e81 31 pc.printf("IP: %s\r\n", network->get_ip_address());
Dongho 1:113541750e81 32 pc.printf("Netmask: %s\r\n", network->get_netmask());
Dongho 1:113541750e81 33 pc.printf("Gateway: %s\r\n", network->get_gateway());
Dongho 1:113541750e81 34 pc.printf("RSSI: %d\r\n\r\n", wifi.get_rssi());
Dongho 1:113541750e81 35
Dongho 1:113541750e81 36 TCPSocket socket; // for TCP
Dongho 1:113541750e81 37 pc.printf("Sending TCP request to %s : %d ...\r\n", SERVER_IP, SERVER_PORT);
Dongho 1:113541750e81 38
Dongho 1:113541750e81 39 // Open a socket on the network interface, and create a TCP connection
Dongho 1:113541750e81 40 socket.open(network);
Dongho 1:113541750e81 41 socket.connect(SERVER_IP, SERVER_PORT);
Dongho 1:113541750e81 42
Dongho 1:113541750e81 43 int err;
Dongho 1:113541750e81 44 wait(1); // wait 1 second for device stable status
Dongho 1:113541750e81 45 while (1) {
Dongho 1:113541750e81 46 err = sensor.readData();
Dongho 1:113541750e81 47 if (err == 0) {
Dongho 1:113541750e81 48 //char sbuffer[] = "{\"Temperature\": \"%4.2f C\", \"Humidity\": \"%4.2f\"} \r\n\r\n", sensor.ReadTemperature(CELCIUS), sensor.ReadHumidity();
Dongho 1:113541750e81 49 char sbuffer[] = "{\"Temperature\": \"22.0 C\", \"Humidity\": \"65 %\"} \r\n\r\n";
Dongho 1:113541750e81 50 int scount = socket.send(sbuffer, sizeof sbuffer);
Dongho 1:113541750e81 51 pc.printf("sent %d [%.*s]\r\n", scount, strstr(sbuffer, "\r\n")-sbuffer, sbuffer);
Dongho 1:113541750e81 52
Dongho 1:113541750e81 53 //Receive a simple http response and print out the response line
Dongho 1:113541750e81 54 char rbuffer[64];
Dongho 1:113541750e81 55 int rcount = socket.recv(rbuffer, sizeof rbuffer);
Dongho 1:113541750e81 56 pc.printf("sent %d [%.*s]\r\n", scount, strstr(rbuffer, "\r\n")-rbuffer, rbuffer);
Dongho 1:113541750e81 57 } else{
Dongho 1:113541750e81 58 printf("\r\nErr %i \n",err);
Dongho 1:113541750e81 59 }
Dongho 1:113541750e81 60 wait(3);
Dongho 1:113541750e81 61 }
Dongho 1:113541750e81 62
Dongho 1:113541750e81 63 socket.close();
Dongho 1:113541750e81 64 }
Dongho 1:113541750e81 65 */
Jan Jongboom 0:0681e205d0e9 66
Jan Jongboom 0:0681e205d0e9 67 #include <string>
Jan Jongboom 0:0681e205d0e9 68 #include "mbed.h"
Jan Jongboom 0:0681e205d0e9 69 #include "easy-connect.h"
Dongho 1:113541750e81 70 #include "TCPSocket.h"
Dongho 1:113541750e81 71 #include "DHT.h"
Jan Jongboom 0:0681e205d0e9 72
Dongho 1:113541750e81 73 #define SERVER_IP "192.168.0.14"
Dongho 1:113541750e81 74 #define SERVER_PORT 50000
Jan Jongboom 0:0681e205d0e9 75
Dongho 1:113541750e81 76 DHT sensor(D6, AM2302);
Jan Jongboom 0:0681e205d0e9 77
Dongho 1:113541750e81 78 Serial pc(USBTX, USBRX); // computer to mbed boardSerial esp(D1, D0);
Jan Jongboom 0:0681e205d0e9 79
Dongho 1:113541750e81 80 void http_demo(NetworkInterface *net)
Dongho 1:113541750e81 81 {
Dongho 1:113541750e81 82 TCPSocket socket; // for HTTP
Dongho 1:113541750e81 83
Dongho 1:113541750e81 84 // Open a socket on the network interface, and create a TCP connection
Dongho 1:113541750e81 85 socket.open(net);
Dongho 1:113541750e81 86 socket.connect(SERVER_IP, SERVER_PORT);
Jan Jongboom 0:0681e205d0e9 87
Dongho 1:113541750e81 88 int err;
Dongho 1:113541750e81 89 wait(1); // wait 1 second for device stable status
Dongho 1:113541750e81 90
Dongho 1:113541750e81 91 err = sensor.readData();
Dongho 1:113541750e81 92 // Send a simple http request
Dongho 1:113541750e81 93 if(err == 0){
Dongho 1:113541750e81 94 char sbuffer[64];
Dongho 1:113541750e81 95 sprintf(sbuffer, "{\"Temperature\": \"%4.2fC\", \"Humidity\": \"%4.2f %\"} \r\n\r\n", sensor.ReadTemperature(CELCIUS), sensor.ReadHumidity());
Dongho 1:113541750e81 96 int scount = socket.send(sbuffer, sizeof sbuffer);
Dongho 1:113541750e81 97 pc.printf("sent %d [%.*s]\r\n", scount, strstr(sbuffer, "\r\n")-sbuffer, sbuffer);
Dongho 1:113541750e81 98
Dongho 1:113541750e81 99 // Recieve a simple http response and print out the response line
Dongho 1:113541750e81 100 char rbuffer[64];
Dongho 1:113541750e81 101 int rcount = socket.recv(rbuffer, sizeof rbuffer);
Dongho 1:113541750e81 102 pc.printf("recv %d [%.*s]\r\n", rcount, strstr(rbuffer, "\r\n")-rbuffer, rbuffer);
Dongho 1:113541750e81 103
Dongho 1:113541750e81 104 rcount = socket.recv(rbuffer, sizeof rbuffer);
Dongho 1:113541750e81 105 pc.printf("recv %d [%.*s]\r\n", rcount, strstr(rbuffer, "\r\n")-rbuffer, rbuffer);
Dongho 1:113541750e81 106 } else {
Dongho 1:113541750e81 107 char err[] = "Error\r\n";
Dongho 1:113541750e81 108 int ecount = socket.send(err, sizeof err);
Dongho 1:113541750e81 109 pc.printf("sent %d [%.*s]\r\n", ecount, strstr(err, "\r\n")-err, err);
Jan Jongboom 0:0681e205d0e9 110 }
Dongho 1:113541750e81 111
Dongho 1:113541750e81 112 // Close the socket to return its memory and bring down the network interface
Dongho 1:113541750e81 113 socket.close();
Jan Jongboom 0:0681e205d0e9 114 }
Jan Jongboom 0:0681e205d0e9 115
Jan Jongboom 0:0681e205d0e9 116 int main() {
Dongho 1:113541750e81 117 pc.baud(115200);
Dongho 1:113541750e81 118 pc.printf("\r\n Simple HTTP example over ESP8266\r\n\r\n");
Dongho 1:113541750e81 119 pc.printf("\r\nConnecting...\r\n");
Dongho 1:113541750e81 120
Jan Jongboom 0:0681e205d0e9 121 NetworkInterface *network = easy_connect(true);
Jan Jongboom 0:0681e205d0e9 122 if (!network) {
Dongho 1:113541750e81 123 pc.printf("Error: Cannot connect to the network\r\n");
Dongho 1:113541750e81 124 return -1;
Jan Jongboom 0:0681e205d0e9 125 }
Jan Jongboom 0:0681e205d0e9 126
Dongho 1:113541750e81 127 pc.printf("Success\r\n\r\n");
Dongho 1:113541750e81 128 pc.printf("MAC: %s\r\n", network->get_mac_address());
Dongho 1:113541750e81 129 pc.printf("IP: %s\r\n", network->get_ip_address());
Dongho 1:113541750e81 130 pc.printf("Netmask: %s\r\n", network->get_netmask());
Dongho 1:113541750e81 131 pc.printf("Gateway: %s\r\n", network->get_gateway());
Dongho 1:113541750e81 132 pc.printf("RSSI: %d\r\n\r\n", wifi.get_rssi());
Dongho 1:113541750e81 133
Dongho 1:113541750e81 134 while(1) {
Dongho 1:113541750e81 135 http_demo(network);
Dongho 1:113541750e81 136 wait(3);
Dongho 1:113541750e81 137 }
Dongho 1:113541750e81 138
Dongho 1:113541750e81 139 network->disconnect();
Dongho 1:113541750e81 140 pc.printf("\r\nDone\r\n");
Dongho 1:113541750e81 141 }