Version 5

Dependencies:   BMP280 TextLCD BME280

Files at this revision

API Documentation at this revision

Comitter:
Alix955
Date:
Fri Dec 07 13:24:50 2018 +0000
Parent:
8:7d218affea71
Commit message:
Version 5, mine & sams versions merged

Changed in this revision

ELEC350-Practicals-FZ429.lib Show annotated file Show diff for this revision Revisions of this file
Initialization.hpp Show annotated file Show diff for this revision Revisions of this file
LCD.hpp Show annotated file Show diff for this revision Revisions of this file
Network.hpp Show annotated file Show diff for this revision Revisions of this file
SwitchManager.hpp Show annotated file Show diff for this revision Revisions of this file
TextLCD.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
ntp-client.lib Show annotated file Show diff for this revision Revisions of this file
--- a/ELEC350-Practicals-FZ429.lib	Fri Nov 30 13:15:30 2018 +0000
+++ b/ELEC350-Practicals-FZ429.lib	Fri Dec 07 13:24:50 2018 +0000
@@ -1,1 +1,1 @@
-https://os.mbed.com/teams/University-of-Plymouth-Stage-2-and-3/code/ELEC350-Practicals-FZ429/#df979097cc71
+https://os.mbed.com/users/Alix955/code/ELEC350-Practicals-FZ429-new/#ca8090c7868e
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Initialization.hpp	Fri Dec 07 13:24:50 2018 +0000
@@ -0,0 +1,26 @@
+#ifndef __Initialization__
+#define __Initialization__
+
+#include "mbed.h"
+#include "EthernetInterface.h"
+#include "ntp-client/NTPClient.h"
+
+
+#define IP        "10.0.0.10" 
+
+#define NETMASK   "255.0.0.0" 
+
+#define GATEWAY   "10.0.0.2" 
+
+
+EthernetInterface eth; 
+
+eth.set_network(IP, NETMASK, GATEWAY); 
+
+eth.connect(); 
+    
+NTPClient ntp(&eth);
+
+
+
+#endif
\ No newline at end of file
--- a/LCD.hpp	Fri Nov 30 13:15:30 2018 +0000
+++ b/LCD.hpp	Fri Dec 07 13:24:50 2018 +0000
@@ -3,14 +3,24 @@
 #include "mbed.h"
 #include "messageStruct.hpp"
 #include "sample_hardware.hpp"
+#include "Network.hpp"
+
+
 class LCD_Data
     {
-     private:  //private variables can only be changed via functions in this function
+     friend class Network;
+     
+     private:  //private variables can only be changed via functions in this class
          float temp;       //current temperature of sensor, updated every 15 seconds
          float pressure;  //current pressure of sensor, updated every 15 seconds
          float fLDR;      //current light level from LDR, updated every 15 seconds
          int flip;
          
+
+     private:
+         struct tm Time_Date; //decale instance Time_Date of structure tm which is defined by mbed / C
+        
+         
          void update_temp(double t) //use this function to update the current temperature value
             {
                 temp = t;
@@ -24,24 +34,43 @@
                 fLDR = L;   
             }
     
+    
+    
     public:
+    
     EventQueue LCD_Queue;  //create an event queue for main
+    time_t timestamp; //current time in format of unix time, can be converted to DAY_OF_WEEK MONTH DAY HOUR:MINUTE:SECOND YEAR using ctime(&timestamp);
+    
+    
+    
     LCD_Data(){ //constructor, initializes the FLIP variable for use in toggling the bottom line of the LCD
         flip = 1;
         temp = 0;
         pressure = 0; 
         fLDR = 0;
+
+
     }   
+    
+    
+    
     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 display_LCD() //updates the current LCD display with the new sensor information
         {
          lcd.cls(); //clear current LCD display
+         
          lcd.printf("%4.2fC", temp); //print temperature to the top line of LCD, 2dp celcius
+         
+         
          switch(flip){
          case 1: 
             lcd.printf("\n%4.2f mbar", pressure); //print pressure to bottom line of LCD, 2dp mbar
@@ -50,13 +79,34 @@
          case 2:
              lcd.printf("\n%4.2f Lux", fLDR); //print pressure to bottom line of LCD, 2dp mbar
              flip = 1;   
-            break;           
+            break;  
+         case 3: //only ever called when the interupt button is pressed to update time
+             lcd.printf("\nTime updated"); //informs the user the current time has been set
+             printf("Current time is %s\r\n", ctime(&timestamp)); //prints current time and date in human readable time to terminal
+             flip = 1;
+            break;         
         default: 
             printf("Error in LCD flip function");
             break;
          }
        } 
-    }; 
+       
+       
+       
+       void update_time_date (){
+        timestamp = ntp.get_timestamp();
+       //timestamp = Network::ntp.get_timestamp(); //gets current time and date from NTP server in unix format
+      // timestamp = m_oNet.ntp.get_timestamp(); //gets current time and date from NTP server in unix format
+       
+       flip = 3; //will tell the user that the time has been updated on next FLIP instance
+       
+       }
+       
+       
+    };  //end of LCD class
+    
+    
+    
 // Define the member object for the LCD
 LCD_Data m_oDisplay;
 #endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Network.hpp	Fri Dec 07 13:24:50 2018 +0000
