Version 8, working version with Alix, sams and ollies code. Displays time, date and sensor info onto terminal, LCD and networking, and saves onto SD card.

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

Files at this revision

API Documentation at this revision

Comitter:
Alix955
Date:
Mon Dec 31 19:20:22 2018 +0000
Parent:
11:42b0c567cc8c
Commit message:
Version 8, integration of Alix & Sams work with older(?) version of ollies. Displays time, date and all sensor information onto LCD, Terminal and Networking, and saves sensor info to SD card.

Changed in this revision

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
SDcard.hpp Show annotated file Show diff for this revision Revisions of this file
Sampler.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
main.cpp Show annotated file Show diff for this revision Revisions of this file
messageStruct.hpp 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/LCD.hpp	Thu Dec 13 15:46:07 2018 +0000
+++ b/LCD.hpp	Mon Dec 31 19:20:22 2018 +0000
@@ -5,8 +5,7 @@
 #include "sample_hardware.hpp"
 #include "Network.hpp"
 #include <stdio.h>
-#include <string.h>
-
+#include <string>
 
 
 class LCD_Data
@@ -17,27 +16,29 @@
          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;
-         string day;
-         string month;
-         string date;
-         string time;
-         string year;
+         
+         int flip; //integer used to flip the bottom line of LCD
          
+         string day; //string containing the current day when pulled from NTP server
+         string month; //string containing the current month when pulled from NTP server
+         string date; //string containing the current date when pulled from NTP server
+         string time; //string containing the current time when pulled from NTP server
+         string year; //string containing the current year when pulled from NTP server
+
 
      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
+         void update_temp(double t) //used to update the current temperature value with an input
             {
-                temp = t;
+                temp = t; //sets private variable temp
             } 
-        void update_pressure(double p) //use this function to update the current pressure value
+        void update_pressure(double p) //used to update the current pressure value with an input
             {
-                pressure = p;    
+                pressure = p; 
             } 
-        void update_LDR(double L)
+        void update_LDR(double L) //used to update the LDR value with an input
             {
                 fLDR = L;   
             }
@@ -46,33 +47,33 @@
     
     public:
     
-    EventQueue LCD_Queue;  //create an event queue for main
+    EventQueue LCD_Queue;  //create an event queue for main to run
     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;
+        flip = 1; //initalize what state the bottom line starts on
+        temp = 0; //set temperature to start at 0
+        pressure = 0; //set pressure to start at 0
+        fLDR = 0; //set LDR value to start at 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
+    void update_sensor_info(sample_message msg) //updates all current sensor information. recieves sensor info in structure type sample_message and takes it apart
     {
-         update_temp(msg.temp);    // Include message class passing of data
-         update_pressure(msg.pressure);
-         update_LDR(msg.ldr);
+         update_temp(msg.temp);    // takes temperature from sample_message and feeds to update temp function
+         update_pressure(msg.pressure); //takes pressure from sample_message and feeds to update pressure function
+         update_LDR(msg.ldr); //takes the LDR value from sample_message and feeds to update LDR function
     }
     
 
     
     
-    void display_LCD() //updates the current LCD display with the new sensor information
+    void display_LCD() //updates the current LCD display with the new sensor information, and flips bottom line of LCD
         {
          lcd.cls(); //clear current LCD display
          
@@ -82,38 +83,39 @@
          switch(flip){
          case 1: 
             lcd.printf("\n%4.2f mbar", pressure); //print pressure to bottom line of LCD, 2dp mbar
-            flip = 2;
+            flip = 2; //swaps to case 2 next time function is run so LDR is printed instead
             break;  
          case 2:
-             lcd.printf("\n%4.2f Lux", fLDR); //print pressure to bottom line of LCD, 2dp mbar
-             flip = 1;   
+             lcd.printf("\n%4.2f Lux", fLDR); //print LDR value to bottom line of LCD, 2dp
+             flip = 1; //swaps to case 1 next time function is run so pressure is printed instead
             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
+         case 3: //only ever used when the interupt button is pressed to update time
+             lcd.printf("\nTime updated"); //informs the user the current time has been set by printing onto bottom line
              printf("Current time is %s\r\n", ctime(&timestamp)); //prints current time and date in human readable time to terminal
-             flip = 1;
+             flip = 1; //swaps back to case 1 next time function is run
             break;         
         default: 
-            printf("Error in LCD flip function");
+            printf("Error in LCD flip function"); //only reached if the case is set incorrectly
             break;
          }
        } 
        
        
        
