needs more commenting, log file errors, LED feedback

Dependencies:   ARCH_GPRS_V2_HW Blinker GPRSInterface HTTPClient_GPRS RTC_WorkingLibrary SDFileSystem USBDevice mbed

Fork of finalv2 by Cellular building monitoring

Revision:
2:89ca14fa896e
Parent:
1:0ec6c4c22623
Child:
3:8d414873535d
--- a/finalCodev3.cpp	Tue May 05 19:53:49 2015 +0000
+++ b/finalCodev3.cpp	Wed May 06 04:59:01 2015 +0000
@@ -10,7 +10,7 @@
 #include "i2c_uart.h"
 #include "ARCH_GPRS_Sleep.h"
 #include "ds1307.h"
-#include "USBSerial.h"
+//#include "USBSerial.h"
 #include <string>
 
 //Arch GPRS v2 SIM900 pins
@@ -23,15 +23,15 @@
 
 #define BROADCAST_TIME            300 //time between sending data [sec]  
 
-USBSerial pc;
+//USBSerial pc;
 
 //LED Blink
 DigitalOut line11(P0_9);
 DigitalOut line13(P0_2);
 DigitalOut line14(P1_28);
 
-Blinker yellowLED(LED1), redLED(LED2), greenLED(LED3), blueLED(LED4);
-
+//Blinker yellowLED(LED1), redLED(LED2), greenLED(LED3), blueLED(LED4);
+ Blinker blueLED(P0_9), greenLED(P0_2), redLED(P1_28);
 //USBSerial pc;
 SDFileSystem sd(P1_22, P1_21, P1_20, P1_23, "sd"); // the pinout on the /Arch GPRS v2 mbed board.
 char filename[60];
@@ -93,6 +93,7 @@
         *humData = sensor.ReadHumidity();  
         wait(1);
         } 
+    
     iot_hw.grovePwrOff();  //power down grove pins  
 }  
 
@@ -103,13 +104,31 @@
         *lightData = lightSensor*1000;
 }
 
-
-int ReadFile (void) {    
+//void errorWrite(const char* errorReport) {
+void errorWrite(string errorReport) {
+    rtc.gettime( &sec, &minute, &hours, &day, &date, &month, &year); //get time from RTC
+    sprintf(timestamp,"20%.2d-%.2d-%.2d %.2d:%.2d:%.2d", year, month, date, hours, minute, sec); // create timmestamp
+    
+    mkdir("/sd", 0777);
+    FILE *fp = fopen("/sd/ErrorLog.txt","a");
+    if (fp == NULL) {
+        blueLED.blink(10); //no sd card blink blue 10 times
+    } else {
+        //pc.printf("%s: %s \r\n",timestamp,errorReport);
+        fprintf(fp,"%s: %s \r\n",timestamp,errorReport); 
+        fclose(fp);
+        blueLED.blink(5);
+    }
+}
+bool ReadFile (void) {    
     mkdir("/sd", 0777);           // All other times open file in append mode
     FILE *fp = fopen("/sd/config.txt","r");
     if (fp==NULL) {
         fclose(fp);
-        return 0;
+        blueLED.blink(10);
+        errorR="Read config.txt file error on SD card";
+        errorWrite(errorR);
+        return false;
         
     } else if (fp) {        
         //fscanf(fp,"%16c %d %s %s", APIKey, &numSensors, sensors[0],sensors[1]);
@@ -121,19 +140,21 @@
 //        pc.printf("number of sensors = %d \r\n",numSensors);
 //        
         for (int i = 0; i<numSensors;i=i+1) {
-            fscanf(fp,"%s",sensors[i]); 
-            //fscanf(fp,"%s",sensorBuff); 
+            //fscanf(fp,"%s",sensors[i]); 
+            fscanf(fp,"%s",sensorBuff); 
             strcpy(sensors[i],sensorBuff);
-            //pc.printf("sensor %d = %s\r\n",i,sensors[i]); 
         }
         
 
         fclose(fp);
-        return 1;
+        return true;
     
     } else {
         fclose(fp);
-        return 0;
+        blueLED.blink(10);
+        errorR="Read config.txt file error on SD card";
+        errorWrite(errorR);
+        return false;
     }
         
 }
@@ -159,13 +180,18 @@
         urlBuffer[0] = 0;
         sprintf(urlBuffer, "%s?key=%s&field%d=%d", thingSpeakUrl, APIKey, field1, sensor1Data); //create url 
         HTTPResult res = http.get(urlBuffer, str,128); //send get request
-        if (res != HTTP_OK) redLED.blink(5);           //get statement failed blink red 5 times
-        else greenLED.blink(5);                        //get statement success blink green 5 times
+        if (res != HTTP_OK) {
+             redLED.blink(5);           //get statement failed blink red 5 times
+            errorR="HTTP Request Error";
+            errorWrite(errorR);
+        } else greenLED.blink(5);                        //get statement success blink green 5 times
         iot_hw.init_io();                              //power down SIM900
           
     } else { //failed to connectGPRS
         redLED.blink(10); //blink red 10 times
         iot_hw.init_io(); //power down SIM900
+        errorR="Failed to connect to GPRS";
+        errorWrite(errorR);
     }
 }
 
