SEND

Fork of Final351CW_FINAL by Liam Grazier

Network/network.cpp

Committer:
liam_grazier
Date:
2018-01-09
Revision:
10:098c2fa0a1a6
Parent:
8:582ac4c5a524

File content as of revision 10:098c2fa0a1a6:

/*   ELEC351 COURSEWORK 2018 
DESIGNED USING MBED ONLINE COMPILER IMPORTED TO KEIL
LIAM GRAZIER // DOUG TILLEY // ALEX BARON 
 */
#if !FEATURE_LWIP
    #error [NOT_SUPPORTED] LWIP not supported for this target
#endif
#include "mbed.h"
#include "EthernetInterface.h"
#include "TCPServer.h"
#include "TCPSocket.h"
char realtime[32]; //char for storing the RTC
#include <iostream>
#include <string> 
#include "BMP280.h"
#include "components.hpp"
#include "network.hpp"
Mutex Net;  //mutex lock for the network.
#define HTTP_STATUS_LINE "HTTP/1.0 200 OK"
#define HTTP_HEADER_FIELDS "Content-Type: text/html; charset=utf-8"
#define HTTP_MESSAGE_BODY1 ""                                    \
"<html>" "\r\n"                                                  \
"  <body style=\"display:flex;text-align:center\">" "\r\n"       \
"    <div style=\"margin:auto\">" "\r\n"                         \
"      <h1>Sensor Data</h1>" "\r\n"                              \
"      <p>Light Level LDR value: " "\r\n" 
                 
#define HTTP_Pressure1 ""                                        \
"<html>" "\r\n"                                                  \
"  <body style=\"display:flex;text-align:center\">" "\r\n"       \
"    <div style=\"margin:auto\">" "\r\n"                         \
"      <h1> </h1>" "\r\n"                                        \
"      <p>Pressure(mbar): " "\r\n"                

#define HTTP_Pressure2 ""                                        \
       "</p>" "\r\n"                                             \
"    </div>" "\r\n"                                              \
"  </body>" "\r\n"                                               \
"</html>"
#define HTTP_TIME ""                                     \
"<html>" "\r\n"                                                  \
"  <body style=\"display:flex;text-align:center\">" "\r\n"       \
"    <div style=\"margin:auto\">" "\r\n"                         \
"      <h1> </h1>" "\r\n"                                        \
"      <p>Time: " "\r\n"       
                 
#define HTTP_Temperature1 ""                                     \
"<html>" "\r\n"                                                  \
"  <body style=\"display:flex;text-align:center\">" "\r\n"       \
"    <div style=\"margin:auto\">" "\r\n"                         \
"      <h1> </h1>" "\r\n"                                        \
"      <p>Temperature(Degrees Celcius): " "\r\n"       

#define HTTP_Temperature2 ""                                     \
       "</p>" "\r\n"                                             \
"    </div>" "\r\n"                                              \
"  </body>" "\r\n"                                               \
"</html>"
#define HTTP_MESSAGE_TIME ""                                    \
       "</p>" "\r\n"                                             \
"    </div>" "\r\n"                                              \
"  </body>" "\r\n"                                               \
"</html>"
#define HTTP_MESSAGE_BODY2 ""                                    \
       "</p>" "\r\n"                                             \
"    </div>" "\r\n"                                              \
"  </body>" "\r\n"                                               \
"</html>"


#define HTTP_RESPONSE HTTP_STATUS_LINE "\r\n"   \
                      HTTP_HEADER_FIELDS "\r\n" \
                      "\r\n"                    \
                      HTTP_MESSAGE_BODY "\r\n"

#define IP        "10.0.0.10" //ipaddress
#define NETMASK   "255.0.0.0" //subnetmask
#define GATEWAY   "10.0.0.1"  //defaultgateway
void dispstralltime() //function for getting time from the RTC
{
    time_t seconds = time(NULL);
    strftime(realtime, 32, "%c\n\r", localtime(&seconds)); //realtime value = yyyy/mm/dd/hh/mm/ss
}
void networksend(void)
{   
    // interrupt routine setup
    Net.lock();
    printf("Network Enabled\n\r");//here to show the user in terminal network is initalised 
    //Configure an ethernet connection
    EthernetInterface eth;
    eth.set_network(IP, NETMASK, GATEWAY);
    eth.connect();
    Net.unlock();
    //Now setup a web server
    TCPServer srv;           //TCP/IP Server
    TCPSocket clt_sock;      //Socket for communication
    SocketAddress clt_addr;  //Address of incoming connection
    /* Open the server on ethernet stack */
    srv.open(&eth);
    /* Bind the HTTP port (TCP 80) to the server */
    srv.bind(eth.get_ip_address(), 80);
    /* Can handle 5 simultaneous connections */
    srv.listen(5); 
    while (true) 
    {
        Net.lock();
        using namespace std;
        //Block and wait on an incoming connection
        srv.accept(&clt_sock, &clt_addr);
        //printf("accept %s:%d\n", clt_addr.get_ip_address(), clt_addr.get_port());
        double temp = sensor.getTemperature();
        double pres = sensor.getPressure();
        //Uses a C++ string to make it easier to concatinate
        string response;
        //Chars for storing the variables
        char adcIn_str[64];
        char pres_str[64];
        char temp_str[64];
        dispstralltime();  //calltime           
        //Read the LDR value
        //declairing floats for storing data
        float u = adcIn;
        float b = pres;
        float a = temp;
        //Converterting the floats to chars ready for print
        sprintf(adcIn_str, "%5.3f", u );
        sprintf(pres_str, "%4.2f", b);
        sprintf(temp_str, "%3.1f", a);
        //Build the C++ string response
        response += HTTP_TIME;
        response += realtime; //write time
        response += HTTP_MESSAGE_BODY1;
        response += adcIn_str; //writeadc
        response += HTTP_Temperature1;
        response += temp_str; //writetemp
        response += HTTP_Temperature2;
        response += HTTP_Pressure1;
        response += pres_str; //writepressure
        response += HTTP_Pressure2;
        response += HTTP_MESSAGE_TIME;
        response += HTTP_MESSAGE_BODY2;       
        //Send static HTML response (as a C string)
        clt_sock.send(response.c_str(), response.size()+6); 
        Net.unlock();   
        Thread::signal_wait(SIG_NET);//thead signal triggered by ticket in main. 
    }
}