Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of _HW3_DHT_TCP by
main.cpp
00001 #include <string> 00002 #include "mbed.h" 00003 #include "easy-connect.h" 00004 #include "TCPSocket.h" 00005 #include "DHT.h" 00006 00007 #define SERVER_IP "192.168.0.9" 00008 #define SERVER_PORT 50000 00009 00010 Serial pc(USBTX, USBRX); // computer to mbed boardSerial esp(D1, D0); 00011 DHT sensor(D7,DHT22); 00012 00013 void task_DHT(); 00014 00015 void http_demo(NetworkInterface *net) 00016 { 00017 int error = 0; 00018 int h, c, f; 00019 float dp = 0.0f; 00020 00021 TCPSocket socket; // for TCP 00022 00023 pc.printf("Sending TCP request to %s:%d ...\r\n", SERVER_IP, SERVER_PORT); 00024 00025 // Open a socket on the network interface, and create a TCP connection 00026 socket.open(net); 00027 socket.connect(SERVER_IP, SERVER_PORT); 00028 00029 int scount; 00030 00031 while(1){ 00032 00033 char buf[64]; 00034 int len = socket.recv(buf, 64); 00035 buf[len] = '\0'; 00036 00037 char buffer[64] = "GET /DHT22"; 00038 int result = strcmp(buf, buffer); 00039 printf("received: %s %d\r\n",buf,result); 00040 00041 if (strcmp(buf, buffer)){ 00042 printf("*****The wrong request come.*****\r\n"); 00043 char sbuffer4[] = "GET /Unknown request\r\n"; 00044 //char sbuffer4[] = "{\"method\":\"GET\",\"content\":\"Unknown request\"}\r\n"; 00045 scount = socket.send(sbuffer4, sizeof sbuffer4); 00046 pc.printf("send error message: %d [%.*s]\r\n", scount, strstr(sbuffer4, "\r\n")-sbuffer4, sbuffer4); 00047 00048 }else{ // strcmp(buf, buffer)값이 0일 때, 즉, 전송된 값이 "GET /DHT22\r\n" 일 때 00049 00050 printf("*****The correct request come.*****\r\n"); 00051 char sbuffer1[] = "GET /Ok\r\n"; 00052 //char sbuffer1[] = "{\"method\":\"GET\",\"message\":\"Ok\"}\r\n"; 00053 scount = socket.send(sbuffer1, sizeof sbuffer1); 00054 pc.printf("send Ok message: %d [%.*s]\r\n", scount, strstr(sbuffer1, "\r\n")-sbuffer1, sbuffer1); 00055 00056 error = sensor.readData(); 00057 if (0 == error) { 00058 c = sensor.ReadTemperature(CELCIUS); 00059 f = sensor.ReadTemperature(FARENHEIT); 00060 h = sensor.ReadHumidity(); 00061 dp = sensor.CalcdewPoint(c, h); 00062 00063 char sbuffer2[33]; 00064 sprintf(sbuffer2, "{\"type\":\"temperature\",\"value\":%d}\r\n", c); 00065 scount = socket.send(sbuffer2, sizeof sbuffer2); 00066 pc.printf("sent %d [%.*s]\r\n", scount, strstr(sbuffer2, "\r\n")-sbuffer2, sbuffer2); 00067 00068 printf("Temperature in Celcius: %d, Farenheit %d\r\n", c, f); 00069 00070 char sbuffer3[32]; 00071 sprintf(sbuffer3, "{\"type\":\"humidity\",\"value\":%d}\r\n", h); 00072 scount = socket.send(sbuffer3, sizeof sbuffer3); 00073 pc.printf("sent %d [%.*s]\r\n", scount, strstr(sbuffer3, "\r\n")-sbuffer3, sbuffer3); 00074 00075 printf("Humidity is %d, Dewpoint: %4.2f\r\n\r\n", h, dp); 00076 00077 } else { // checksum error가 발생하는 경우 터미널에 error 발생을 표시 00078 printf("Checksum Error: %d\r\n", error); 00079 } 00080 } 00081 } 00082 // Close the socket to return its memory and bring down the network interface 00083 socket.close(); 00084 } 00085 /* 00086 void task_DHT(){ 00087 00088 int error = 0; 00089 int h, c, f; 00090 float dp = 0.0f; 00091 char buf[256]; 00092 00093 wait(3); 00094 error = sensor.readData(); 00095 if (0 == error) { 00096 c = sensor.ReadTemperature(CELCIUS); 00097 f = sensor.ReadTemperature(FARENHEIT); 00098 h = sensor.ReadHumidity(); 00099 dp = sensor.CalcdewPoint(c, h); 00100 sprintf(buf, "Temperature in Celcius: %d, Farenheit %d\r\n", c, f); 00101 //printf("Temperature in Celcius: %d, Farenheit %d\r\n", c, f); 00102 sprintf(buf, "Humidity is %d, Dewpoint: %4.2f\r\n\n", h, dp); 00103 //printf("Humidity is %d, Dewpoint: %4.2f\r\n\n", h, dp); 00104 00105 } else { 00106 printf("Error: %d\r\n", error); 00107 } 00108 } 00109 */ 00110 int main() { 00111 00112 pc.baud(115200); 00113 pc.printf("\r\n Simple TCP example over ESP8266\r\n\r\n"); 00114 00115 pc.printf("\r\nConnecting...\r\n"); 00116 00117 NetworkInterface *network = easy_connect(true); 00118 00119 if (!network) { 00120 pc.printf("Error: Cannot connect to the network\r\n"); 00121 return -1; 00122 } 00123 00124 pc.printf("Success\r\n\r\n"); 00125 pc.printf("MAC: %s\r\n", network->get_mac_address()); 00126 pc.printf("IP: %s\r\n", network->get_ip_address()); 00127 pc.printf("Netmask: %s\r\n", network->get_netmask()); 00128 pc.printf("Gateway: %s\r\n", network->get_gateway()); 00129 pc.printf("RSSI: %d\r\n\r\n", wifi.get_rssi()); 00130 00131 http_demo(network); 00132 //network->disconnect(); 00133 //pc.printf("\r\nDone\r\n"); 00134 00135 while(1) { 00136 //task_DHT(); 00137 } 00138 }
Generated on Fri Jul 15 2022 05:54:01 by
1.7.2
