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

Committer:
mbotkinl
Date:
Wed May 06 04:59:01 2015 +0000
Revision:
2:89ca14fa896e
Parent:
1:0ec6c4c22623
Child:
3:8d414873535d
New code with LED indicators, error Log other bug fixes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
seedteam20 1:0ec6c4c22623 1 //SEED Team 20 Final Code draft 1 4/16/15
seedteam20 1:0ec6c4c22623 2
seedteam20 1:0ec6c4c22623 3 #include "mbed.h"
seedteam20 1:0ec6c4c22623 4 #include "SDFileSystem.h"
seedteam20 1:0ec6c4c22623 5 #include "ARCH_GPRS_V2_HW.h"
seedteam20 1:0ec6c4c22623 6 #include "HTTPClient.h"
seedteam20 1:0ec6c4c22623 7 #include "GPRSInterface.h"
seedteam20 1:0ec6c4c22623 8 #include "Blinker.h"
seedteam20 1:0ec6c4c22623 9 #include "DHT.h"
seedteam20 1:0ec6c4c22623 10 #include "i2c_uart.h"
seedteam20 1:0ec6c4c22623 11 #include "ARCH_GPRS_Sleep.h"
seedteam20 1:0ec6c4c22623 12 #include "ds1307.h"
mbotkinl 2:89ca14fa896e 13 //#include "USBSerial.h"
seedteam20 1:0ec6c4c22623 14 #include <string>
seedteam20 1:0ec6c4c22623 15
seedteam20 1:0ec6c4c22623 16 //Arch GPRS v2 SIM900 pins
seedteam20 1:0ec6c4c22623 17 #define PIN_TX P1_27
seedteam20 1:0ec6c4c22623 18 #define PIN_RX P1_26
seedteam20 1:0ec6c4c22623 19
seedteam20 1:0ec6c4c22623 20 //RTC
seedteam20 1:0ec6c4c22623 21 #define SDA P0_5
seedteam20 1:0ec6c4c22623 22 #define SCL P0_4
seedteam20 1:0ec6c4c22623 23
seedteam20 1:0ec6c4c22623 24 #define BROADCAST_TIME 300 //time between sending data [sec]
seedteam20 1:0ec6c4c22623 25
mbotkinl 2:89ca14fa896e 26 //USBSerial pc;
seedteam20 1:0ec6c4c22623 27
seedteam20 1:0ec6c4c22623 28 //LED Blink
seedteam20 1:0ec6c4c22623 29 DigitalOut line11(P0_9);
seedteam20 1:0ec6c4c22623 30 DigitalOut line13(P0_2);
seedteam20 1:0ec6c4c22623 31 DigitalOut line14(P1_28);
seedteam20 1:0ec6c4c22623 32
mbotkinl 2:89ca14fa896e 33 //Blinker yellowLED(LED1), redLED(LED2), greenLED(LED3), blueLED(LED4);
mbotkinl 2:89ca14fa896e 34 Blinker blueLED(P0_9), greenLED(P0_2), redLED(P1_28);
seedteam20 1:0ec6c4c22623 35 //USBSerial pc;
seedteam20 1:0ec6c4c22623 36 SDFileSystem sd(P1_22, P1_21, P1_20, P1_23, "sd"); // the pinout on the /Arch GPRS v2 mbed board.
seedteam20 1:0ec6c4c22623 37 char filename[60];
seedteam20 1:0ec6c4c22623 38
seedteam20 1:0ec6c4c22623 39 //RTC
seedteam20 1:0ec6c4c22623 40 DS1307 rtc(SDA,SCL);
seedteam20 1:0ec6c4c22623 41 int sec, minute, hours, day, date, month, year;
seedteam20 1:0ec6c4c22623 42 char timestamp[17];
seedteam20 1:0ec6c4c22623 43
seedteam20 1:0ec6c4c22623 44 //variables for reading data from SD card
seedteam20 1:0ec6c4c22623 45 char APIKey[17];
seedteam20 1:0ec6c4c22623 46 char sensors[3][5];
seedteam20 1:0ec6c4c22623 47 char sensorBuff[5];
seedteam20 1:0ec6c4c22623 48 int tempField, humField, lightField;
seedteam20 1:0ec6c4c22623 49 int field1, field2, field3;
seedteam20 1:0ec6c4c22623 50 int numSensors;
seedteam20 1:0ec6c4c22623 51 bool tempSensorflag=false, lightSensorflag=false, SomeotherSensorflag=false;
seedteam20 1:0ec6c4c22623 52
seedteam20 1:0ec6c4c22623 53 //GPRS setup
seedteam20 1:0ec6c4c22623 54 GPRSInterface gprs(PIN_TX,PIN_RX,115200,"internetd.gdsp",NULL,NULL);
seedteam20 1:0ec6c4c22623 55 HTTPClient http;
seedteam20 1:0ec6c4c22623 56 char* thingSpeakUrl = "http://api.thingspeak.com/update";
seedteam20 1:0ec6c4c22623 57 char urlBuffer[256];
seedteam20 1:0ec6c4c22623 58 char str[1024];
seedteam20 1:0ec6c4c22623 59 //char errorR[1024];
seedteam20 1:0ec6c4c22623 60 string errorR;
seedteam20 1:0ec6c4c22623 61
seedteam20 1:0ec6c4c22623 62 //Sensors
seedteam20 1:0ec6c4c22623 63 //DHT sensor(P1_14,SEN51035P);
seedteam20 1:0ec6c4c22623 64 DHT sensor(P1_14,SEN11301P); //grove connected DHT22
seedteam20 1:0ec6c4c22623 65 AnalogIn lightSensor(P0_12); //TEMT6000 sparkfun break out board ambient light sensor
seedteam20 1:0ec6c4c22623 66
seedteam20 1:0ec6c4c22623 67 int tempData,humData, lightData; //variables to hold enviroment
seedteam20 1:0ec6c4c22623 68
seedteam20 1:0ec6c4c22623 69 //method to connect GPRS to the network
seedteam20 1:0ec6c4c22623 70 //@return true if successful, false if failed
seedteam20 1:0ec6c4c22623 71 bool connectGPRS(void) {
seedteam20 1:0ec6c4c22623 72 iot_hw.init(); // power on SIM900
seedteam20 1:0ec6c4c22623 73 int count = 0;
seedteam20 1:0ec6c4c22623 74 while(false == gprs.connect() && count < 5) {
seedteam20 1:0ec6c4c22623 75 wait(2);
seedteam20 1:0ec6c4c22623 76 count += 1;
seedteam20 1:0ec6c4c22623 77 }
seedteam20 1:0ec6c4c22623 78 if (count != 5) return true; // success!
seedteam20 1:0ec6c4c22623 79 else return false; //failed
seedteam20 1:0ec6c4c22623 80 }
seedteam20 1:0ec6c4c22623 81
seedteam20 1:0ec6c4c22623 82 //method to collect temperature and humidity
seedteam20 1:0ec6c4c22623 83 //using grove DHT22 sensors connected to P1_14, P1_13
seedteam20 1:0ec6c4c22623 84 void getTempHumid(int* tempData,int* humData){
seedteam20 1:0ec6c4c22623 85 int err = 1;
seedteam20 1:0ec6c4c22623 86 int count = 0;
seedteam20 1:0ec6c4c22623 87 iot_hw.grovePwrOn(); // power on grove pins
seedteam20 1:0ec6c4c22623 88 wait(1); // wait 1 second for device stable status
seedteam20 1:0ec6c4c22623 89 while (err != 0 && count < 4) {
seedteam20 1:0ec6c4c22623 90 err = sensor.readData();
seedteam20 1:0ec6c4c22623 91 count += 1;
seedteam20 1:0ec6c4c22623 92 *tempData = sensor.ReadTemperature(FARENHEIT);
seedteam20 1:0ec6c4c22623 93 *humData = sensor.ReadHumidity();
seedteam20 1:0ec6c4c22623 94 wait(1);
seedteam20 1:0ec6c4c22623 95 }
mbotkinl 2:89ca14fa896e 96
seedteam20 1:0ec6c4c22623 97 iot_hw.grovePwrOff(); //power down grove pins
seedteam20 1:0ec6c4c22623 98 }
seedteam20 1:0ec6c4c22623 99
seedteam20 1:0ec6c4c22623 100 //method to collect light level data
seedteam20 1:0ec6c4c22623 101 //using TEMT6000 sparkfun breakout board
seedteam20 1:0ec6c4c22623 102 void getLightReading(int* lightData)
seedteam20 1:0ec6c4c22623 103 {
seedteam20 1:0ec6c4c22623 104 *lightData = lightSensor*1000;
seedteam20 1:0ec6c4c22623 105 }
seedteam20 1:0ec6c4c22623 106
mbotkinl 2:89ca14fa896e 107 //void errorWrite(const char* errorReport) {
mbotkinl 2:89ca14fa896e 108 void errorWrite(string errorReport) {
mbotkinl 2:89ca14fa896e 109 rtc.gettime( &sec, &minute, &hours, &day, &date, &month, &year); //get time from RTC
mbotkinl 2:89ca14fa896e 110 sprintf(timestamp,"20%.2d-%.2d-%.2d %.2d:%.2d:%.2d", year, month, date, hours, minute, sec); // create timmestamp
mbotkinl 2:89ca14fa896e 111
mbotkinl 2:89ca14fa896e 112 mkdir("/sd", 0777);
mbotkinl 2:89ca14fa896e 113 FILE *fp = fopen("/sd/ErrorLog.txt","a");
mbotkinl 2:89ca14fa896e 114 if (fp == NULL) {
mbotkinl 2:89ca14fa896e 115 blueLED.blink(10); //no sd card blink blue 10 times
mbotkinl 2:89ca14fa896e 116 } else {
mbotkinl 2:89ca14fa896e 117 //pc.printf("%s: %s \r\n",timestamp,errorReport);
mbotkinl 2:89ca14fa896e 118 fprintf(fp,"%s: %s \r\n",timestamp,errorReport);
mbotkinl 2:89ca14fa896e 119 fclose(fp);
mbotkinl 2:89ca14fa896e 120 blueLED.blink(5);
mbotkinl 2:89ca14fa896e 121 }
mbotkinl 2:89ca14fa896e 122 }
mbotkinl 2:89ca14fa896e 123 bool ReadFile (void) {
seedteam20 1:0ec6c4c22623 124 mkdir("/sd", 0777); // All other times open file in append mode
seedteam20 1:0ec6c4c22623 125 FILE *fp = fopen("/sd/config.txt","r");
seedteam20 1:0ec6c4c22623 126 if (fp==NULL) {
seedteam20 1:0ec6c4c22623 127 fclose(fp);
mbotkinl 2:89ca14fa896e 128 blueLED.blink(10);
mbotkinl 2:89ca14fa896e 129 errorR="Read config.txt file error on SD card";
mbotkinl 2:89ca14fa896e 130 errorWrite(errorR);
mbotkinl 2:89ca14fa896e 131 return false;
seedteam20 1:0ec6c4c22623 132
seedteam20 1:0ec6c4c22623 133 } else if (fp) {
seedteam20 1:0ec6c4c22623 134 //fscanf(fp,"%16c %d %s %s", APIKey, &numSensors, sensors[0],sensors[1]);
seedteam20 1:0ec6c4c22623 135 fscanf(fp,"%16c",APIKey);
seedteam20 1:0ec6c4c22623 136
seedteam20 1:0ec6c4c22623 137 // pc.printf("APIKEY= %s\r\n",APIKey);
seedteam20 1:0ec6c4c22623 138 //
seedteam20 1:0ec6c4c22623 139 fscanf(fp,"%d",&numSensors);
seedteam20 1:0ec6c4c22623 140 // pc.printf("number of sensors = %d \r\n",numSensors);
seedteam20 1:0ec6c4c22623 141 //
seedteam20 1:0ec6c4c22623 142 for (int i = 0; i<numSensors;i=i+1) {
mbotkinl 2:89ca14fa896e 143 //fscanf(fp,"%s",sensors[i]);
mbotkinl 2:89ca14fa896e 144 fscanf(fp,"%s",sensorBuff);
seedteam20 1:0ec6c4c22623 145 strcpy(sensors[i],sensorBuff);
seedteam20 1:0ec6c4c22623 146 }
seedteam20 1:0ec6c4c22623 147
seedteam20 1:0ec6c4c22623 148
seedteam20 1:0ec6c4c22623 149 fclose(fp);
mbotkinl 2:89ca14fa896e 150 return true;
seedteam20 1:0ec6c4c22623 151
seedteam20 1:0ec6c4c22623 152 } else {
seedteam20 1:0ec6c4c22623 153 fclose(fp);
mbotkinl 2:89ca14fa896e 154 blueLED.blink(10);
mbotkinl 2:89ca14fa896e 155 errorR="Read config.txt file error on SD card";
mbotkinl 2:89ca14fa896e 156 errorWrite(errorR);
mbotkinl 2:89ca14fa896e 157 return false;
seedteam20 1:0ec6c4c22623 158 }
seedteam20 1:0ec6c4c22623 159
seedteam20 1:0ec6c4c22623 160 }
seedteam20 1:0ec6c4c22623 161
seedteam20 1:0ec6c4c22623 162 bool stringComparison (const char *string1, const char *string2) {
seedteam20 1:0ec6c4c22623 163 int count = 0;
seedteam20 1:0ec6c4c22623 164 while (string1[count] != 0) {
seedteam20 1:0ec6c4c22623 165 if (string1[count] != string2[count])
seedteam20 1:0ec6c4c22623 166 return false;
seedteam20 1:0ec6c4c22623 167 count++;
seedteam20 1:0ec6c4c22623 168 }
seedteam20 1:0ec6c4c22623 169 return true;
seedteam20 1:0ec6c4c22623 170 }
seedteam20 1:0ec6c4c22623 171
seedteam20 1:0ec6c4c22623 172
seedteam20 1:0ec6c4c22623 173 //method to send 1 field of data to ThingsSpeak
seedteam20 1:0ec6c4c22623 174 //blinks green 5 times if successful
seedteam20 1:0ec6c4c22623 175 //blinks red 5 times if get statement fails
seedteam20 1:0ec6c4c22623 176 //blinks red 10 times if fails to connectGPRS
seedteam20 1:0ec6c4c22623 177 void sendData1(int sensor1Data, int field1)
seedteam20 1:0ec6c4c22623 178 {
seedteam20 1:0ec6c4c22623 179 if (connectGPRS() == true) { //connect is success
seedteam20 1:0ec6c4c22623 180 urlBuffer[0] = 0;
seedteam20 1:0ec6c4c22623 181 sprintf(urlBuffer, "%s?key=%s&field%d=%d", thingSpeakUrl, APIKey, field1, sensor1Data); //create url
seedteam20 1:0ec6c4c22623 182 HTTPResult res = http.get(urlBuffer, str,128); //send get request
mbotkinl 2:89ca14fa896e 183 if (res != HTTP_OK) {
mbotkinl 2:89ca14fa896e 184 redLED.blink(5); //get statement failed blink red 5 times
mbotkinl 2:89ca14fa896e 185 errorR="HTTP Request Error";
mbotkinl 2:89ca14fa896e 186 errorWrite(errorR);
mbotkinl 2:89ca14fa896e 187 } else greenLED.blink(5); //get statement success blink green 5 times
seedteam20 1:0ec6c4c22623 188 iot_hw.init_io(); //power down SIM900
seedteam20 1:0ec6c4c22623 189
seedteam20 1:0ec6c4c22623 190 } else { //failed to connectGPRS
seedteam20 1:0ec6c4c22623 191 redLED.blink(10); //blink red 10 times
seedteam20 1:0ec6c4c22623 192 iot_hw.init_io(); //power down SIM900
mbotkinl 2:89ca14fa896e 193 errorR="Failed to connect to GPRS";
mbotkinl 2:89ca14fa896e 194 errorWrite(errorR);
seedteam20 1:0ec6c4c22623 195 }
seedteam20 1:0ec6c4c22623 196 }
seedteam20 1:0ec6c4c22623 197
seedteam20 1:0ec6c4c22623 198 //method to send 2 field of data to ThingsSpeak
seedteam20 1:0ec6c4c22623 199 void sendData2(int sensor1Data, int field1, int sensor2Data, int field2)
seedteam20 1:0ec6c4c22623 200 {
seedteam20 1:0ec6c4c22623 201 if (connectGPRS() == true) {
seedteam20 1:0ec6c4c22623 202 urlBuffer[0] = 0;
seedteam20 1:0ec6c4c22623 203 sprintf(urlBuffer, "%s?key=%s&field%d=%d&field%d=%d", thingSpeakUrl, APIKey, field1, sensor1Data, field2, sensor2Data); //create url
seedteam20 1:0ec6c4c22623 204 HTTPResult res = http.get(urlBuffer, str,128); //send get request
mbotkinl 2:89ca14fa896e 205 if (res != HTTP_OK) {
mbotkinl 2:89ca14fa896e 206 redLED.blink(5); //get statement failed blink red 5 times
mbotkinl 2:89ca14fa896e 207 errorR="HTTP Request Error";
mbotkinl 2:89ca14fa896e 208 errorWrite(errorR);
mbotkinl 2:89ca14fa896e 209 } else greenLED.blink(5); //get statement success blink green 5 times
mbotkinl 2:89ca14fa896e 210 iot_hw.init_io(); //power down SIM900
mbotkinl 2:89ca14fa896e 211
mbotkinl 2:89ca14fa896e 212 } else { //failed to connectGPRS
seedteam20 1:0ec6c4c22623 213 redLED.blink(10); //blink red 10 times
seedteam20 1:0ec6c4c22623 214 iot_hw.init_io(); //power down SIM900
mbotkinl 2:89ca14fa896e 215 errorR="Failed to connect to GPRS";
mbotkinl 2:89ca14fa896e 216 errorWrite(errorR);
seedteam20 1:0ec6c4c22623 217 }
seedteam20 1:0ec6c4c22623 218 }
seedteam20 1:0ec6c4c22623 219
seedteam20 1:0ec6c4c22623 220 //method to send 3 field of data to ThingsSpeak
seedteam20 1:0ec6c4c22623 221 void sendData3(int sensor1Data, int field1, int sensor2Data, int field2, int sensor3Data, int field3)
seedteam20 1:0ec6c4c22623 222 {
seedteam20 1:0ec6c4c22623 223 if (connectGPRS() == true) {
seedteam20 1:0ec6c4c22623 224 urlBuffer[0] = 0;
seedteam20 1:0ec6c4c22623 225 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
seedteam20 1:0ec6c4c22623 226 HTTPResult res = http.get(urlBuffer, str,128); //send get request
mbotkinl 2:89ca14fa896e 227 if (res != HTTP_OK) {
mbotkinl 2:89ca14fa896e 228 redLED.blink(5); //get statement failed blink red 5 times
mbotkinl 2:89ca14fa896e 229 errorR="HTTP Request Error";
mbotkinl 2:89ca14fa896e 230 errorWrite(errorR);
mbotkinl 2:89ca14fa896e 231 } else greenLED.blink(5); //get statement success blink green 5 times
mbotkinl 2:89ca14fa896e 232 iot_hw.init_io(); //power down SIM900
mbotkinl 2:89ca14fa896e 233
mbotkinl 2:89ca14fa896e 234 } else { //failed to connectGPRS
seedteam20 1:0ec6c4c22623 235 redLED.blink(10); //blink red 10 times
seedteam20 1:0ec6c4c22623 236 iot_hw.init_io(); //power down SIM900
mbotkinl 2:89ca14fa896e 237 errorR="Failed to connect to GPRS";
mbotkinl 2:89ca14fa896e 238 errorWrite(errorR);
seedteam20 1:0ec6c4c22623 239 }
seedteam20 1:0ec6c4c22623 240 }
seedteam20 1:0ec6c4c22623 241
mbotkinl 2:89ca14fa896e 242
seedteam20 1:0ec6c4c22623 243
seedteam20 1:0ec6c4c22623 244 //writes data to SDcard for 1 field
seedteam20 1:0ec6c4c22623 245 //if no SDcard blink blue 10 times
seedteam20 1:0ec6c4c22623 246 void sdWrite1(int sensor1Data)
seedteam20 1:0ec6c4c22623 247 {
seedteam20 1:0ec6c4c22623 248 rtc.gettime( &sec, &minute, &hours, &day, &date, &month, &year); //get time from RTC
seedteam20 1:0ec6c4c22623 249 sprintf(timestamp,"20%.2d-%.2d-%.2d %.2d:%.2d:%.2d", year, month, date, hours, minute, sec); //create time stamp
seedteam20 1:0ec6c4c22623 250
seedteam20 1:0ec6c4c22623 251 mkdir("/sd", 0777);
seedteam20 1:0ec6c4c22623 252 FILE *fp = fopen("/sd/node1.csv","a");
seedteam20 1:0ec6c4c22623 253 if (fp == NULL) {
mbotkinl 2:89ca14fa896e 254 blueLED.blink(10); //no sd Card blink Blue 10 times
seedteam20 1:0ec6c4c22623 255 } else {
seedteam20 1:0ec6c4c22623 256 fprintf(fp,"%s, %d \r\n",timestamp,sensor1Data); //write time stamp and data to microSD
seedteam20 1:0ec6c4c22623 257 fclose(fp);
mbotkinl 2:89ca14fa896e 258 blueLED.blink(5);
seedteam20 1:0ec6c4c22623 259 }
seedteam20 1:0ec6c4c22623 260 }
seedteam20 1:0ec6c4c22623 261
seedteam20 1:0ec6c4c22623 262 //writes data to SDcard for 2 fields
seedteam20 1:0ec6c4c22623 263 //if no SDcard blink blue 10 times
seedteam20 1:0ec6c4c22623 264 void sdWrite2(int sensor1Data, int sensor2Data)
seedteam20 1:0ec6c4c22623 265 {
seedteam20 1:0ec6c4c22623 266 rtc.gettime( &sec, &minute, &hours, &day, &date, &month, &year); //get time from RTC
seedteam20 1:0ec6c4c22623 267 sprintf(timestamp,"20%.2d-%.2d-%.2d %.2d:%.2d:%.2d", year, month, date, hours, minute, sec); //create timestamp
seedteam20 1:0ec6c4c22623 268
seedteam20 1:0ec6c4c22623 269 mkdir("/sd", 0777);
seedteam20 1:0ec6c4c22623 270 FILE *fp = fopen("/sd/node1.csv","a");
seedteam20 1:0ec6c4c22623 271 if (fp == NULL) {
seedteam20 1:0ec6c4c22623 272 blueLED.blink(10); //no sd Card blink blue 10 times
seedteam20 1:0ec6c4c22623 273 } else {
seedteam20 1:0ec6c4c22623 274 fprintf(fp,"%s, %d, %d\r\n",timestamp, sensor1Data, sensor2Data); //write timestamp+data to SD
seedteam20 1:0ec6c4c22623 275 fclose(fp);
mbotkinl 2:89ca14fa896e 276 blueLED.blink(5);
seedteam20 1:0ec6c4c22623 277 }
seedteam20 1:0ec6c4c22623 278 }
seedteam20 1:0ec6c4c22623 279
seedteam20 1:0ec6c4c22623 280 //writes data to SDcard for 3 fields
seedteam20 1:0ec6c4c22623 281 //if no SDcard blink blue 10 times
seedteam20 1:0ec6c4c22623 282 void sdWrite3(int sensor1Data, int sensor2Data,int sensor3Data)
seedteam20 1:0ec6c4c22623 283 {
seedteam20 1:0ec6c4c22623 284 rtc.gettime( &sec, &minute, &hours, &day, &date, &month, &year); //get time from RTC
seedteam20 1:0ec6c4c22623 285 sprintf(timestamp,"20%.2d-%.2d-%.2d %.2d:%.2d:%.2d", year, month, date, hours, minute, sec); //create timestamp
seedteam20 1:0ec6c4c22623 286
seedteam20 1:0ec6c4c22623 287 mkdir("/sd", 0777);
seedteam20 1:0ec6c4c22623 288 FILE *fp = fopen("/sd/node1.csv","a");
seedteam20 1:0ec6c4c22623 289 if (fp == NULL) {
seedteam20 1:0ec6c4c22623 290 blueLED.blink(10); //no sd card blink blue 10 times
seedteam20 1:0ec6c4c22623 291 } else {
seedteam20 1:0ec6c4c22623 292 fprintf(fp,"%s, %d, %d, %d\r\n",timestamp, sensor1Data, sensor2Data, sensor3Data); //write timestamp+data to sd
seedteam20 1:0ec6c4c22623 293 fclose(fp);
mbotkinl 2:89ca14fa896e 294 blueLED.blink(5);
seedteam20 1:0ec6c4c22623 295 }
seedteam20 1:0ec6c4c22623 296 }
seedteam20 1:0ec6c4c22623 297
seedteam20 1:0ec6c4c22623 298 /////////////////////////////////////////////////////////////////////////////////////////////////////////////
seedteam20 1:0ec6c4c22623 299 /////////////////////////////////////////////////////////////////////////////////////////////////////////////
seedteam20 1:0ec6c4c22623 300 int main() {
seedteam20 1:0ec6c4c22623 301 wdt_sleep.wdtClkSetup(WDTCLK_SRC_IRC_OSC); //set up sleep
mbotkinl 2:89ca14fa896e 302 wait(2);
seedteam20 1:0ec6c4c22623 303 //test of breadboard
seedteam20 1:0ec6c4c22623 304 // while (1) {
seedteam20 1:0ec6c4c22623 305 // //rtc test
seedteam20 1:0ec6c4c22623 306 // errorR="Test rtc";
seedteam20 1:0ec6c4c22623 307 // rtc.gettime( &sec, &minute, &hours, &day, &date, &month, &year);
seedteam20 1:0ec6c4c22623 308 // sprintf(timestamp,"20%.2d-%.2d-%.2d %.2d:%.2d:%.2d", year, month, date, hours, minute, sec);
seedteam20 1:0ec6c4c22623 309 // pc.printf("%s: %s\r\n",timestamp,errorR);
mbotkinl 2:89ca14fa896e 310 // wait(1);
mbotkinl 2:89ca14fa896e 311 // // //rgb test
mbotkinl 2:89ca14fa896e 312 // pc.printf("green blink\r\n");
mbotkinl 2:89ca14fa896e 313 // greenLED.blink(5);
seedteam20 1:0ec6c4c22623 314 //
mbotkinl 2:89ca14fa896e 315 // pc.printf("red blink\r\n");
mbotkinl 2:89ca14fa896e 316 // redLED.blink(5);
mbotkinl 2:89ca14fa896e 317 //
mbotkinl 2:89ca14fa896e 318 // pc.printf("blue blink\r\n");
mbotkinl 2:89ca14fa896e 319 // blueLED.blink(5);
seedteam20 1:0ec6c4c22623 320 //
seedteam20 1:0ec6c4c22623 321 // //light sensor test
seedteam20 1:0ec6c4c22623 322 // getLightReading(&lightData);
seedteam20 1:0ec6c4c22623 323 // pc.printf("light reading: %d \r\n",lightData);
seedteam20 1:0ec6c4c22623 324 //
mbotkinl 2:89ca14fa896e 325 // wait(5);
seedteam20 1:0ec6c4c22623 326 // }
seedteam20 1:0ec6c4c22623 327
seedteam20 1:0ec6c4c22623 328
seedteam20 1:0ec6c4c22623 329 //read SD card
mbotkinl 2:89ca14fa896e 330 while(ReadFile()==false) {
seedteam20 1:0ec6c4c22623 331 wait(5);
mbotkinl 2:89ca14fa896e 332 blueLED.blink(10);
seedteam20 1:0ec6c4c22623 333 }
seedteam20 1:0ec6c4c22623 334 greenLED.blink(10);
seedteam20 1:0ec6c4c22623 335
seedteam20 1:0ec6c4c22623 336
seedteam20 1:0ec6c4c22623 337 //identify sensors and find order of fields
seedteam20 1:0ec6c4c22623 338 int field=1;
seedteam20 1:0ec6c4c22623 339 for (int i=0;i<numSensors;i=i+1) {
seedteam20 1:0ec6c4c22623 340 if (stringComparison(sensors[i],"temp")==true) {
seedteam20 1:0ec6c4c22623 341 tempSensorflag=true;
seedteam20 1:0ec6c4c22623 342 tempField=field;
seedteam20 1:0ec6c4c22623 343 humField=field+1;
seedteam20 1:0ec6c4c22623 344 field=field+1;
seedteam20 1:0ec6c4c22623 345 } else if (stringComparison(sensors[i],"light")==true) {
seedteam20 1:0ec6c4c22623 346 lightSensorflag=true;
seedteam20 1:0ec6c4c22623 347 lightField=field;
seedteam20 1:0ec6c4c22623 348 } else if (stringComparison(sensors[i],"some other sensor")==true) {
seedteam20 1:0ec6c4c22623 349 SomeotherSensorflag=true;
seedteam20 1:0ec6c4c22623 350 }
seedteam20 1:0ec6c4c22623 351 field=field+1;
seedteam20 1:0ec6c4c22623 352 }
seedteam20 1:0ec6c4c22623 353
seedteam20 1:0ec6c4c22623 354 //set up CSV file
seedteam20 1:0ec6c4c22623 355 mkdir("/sd", 0777);
seedteam20 1:0ec6c4c22623 356 rtc.gettime( &sec, &minute, &hours, &day, &date, &month, &year);
seedteam20 1:0ec6c4c22623 357 sprintf(filename,"/sd/DataLog_20%.2d.%.2d.%.2d_%.2d.%.2d.csv",year, month, date, hours, minute);
seedteam20 1:0ec6c4c22623 358 FILE *fp = fopen(filename,"a");
seedteam20 1:0ec6c4c22623 359 if (fp == NULL) {
seedteam20 1:0ec6c4c22623 360 //no sd card
mbotkinl 2:89ca14fa896e 361 blueLED.blink(10);
seedteam20 1:0ec6c4c22623 362 } else {
seedteam20 1:0ec6c4c22623 363
seedteam20 1:0ec6c4c22623 364 //print header
seedteam20 1:0ec6c4c22623 365 fprintf(fp,"Timestamp, sensor1, sensor2 \r\n");
seedteam20 1:0ec6c4c22623 366 fclose(fp);
seedteam20 1:0ec6c4c22623 367 blueLED.blink(5);
seedteam20 1:0ec6c4c22623 368 }
seedteam20 1:0ec6c4c22623 369
seedteam20 1:0ec6c4c22623 370 //Debug Mode
mbotkinl 2:89ca14fa896e 371
seedteam20 1:0ec6c4c22623 372 for (int dd=0;dd<30;dd=dd+1) {
mbotkinl 2:89ca14fa896e 373
seedteam20 1:0ec6c4c22623 374 if (numSensors == 3) {
seedteam20 1:0ec6c4c22623 375
seedteam20 1:0ec6c4c22623 376 } else if (numSensors ==2) {
mbotkinl 2:89ca14fa896e 377
seedteam20 1:0ec6c4c22623 378 if (tempSensorflag==true) {
mbotkinl 2:89ca14fa896e 379
seedteam20 1:0ec6c4c22623 380 if (lightSensorflag==true) {
seedteam20 1:0ec6c4c22623 381 getLightReading(&lightData);
seedteam20 1:0ec6c4c22623 382 getTempHumid(&tempData,&humData);
seedteam20 1:0ec6c4c22623 383 sendData3(tempData,tempField,humData,humField,lightData,lightField);
seedteam20 1:0ec6c4c22623 384 sdWrite3(tempData,humData,lightData);
mbotkinl 2:89ca14fa896e 385 } else if (SomeotherSensorflag==true) {
mbotkinl 2:89ca14fa896e 386
seedteam20 1:0ec6c4c22623 387 }
seedteam20 1:0ec6c4c22623 388 } else if (lightSensorflag==true) {
mbotkinl 2:89ca14fa896e 389
mbotkinl 2:89ca14fa896e 390
seedteam20 1:0ec6c4c22623 391 if (SomeotherSensorflag==true) {
seedteam20 1:0ec6c4c22623 392 getLightReading(&lightData);
seedteam20 1:0ec6c4c22623 393
seedteam20 1:0ec6c4c22623 394
seedteam20 1:0ec6c4c22623 395 }
seedteam20 1:0ec6c4c22623 396 }
seedteam20 1:0ec6c4c22623 397 } else {
seedteam20 1:0ec6c4c22623 398 if (tempSensorflag==true) {
seedteam20 1:0ec6c4c22623 399 getTempHumid(&tempData,&humData);
seedteam20 1:0ec6c4c22623 400 sendData2(tempData,tempField,humData,humField);
seedteam20 1:0ec6c4c22623 401 sdWrite2(tempData,humData);
seedteam20 1:0ec6c4c22623 402 } else if (lightSensorflag==true) {
seedteam20 1:0ec6c4c22623 403 getLightReading(&lightData);
seedteam20 1:0ec6c4c22623 404 sendData1(lightData,lightField);
seedteam20 1:0ec6c4c22623 405 sdWrite1(lightData);
seedteam20 1:0ec6c4c22623 406 } else if (SomeotherSensorflag==true) {
seedteam20 1:0ec6c4c22623 407
seedteam20 1:0ec6c4c22623 408 }
seedteam20 1:0ec6c4c22623 409 }
seedteam20 1:0ec6c4c22623 410 wait(30);
seedteam20 1:0ec6c4c22623 411 }
seedteam20 1:0ec6c4c22623 412
mbotkinl 2:89ca14fa896e 413
seedteam20 1:0ec6c4c22623 414
seedteam20 1:0ec6c4c22623 415
seedteam20 1:0ec6c4c22623 416
seedteam20 1:0ec6c4c22623 417 //normal mode
seedteam20 1:0ec6c4c22623 418 while (1) {
seedteam20 1:0ec6c4c22623 419
seedteam20 1:0ec6c4c22623 420 if (numSensors == 3) {
seedteam20 1:0ec6c4c22623 421
seedteam20 1:0ec6c4c22623 422 } else if (numSensors ==2) {
seedteam20 1:0ec6c4c22623 423 if (tempSensorflag==true) {
seedteam20 1:0ec6c4c22623 424 if (lightSensorflag==true) {
seedteam20 1:0ec6c4c22623 425 getLightReading(&lightData);
seedteam20 1:0ec6c4c22623 426 getTempHumid(&tempData,&humData);
seedteam20 1:0ec6c4c22623 427 sendData3(tempData,tempField,humData,humField,lightData,lightField);
seedteam20 1:0ec6c4c22623 428 sdWrite3(tempData,humData,lightData);
seedteam20 1:0ec6c4c22623 429 } else if (SomeotherSensorflag==true) {
seedteam20 1:0ec6c4c22623 430
seedteam20 1:0ec6c4c22623 431 }
seedteam20 1:0ec6c4c22623 432 } else if (lightSensorflag==true) {
seedteam20 1:0ec6c4c22623 433 if (SomeotherSensorflag==true) {
seedteam20 1:0ec6c4c22623 434 getLightReading(&lightData);
seedteam20 1:0ec6c4c22623 435
seedteam20 1:0ec6c4c22623 436
seedteam20 1:0ec6c4c22623 437 }
seedteam20 1:0ec6c4c22623 438 }
seedteam20 1:0ec6c4c22623 439 } else {
seedteam20 1:0ec6c4c22623 440 if (tempSensorflag==true) {
seedteam20 1:0ec6c4c22623 441 getTempHumid(&tempData,&humData);
seedteam20 1:0ec6c4c22623 442 sendData2(tempData,tempField,humData,humField);
seedteam20 1:0ec6c4c22623 443 sdWrite2(tempData,humData);
seedteam20 1:0ec6c4c22623 444 } else if (lightSensorflag==true) {
seedteam20 1:0ec6c4c22623 445 getLightReading(&lightData);
seedteam20 1:0ec6c4c22623 446 sendData1(lightData,lightField);
seedteam20 1:0ec6c4c22623 447 sdWrite1(lightData);
seedteam20 1:0ec6c4c22623 448 } else if (SomeotherSensorflag==true) {
seedteam20 1:0ec6c4c22623 449
seedteam20 1:0ec6c4c22623 450 }
seedteam20 1:0ec6c4c22623 451 }
seedteam20 1:0ec6c4c22623 452 wdt_sleep.sleep(BROADCAST_TIME);
seedteam20 1:0ec6c4c22623 453
seedteam20 1:0ec6c4c22623 454 }
seedteam20 1:0ec6c4c22623 455 }