@@ -176,12 +202,18 @@
         urlBuffer[0] = 0;
         sprintf(urlBuffer, "%s?key=%s&field%d=%d&field%d=%d", thingSpeakUrl, APIKey, field1, sensor1Data, field2, sensor2Data); //create url
         HTTPResult res = http.get(urlBuffer, str,128); //send get request
-        if (res != HTTP_OK) redLED.blink(5); //get statement failed blink red 5 times
-        else greenLED.blink(5); //get statement success bink green 5 times
-        iot_hw.init_io(); //power down SIM900
-    } else {
+        if (res != HTTP_OK) {
+             redLED.blink(5);           //get statement failed blink red 5 times
+            errorR="HTTP Request Error";
+            errorWrite(errorR);
+        } else greenLED.blink(5);                        //get statement success blink green 5 times
+        iot_hw.init_io();                              //power down SIM900
+          
+    } else { //failed to connectGPRS
         redLED.blink(10); //blink red 10 times
         iot_hw.init_io(); //power down SIM900
+        errorR="Failed to connect to GPRS";
+        errorWrite(errorR);
     }
 }
 
@@ -192,31 +224,22 @@
         urlBuffer[0] = 0;
         sprintf(urlBuffer, "%s?key=%s&field%d=%d&field%d=%d&field%d=%d", thingSpeakUrl, APIKey, field1, sensor1Data, field2, sensor2Data, field3, sensor3Data); //create url to send
         HTTPResult res = http.get(urlBuffer, str,128); //send get request
-        if (res != HTTP_OK) redLED.blink(5); //get statement failed blink red 5 times
-        else greenLED.blink(5); //get statemnet success blink green 5 times
-        iot_hw.init_io(); //power down SIM900
-    } else {
+        if (res != HTTP_OK) {
+            redLED.blink(5);           //get statement failed blink red 5 times
+            errorR="HTTP Request Error";
+            errorWrite(errorR);
+        } else greenLED.blink(5);                        //get statement success blink green 5 times
+        iot_hw.init_io();                              //power down SIM900
+          
+    } else { //failed to connectGPRS
         redLED.blink(10); //blink red 10 times
         iot_hw.init_io(); //power down SIM900
+        errorR="Failed to connect to GPRS";
+        errorWrite(errorR);
     }
 }
 
-//void errorWrite(const char* errorReport) {
-void errorWrite(string errorReport) {
-    rtc.gettime( &sec, &minute, &hours, &day, &date, &month, &year); //get time from RTC
-    sprintf(timestamp,"20%.2d-%.2d-%.2d %.2d:%.2d:%.2d", year, month, date, hours, minute, sec); // create timmestamp
-    
-    mkdir("/sd", 0777);
-    FILE *fp = fopen("/sd/ErrorLog.txt","a");
-    if (fp == NULL) {
-        blueLED.blink(10); //no sd card blink blue 10 times
-    } else {
-        //pc.printf("%s: %s \r\n",timestamp,errorReport);
-        fprintf(fp,"%s: %s \r\n",timestamp,errorReport); 
-        fclose(fp);
-        blueLED.blink(5);
-    }
-}
+
 
 //writes data to SDcard for 1 field
 //if no SDcard blink blue 10 times 
