HTTP_SERVER
Dependencies: mbed mbed-rtos SDFileSystem EthernetInterface BME280
Diff: main.cpp
- Revision:
- 1:baaf95f8d272
- Parent:
- 0:795a02b2bb68
- Child:
- 2:33833f64246f
--- a/main.cpp Tue Nov 06 19:47:08 2018 +0000 +++ b/main.cpp Tue Nov 13 11:46:10 2018 +0000 @@ -1,39 +1,76 @@ #include "mbed.h" #include "SDFileSystem.h" +#include "EthernetInterface.h" +#include <iostream> +#include <fstream> -SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // MOSI, MISO, SCK, CS +#define NUMBER_OF_LINES 23 +#define COLOR_LINE 12 +#define TEMPERATURE_LINE 17 +#define PRESSURE_LINE 18 +#define HUMADITY_LINE 19 +#define RAINFALL_LINE 20 +#define TIME_LINE 21 + +SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); Serial pc(USBTX, USBRX); -FILE *fp; -char buffer[1024]; +string file_content[NUMBER_OF_LINES]; -int main() { - pc.printf("Initializing \n"); - wait(2); - /* - fp = fopen("/sd/hello.txt", "r"); - if (fp != NULL) { - fclose(fp); - remove("/sd/hello.txt"); - pc.printf("Remove an existing file with the same name \n"); +void initializeEthernet(void) +{ + EthernetInterface eth; + const char *ip = "192.168.1.2"; + const char *mask = "255.255.255.0"; + const char *gateway = "192.168.1.1"; + if(!eth.init(ip,mask,gateway)) + { + pc.printf("Ethernet card initialized successfully.\r"); + eth.connect(); + pc.printf("IP address: %s\n",eth.getIPAddress()); + pc.printf("Network mask: %s\n",eth.getNetworkMask()); + pc.printf("Gateway: %s\n",eth.getGateway()); + pc.printf("MAC address: %s\n",eth.getMACAddress()); } + else + pc.printf("Ethernet card could not be initialized.\r"); + return; +} - printf("\nWriting data to the sd card \n"); - fp = fopen("/sd/hello.txt", "w"); - if (fp == NULL) { - pc.printf("Unable to write the file \n"); - } else { - fprintf(fp, "mbed SDCard application!"); - fclose(fp); - pc.printf("File successfully written! \n"); +void readHTMLCode(void) +{ + fstream file; + int cnt = 0; + for (int i = 0; i < NUMBER_OF_LINES; i++) + file_content[i] = " "; + file.open("/sd/index.html", ios::in); + if (file.good()) + { + pc.printf("The index.html file has been opened. \r"); + while(!file.eof()) + { + getline(file,file_content[cnt]); + cnt++; + } + file.close(); + pc.printf("The file has been closed. \r"); } + else + { + pc.printf("The file could not be opened. \r"); + return; + } +} - printf("\nReading data from the SD card. \n"); - fp = fopen("/sd/hello.txt", "r"); - if (fp != NULL) { - int size = fread(buffer, sizeof(char), 1024, fp); - printf("Number of data read: %d, text from hello.txt file: %s \n", size, buffer); - fclose(fp); - } - printf("End of Lab 4. \n"); - */ +void initializeSerialPC(void) +{ + pc.baud(115200); + pc.printf("Serial port initialized. \r"); } + +int main() +{ + initializeSerialPC(); + readHTMLCode(); + initializeEthernet(); + while(1); +}