Geolocation through Ethernet

Dependencies:   mbed HTTPClient MMA8452Q mbed-rtos 4DGL-uLCD-SE NTPClient SDFileSystem1 EthernetInterface

Files at this revision

API Documentation at this revision

Comitter:
stephenb
Date:
Fri Dec 18 19:36:51 2020 +0000
Parent:
2:bab6baa53f21
Commit message:
First Final Version

Changed in this revision

MMA8452Q.lib Show annotated file Show diff for this revision Revisions of this file
alarm.h 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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MMA8452Q.lib	Fri Dec 18 19:36:51 2020 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/teams/EE3023-Project/code/MMA8452Q/#98d418a0bfc1
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/alarm.h	Fri Dec 18 19:36:51 2020 +0000
@@ -0,0 +1,73 @@
+#include "mbed.h"
+#include "EthernetInterface.h"
+#include "NTPClient.h"
+#include "uLCD_4DGL.h"
+
+#define VOLUME 1
+// Parameters
+int port_number = 123;
+
+// Networking
+EthernetInterface eth;
+NTPClient ntpClient;
+
+//Pins
+PwmOut pwm_pin(p21);
+DigitalIn button(p12);
+BusOut LEDS(LED1,LED2,LED3,LED4);
+uLCD_4DGL uLCD(p9,p10,p11);
+
+// Plays a sound with the defined frequency, duration, and volume
+void playNote(float frequency, float duration, float volume) {
+    pwm_pin.period(1.0/frequency);
+    pwm_pin = volume/2.0;
+    wait(duration);
+    pwm_pin = 0.0;
+}
+
+void soundalarm()
+{
+    time_t ctTime; 
+    eth.init();
+    eth.connect();
+    
+    char* domainName="0.uk.pool.ntp.org";
+    //Initilaise LCD
+    uLCD.baudrate(115200);
+
+    // Read time from server
+    ntpClient.setTime(domainName,123,0x00005000);
+    //Buffers for holding time when the alarm begins and ends
+    char buffer1[80];
+    char buffer2[80];
+    wait(2);
+    eth.disconnect();
+    ctTime = time(NULL); 
+    strftime(buffer1, 80, "    %a %b %d\n    %T %p %z\n    %Z\n", \
+                                                localtime(&ctTime));
+    // Loop
+    while((button==1))
+     {   
+    playNote(960,1.2,VOLUME);
+    LEDS=0x0F;
+    wait(0.1);
+    playNote(770,1.2,VOLUME);
+    LEDS=0;
+    }
+    uLCD.locate(0,12);
+    uLCD.printf("Alarm Began On:\n");
+    uLCD.printf("    UTC/GMT:\n%s", buffer1); 
+    ctTime = time(NULL); 
+    strftime(buffer2, 80, "    %a %b %d\n    %T %p %z\n    %Z\n", \
+                                                localtime(&ctTime));
+    uLCD.printf("Alarm Stopped On:\n");
+    uLCD.printf("    UTC/GMT:\n%s", buffer2);
+    wait(30);
+    uLCD.cls();                                   
+}
+
+
+
+
+
+
--- a/main.cpp	Thu Dec 17 18:50:20 2020 +0000
+++ b/main.cpp	Fri Dec 18 19:36:51 2020 +0000
@@ -1,32 +1,94 @@
 #include "mbed.h"
 #include "NTPClient.h"
-#include "uLCD_4DGL.h"
 #include "EthernetInterface.h"
 #include "HTTPClient.h"
 #include "SDFileSystem.h"
-//Geolocation output to LCD and SD card reader
-//Code based on: https://os.mbed.com/users/tlisowski3/notebook/geolocation-and-ntp-clock-on-ulcd-144-g2/
+#include "MMA8452Q.h"
+#include "alarm.h"
 
-//Set up Networking, LCD and SD Card reader
-uLCD_4DGL uLCD(p9,p10,p11); // serial tx, serial rx, reset pin;
-SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card, SPI pins
-EthernetInterface eth;
-NTPClient ntpClient;
+//Set up 
+SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card
+MMA8452Q accel(p28, p27, 0x1D); //Accelerometer
+InterruptIn ain1(p16); //Temperature Sensor Interrupt
+AnalogIn ain(p15); //Temperature Sensor input
 HTTPClient httpClient;
+Thread thread; //Temperature check thread
+Thread accelerometer; //Accelerometer check thread
+Timer Clock; //Timer for timestamps
+
 
 void parse(char buffer[], int *j, char *string); //FUNCTION TO PARSE HTTP GET DATA
 void sdlog(int type, float time, float input0, char input1[], char input2[], char input3[]); //Function to write to the SD card
 char httpGetData[200]; //BUFFER TO HOLD DATA FROM HTTP GET REQUEST
