Using Ethernet Interface to send temperature/humidity info to client
Dependencies: EthernetInterface SDFileSystem SHTx mbed-rtos mbed
main.cpp
- Committer:
- airaylee
- Date:
- 2013-10-17
- Revision:
- 0:e26dfd507000
- Child:
- 1:788d545c9cd1
File content as of revision 0:e26dfd507000:
#include "mbed.h" #include "SHTx/sht15.hpp" #include "SDFileSystem.h" #include "EthernetInterface.h" //SPI-mosi,miso,sclk,DigitalOut-cs, name used to access the filesystem SDFileSystem sd(p5, p6, p7, p8, "sd1"); //Terminal Port to display the file back Serial pc(USBTX, USBRX); DigitalOut busy(LED1); // Use p28-sda--data,p27-scl--sck SHTx::SHT15 sensor(p28, p27); EthernetInterface eth; #define ECHO_SERVER_PORT 7 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 TCPSocketServer server; server.bind(ECHO_SERVER_PORT); server.listen(); while(1) { printf("\nWait for new connection...\r\n"); TCPSocketConnection client; server.accept(client); client.set_blocking(false); // Timeout after (5)s printf("Connection from: %s\r\n", client.get_address()); busy = true; sensor.update(); busy = false; char buffer[256]={0}; // Temperature in celcius sensor.setScale(false); sprintf(buffer,"Temperature [ %3.2f C ]\r\nHumdity [ %3.2f %% ]\r\n\n", sensor.getTemperature(), sensor.getHumidity()); int n = sizeof(buffer); client.send_all(buffer, n); client.close(); printf(" Connection over\r\n"); } }