lab 3 remote weather sensor

Dependencies:   TextLCD WiflyInterface mbed

Fork of Wifly_HelloWorld by Samuel Mokrani

Committer:
jstockhoff3
Date:
Wed Oct 16 18:06:22 2013 +0000
Revision:
6:31b6605cf1a7
Parent:
5:867d16e948eb
Final lab 3 code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samux 0:90ba0f51aa64 1 #include "mbed.h"
jstockhoff3 6:31b6605cf1a7 2 #include "TextLCD.h"
samux 3:3b84102f022e 3 #include "WiflyInterface.h"
samux 1:49e1e9ed6e39 4
jstockhoff3 6:31b6605cf1a7 5 TextLCD lcd(p26, p25, p24, p23, p22, p21); // rs, e, d4-d7
samux 1:49e1e9ed6e39 6 Serial pc(USBTX, USBRX);
samux 1:49e1e9ed6e39 7
samux 1:49e1e9ed6e39 8 /* wifly object where:
jstockhoff3 6:31b6605cf1a7 9 * - p28 and p27 are for the serial communication
samux 3:3b84102f022e 10 * - p25 is for the reset pin
samux 3:3b84102f022e 11 * - p26 is for the connection status
samux 1:49e1e9ed6e39 12 */
jstockhoff3 6:31b6605cf1a7 13 WiflyInterface wifly(p28, p27, p10, p10,"ADHOC","",Adhoc);
jstockhoff3 6:31b6605cf1a7 14 float temp;
jstockhoff3 6:31b6605cf1a7 15 float humid;
jstockhoff3 6:31b6605cf1a7 16 int bound = 0;
samux 0:90ba0f51aa64 17
samux 1:49e1e9ed6e39 18 int main() {
jstockhoff3 6:31b6605cf1a7 19
jstockhoff3 6:31b6605cf1a7 20 lcd.cls();
jstockhoff3 6:31b6605cf1a7 21 lcd.printf("Hello!\n");
jstockhoff3 6:31b6605cf1a7 22
jstockhoff3 6:31b6605cf1a7 23 while(!bound){
jstockhoff3 6:31b6605cf1a7 24 lcd.cls();
jstockhoff3 6:31b6605cf1a7 25 lcd.printf("Initiating WiFly...");
jstockhoff3 6:31b6605cf1a7 26 printf("wifly.init() returned:%d\n",wifly.init("169.254.1.2","255.255.0.0","10.0.0.1"));
jstockhoff3 6:31b6605cf1a7 27 printf("wifly.connect returned:%d\n",wifly.connect());
jstockhoff3 6:31b6605cf1a7 28 printf("IP Address is %s\n\r", wifly.getIPAddress());
jstockhoff3 6:31b6605cf1a7 29 lcd.cls();
jstockhoff3 6:31b6605cf1a7 30 lcd.printf("WiFly Initiated...");
jstockhoff3 6:31b6605cf1a7 31
jstockhoff3 6:31b6605cf1a7 32
jstockhoff3 6:31b6605cf1a7 33 TCPSocketServer server;
jstockhoff3 6:31b6605cf1a7 34 int n = 8042;
jstockhoff3 6:31b6605cf1a7 35 if(server.bind(n) == 0){
jstockhoff3 6:31b6605cf1a7 36 bound = 1;
jstockhoff3 6:31b6605cf1a7 37 printf("Bind to port %d succeeded\n",n);
jstockhoff3 6:31b6605cf1a7 38 lcd.cls();
jstockhoff3 6:31b6605cf1a7 39 lcd.printf("Listening on port %d...",n);
jstockhoff3 6:31b6605cf1a7 40 server.listen();
jstockhoff3 6:31b6605cf1a7 41 TCPSocketConnection client;
jstockhoff3 6:31b6605cf1a7 42 printf("\nWait for new connection...\n");
jstockhoff3 6:31b6605cf1a7 43 printf("server.accept() returned:%d\n", server.accept(client));
jstockhoff3 6:31b6605cf1a7 44 char buffer[256];
jstockhoff3 6:31b6605cf1a7 45 int i = 0;
jstockhoff3 6:31b6605cf1a7 46 int j = 0;
jstockhoff3 6:31b6605cf1a7 47 int tempFound = 0;
jstockhoff3 6:31b6605cf1a7 48 int humidFound = 0;
jstockhoff3 6:31b6605cf1a7 49 while (true) {
jstockhoff3 6:31b6605cf1a7 50 int n = client.receive(buffer, sizeof(buffer));
jstockhoff3 6:31b6605cf1a7 51 j += n;
jstockhoff3 6:31b6605cf1a7 52 if (j > 15){
jstockhoff3 6:31b6605cf1a7 53 for(i=5;i<j;i++){
jstockhoff3 6:31b6605cf1a7 54 if(buffer[i] == '^'){
jstockhoff3 6:31b6605cf1a7 55 //is Temp
jstockhoff3 6:31b6605cf1a7 56 printf("Temp: %c%c%c%c%c\n",buffer[i-5],buffer[i-4],buffer[i-3],buffer[i-2],buffer[i-1]);
jstockhoff3 6:31b6605cf1a7 57 if(buffer[i-6] - '0' <= 9 && buffer[i-6] - '0' >= 0){
jstockhoff3 6:31b6605cf1a7 58 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);
jstockhoff3 6:31b6605cf1a7 59 }else{
jstockhoff3 6:31b6605cf1a7 60 temp = ((buffer[i-5] - '0') * 10) + (buffer[i-4] - '0') + ((buffer[i-2] - '0') * .1) + ((buffer[i-1] - '0') * .01);
jstockhoff3 6:31b6605cf1a7 61 }
jstockhoff3 6:31b6605cf1a7 62 printf("TEMP: %f\n\n",temp);
jstockhoff3 6:31b6605cf1a7 63 tempFound = 1;
jstockhoff3 6:31b6605cf1a7 64 }else if(buffer[i] == '$'){
jstockhoff3 6:31b6605cf1a7 65 printf("Humid: %c%c%c%c%c\n",buffer[i-5],buffer[i-4],buffer[i-3],buffer[i-2],buffer[i-1]);
jstockhoff3 6:31b6605cf1a7 66 if(buffer[i-6] - '0' <= 9 && buffer[i-6] - '0' >= 0){
jstockhoff3 6:31b6605cf1a7 67 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);
jstockhoff3 6:31b6605cf1a7 68 }else{
jstockhoff3 6:31b6605cf1a7 69 humid = ((buffer[i-5] - '0') * 10) + (buffer[i-4] - '0') + ((buffer[i-2] - '0') * .1) + ((buffer[i-1] - '0') * .01);
jstockhoff3 6:31b6605cf1a7 70 }
jstockhoff3 6:31b6605cf1a7 71 printf("HUMID: %f\n\n",humid);
jstockhoff3 6:31b6605cf1a7 72 humidFound = 1;
jstockhoff3 6:31b6605cf1a7 73 }
jstockhoff3 6:31b6605cf1a7 74 if((humidFound && tempFound) || j > 250){
jstockhoff3 6:31b6605cf1a7 75 humidFound = 0;
jstockhoff3 6:31b6605cf1a7 76 tempFound = 0;
jstockhoff3 6:31b6605cf1a7 77 buffer[j] = 0;
jstockhoff3 6:31b6605cf1a7 78 j = 0;
jstockhoff3 6:31b6605cf1a7 79 lcd.cls();
jstockhoff3 6:31b6605cf1a7 80 lcd.printf("Temp:%.2f\nHumid:%.2f%%",temp,humid);
jstockhoff3 6:31b6605cf1a7 81 }
jstockhoff3 6:31b6605cf1a7 82 }
jstockhoff3 6:31b6605cf1a7 83 }
jstockhoff3 6:31b6605cf1a7 84 }
jstockhoff3 6:31b6605cf1a7 85 }else{
jstockhoff3 6:31b6605cf1a7 86 printf("Bind to port %d failed\n",n);
jstockhoff3 6:31b6605cf1a7 87 lcd.cls();
jstockhoff3 6:31b6605cf1a7 88 lcd.printf("Bind to port %d failed\n",n);
jstockhoff3 6:31b6605cf1a7 89 bound = 0;
jstockhoff3 6:31b6605cf1a7 90 server.close();
jstockhoff3 6:31b6605cf1a7 91 }
jstockhoff3 6:31b6605cf1a7 92 }
jstockhoff3 6:31b6605cf1a7 93 }