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 coap-example by
main.cpp
00001 #include <string.h> 00002 #include "mbed.h" 00003 #include "easy-connect.h" 00004 #include "TCPSocket.h" 00005 #include "DHT.h" 00006 00007 DHT dht22(A0,DHT22); 00008 00009 #define SERVER_IP "192.168.0.8" 00010 #define SERVER_PORT 50000 00011 00012 Serial pc(USBTX, USBRX); 00013 00014 00015 00016 int main(){ 00017 int error = 0; 00018 pc.baud(115200); 00019 pc.printf("\r\nConnecting...\r\n"); 00020 NetworkInterface * network = easy_connect(true); 00021 00022 // couldn't find network. 00023 if(!network) 00024 { 00025 pc.printf("Error: Cannot connect to the network\r\n"); 00026 00027 } 00028 //success 00029 pc.printf("Success\r\n\r\n"); 00030 pc.printf("MAC: %s\r\n", network->get_mac_address()); 00031 pc.printf("IP: %s\r\n", network->get_ip_address()); 00032 pc.printf("Netmask: %s\r\n", network->get_netmask()); 00033 pc.printf("Gateway: %s\r\n", network->get_gateway()); 00034 pc.printf("RSSI: %d\r\n\r\n", wifi.get_rssi()); 00035 00036 TCPSocket socket; 00037 socket.open(network); 00038 socket.connect(SERVER_IP, SERVER_PORT); 00039 00040 float cel = 0, humid = 0; 00041 int send_message, receive_message; 00042 while(1){ 00043 char rbuffer[64] ={}; 00044 pc.printf("Sending request to %s : %d ...\r\n", SERVER_IP, SERVER_PORT); 00045 // artik to nucleo 00046 receive_message = socket.recv(rbuffer, sizeof rbuffer); 00047 pc.printf("%s\r\n", rbuffer); 00048 if(!strcmp(rbuffer,"GET /DHT22\r\n")) 00049 { 00050 char sbuffer[64]={}; 00051 send_message = socket.send("OKAY\r", sizeof("OKAY\r")); 00052 // 00053 error = dht22.readData(); 00054 if (error == 0) { 00055 cel = dht22.ReadTemperature(CELCIUS); 00056 humid = dht22.ReadHumidity(); 00057 } 00058 else { 00059 printf("ERROR : %d\r\n", error); 00060 } 00061 sprintf(sbuffer, "TEMP = %.2f humid = %.2f!\r", cel, humid); 00062 send_message = socket.send(sbuffer, sizeof(sbuffer)); 00063 pc.printf(sbuffer); 00064 } 00065 else{ 00066 pc.printf("Unknown request\r"); 00067 send_message = socket.send("Unknown request\r", sizeof("Unknown request\r")); 00068 } 00069 } 00070 }
Generated on Mon Jul 18 2022 21:04:50 by
1.7.2
