Repo. for the ELEC351 Coursework - Oliver Thompson

Dependencies:   BMP280 ELEC350-Practicals-FZ429- TextLCD watchdog_RTOS BME280 ntp-client

Network.hpp

Committer:
O_Thom
Date:
2019-01-02
Revision:
20:2ce28a5032d5
Parent:
19:c3b396b65f2a

File content as of revision 20:2ce28a5032d5:

#ifndef _NETWORK_ 

#define _NETWORK_ 

#include "mbed.h" 
#include "FATFileSystem.h" 
#include "sample_hardware.hpp" 
#include "EthernetInterface.h" 
#include "TCPServer.h" 
//#include "messageStruct.hpp" 
//#include "LCD.hpp"
#include "TCPSocket.h" 
#include <iostream> 
#include <string>  
#include "ntp-client/NTPClient.h"

#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 id=\"Sensor Data\" name=\"Sensor Date\" style=\"margin:auto\">" "\r\n"        \
"      <h1> <b>Sensor Data</b> </h1>" "\r\n"                     \
"      <p>The LDR value is "                                    

#define HTTP_MESSAGE_BODY2 ""                                    \
       " Lux </p>" "\r\n"                                        \
"    <div style=\"margin:auto\">" "\r\n"                         \
"      <p>The Temperature value is "     

#define HTTP_MESSAGE_BODY3 ""                                    \
       " Degrees</p>" "\r\n"                                     \
"    <div style=\"margin:auto\">" "\r\n"                         \
"      <p>The Pressure value is "       

#define HTTP_MESSAGE_BODY4 ""                                    \
       " mbar </p>" "\r\n"                                     \
"    <div style=\"margin:auto\">" "\r\n"                         \
"      <p>The current Time is  "       

#define HTTP_MESSAGE_BODY5 ""                                    \
       " </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"
#define NETMASK   "255.0.0.0"
#define GATEWAY   "10.0.0.2"
EthernetInterface eth; 
NTPClient ntp(&eth);   


Mail<time_and_date,5> Serial2Net;           // Serial -> Net Mailbox 5 Elements Wide Time&Date


class Network 
{ 
//friend class LCD_Data;
    private: 
     double temp;       //current temperature of sensor 
     double pressure;  //current pressure of sensor 
     double fLDR;      //current light level from LDR 
     string time;
        
         void update_temp(double t) //use this function to update the current temperature value
                {
                        temp = t;
                } 
        void update_pressure(double p) //use this function to update the current pressure value
                {
                        pressure = p;    
                } 
        void update_LDR(double L)
                {
                        fLDR = L;   
                }
             
    public: 
    EventQueue Network_Queue; 



    Network(){     //constructor  

    //Configure an ethernet connection 
    eth.set_network(IP, NETMASK, GATEWAY); 
    eth.connect(); 
      
    } 
  

    ~Network(){     //deconstructor  
    } 

        void getSerial2Net()    // Queue Consumer
        {
            if (!Serial2Net.empty())
            {
                osEvent evt = Serial2Net.get(); 
        switch (evt.status) 
                {
                    case osEventMail:
                //Normal status
                                time_and_date *time_copy = (time_and_date*)evt.value.p; 
                                time = time_copy->current_time; // Update the internal clock
                                Serial2Net.free(time_copy); // Free up the space in the memory Pool
                    default:
                //All other errors (see cmsis_os.h for meaning of error code)
                //printf("Serial2Net->get() returned %02x status\n\r", evt.status);
                }               
            }
        }


    void update_sensor_info(sample_message msg) //updates all current sensor information, this is called by a ticker every 5 seconds to read from the mailbox
    { 
         update_temp(msg.temp);                  // Include message class passing of data 
         update_pressure(msg.pressure); 
         update_LDR(msg.ldr); 
    } 

     
    void update_Time(string tm)
    {
        time = tm;
    }
     

    void NetPush(){ 
  //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); 

      //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()); 

             

        //Uses a C++ string to make it easier to concatinate 

        string response; 

         

        //This is a C string 

        char ldr_str[64]; 

        char temp_str[64]; 

        char pressure_str[64]; 

         

     

        //Convert to a C String 

        sprintf(ldr_str, "%5.3f", fLDR ); 

        sprintf(temp_str, "%5.3f", temp ); 

        sprintf(pressure_str, "%5.3f", pressure ); 

         

         
        //Build the C++ string response 

        response = HTTP_MESSAGE_BODY1; 

        response += ldr_str; 

        response += HTTP_MESSAGE_BODY2; 

        response += temp_str; 

        response += HTTP_MESSAGE_BODY3; 

        response += pressure_str; 

        response += HTTP_MESSAGE_BODY4; 

        response += time;
        
        response += HTTP_MESSAGE_BODY5; 

        //Send static HTML response (as a C string) 

        clt_sock.send(response.c_str(), response.size());  
        } 

}; 

     

  

Network m_oNet; 

#endif