- 
+int location(); //Function to get the location data
+void temperature(); //Outputs the temeprature to LCD and SD card
+void tempchange_thread(); //Temperature function to check value exceeds limits
+void accel_thread(); //Function to measure the accelerometer
+
 int main() {
+    Clock.start(); //Start timer
+    accel.init(); //initalize accelerometer
+    thread.start(tempchange_thread); //Start temperature check thread
+    accelerometer.start(accel_thread); //Start accelerometer thread
+    ain1.rise(tempchange_thread);
+    ain1.fall(tempchange_thread);
+
+
+
     //Display Settings
     uLCD.baudrate(115200); //Baudrate of display
     uLCD.cls();    //Clear uLCD screen
-    uLCD.background_color(BLACK); //SET BACKGROUND COLOR TO WHITE
-    uLCD.color(WHITE);
-    uLCD.locate(0,0);  //Start printing on col0, row0
-    uLCD.printf("Getting Location \nInformation...");
+    uLCD.background_color(BLACK); //Background colour
+    uLCD.color(WHITE);  //Text colour
+    
+    
+    while(true){ //Loop to control timing of temperature and location updates
+        location();
+        for (int k = 0; k < 20; k++){ //Temperature fcn runs every 30 seconds, location fcn runs every 10 minutes
+            temperature();  
+            wait(30); //waits 30 seconds before running the fcn again
+            } 
+        }
+}
+
 
+void parse(char buffer[], int *j, char *string) {
+//extracts next location string data item from buffer
+    int i=0;
+    for (i=0; i<=strlen(buffer); i++) {  //TOTAL SIZE OF RETURNED DATA
+        if ((buffer[*j+i] == ',')||(buffer[*j+i] == '\0' )) { //IF comma or end of string
+            //comma is the string field delimiter
+            string[i]=0; //SETS END OF STRING TO 0
+            *j=*j+i+1; //UPDATES to 1 after comma seperated value
+            break;
+        } else string[i]=buffer[*j+i]; //Keep adding to the string
+    }
+}
+
+void sdlog(int type, float time, float input0, char input1[], char input2[], char input3[]){
+     FILE *file;
+    switch (type){
+    case 0:
+            file = fopen("/sd/location.txt", "w");
+        fprintf(file, "TIME: %f, Latitude: %s Longitude: %s, City: %s\n\r", time, input1, input2, input3);
+        fclose(file);
+        break;
+    case 1:
+            file = fopen("/sd/temperature.txt", "w");
+        fprintf(file, "TIME: %f, %f degC\n\r", time, input0);
+        fclose(file);
+        break;
+    case 2:
+            file = fopen("/sd/acceleration.txt", "w");
+        fprintf(file, "TIME: %f, WARNING: LOAD EXCEDDED MAXIMUM ALLOWED ANGLE\n\r", time);
+        fclose(file);
+        break;
+    default:
+    break;
+    }
+}
+
+int location(){
     eth.init(); //USE DHCP to get local IP address
     eth.connect(); //Connect to the network
         
@@ -62,50 +124,103 @@
         uLCD.printf("HTTP Error %d", r);
         return -1;
     }
-    sdlog(0, 1, 2, latitude, longitude, city);// Save info to SD card
-    //Print Location Information
-    uLCD.cls();
+    sdlog(0, Clock.read(), 2, latitude, longitude, city);// Save info to SD card
+    eth.disconnect();      //DISCONNECT FROM THE NETWORK 
+        //Print Location Information
     uLCD.locate(0,0);
+    uLCD.textbackground_color(BLACK);
     uLCD.printf("%s, %s\n",city,countryFull); //Print City and Country
     uLCD.printf("LAT:%s\nLONG:%s\n",latitude,longitude); //LATITUDE AND LONGITUDE 
