draft2
Dependencies: ARCH_GPRS_V2_HW Blinker GPRSInterface HTTPClient_GPRS RTC_WorkingLibrary SDFileSystem USBDevice mbed
finalCodev2.cpp
00001 //SEED Team 20 Final Code draft 1 4/16/15 00002 00003 #include "mbed.h" 00004 #include "SDFileSystem.h" 00005 #include "ARCH_GPRS_V2_HW.h" 00006 #include "HTTPClient.h" 00007 #include "GPRSInterface.h" 00008 #include "Blinker.h" 00009 #include "DHT.h" 00010 #include "i2c_uart.h" 00011 #include "ARCH_GPRS_Sleep.h" 00012 #include "ds1307.h" 00013 #include "USBSerial.h" 00014 #include <string> 00015 00016 #define PIN_TX P1_27 00017 #define PIN_RX P1_26 00018 00019 #define SDA P0_5 00020 #define SCL P0_4 00021 00022 00023 #define BROADCAST_TIME 300 00024 00025 USBSerial pc; 00026 00027 //LED Blink 00028 DigitalOut line11(P0_9); 00029 DigitalOut line13(P0_2); 00030 DigitalOut line14(P1_28); 00031 00032 Blinker yellowLED(LED1), redLED(LED2), greenLED(LED3), blueLED(LED4); 00033 00034 //USBSerial pc; 00035 SDFileSystem sd(P1_22, P1_21, P1_20, P1_23, "sd"); // the pinout on the /Arch GPRS v2 mbed board. 00036 char filename[60]; 00037 00038 //RTC 00039 DS1307 rtc(SDA,SCL); 00040 int sec, minute, hours, day, date, month, year; 00041 char timestamp[17]; 00042 00043 //variables for reading data from SD card 00044 char APIKey[17]; 00045 char sensors[3][5]; 00046 char sensorBuff[5]; 00047 int tempField, humField, lightField; 00048 int field1, field2, field3; 00049 int numSensors; 00050 bool tempSensorflag=false, lightSensorflag=false, SomeotherSensorflag=false; 00051 00052 //GPRS setup 00053 GPRSInterface gprs(PIN_TX,PIN_RX,115200,"internetd.gdsp",NULL,NULL); 00054 HTTPClient http; 00055 char* thingSpeakUrl = "http://api.thingspeak.com/update"; 00056 char urlBuffer[256]; 00057 char str[1024]; 00058 //char errorR[1024]; 00059 string errorR; 00060 00061 //Sensors 00062 //DHT sensor(P1_14,SEN51035P); 00063 DHT sensor(P1_14,SEN11301P); 00064 AnalogIn lightSensor(P0_12); 00065 00066 00067 int tempData,humData, lightData; 00068 00069 00070 void connectGPRS(void) { 00071 iot_hw.init(); // power on SIM900 00072 00073 int count = 0; 00074 while(false == gprs.connect() && count < 5) { 00075 wait(2); 00076 count += 1; 00077 } 00078 } 00079 00080 void getTempHumid(int* tempData,int* humData){ 00081 int err = 1; 00082 int count = 0; 00083 iot_hw.grovePwrOn(); 00084 wait(1); // wait 1 second for device stable status 00085 while (err != 0 && count < 4) { 00086 err = sensor.readData(); 00087 count += 1; 00088 *tempData = sensor.ReadTemperature(FARENHEIT); 00089 *humData = sensor.ReadHumidity(); 00090 00091 wait(1); 00092 } 00093 iot_hw.grovePwrOff(); 00094 } 00095 00096 void getLightReading(int* lightData) 00097 { 00098 *lightData = lightSensor*1000; 00099 } 00100 00101 00102 int ReadFile (void) { 00103 mkdir("/sd", 0777); // All other times open file in append mode 00104 FILE *fp = fopen("/sd/config.txt","r"); 00105 if (fp==NULL) { 00106 fclose(fp); 00107 return 0; 00108 00109 } else if (fp) { 00110 //fscanf(fp,"%16c %d %s %s", APIKey, &numSensors, sensors[0],sensors[1]); 00111 fscanf(fp,"%16c",APIKey); 00112 00113 // pc.printf("APIKEY= %s\r\n",APIKey); 00114 // 00115 fscanf(fp,"%d",&numSensors); 00116 // pc.printf("number of sensors = %d \r\n",numSensors); 00117 // 00118 for (int i = 0; i<numSensors;i=i+1) { 00119 fscanf(fp,"%s",sensors[i]); 00120 //fscanf(fp,"%s",sensorBuff); 00121 strcpy(sensors[i],sensorBuff); 00122 //pc.printf("sensor %d = %s\r\n",i,sensors[i]); 00123 } 00124 00125 00126 fclose(fp); 00127 return 1; 00128 00129 } else { 00130 fclose(fp); 00131 return 0; 00132 } 00133 00134 } 00135 00136 bool stringComparison (const char *string1, const char *string2) { 00137 int count = 0; 00138 while (string1[count] != 0) { 00139 if (string1[count] != string2[count]) 00140 return false; 00141 count++; 00142 } 00143 return true; 00144 } 00145 00146 00147 void sendData1(int sensor1Data, int field1) { 00148 connectGPRS(); 00149 urlBuffer[0] = 0; 00150 sprintf(urlBuffer, "%s?key=%s&field%d=%d", thingSpeakUrl, APIKey, field1, sensor1Data); 00151 HTTPResult res = http.get(urlBuffer, str,128); 00152 if (res != HTTP_OK) { 00153 redLED.blink(5); 00154 } else { 00155 greenLED.blink(5); 00156 } 00157 00158 00159 iot_hw.init_io(); //power down SIM900 00160 } 00161 00162 void sendData2(int sensor1Data, int field1, int sensor2Data, int field2) { 00163 connectGPRS(); 00164 urlBuffer[0] = 0; 00165 sprintf(urlBuffer, "%s?key=%s&field%d=%d&field%d=%d", thingSpeakUrl, APIKey, field1, sensor1Data, field2, sensor2Data); 00166 HTTPResult res = http.get(urlBuffer, str,128); 00167 if (res != HTTP_OK) { 00168 redLED.blink(5); 00169 } else { 00170 greenLED.blink(5); 00171 } 00172 iot_hw.init_io(); //power down SIM900 00173 } 00174 00175 void sendData3(int sensor1Data, int field1, int sensor2Data, int field2, int sensor3Data, int field3) { 00176 connectGPRS(); 00177 urlBuffer[0] = 0; 00178 sprintf(urlBuffer, "%s?key=%s&field%d=%d&field%d=%d&field%d=%d", thingSpeakUrl, APIKey, field1, sensor1Data, field2, sensor2Data, field3, sensor3Data); 00179 HTTPResult res = http.get(urlBuffer, str,128); 00180 if (res != HTTP_OK) { 00181 redLED.blink(5); 00182 } else { 00183 greenLED.blink(5); 00184 } 00185 iot_hw.init_io(); //power down SIM900 00186 } 00187 00188 //void errorWrite(const char* errorReport) { 00189 void errorWrite(string errorReport) { 00190 rtc.gettime( &sec, &minute, &hours, &day, &date, &month, &year); 00191 sprintf(timestamp,"20%.2d-%.2d-%.2d %.2d:%.2d:%.2d", year, month, date, hours, minute, sec); 00192 00193 mkdir("/sd", 0777); 00194 FILE *fp = fopen("/sd/ErrorLog.txt","a"); 00195 if (fp == NULL) { 00196 //no sd card 00197 redLED.blink(10); 00198 } else { 00199 pc.printf("%s: %s \r\n",timestamp,errorReport); 00200 fprintf(fp,"%s: %s \r\n",timestamp,errorReport); 00201 fclose(fp); 00202 blueLED.blink(5); 00203 } 00204 } 00205 00206 void sdWrite1(int sensor1Data) 00207 { 00208 rtc.gettime( &sec, &minute, &hours, &day, &date, &month, &year); 00209 sprintf(timestamp,"20%.2d-%.2d-%.2d %.2d:%.2d:%.2d", year, month, date, hours, minute, sec); 00210 00211 mkdir("/sd", 0777); 00212 FILE *fp = fopen("/sd/node1.csv","a"); 00213 if (fp == NULL) { 00214 //no sd card 00215 redLED.blink(10); 00216 } else { 00217 00218 fprintf(fp,"%s, %d \r\n",timestamp,sensor1Data); 00219 fclose(fp); 00220 blueLED.blink(5); 00221 } 00222 } 00223 00224 void sdWrite2(int sensor1Data, int sensor2Data) 00225 { 00226 rtc.gettime( &sec, &minute, &hours, &day, &date, &month, &year); 00227 sprintf(timestamp,"20%.2d-%.2d-%.2d %.2d:%.2d:%.2d", year, month, date, hours, minute, sec); 00228 00229 mkdir("/sd", 0777); 00230 FILE *fp = fopen("/sd/node1.csv","a"); 00231 if (fp == NULL) { 00232 //no sd card 00233 redLED.blink(10); 00234 } else { 00235 00236 fprintf(fp,"%s, %d, %d\r\n",timestamp, sensor1Data, sensor2Data); 00237 fclose(fp); 00238 blueLED.blink(5); 00239 } 00240 } 00241 00242 void sdWrite3(int sensor1Data, int sensor2Data,int sensor3Data) 00243 { 00244 rtc.gettime( &sec, &minute, &hours, &day, &date, &month, &year); 00245 sprintf(timestamp,"20%.2d-%.2d-%.2d %.2d:%.2d:%.2d", year, month, date, hours, minute, sec); 00246 00247 mkdir("/sd", 0777); 00248 FILE *fp = fopen("/sd/node1.csv","a"); 00249 if (fp == NULL) { 00250 //no sd card 00251 redLED.blink(10); 00252 } else { 00253 00254 fprintf(fp,"%s, %d, %d, %d\r\n",timestamp, sensor1Data, sensor2Data, sensor3Data); 00255 fclose(fp); 00256 blueLED.blink(5); 00257 } 00258 } 00259 00260 00261 00262 00263 int main() { 00264 wdt_sleep.wdtClkSetup(WDTCLK_SRC_IRC_OSC); //set up sleep 00265 00266 00267 00268 //test of breadboard 00269 // while (1) { 00270 // //rtc test 00271 // errorR="Test rtc"; 00272 // rtc.gettime( &sec, &minute, &hours, &day, &date, &month, &year); 00273 // sprintf(timestamp,"20%.2d-%.2d-%.2d %.2d:%.2d:%.2d", year, month, date, hours, minute, sec); 00274 // pc.printf("%s: %s\r\n",timestamp,errorR); 00275 // 00276 // //rgb test 00277 // int n=5; 00278 // int t=1; 00279 // pc.printf("line11 blink\r\n"); 00280 // for (int i = 0; i < n; i++) { 00281 // line11 = 1; 00282 // wait(t); 00283 // line11 = 0; 00284 // wait(t); 00285 // } 00286 // 00287 // pc.printf("line13 blink\r\n"); 00288 // 00289 // for (int i = 0; i < n; i++) { 00290 // line13 = 1; 00291 // wait(t); 00292 // line13 = 0; 00293 // wait(t); 00294 // } 00295 // 00296 // pc.printf("line11 blink\r\n"); 00297 // 00298 // for (int i = 0; i < n; i++) { 00299 // line14 = 1; 00300 // wait(t); 00301 // line14 = 0; 00302 // wait(t); 00303 // } 00304 // //light sensor test 00305 // getLightReading(&lightData); 00306 // pc.printf("light reading: %d \r\n",lightData); 00307 // 00308 // wait(15); 00309 // } 00310 00311 00312 //read SD card 00313 while (ReadFile()==0) { 00314 wait(5); 00315 redLED.blink(10); 00316 } 00317 greenLED.blink(10); 00318 00319 00320 //identify sensors and find order of fields 00321 int field=1; 00322 for (int i=0;i<numSensors;i=i+1) { 00323 if (stringComparison(sensors[i],"temp")==true) { 00324 tempSensorflag=true; 00325 tempField=field; 00326 humField=field+1; 00327 field=field+1; 00328 } else if (stringComparison(sensors[i],"light")==true) { 00329 lightSensorflag=true; 00330 lightField=field; 00331 } else if (stringComparison(sensors[i],"some other sensor")==true) { 00332 SomeotherSensorflag=true; 00333 } 00334 field=field+1; 00335 } 00336 00337 //set up CSV file 00338 mkdir("/sd", 0777); 00339 rtc.gettime( &sec, &minute, &hours, &day, &date, &month, &year); 00340 sprintf(filename,"/sd/DataLog_20%.2d.%.2d.%.2d_%.2d.%.2d.csv",year, month, date, hours, minute); 00341 FILE *fp = fopen(filename,"a"); 00342 if (fp == NULL) { 00343 //no sd card 00344 redLED.blink(10); 00345 } else { 00346 00347 //print header 00348 fprintf(fp,"Timestamp, sensor1, sensor2 \r\n"); 00349 fclose(fp); 00350 blueLED.blink(5); 00351 } 00352 00353 //Debug Mode 00354 for (int dd=0;dd<30;dd=dd+1) { 00355 00356 if (numSensors == 3) { 00357 00358 } else if (numSensors ==2) { 00359 if (tempSensorflag==true) { 00360 if (lightSensorflag==true) { 00361 getLightReading(&lightData); 00362 getTempHumid(&tempData,&humData); 00363 sendData3(tempData,tempField,humData,humField,lightData,lightField); 00364 sdWrite3(tempData,humData,lightData); 00365 } else if (SomeotherSensorflag==true) { 00366 00367 } 00368 } else if (lightSensorflag==true) { 00369 if (SomeotherSensorflag==true) { 00370 getLightReading(&lightData); 00371 00372 00373 } 00374 } 00375 } else { 00376 if (tempSensorflag==true) { 00377 getTempHumid(&tempData,&humData); 00378 sendData2(tempData,tempField,humData,humField); 00379 sdWrite2(tempData,humData); 00380 } else if (lightSensorflag==true) { 00381 getLightReading(&lightData); 00382 sendData1(lightData,lightField); 00383 sdWrite1(lightData); 00384 } else if (SomeotherSensorflag==true) { 00385 00386 } 00387 } 00388 wait(30); 00389 } 00390 00391 00392 00393 00394 00395 //normal mode 00396 while (1) { 00397 00398 if (numSensors == 3) { 00399 00400 } else if (numSensors ==2) { 00401 if (tempSensorflag==true) { 00402 if (lightSensorflag==true) { 00403 getLightReading(&lightData); 00404 getTempHumid(&tempData,&humData); 00405 sendData3(tempData,tempField,humData,humField,lightData,lightField); 00406 sdWrite3(tempData,humData,lightData); 00407 } else if (SomeotherSensorflag==true) { 00408 00409 } 00410 } else if (lightSensorflag==true) { 00411 if (SomeotherSensorflag==true) { 00412 getLightReading(&lightData); 00413 00414 00415 } 00416 } 00417 } else { 00418 if (tempSensorflag==true) { 00419 getTempHumid(&tempData,&humData); 00420 sendData2(tempData,tempField,humData,humField); 00421 sdWrite2(tempData,humData); 00422 } else if (lightSensorflag==true) { 00423 getLightReading(&lightData); 00424 sendData1(lightData,lightField); 00425 sdWrite1(lightData); 00426 } else if (SomeotherSensorflag==true) { 00427 00428 } 00429 } 00430 wdt_sleep.sleep(BROADCAST_TIME); 00431 00432 } 00433 }
Generated on Thu Jul 14 2022 16:35:32 by 1.7.2