Using Ethernet Interface to send temperature/humidity info to client

Dependencies:   EthernetInterface SDFileSystem SHTx mbed-rtos mbed

Committer:
airaylee
Date:
Fri Oct 18 15:02:42 2013 +0000
Revision:
1:788d545c9cd1
Parent:
0:e26dfd507000
Second Version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
airaylee 0:e26dfd507000 1
airaylee 0:e26dfd507000 2 #include "mbed.h"
airaylee 0:e26dfd507000 3 #include "SHTx/sht15.hpp"
airaylee 0:e26dfd507000 4 #include "SDFileSystem.h"
airaylee 0:e26dfd507000 5 #include "EthernetInterface.h"
airaylee 0:e26dfd507000 6
airaylee 0:e26dfd507000 7 //SPI-mosi,miso,sclk,DigitalOut-cs, name used to access the filesystem
airaylee 1:788d545c9cd1 8 //For further update
airaylee 0:e26dfd507000 9 SDFileSystem sd(p5, p6, p7, p8, "sd1");
airaylee 0:e26dfd507000 10
airaylee 0:e26dfd507000 11 //Terminal Port to display the file back
airaylee 0:e26dfd507000 12 Serial pc(USBTX, USBRX);
airaylee 0:e26dfd507000 13 DigitalOut busy(LED1);
airaylee 0:e26dfd507000 14
airaylee 0:e26dfd507000 15 // Use p28-sda--data,p27-scl--sck
airaylee 0:e26dfd507000 16 SHTx::SHT15 sensor(p28, p27);
airaylee 0:e26dfd507000 17
airaylee 0:e26dfd507000 18 EthernetInterface eth;
airaylee 0:e26dfd507000 19 #define ECHO_SERVER_PORT 7
airaylee 0:e26dfd507000 20
airaylee 0:e26dfd507000 21 int main() {
airaylee 0:e26dfd507000 22
airaylee 0:e26dfd507000 23 // Speed things up a bit.
airaylee 0:e26dfd507000 24 sensor.setOTPReload(false);
airaylee 0:e26dfd507000 25 sensor.setResolution(true);
airaylee 1:788d545c9cd1 26
airaylee 0:e26dfd507000 27 eth.init(); //Use DHCP
airaylee 0:e26dfd507000 28 eth.connect(7000);//Longer timeout here
airaylee 0:e26dfd507000 29 //print out the MAC address first
airaylee 0:e26dfd507000 30 printf("MAC Address is %s\r\n", eth.getMACAddress());
airaylee 0:e26dfd507000 31 printf("IP Address is %s\r\n", eth.getIPAddress());
airaylee 0:e26dfd507000 32 //IP address is 130.207.234.205
airaylee 1:788d545c9cd1 33 // Declare the TCP server
airaylee 0:e26dfd507000 34 TCPSocketServer server;
airaylee 0:e26dfd507000 35 server.bind(ECHO_SERVER_PORT);
airaylee 0:e26dfd507000 36 server.listen();
airaylee 0:e26dfd507000 37
airaylee 0:e26dfd507000 38 while(1) {
airaylee 0:e26dfd507000 39 printf("\nWait for new connection...\r\n");
airaylee 1:788d545c9cd1 40 TCPSocketConnection client; //wait for a TCP connection
airaylee 0:e26dfd507000 41 server.accept(client);
airaylee 1:788d545c9cd1 42 client.set_blocking(false); // Non-blocking
airaylee 0:e26dfd507000 43
airaylee 0:e26dfd507000 44 printf("Connection from: %s\r\n", client.get_address());
airaylee 1:788d545c9cd1 45 //Update the sensor
airaylee 0:e26dfd507000 46 busy = true;
airaylee 0:e26dfd507000 47 sensor.update();
airaylee 0:e26dfd507000 48 busy = false;
airaylee 1:788d545c9cd1 49 //Initialize the buffer to send
airaylee 0:e26dfd507000 50 char buffer[256]={0};
airaylee 0:e26dfd507000 51
airaylee 1:788d545c9cd1 52 // Temperature in celcius and Humidity in percent
airaylee 0:e26dfd507000 53 sensor.setScale(false);
airaylee 0:e26dfd507000 54 sprintf(buffer,"Temperature [ %3.2f C ]\r\nHumdity [ %3.2f %% ]\r\n\n", sensor.getTemperature(), sensor.getHumidity());
airaylee 1:788d545c9cd1 55 //Send the data
airaylee 0:e26dfd507000 56 int n = sizeof(buffer);
airaylee 0:e26dfd507000 57 client.send_all(buffer, n);
airaylee 1:788d545c9cd1 58 //close the connection and wait for new connection
airaylee 0:e26dfd507000 59 client.close();
airaylee 0:e26dfd507000 60 printf(" Connection over\r\n");
airaylee 0:e26dfd507000 61
airaylee 0:e26dfd507000 62 }
airaylee 0:e26dfd507000 63 }