Using Ethernet Interface to send temperature/humidity info to client
Dependencies: EthernetInterface SDFileSystem SHTx mbed-rtos mbed
main.cpp
00001 00002 #include "mbed.h" 00003 #include "SHTx/sht15.hpp" 00004 #include "SDFileSystem.h" 00005 #include "EthernetInterface.h" 00006 00007 //SPI-mosi,miso,sclk,DigitalOut-cs, name used to access the filesystem 00008 //For further update 00009 SDFileSystem sd(p5, p6, p7, p8, "sd1"); 00010 00011 //Terminal Port to display the file back 00012 Serial pc(USBTX, USBRX); 00013 DigitalOut busy(LED1); 00014 00015 // Use p28-sda--data,p27-scl--sck 00016 SHTx::SHT15 sensor(p28, p27); 00017 00018 EthernetInterface eth; 00019 #define ECHO_SERVER_PORT 7 00020 00021 int main() { 00022 00023 // Speed things up a bit. 00024 sensor.setOTPReload(false); 00025 sensor.setResolution(true); 00026 00027 eth.init(); //Use DHCP 00028 eth.connect(7000);//Longer timeout here 00029 //print out the MAC address first 00030 printf("MAC Address is %s\r\n", eth.getMACAddress()); 00031 printf("IP Address is %s\r\n", eth.getIPAddress()); 00032 //IP address is 130.207.234.205 00033 // Declare the TCP server 00034 TCPSocketServer server; 00035 server.bind(ECHO_SERVER_PORT); 00036 server.listen(); 00037 00038 while(1) { 00039 printf("\nWait for new connection...\r\n"); 00040 TCPSocketConnection client; //wait for a TCP connection 00041 server.accept(client); 00042 client.set_blocking(false); // Non-blocking 00043 00044 printf("Connection from: %s\r\n", client.get_address()); 00045 //Update the sensor 00046 busy = true; 00047 sensor.update(); 00048 busy = false; 00049 //Initialize the buffer to send 00050 char buffer[256]={0}; 00051 00052 // Temperature in celcius and Humidity in percent 00053 sensor.setScale(false); 00054 sprintf(buffer,"Temperature [ %3.2f C ]\r\nHumdity [ %3.2f %% ]\r\n\n", sensor.getTemperature(), sensor.getHumidity()); 00055 //Send the data 00056 int n = sizeof(buffer); 00057 client.send_all(buffer, n); 00058 //close the connection and wait for new connection 00059 client.close(); 00060 printf(" Connection over\r\n"); 00061 00062 } 00063 }
Generated on Sat Jul 30 2022 00:11:06 by 1.7.2