-    uLCD.printf("Timezone:\n%s",timeZone); //PRINT TIMEZONE
-    eth.disconnect(); //DISCONNECT FROM THE NETWORK 
+    return 1;
+    }
+  
+  
+  
+void accel_thread(){
+        
+        float X=accel.readX();
+        float Y=accel.readY();
+        
+        while(true){ 
+        if((X <-0.6 && X>=-0.9)||(X >0.6&&X<=0.9)||(Y <-0.6 && Y>=-0.9)||(Y >0.6 && Y<=0.9)){
+            uLCD.textbackground_color(RED);
+            uLCD.locate(0, 5);      // setting  cursor on LED
+            uLCD.printf("WARNING");
+            uLCD.locate(0,6);
+            uLCD.printf("CONTAINER UNSTABLE");
+            uLCD.locate(0,7);
+            uLCD.printf("CHECK CONTAINER");
+                        }
+        else if ((X<-0.9)||(X>0.9)||(Y<-0.9)||(Y>0.9)){
+           soundalarm(); //Sounds alarm
+           sdlog(2, Clock.read(), 0,0,0,0); //Records tipping to SD card with timestamp
+            uLCD.textbackground_color(RED);
+            uLCD.locate(0,5);      // Move cursor
+            uLCD.printf("WARNING");
+            uLCD.locate(0,6);
+            uLCD.printf("CONTAINER INVERTED");
+            uLCD.locate(0,7);
+            uLCD.textbackground_color(BLUE);
+            uLCD.printf("Press to clear");    
+            }
+            else{
+            uLCD.textbackground_color(BLACK);
+            uLCD.locate(0,5);      // setting  cursor on LED
+            uLCD.printf("--------------");
+            uLCD.locate(0,6);
+            uLCD.printf("Container Stable");
+            uLCD.locate(0,7);
+            uLCD.printf("--------------");
+                }
+        }
+  }
+  
     
-}
-
-//SET FOR CSV FORMAT: NEEDS TO BE EDITED IF DIFFERENT FORMAT
-void parse(char buffer[], int *j, char *string) {
-//extracts next location string data item from buffer
-    int i=0;
-    for (i=0; i<=strlen(buffer); i++) {  //TOTAL SIZE OF RETURNED DATA
-        if ((buffer[*j+i] == ',')||(buffer[*j+i] == '\0' )) { //IF comma or end of string
-            //comma is the string field delimiter
-            string[i]=0; //SETS END OF SRTRING TO 0
-            *j=*j+i+1; //UPDATES to 1 after comma seperated value
-            break;
-        } else string[i]=buffer[*j+i]; //Keep adding to the string
+void tempchange_thread(){ //Constantly checking if temperature goes above 20C
+    
+    float tempnotright;
+    float change;
+    change = ain1.read() * 3.3;
+    tempnotright = (change - 0.5) * 100.0;
+    
+    if (tempnotright > 20) {
+        uLCD.locate(0,9);
+        uLCD.textbackground_color(BLACK);
+        uLCD.printf("%fs: %f C", Clock.read(), tempnotright);
+        uLCD.locate(0,10);
+        uLCD.textbackground_color(RED);
+        uLCD.printf("CORRECTION NEEDED!");
+        soundalarm();
+        sdlog(1, Clock.read(), tempnotright,0,0,0);
+          }
+         Thread:: wait(5);
     }
-}
 
-void sdlog(int type, float time, float input0, char input1[], char input2[], char input3[]){
-     FILE *file;
-    switch (type){
-    case 0:
-            file = fopen("/sd/location.txt", "w");
-        fprintf(file, "TIME: %f, Latitude: %s Longitude: %s, City: %s\n\r", time, input1, input2, input3);
-        fclose(file);
-        break;
-    case 1:
-            file = fopen("/sd/temperature.txt", "w");
-        fprintf(file, "TIME: %f, %f degC\n\r", time, input0);
-        fclose(file);
-        break;
-    case 2:
-            file = fopen("/sd/acceleration.txt", "w");
-        fprintf(file, "TIME: %f, WARNING: LOAD EXCEDDED MAXIMUM ALLOWED ANGLE\n\r", time);
-        fclose(file);
-        break;
-    default:
-    break;
-    }
-}
\ No newline at end of file
+
+    void temperature(){ //Sort LCD
+        float voltage_in;
+        float degrees_c;
+        int i;
+                for(i = 0; i < 1000; i++) {
+            voltage_in = ain * 3.3;
+            degrees_c = (voltage_in - 0.5) * 100.0;
+            uLCD.locate(0,9);
+            uLCD.textbackground_color(BLACK);
+            uLCD.printf("%fs: %f C", Clock.read(), degrees_c);
+            
+            if(degrees_c <= 20){ //Correct Temp Range
+                sdlog(1, Clock.read(), degrees_c,0,0,0);
+                uLCD.locate(0,10);
+                uLCD.textbackground_color(GREEN);
+                uLCD.printf("Correct Temperature");
+
+            }
+            else{ //Incorrect Temp Range
+                soundalarm();
+                sdlog(1, Clock.read(), degrees_c,0,0,0);
+                uLCD.textbackground_color(RED);
+                uLCD.locate(0,10);
+                uLCD.printf("CORRECTION NEEDED!");
+            }
+        }
+        }
\ No newline at end of file