HTTP_SERVER
Dependencies: mbed mbed-rtos SDFileSystem EthernetInterface BME280
Diff: main.cpp
- Revision:
- 2:33833f64246f
- Parent:
- 1:baaf95f8d272
--- a/main.cpp Tue Nov 13 11:46:10 2018 +0000 +++ b/main.cpp Fri Nov 23 13:27:35 2018 +0000 @@ -1,6 +1,7 @@ #include "mbed.h" #include "SDFileSystem.h" #include "EthernetInterface.h" +#include "BME280.h" #include <iostream> #include <fstream> @@ -12,9 +13,74 @@ #define RAINFALL_LINE 20 #define TIME_LINE 21 +Serial pc(USBTX, USBRX); SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); -Serial pc(USBTX, USBRX); +BME280 sensor(PTC11, PTC10); +DigitalIn sw(PTD1); +TCPSocketServer svr; +TCPSocketConnection client; + string file_content[NUMBER_OF_LINES]; +volatile bool rain = false; + +void sendData(void); +void initializeEthernet(void); +void readHTMLCode(void); +void getWeatherConditions(void); +void initializeSerialPC(void); +void initializeTCP(void); + +int main() +{ + initializeSerialPC(); + readHTMLCode(); + initializeEthernet(); + initializeTCP(); + while(1) + { + if (svr.accept(client)) + { + pc.printf("Connection with client can not be established.\r"); + } + else + { + pc.printf("Connection with client established, client ip addres: %s.\r", client.get_address()); + wait(1); + sendData(); + client.close(); + } + //getWeatherConditions(); + + } +} + +void sendData(void) +{ + string tmp = ""; + for (int i = 0; i < NUMBER_OF_LINES; i++) + tmp += file_content[i]; + client.send(const_cast<char*>(tmp.c_str()),tmp.length()); +} + +void initializeTCP(void) +{ + if (svr.bind(80) < 0) + { + pc.printf("Server TCP cannot bound with port 80(HTTP).\r"); + return; + } + else + pc.printf("Server TCP bound with port 80(HTTP).\r"); + if (svr.listen(1) < 0) + { + pc.printf("Server TCP cannot listen incominng conections.\r"); + return; + } + else + { + pc.printf("Server TCP started listen incominng conections.\r"); + } +} void initializeEthernet(void) { @@ -61,16 +127,31 @@ } } +void getWeatherConditions(void) +{ + float temperature; + float humidity; + float pressure; + pressure = sensor.getPressure(); + temperature = sensor.getTemperature(); + humidity = sensor.getHumidity(); + pc.printf("%2.2f degC, %04.2f hPa, %2.2f %%\r", temperature, pressure, humidity); + + if (sw == 1) + { + pc.printf("Rain!\r"); + rain = true; + } + else + { + pc.printf("No rain!\r"); + rain = false; + } +} + void initializeSerialPC(void) { pc.baud(115200); pc.printf("Serial port initialized. \r"); } -int main() -{ - initializeSerialPC(); - readHTMLCode(); - initializeEthernet(); - while(1); -}