-       time_and_date update_time_date (){
+       void update_time_date (){ //used to update time and date, called only when button interrupt is pressed
            
        timestamp = ntp.get_timestamp(); //reads the current time from a local NTP server in UNIX format
-        
+       
        string current_time =  ctime(&timestamp); //converts time to human readable format string
-
+        
+       printf("%s", current_time); //prints current time and date onto terminal
 
      
      day.assign(current_time, 0, 3); //extract only day from current_time string
-     month.assign(current_time, 4, 3); //extract only month from ""
-     date.assign(current_time, 9, 1); //extract only date from ""
-     time.assign(current_time, 11, 8); //extract only time from ""
-     year.assign(current_time, 20, 4); //extract only year from ""
+     month.assign(current_time, 4, 3); //extract only month from current_time string
+     date.assign(current_time, 9, 1); //extract only date from current_time string
+     time.assign(current_time, 11, 8); //extract only time from current_time string
+     year.assign(current_time, 20, 4); //extract only year from current_time string
      
      
      //printf("current day is: %s\n", day);
@@ -124,10 +126,10 @@
      
  
           
-       m_oNet.Network_Queue.call(&m_oNet, &Network::update_Time, current_time);
+       m_oNet.Network_Queue.call(&m_oNet, &Network::update_Time, current_time); //sends the whole time and date string to networking to be displayed
        
-       time_and_date Timemsg; // Define instance of message structure
-        Timemsg.day = day;
+       time_and_date Timemsg; // Define instance of message structure, used by serial communications
+        Timemsg.day = day; 
         Timemsg.month = month;
         Timemsg.date = date;
         Timemsg.time = time;
@@ -136,7 +138,7 @@
         
        flip = 3; //will tell the user that the time has been updated on next FLIP instance with the LCD
        
-       return Timemsg; //swap this for a function that sends the structure to ollie?
+     //  return Timemsg; //swap this for a function that sends the structure to ollie?
         
        }
        
--- a/Network.hpp	Thu Dec 13 15:46:07 2018 +0000
+++ b/Network.hpp	Mon Dec 31 19:20:22 2018 +0000
@@ -95,17 +95,17 @@
 
       void update_temp(double t) //use this function to update the current temperature value 
             { 
-                temp = 5; 
+                temp = t; 
             }  
 
         void update_pressure(double p) //use this function to update the current pressure value 
             { 
-                pressure = 4;     
+                pressure = p;     
             }  
 
         void update_LDR(double L) 
             { 
-                fLDR = 3;    
+                fLDR = L;    
             } 
 
              
@@ -119,7 +119,6 @@
     //Configure an ethernet connection 
     eth.set_network(IP, NETMASK, GATEWAY); 
     eth.connect(); 
-           
     } 
   
 
@@ -173,7 +172,7 @@
 
         srv.accept(&clt_sock, &clt_addr); 
 
-        printf("accept %s:%d\n", clt_addr.get_ip_address(), clt_addr.get_port()); 
+        //printf("accept %s:%d\n", clt_addr.get_ip_address(), clt_addr.get_port()); 
 
              
 
@@ -191,9 +190,7 @@
 
         char pressure_str[64]; 
 
-         
-
-     
+        //printf("%s", time);
 
         //Convert to a C String 
 
@@ -233,7 +230,8 @@
 
         //Send static HTML response (as a C string) 
 
-        clt_sock.send(response.c_str(), response.size()+6);  
+      //  clt_sock.send(response.c_str(), response.size()+6);  
+        clt_sock.send(response.c_str(), response.size());  
 
                
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SDcard.hpp	Mon Dec 31 19:20:22 2018 +0000
@@ -0,0 +1,105 @@
+#ifndef _SDCARD_ 
+#define _SDCARD_  
+#include "mbed.h" 
+#include "SDBlockDevice.h" 
+#include "FATFileSystem.h" 
+#include "sample_hardware.hpp" 
+
+  
+
+class SDcard 
+
+{     
+
+    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 = 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 SDcard_Queue; 
+    
+     SDcard(){                      //constructor, 
+
+    }    
+
+   
+
+      ~SDcard(){                      //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 Save_Data() {          
+
+      
+    // initalising the SD card 
+    if ( sd.init() != 0) {
+        printf("Init failed \n");
+        errorCode(FATAL);
+    }     
+
+    //Create a filing system for SD Card 
+
+    FATFileSystem fs("sd", &sd);
+        
+    FILE* fp = fopen("/sd/SensorData.csv","a");
+
+    if (fp == NULL) {
+        error("Could not open file for write\n");
+        errorCode(FATAL);
+    } 
+
+    //Storing sensor data in csv file 
+
+    fprintf(fp, " Temperature(C) , %4.2f , Pressure(mbar) , %4.2f , Lux , %4.2f \n", temp , pressure , fLDR );
+
+    //Close the file
+    fclose(fp); 
+
+   //Close down SD card 
+    sd.deinit(); 
+    
+    errorCode(OK);
+    
+        temp = 0;
+        pressure = 0;
+        fLDR = 0;
+        
+   } 
+}; 
+// creating the instance SD of the class SDcard 
+
+
+SDcard m_oSD; 
+
+#endif
\ No newline at end of file
--- a/Sampler.hpp	Thu Dec 13 15:46:07 2018 +0000
+++ b/Sampler.hpp	Mon Dec 31 19:20:22 2018 +0000
@@ -4,6 +4,7 @@
 #include "LCD.hpp"
 #include "SerialComms.hpp"
 #include "sample_hardware.hpp"
