Version 5

Dependencies:   BMP280 TextLCD BME280

Network.hpp

Committer:
Alix955
Date:
2018-12-07
Revision:
9:f5eae5211225

File content as of revision 9:f5eae5211225:

#ifndef _NETWORK_ 

#define _NETWORK_ 

#include "mbed.h" 

#include "FATFileSystem.h" 

#include "sample_hardware.hpp" 

#include "EthernetInterface.h" 

#include "TCPServer.h" 

//#include "messageStruct.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 style=\"margin:auto\">" "\r\n"                         \ 
"      <h1>Alright sunshine</h1>" "\r\n"                              \ 
"      <p>The LDR value is "                                    

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

#define HTTP_MESSAGE_BODY3 ""                                    \ 
"<html>" "\r\n"                                                  \ 
"  <body style=\"display:flex;text-align:center\">" "\r\n"       \ 
"    <div style=\"margin:auto\">" "\r\n"                         \ 
"      <p>The Temperature value is "     

#define HTTP_MESSAGE_BODY4 ""                                    \ 
       "</p>" "\r\n"                                             \ 
"    </div>" "\r\n"                                              \ 
"  </body>" "\r\n"                                               \ 
"</html>" 

#define HTTP_MESSAGE_BODY5 ""                                    \ 
"<html>" "\r\n"                                                  \ 
"  <body style=\"display:flex;text-align:center\">" "\r\n"       \ 
"    <div style=\"margin:auto\">" "\r\n"                         \ 
"      <p>The Pressure value is "       

#define HTTP_MESSAGE_BODY6 ""                                    \ 
       "</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);   


  

class Network 

{ 

    private: 

     

     float temp;       //current temperature of sensor 

     float pressure;  //current pressure of sensor 

     float fLDR;      //current light level from LDR 

      

      void update_temp(double t) //use this function to update the current temperature value 

            { 

                temp = 5; 

            }  

        void update_pressure(double p) //use this function to update the current pressure value 

            { 

                pressure = 4;     

            }  

        void update_LDR(double L) 

            { 

                fLDR = 3;    

            } 

             

     

    public: 

    EventQueue Network_Queue; 

    //EthernetInterface eth; 

    //NTPClient ntp(&eth);   




    Network(){     //constructor  

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

  

  

    ~Network(){     //deconstructor  

  

    } 

  

  

      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 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 = HTTP_MESSAGE_BODY3; 

        response += temp_str; 

        response += HTTP_MESSAGE_BODY4; 

         

         response += HTTP_MESSAGE_BODY5; 

        response += pressure_str; 

        response += HTTP_MESSAGE_BODY6; 

              

         

         

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

        clt_sock.send(response.c_str(), response.size()+6);  

               

         

        } 

}; 

     

  

Network m_oNet; 

#endif