@@ -228,10 +251,11 @@
     mkdir("/sd", 0777);
     FILE *fp = fopen("/sd/node1.csv","a");
     if (fp == NULL) {
-        blueLED.blink(10); //no sd Card blink Blue 10 times 
+        blueLED.blink(10); //no sd Card blink Blue 10 times
     } else {
         fprintf(fp,"%s, %d \r\n",timestamp,sensor1Data); //write time stamp and data to microSD
         fclose(fp);
+        blueLED.blink(5);
     }
 }
 
@@ -249,6 +273,7 @@
     } else {
         fprintf(fp,"%s, %d, %d\r\n",timestamp, sensor1Data, sensor2Data); //write timestamp+data to SD
         fclose(fp);
+        blueLED.blink(5);
     }
 }
 
@@ -266,6 +291,7 @@
     } else {
         fprintf(fp,"%s, %d, %d, %d\r\n",timestamp, sensor1Data, sensor2Data, sensor3Data); //write timestamp+data to sd
         fclose(fp);
+        blueLED.blink(5);
     }
 }
 
@@ -273,9 +299,7 @@
 /////////////////////////////////////////////////////////////////////////////////////////////////////////////
 int main() {
     wdt_sleep.wdtClkSetup(WDTCLK_SRC_IRC_OSC); //set up sleep
-    
-
-    
+    wait(2);
     //test of breadboard
 //    while (1) {
 //        //rtc test
@@ -283,47 +307,29 @@
 //        rtc.gettime( &sec, &minute, &hours, &day, &date, &month, &year);
 //        sprintf(timestamp,"20%.2d-%.2d-%.2d %.2d:%.2d:%.2d", year, month, date, hours, minute, sec);
 //        pc.printf("%s: %s\r\n",timestamp,errorR);
-//        
-//        //rgb test
-//        int n=5;
-//        int t=1;
-//        pc.printf("line11 blink\r\n");
-//        for (int i = 0; i < n; i++) {
-//            line11 = 1;
-//            wait(t);
-//            line11 = 0;
-//            wait(t);
-//        }
-//
-//        pc.printf("line13 blink\r\n");
+//        wait(1);
+//       // //rgb test
+//       pc.printf("green blink\r\n");
+//       greenLED.blink(5);
 //
-//        for (int i = 0; i < n; i++) {
-//            line13 = 1;
-//            wait(t);
-//            line13 = 0;
-//            wait(t);
-//        }
-//        
-//        pc.printf("line11 blink\r\n");
+//        pc.printf("red blink\r\n");
+//        redLED.blink(5);
+//  
+//        pc.printf("blue blink\r\n");
+//        blueLED.blink(5);
 //
-//        for (int i = 0; i < n; i++) {
-//            line14 = 1;
-//            wait(t);
-//            line14 = 0;
-//            wait(t);
-//        }
 //        //light sensor test
 //        getLightReading(&lightData);
 //        pc.printf("light reading: %d \r\n",lightData);
 //        
-//        wait(15);
+//        wait(5);
 //    }
     
         
     //read SD card
-    while (ReadFile()==0) {
+    while(ReadFile()==false) {
         wait(5);
-        redLED.blink(10);
+        blueLED.blink(10);
     }
     greenLED.blink(10);
     
@@ -352,7 +358,7 @@
     FILE *fp = fopen(filename,"a");
     if (fp == NULL) {
         //no sd card
-        redLED.blink(10);
+        blueLED.blink(10);
     } else {
         
         //print header
@@ -362,21 +368,26 @@
     }
     
     //Debug Mode
+
     for (int dd=0;dd<30;dd=dd+1) {
-        
+
         if (numSensors == 3) {
                 
             } else if (numSensors ==2) {
+
                 if (tempSensorflag==true) {
+
                     if (lightSensorflag==true) {
                         getLightReading(&lightData);
                         getTempHumid(&tempData,&humData);
                         sendData3(tempData,tempField,humData,humField,lightData,lightField);
                         sdWrite3(tempData,humData,lightData);
-                    } else if (SomeotherSensorflag==true) { 
-                    
+                    } else if (SomeotherSensorflag==true) {
+                         
                     }  
                 } else if (lightSensorflag==true) {
+               
+
                     if (SomeotherSensorflag==true) {
                         getLightReading(&lightData);
                         
@@ -399,7 +410,7 @@
             wait(30);
         }
         
-    
+