+#include "SDcard.hpp"
 #define Activate_Flag 1
 
 class Sampler
@@ -16,9 +17,10 @@
     void publishSample()
     {
         sample_message message = getData();
-        m_oDisplay.LCD_Queue.call(&m_oDisplay, &LCD_Data::update_sensor_info, message);
-        m_oSerial.SERIAL_Queue.call(&m_oSerial, &Serialcomms::setsampledata, message);
-        //SD_Queue.call(&m_oSD_data, &SD_Queue::update_sensor_info, sample_data, sample_data);
+        m_oDisplay.LCD_Queue.call(&m_oDisplay, &LCD_Data::update_sensor_info, message); //Send sensor information to LCD via putting update_sensor_info function onto LCD queue with structure as input
+        m_oSerial.SERIAL_Queue.call(&m_oSerial, &Serialcomms::setsampledata, message);  //Send sensor information to Serial via putting setsampledata function onto Serial queue with structure as input
+        m_oNet.Network_Queue.call(&m_oNet, &Network::update_sensor_info, message);  //Send sensor information to Network via putting update_sensor_info function onto Network queue with structure as input
+        m_oSD.SDcard_Queue.call(&m_oSD, &SDcard::update_sensor_info, message);  //Send sensor information to SD card via putting update_sensor_info function onto SD card queue with structure as input
     }
     
     sample_message getData()
--- a/SwitchManager.hpp	Thu Dec 13 15:46:07 2018 +0000
+++ b/SwitchManager.hpp	Mon Dec 31 19:20:22 2018 +0000
@@ -29,6 +29,7 @@
     }
     
     void waitForFalling() {
+        //When button is pressed, adds update_time_date function to the LCD queue
         m_oDisplay.LCD_Queue.call(&m_oDisplay, &LCD_Data::update_time_date);
         switchInterrupt.fall(NULL);
         t.attach(callback(this, &SwitchManager::waitForStabilityFalling), 0.2);
--- a/main.cpp	Thu Dec 13 15:46:07 2018 +0000
+++ b/main.cpp	Mon Dec 31 19:20:22 2018 +0000
@@ -3,11 +3,14 @@
 #include "sample_hardware.hpp"
 #include "Sampler.hpp"
 #include "SwitchManager.hpp"
+#include "SDcard.hpp"
 
 void LCD_Thread(void);
 void SAMP_Thread(void);
 void SERIAL_Thread(void);
 void Network_Thread(void);
+void SD_Thread(void);
+
 
 Thread tLCD, tSAMP, tSERIAL, tSD, tNET;
 
@@ -16,10 +19,11 @@
 
 int main()
 {  
-    tLCD.start(LCD_Thread);
-    tSAMP.start(SAMP_Thread);
-    tSERIAL.start(SERIAL_Thread);
-    tNET.start(Network_Thread);
+    tLCD.start(LCD_Thread); //start the LCD thread
+    tSAMP.start(SAMP_Thread); //start the sampling thread
+    tSERIAL.start(SERIAL_Thread); //start the serial communications thread
+    tNET.start(Network_Thread); //start the networking thread
+    tSD.start(SD_Thread); //start the SD card thread
     Thread::wait(osWaitForever);
 }
 
@@ -74,12 +78,30 @@
 }
 
 
+void SD_Thread() 
+{ 
+    while(1) 
+    { 
+         m_oSD.SDcard_Queue.call_every(5000, &m_oSD, &SDcard::Save_Data); 
+         m_oSD.SDcard_Queue.dispatch(); 
+          while(true)  
+       {   // Flash if the event queue is exited. 
+            greenLED = 1; 
+            wait(0.5); 
+            greenLED = 0; 
+            wait(0.1);     
+        } 
+    } 
+}    
+
+     
+
 
     void Network_Thread() 
 { 
     while(1) 
     { 
-         m_oNet.Network_Queue.call_every(10, &m_oNet, &Network::NetPush); 
+         m_oNet.Network_Queue.call_every(5000, &m_oNet, &Network::NetPush); 
          m_oNet.Network_Queue.dispatch(); 
 
           while(true)  
--- a/messageStruct.hpp	Thu Dec 13 15:46:07 2018 +0000
+++ b/messageStruct.hpp	Mon Dec 31 19:20:22 2018 +0000
@@ -12,7 +12,7 @@
 extern sample_message msg;
 
 
-typedef struct
+typedef struct //structure used to store time and date info that is sent between LCD class and serial communcations
 {
    string day; //current day, mon - sun
    string month; //current month, jan - dec
--- a/ntp-client.lib	Thu Dec 13 15:46:07 2018 +0000
+++ b/ntp-client.lib	Mon Dec 31 19:20:22 2018 +0000
@@ -1,1 +1,1 @@
-https://os.mbed.com/users/Alix955/code/ntp-client/#44bee8873cf0
+https://os.mbed.com/users/Alix955/code/ntp-client/#63fdce79a4fa