Using Ethernet Interface to send temperature/humidity info to client
Dependencies: EthernetInterface SDFileSystem SHTx mbed-rtos mbed
Revision 1:788d545c9cd1, committed 2013-10-18
- Comitter:
- airaylee
- Date:
- Fri Oct 18 15:02:42 2013 +0000
- Parent:
- 0:e26dfd507000
- Commit message:
- Second Version
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r e26dfd507000 -r 788d545c9cd1 main.cpp --- a/main.cpp Thu Oct 17 23:59:17 2013 +0000 +++ b/main.cpp Fri Oct 18 15:02:42 2013 +0000 @@ -5,6 +5,7 @@ #include "EthernetInterface.h" //SPI-mosi,miso,sclk,DigitalOut-cs, name used to access the filesystem +//For further update SDFileSystem sd(p5, p6, p7, p8, "sd1"); //Terminal Port to display the file back @@ -19,64 +20,42 @@ int main() { - /*FILE *fw = fopen("/sd1/mydir/sdtest.txt", "w"); - if(fw == NULL) { - pc.printf("Could not open file for write\n"); - } - else{ - fprintf(fw, "Hello SD file World!LOLOLOL\n"); - fclose(fw); - } - //Begin reading from the SD file - pc.printf("Begin reading from the SD file\n"); - FILE *fr = fopen("/sd1/mydir/sdtest.txt", "r"); - if(fr== NULL) { - pc.printf("Could not open file for read\n"); - } - else{ - while(!feof(fr)){ - pc.putc(fgetc(fr)); - } - fclose(fr); - } - */ - // Speed things up a bit. sensor.setOTPReload(false); sensor.setResolution(true); - + eth.init(); //Use DHCP eth.connect(7000);//Longer timeout here //print out the MAC address first printf("MAC Address is %s\r\n", eth.getMACAddress()); printf("IP Address is %s\r\n", eth.getIPAddress()); //IP address is 130.207.234.205 - - + // Declare the TCP server TCPSocketServer server; server.bind(ECHO_SERVER_PORT); server.listen(); while(1) { printf("\nWait for new connection...\r\n"); - TCPSocketConnection client; + TCPSocketConnection client; //wait for a TCP connection server.accept(client); - client.set_blocking(false); // Timeout after (5)s + client.set_blocking(false); // Non-blocking printf("Connection from: %s\r\n", client.get_address()); - + //Update the sensor busy = true; sensor.update(); busy = false; + //Initialize the buffer to send char buffer[256]={0}; - // Temperature in celcius + // Temperature in celcius and Humidity in percent sensor.setScale(false); sprintf(buffer,"Temperature [ %3.2f C ]\r\nHumdity [ %3.2f %% ]\r\n\n", sensor.getTemperature(), sensor.getHumidity()); - + //Send the data int n = sizeof(buffer); client.send_all(buffer, n); - + //close the connection and wait for new connection client.close(); printf(" Connection over\r\n");