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.
Dependencies: TextLCD WiflyInterface mbed
Fork of Wifly_HelloWorld by
main.cpp
00001 #include "mbed.h" 00002 #include "TextLCD.h" 00003 #include "WiflyInterface.h" 00004 00005 TextLCD lcd(p26, p25, p24, p23, p22, p21); // rs, e, d4-d7 00006 Serial pc(USBTX, USBRX); 00007 00008 /* wifly object where: 00009 * - p28 and p27 are for the serial communication 00010 * - p25 is for the reset pin 00011 * - p26 is for the connection status 00012 */ 00013 WiflyInterface wifly(p28, p27, p10, p10,"ADHOC","",Adhoc); 00014 float temp; 00015 float humid; 00016 int bound = 0; 00017 00018 int main() { 00019 00020 lcd.cls(); 00021 lcd.printf("Hello!\n"); 00022 00023 while(!bound){ 00024 lcd.cls(); 00025 lcd.printf("Initiating WiFly..."); 00026 printf("wifly.init() returned:%d\n",wifly.init("169.254.1.2","255.255.0.0","10.0.0.1")); 00027 printf("wifly.connect returned:%d\n",wifly.connect()); 00028 printf("IP Address is %s\n\r", wifly.getIPAddress()); 00029 lcd.cls(); 00030 lcd.printf("WiFly Initiated..."); 00031 00032 00033 TCPSocketServer server; 00034 int n = 8042; 00035 if(server.bind(n) == 0){ 00036 bound = 1; 00037 printf("Bind to port %d succeeded\n",n); 00038 lcd.cls(); 00039 lcd.printf("Listening on port %d...",n); 00040 server.listen(); 00041 TCPSocketConnection client; 00042 printf("\nWait for new connection...\n"); 00043 printf("server.accept() returned:%d\n", server.accept(client)); 00044 char buffer[256]; 00045 int i = 0; 00046 int j = 0; 00047 int tempFound = 0; 00048 int humidFound = 0; 00049 while (true) { 00050 int n = client.receive(buffer, sizeof(buffer)); 00051 j += n; 00052 if (j > 15){ 00053 for(i=5;i<j;i++){ 00054 if(buffer[i] == '^'){ 00055 //is Temp 00056 printf("Temp: %c%c%c%c%c\n",buffer[i-5],buffer[i-4],buffer[i-3],buffer[i-2],buffer[i-1]); 00057 if(buffer[i-6] - '0' <= 9 && buffer[i-6] - '0' >= 0){ 00058 temp = ((buffer[i-6] - '0') * 100) + ((buffer[i-5] - '0') * 10) + (buffer[i-4] - '0') + ((buffer[i-2] - '0') * .1) + ((buffer[i-1] - '0') * .01); 00059 }else{ 00060 temp = ((buffer[i-5] - '0') * 10) + (buffer[i-4] - '0') + ((buffer[i-2] - '0') * .1) + ((buffer[i-1] - '0') * .01); 00061 } 00062 printf("TEMP: %f\n\n",temp); 00063 tempFound = 1; 00064 }else if(buffer[i] == '$'){ 00065 printf("Humid: %c%c%c%c%c\n",buffer[i-5],buffer[i-4],buffer[i-3],buffer[i-2],buffer[i-1]); 00066 if(buffer[i-6] - '0' <= 9 && buffer[i-6] - '0' >= 0){ 00067 humid = ((buffer[i-6] - '0') * 100) + ((buffer[i-5] - '0') * 10) + (buffer[i-4] - '0') + ((buffer[i-2] - '0') * .1) + ((buffer[i-1] - '0') * .01); 00068 }else{ 00069 humid = ((buffer[i-5] - '0') * 10) + (buffer[i-4] - '0') + ((buffer[i-2] - '0') * .1) + ((buffer[i-1] - '0') * .01); 00070 } 00071 printf("HUMID: %f\n\n",humid); 00072 humidFound = 1; 00073 } 00074 if((humidFound && tempFound) || j > 250){ 00075 humidFound = 0; 00076 tempFound = 0; 00077 buffer[j] = 0; 00078 j = 0; 00079 lcd.cls(); 00080 lcd.printf("Temp:%.2f\nHumid:%.2f%%",temp,humid); 00081 } 00082 } 00083 } 00084 } 00085 }else{ 00086 printf("Bind to port %d failed\n",n); 00087 lcd.cls(); 00088 lcd.printf("Bind to port %d failed\n",n); 00089 bound = 0; 00090 server.close(); 00091 } 00092 } 00093 }
Generated on Sat Jul 23 2022 10:59:24 by
1.7.2