@@ -0,0 +1,301 @@
+#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
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SwitchManager.hpp	Fri Dec 07 13:24:50 2018 +0000
@@ -0,0 +1,57 @@
+#ifndef _SWITCHMANAGER_
+#define _SWITCHMANAGER_
+
+#include "mbed.h"
+#include "sample_hardware.hpp"
+#include "LCD.hpp"
+
+InterruptIn sw1(PE_12);
+
+//This class manages an Interrupt attached to a button
+//It automatically manages the switch-debounce using edge detection and timers
+
+class SwitchManager {
+private:
+// enum State {LOW, LOW_DEBOUNCE, HIGH, HIGH_DEBOUNCE};  
+    InterruptIn& switchInterrupt;
+    Timeout t;
+    
+    void waitForRising() {
+        //Turn off interrupt
+        switchInterrupt.rise(NULL);
+        //Turn on timer
+        t.attach(callback(this, &SwitchManager::waitForStabilityRising), 0.2);
+    }
+    
+    void waitForStabilityRising() {
+        //Look for falling edge
+        switchInterrupt.fall(callback(this, &SwitchManager::waitForFalling));
+    }
+    
+    void waitForFalling() {
+        m_oDisplay.LCD_Queue.call(&m_oDisplay, &LCD_Data::update_time_date);
+        switchInterrupt.fall(NULL);
+        t.attach(callback(this, &SwitchManager::waitForStabilityFalling), 0.2);
+    }
+    
+    void waitForStabilityFalling() {
+        //Look for rising edge
+        switchInterrupt.rise(callback(this, &SwitchManager::waitForRising));
+    }
+    
+public:
+    SwitchManager(InterruptIn& intIn) : switchInterrupt(intIn) {
+        //Listen for rising edge
+        switchInterrupt.rise(callback(this, &SwitchManager::waitForRising));
+    }
+    ~SwitchManager() {
+        //Turn off LED and shut off any ISRs
+        switchInterrupt.rise(NULL);
+        switchInterrupt.fall(NULL);
+        t.detach();
+    }
+};
+     
+SwitchManager sm1(sw1);       
+
+#endif
\ No newline at end of file
--- a/TextLCD.lib	Fri Nov 30 13:15:30 2018 +0000
+++ b/TextLCD.lib	Fri Dec 07 13:24:50 2018 +0000
@@ -1,1 +1,1 @@
-https://os.mbed.com/users/simon/code/TextLCD/#308d188a2d3a
+https://os.mbed.com/users/Alix955/code/TextLCD/#fa2c79151a92
--- a/main.cpp	Fri Nov 30 13:15:30 2018 +0000
+++ b/main.cpp	Fri Dec 07 13:24:50 2018 +0000
@@ -1,6 +1,8 @@
 #include "mbed.h"
+#include "ntp-client/NTPClient.h"
 #include "sample_hardware.hpp"
 #include "Sampler.hpp"
+#include "SwitchManager.hpp"
 
 void LCD_Thread(void);
 void SAMP_Thread(void);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ntp-client.lib	Fri Dec 07 13:24:50 2018 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/Alix955/code/ntp-client/#099750f42b02