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
finalCodev3.cpp@5:96d91bbd6c14, 2015-05-08 (annotated)
- Committer:
- mbotkinl
- Date:
- Fri May 08 14:57:00 2015 +0000
- Revision:
- 5:96d91bbd6c14
- Parent:
- 4:f7f454e6c865
Final working version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
seedteam20 | 1:0ec6c4c22623 | 1 | //SEED Team 20 Final Code draft 1 4/16/15 |
mbotkinl | 5:96d91bbd6c14 | 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 |
mbotkinl | 3:8d414873535d | 63 | DHT sensor(P1_14,SEN51035P); |
mbotkinl | 3:8d414873535d | 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 | 5:96d91bbd6c14 | 107 | //method to write error reports to ErrorLog.txt file on SD card |
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 | 5:96d91bbd6c14 | 123 | |
mbotkinl | 5:96d91bbd6c14 | 124 | //method to read config.txt file on SD card |
mbotkinl | 2:89ca14fa896e | 125 | bool ReadFile (void) { |
seedteam20 | 1:0ec6c4c22623 | 126 | mkdir("/sd", 0777); // All other times open file in append mode |
seedteam20 | 1:0ec6c4c22623 | 127 | FILE *fp = fopen("/sd/config.txt","r"); |
seedteam20 | 1:0ec6c4c22623 | 128 | if (fp==NULL) { |
seedteam20 | 1:0ec6c4c22623 | 129 | fclose(fp); |
mbotkinl | 2:89ca14fa896e | 130 | blueLED.blink(10); |
mbotkinl | 2:89ca14fa896e | 131 | errorR="Read config.txt file error on SD card"; |
mbotkinl | 2:89ca14fa896e | 132 | errorWrite(errorR); |
mbotkinl | 2:89ca14fa896e | 133 | return false; |
seedteam20 | 1:0ec6c4c22623 | 134 | |
seedteam20 | 1:0ec6c4c22623 | 135 | } else if (fp) { |
mbotkinl | 5:96d91bbd6c14 | 136 | |
mbotkinl | 5:96d91bbd6c14 | 137 | fscanf(fp,"%16c",APIKey); |
seedteam20 | 1:0ec6c4c22623 | 138 | fscanf(fp,"%d",&numSensors); |
mbotkinl | 5:96d91bbd6c14 | 139 | |
seedteam20 | 1:0ec6c4c22623 | 140 | for (int i = 0; i<numSensors;i=i+1) { |
mbotkinl | 2:89ca14fa896e | 141 | fscanf(fp,"%s",sensorBuff); |
seedteam20 | 1:0ec6c4c22623 | 142 | strcpy(sensors[i],sensorBuff); |
seedteam20 | 1:0ec6c4c22623 | 143 | } |
seedteam20 | 1:0ec6c4c22623 | 144 | |
seedteam20 | 1:0ec6c4c22623 | 145 | fclose(fp); |
mbotkinl | 2:89ca14fa896e | 146 | return true; |
seedteam20 | 1:0ec6c4c22623 | 147 | |
seedteam20 | 1:0ec6c4c22623 | 148 | } else { |
seedteam20 | 1:0ec6c4c22623 | 149 | fclose(fp); |
mbotkinl | 2:89ca14fa896e | 150 | blueLED.blink(10); |
mbotkinl | 2:89ca14fa896e | 151 | errorR="Read config.txt file error on SD card"; |
mbotkinl | 2:89ca14fa896e | 152 | errorWrite(errorR); |
mbotkinl | 2:89ca14fa896e | 153 | return false; |
seedteam20 | 1:0ec6c4c22623 | 154 | } |
seedteam20 | 1:0ec6c4c22623 | 155 | |
seedteam20 | 1:0ec6c4c22623 | 156 | } |
seedteam20 | 1:0ec6c4c22623 | 157 | |
seedteam20 | 1:0ec6c4c22623 | 158 | bool stringComparison (const char *string1, const char *string2) { |
seedteam20 | 1:0ec6c4c22623 | 159 | int count = 0; |
seedteam20 | 1:0ec6c4c22623 | 160 | while (string1[count] != 0) { |
seedteam20 | 1:0ec6c4c22623 | 161 | if (string1[count] != string2[count]) |
seedteam20 | 1:0ec6c4c22623 | 162 | return false; |
seedteam20 | 1:0ec6c4c22623 | 163 | count++; |
seedteam20 | 1:0ec6c4c22623 | 164 | } |
seedteam20 | 1:0ec6c4c22623 | 165 | return true; |
seedteam20 | 1:0ec6c4c22623 | 166 | } |
seedteam20 | 1:0ec6c4c22623 | 167 | |
seedteam20 | 1:0ec6c4c22623 | 168 | |
seedteam20 | 1:0ec6c4c22623 | 169 | //method to send 1 field of data to ThingsSpeak |
seedteam20 | 1:0ec6c4c22623 | 170 | //blinks green 5 times if successful |
seedteam20 | 1:0ec6c4c22623 | 171 | //blinks red 5 times if get statement fails |
seedteam20 | 1:0ec6c4c22623 | 172 | //blinks red 10 times if fails to connectGPRS |
seedteam20 | 1:0ec6c4c22623 | 173 | void sendData1(int sensor1Data, int field1) |
seedteam20 | 1:0ec6c4c22623 | 174 | { |
seedteam20 | 1:0ec6c4c22623 | 175 | if (connectGPRS() == true) { //connect is success |
seedteam20 | 1:0ec6c4c22623 | 176 | urlBuffer[0] = 0; |
seedteam20 | 1:0ec6c4c22623 | 177 | sprintf(urlBuffer, "%s?key=%s&field%d=%d", thingSpeakUrl, APIKey, field1, sensor1Data); //create url |
seedteam20 | 1:0ec6c4c22623 | 178 | HTTPResult res = http.get(urlBuffer, str,128); //send get request |
mbotkinl | 2:89ca14fa896e | 179 | if (res != HTTP_OK) { |
mbotkinl | 2:89ca14fa896e | 180 | redLED.blink(5); //get statement failed blink red 5 times |
mbotkinl | 2:89ca14fa896e | 181 | errorR="HTTP Request Error"; |
mbotkinl | 2:89ca14fa896e | 182 | errorWrite(errorR); |
mbotkinl | 2:89ca14fa896e | 183 | } else greenLED.blink(5); //get statement success blink green 5 times |
seedteam20 | 1:0ec6c4c22623 | 184 | iot_hw.init_io(); //power down SIM900 |
seedteam20 | 1:0ec6c4c22623 | 185 | |
seedteam20 | 1:0ec6c4c22623 | 186 | } else { //failed to connectGPRS |
seedteam20 | 1:0ec6c4c22623 | 187 | redLED.blink(10); //blink red 10 times |
seedteam20 | 1:0ec6c4c22623 | 188 | iot_hw.init_io(); //power down SIM900 |
mbotkinl | 2:89ca14fa896e | 189 | errorR="Failed to connect to GPRS"; |
mbotkinl | 2:89ca14fa896e | 190 | errorWrite(errorR); |
seedteam20 | 1:0ec6c4c22623 | 191 | } |
seedteam20 | 1:0ec6c4c22623 | 192 | } |
seedteam20 | 1:0ec6c4c22623 | 193 | |
seedteam20 | 1:0ec6c4c22623 | 194 | //method to send 2 field of data to ThingsSpeak |
seedteam20 | 1:0ec6c4c22623 | 195 | void sendData2(int sensor1Data, int field1, int sensor2Data, int field2) |
seedteam20 | 1:0ec6c4c22623 | 196 | { |
seedteam20 | 1:0ec6c4c22623 | 197 | if (connectGPRS() == true) { |
seedteam20 | 1:0ec6c4c22623 | 198 | urlBuffer[0] = 0; |
seedteam20 | 1:0ec6c4c22623 | 199 | sprintf(urlBuffer, "%s?key=%s&field%d=%d&field%d=%d", thingSpeakUrl, APIKey, field1, sensor1Data, field2, sensor2Data); //create url |
seedteam20 | 1:0ec6c4c22623 | 200 | HTTPResult res = http.get(urlBuffer, str,128); //send get request |
mbotkinl | 2:89ca14fa896e | 201 | if (res != HTTP_OK) { |
mbotkinl | 2:89ca14fa896e | 202 | redLED.blink(5); //get statement failed blink red 5 times |
mbotkinl | 2:89ca14fa896e | 203 | errorR="HTTP Request Error"; |
mbotkinl | 2:89ca14fa896e | 204 | errorWrite(errorR); |
mbotkinl | 2:89ca14fa896e | 205 | } else greenLED.blink(5); //get statement success blink green 5 times |
mbotkinl | 2:89ca14fa896e | 206 | iot_hw.init_io(); //power down SIM900 |
mbotkinl | 2:89ca14fa896e | 207 | |
mbotkinl | 2:89ca14fa896e | 208 | } else { //failed to connectGPRS |
seedteam20 | 1:0ec6c4c22623 | 209 | redLED.blink(10); //blink red 10 times |
seedteam20 | 1:0ec6c4c22623 | 210 | iot_hw.init_io(); //power down SIM900 |
mbotkinl | 2:89ca14fa896e | 211 | errorR="Failed to connect to GPRS"; |
mbotkinl | 2:89ca14fa896e | 212 | errorWrite(errorR); |
seedteam20 | 1:0ec6c4c22623 | 213 | } |
seedteam20 | 1:0ec6c4c22623 | 214 | } |
seedteam20 | 1:0ec6c4c22623 | 215 | |
seedteam20 | 1:0ec6c4c22623 | 216 | //method to send 3 field of data to ThingsSpeak |
seedteam20 | 1:0ec6c4c22623 | 217 | void sendData3(int sensor1Data, int field1, int sensor2Data, int field2, int sensor3Data, int field3) |
seedteam20 | 1:0ec6c4c22623 | 218 | { |
seedteam20 | 1:0ec6c4c22623 | 219 | if (connectGPRS() == true) { |
seedteam20 | 1:0ec6c4c22623 | 220 | urlBuffer[0] = 0; |
seedteam20 | 1:0ec6c4c22623 | 221 | 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 | 222 | HTTPResult res = http.get(urlBuffer, str,128); //send get request |
mbotkinl | 2:89ca14fa896e | 223 | if (res != HTTP_OK) { |
mbotkinl | 2:89ca14fa896e | 224 | redLED.blink(5); //get statement failed blink red 5 times |
mbotkinl | 2:89ca14fa896e | 225 | errorR="HTTP Request Error"; |
mbotkinl | 2:89ca14fa896e | 226 | errorWrite(errorR); |
mbotkinl | 2:89ca14fa896e | 227 | } else greenLED.blink(5); //get statement success blink green 5 times |
mbotkinl | 2:89ca14fa896e | 228 | iot_hw.init_io(); //power down SIM900 |
mbotkinl | 2:89ca14fa896e | 229 | |
mbotkinl | 2:89ca14fa896e | 230 | } else { //failed to connectGPRS |
seedteam20 | 1:0ec6c4c22623 | 231 | redLED.blink(10); //blink red 10 times |
seedteam20 | 1:0ec6c4c22623 | 232 | iot_hw.init_io(); //power down SIM900 |
mbotkinl | 2:89ca14fa896e | 233 | errorR="Failed to connect to GPRS"; |
mbotkinl | 2:89ca14fa896e | 234 | errorWrite(errorR); |
seedteam20 | 1:0ec6c4c22623 | 235 | } |
seedteam20 | 1:0ec6c4c22623 | 236 | } |
seedteam20 | 1:0ec6c4c22623 | 237 | |
mbotkinl | 2:89ca14fa896e | 238 | |
seedteam20 | 1:0ec6c4c22623 | 239 | |
seedteam20 | 1:0ec6c4c22623 | 240 | //writes data to SDcard for 1 field |
seedteam20 | 1:0ec6c4c22623 | 241 | //if no SDcard blink blue 10 times |
seedteam20 | 1:0ec6c4c22623 | 242 | void sdWrite1(int sensor1Data) |
seedteam20 | 1:0ec6c4c22623 | 243 | { |
seedteam20 | 1:0ec6c4c22623 | 244 | rtc.gettime( &sec, &minute, &hours, &day, &date, &month, &year); //get time from RTC |
seedteam20 | 1:0ec6c4c22623 | 245 | sprintf(timestamp,"20%.2d-%.2d-%.2d %.2d:%.2d:%.2d", year, month, date, hours, minute, sec); //create time stamp |
seedteam20 | 1:0ec6c4c22623 | 246 | |
seedteam20 | 1:0ec6c4c22623 | 247 | mkdir("/sd", 0777); |
mbotkinl | 4:f7f454e6c865 | 248 | //FILE *fp = fopen("/sd/node1.csv","a"); |
mbotkinl | 4:f7f454e6c865 | 249 | FILE *fp = fopen(filename,"a"); |
mbotkinl | 4:f7f454e6c865 | 250 | |
seedteam20 | 1:0ec6c4c22623 | 251 | if (fp == NULL) { |
mbotkinl | 2:89ca14fa896e | 252 | blueLED.blink(10); //no sd Card blink Blue 10 times |
seedteam20 | 1:0ec6c4c22623 | 253 | } else { |
seedteam20 | 1:0ec6c4c22623 | 254 | fprintf(fp,"%s, %d \r\n",timestamp,sensor1Data); //write time stamp and data to microSD |
seedteam20 | 1:0ec6c4c22623 | 255 | fclose(fp); |
mbotkinl | 2:89ca14fa896e | 256 | blueLED.blink(5); |
seedteam20 | 1:0ec6c4c22623 | 257 | } |
seedteam20 | 1:0ec6c4c22623 | 258 | } |
seedteam20 | 1:0ec6c4c22623 | 259 | |
seedteam20 | 1:0ec6c4c22623 | 260 | //writes data to SDcard for 2 fields |
seedteam20 | 1:0ec6c4c22623 | 261 | //if no SDcard blink blue 10 times |
seedteam20 | 1:0ec6c4c22623 | 262 | void sdWrite2(int sensor1Data, int sensor2Data) |
seedteam20 | 1:0ec6c4c22623 | 263 | { |
seedteam20 | 1:0ec6c4c22623 | 264 | rtc.gettime( &sec, &minute, &hours, &day, &date, &month, &year); //get time from RTC |
seedteam20 | 1:0ec6c4c22623 | 265 | sprintf(timestamp,"20%.2d-%.2d-%.2d %.2d:%.2d:%.2d", year, month, date, hours, minute, sec); //create timestamp |
seedteam20 | 1:0ec6c4c22623 | 266 | |
seedteam20 | 1:0ec6c4c22623 | 267 | mkdir("/sd", 0777); |
mbotkinl | 4:f7f454e6c865 | 268 | //FILE *fp = fopen("/sd/node1.csv","a"); |
mbotkinl | 4:f7f454e6c865 | 269 | FILE *fp = fopen(filename,"a"); |
mbotkinl | 4:f7f454e6c865 | 270 | |
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); |
mbotkinl | 4:f7f454e6c865 | 288 | //FILE *fp = fopen("/sd/node1.csv","a"); |
mbotkinl | 4:f7f454e6c865 | 289 | FILE *fp = fopen(filename,"a"); |
seedteam20 | 1:0ec6c4c22623 | 290 | if (fp == NULL) { |
seedteam20 | 1:0ec6c4c22623 | 291 | blueLED.blink(10); //no sd card blink blue 10 times |
seedteam20 | 1:0ec6c4c22623 | 292 | } else { |
seedteam20 | 1:0ec6c4c22623 | 293 | fprintf(fp,"%s, %d, %d, %d\r\n",timestamp, sensor1Data, sensor2Data, sensor3Data); //write timestamp+data to sd |
seedteam20 | 1:0ec6c4c22623 | 294 | fclose(fp); |
mbotkinl | 2:89ca14fa896e | 295 | blueLED.blink(5); |
seedteam20 | 1:0ec6c4c22623 | 296 | } |
seedteam20 | 1:0ec6c4c22623 | 297 | } |
seedteam20 | 1:0ec6c4c22623 | 298 | |
seedteam20 | 1:0ec6c4c22623 | 299 | ///////////////////////////////////////////////////////////////////////////////////////////////////////////// |
seedteam20 | 1:0ec6c4c22623 | 300 | ///////////////////////////////////////////////////////////////////////////////////////////////////////////// |
seedteam20 | 1:0ec6c4c22623 | 301 | int main() { |
seedteam20 | 1:0ec6c4c22623 | 302 | wdt_sleep.wdtClkSetup(WDTCLK_SRC_IRC_OSC); //set up sleep |
mbotkinl | 2:89ca14fa896e | 303 | wait(2); |
seedteam20 | 1:0ec6c4c22623 | 304 | |
seedteam20 | 1:0ec6c4c22623 | 305 | |
seedteam20 | 1:0ec6c4c22623 | 306 | //read SD card |
mbotkinl | 2:89ca14fa896e | 307 | while(ReadFile()==false) { |
seedteam20 | 1:0ec6c4c22623 | 308 | wait(5); |
mbotkinl | 2:89ca14fa896e | 309 | blueLED.blink(10); |
seedteam20 | 1:0ec6c4c22623 | 310 | } |
seedteam20 | 1:0ec6c4c22623 | 311 | greenLED.blink(10); |
seedteam20 | 1:0ec6c4c22623 | 312 | |
seedteam20 | 1:0ec6c4c22623 | 313 | |
seedteam20 | 1:0ec6c4c22623 | 314 | //identify sensors and find order of fields |
seedteam20 | 1:0ec6c4c22623 | 315 | int field=1; |
seedteam20 | 1:0ec6c4c22623 | 316 | for (int i=0;i<numSensors;i=i+1) { |
seedteam20 | 1:0ec6c4c22623 | 317 | if (stringComparison(sensors[i],"temp")==true) { |
seedteam20 | 1:0ec6c4c22623 | 318 | tempSensorflag=true; |
seedteam20 | 1:0ec6c4c22623 | 319 | tempField=field; |
seedteam20 | 1:0ec6c4c22623 | 320 | humField=field+1; |
seedteam20 | 1:0ec6c4c22623 | 321 | field=field+1; |
seedteam20 | 1:0ec6c4c22623 | 322 | } else if (stringComparison(sensors[i],"light")==true) { |
seedteam20 | 1:0ec6c4c22623 | 323 | lightSensorflag=true; |
seedteam20 | 1:0ec6c4c22623 | 324 | lightField=field; |
seedteam20 | 1:0ec6c4c22623 | 325 | } else if (stringComparison(sensors[i],"some other sensor")==true) { |
seedteam20 | 1:0ec6c4c22623 | 326 | SomeotherSensorflag=true; |
seedteam20 | 1:0ec6c4c22623 | 327 | } |
seedteam20 | 1:0ec6c4c22623 | 328 | field=field+1; |
seedteam20 | 1:0ec6c4c22623 | 329 | } |
seedteam20 | 1:0ec6c4c22623 | 330 | |
seedteam20 | 1:0ec6c4c22623 | 331 | //set up CSV file |
seedteam20 | 1:0ec6c4c22623 | 332 | mkdir("/sd", 0777); |
seedteam20 | 1:0ec6c4c22623 | 333 | rtc.gettime( &sec, &minute, &hours, &day, &date, &month, &year); |
seedteam20 | 1:0ec6c4c22623 | 334 | sprintf(filename,"/sd/DataLog_20%.2d.%.2d.%.2d_%.2d.%.2d.csv",year, month, date, hours, minute); |
seedteam20 | 1:0ec6c4c22623 | 335 | FILE *fp = fopen(filename,"a"); |
seedteam20 | 1:0ec6c4c22623 | 336 | if (fp == NULL) { |
seedteam20 | 1:0ec6c4c22623 | 337 | //no sd card |
mbotkinl | 2:89ca14fa896e | 338 | blueLED.blink(10); |
seedteam20 | 1:0ec6c4c22623 | 339 | } else { |
seedteam20 | 1:0ec6c4c22623 | 340 | |
seedteam20 | 1:0ec6c4c22623 | 341 | //print header |
seedteam20 | 1:0ec6c4c22623 | 342 | fprintf(fp,"Timestamp, sensor1, sensor2 \r\n"); |
seedteam20 | 1:0ec6c4c22623 | 343 | fclose(fp); |
seedteam20 | 1:0ec6c4c22623 | 344 | blueLED.blink(5); |
seedteam20 | 1:0ec6c4c22623 | 345 | } |
seedteam20 | 1:0ec6c4c22623 | 346 | |
seedteam20 | 1:0ec6c4c22623 | 347 | //Debug Mode |
mbotkinl | 2:89ca14fa896e | 348 | |
seedteam20 | 1:0ec6c4c22623 | 349 | for (int dd=0;dd<30;dd=dd+1) { |
mbotkinl | 2:89ca14fa896e | 350 | |
seedteam20 | 1:0ec6c4c22623 | 351 | if (numSensors == 3) { |
seedteam20 | 1:0ec6c4c22623 | 352 | |
seedteam20 | 1:0ec6c4c22623 | 353 | } else if (numSensors ==2) { |
mbotkinl | 2:89ca14fa896e | 354 | |
seedteam20 | 1:0ec6c4c22623 | 355 | if (tempSensorflag==true) { |
mbotkinl | 2:89ca14fa896e | 356 | |
seedteam20 | 1:0ec6c4c22623 | 357 | if (lightSensorflag==true) { |
seedteam20 | 1:0ec6c4c22623 | 358 | getLightReading(&lightData); |
seedteam20 | 1:0ec6c4c22623 | 359 | getTempHumid(&tempData,&humData); |
seedteam20 | 1:0ec6c4c22623 | 360 | sendData3(tempData,tempField,humData,humField,lightData,lightField); |
seedteam20 | 1:0ec6c4c22623 | 361 | sdWrite3(tempData,humData,lightData); |
mbotkinl | 2:89ca14fa896e | 362 | } else if (SomeotherSensorflag==true) { |
mbotkinl | 2:89ca14fa896e | 363 | |
seedteam20 | 1:0ec6c4c22623 | 364 | } |
seedteam20 | 1:0ec6c4c22623 | 365 | } else if (lightSensorflag==true) { |
mbotkinl | 2:89ca14fa896e | 366 | |
mbotkinl | 2:89ca14fa896e | 367 | |
seedteam20 | 1:0ec6c4c22623 | 368 | if (SomeotherSensorflag==true) { |
seedteam20 | 1:0ec6c4c22623 | 369 | getLightReading(&lightData); |
seedteam20 | 1:0ec6c4c22623 | 370 | |
seedteam20 | 1:0ec6c4c22623 | 371 | |
seedteam20 | 1:0ec6c4c22623 | 372 | } |
seedteam20 | 1:0ec6c4c22623 | 373 | } |
seedteam20 | 1:0ec6c4c22623 | 374 | } else { |
seedteam20 | 1:0ec6c4c22623 | 375 | if (tempSensorflag==true) { |
seedteam20 | 1:0ec6c4c22623 | 376 | getTempHumid(&tempData,&humData); |
seedteam20 | 1:0ec6c4c22623 | 377 | sendData2(tempData,tempField,humData,humField); |
seedteam20 | 1:0ec6c4c22623 | 378 | sdWrite2(tempData,humData); |
seedteam20 | 1:0ec6c4c22623 | 379 | } else if (lightSensorflag==true) { |
seedteam20 | 1:0ec6c4c22623 | 380 | getLightReading(&lightData); |
seedteam20 | 1:0ec6c4c22623 | 381 | sendData1(lightData,lightField); |
seedteam20 | 1:0ec6c4c22623 | 382 | sdWrite1(lightData); |
seedteam20 | 1:0ec6c4c22623 | 383 | } else if (SomeotherSensorflag==true) { |
seedteam20 | 1:0ec6c4c22623 | 384 | |
seedteam20 | 1:0ec6c4c22623 | 385 | } |
seedteam20 | 1:0ec6c4c22623 | 386 | } |
seedteam20 | 1:0ec6c4c22623 | 387 | wait(30); |
seedteam20 | 1:0ec6c4c22623 | 388 | } |
seedteam20 | 1:0ec6c4c22623 | 389 | |
mbotkinl | 2:89ca14fa896e | 390 | |
seedteam20 | 1:0ec6c4c22623 | 391 | |
seedteam20 | 1:0ec6c4c22623 | 392 | |
seedteam20 | 1:0ec6c4c22623 | 393 | |
seedteam20 | 1:0ec6c4c22623 | 394 | //normal mode |
seedteam20 | 1:0ec6c4c22623 | 395 | while (1) { |
seedteam20 | 1:0ec6c4c22623 | 396 | |
seedteam20 | 1:0ec6c4c22623 | 397 | if (numSensors == 3) { |
seedteam20 | 1:0ec6c4c22623 | 398 | |
seedteam20 | 1:0ec6c4c22623 | 399 | } else if (numSensors ==2) { |
seedteam20 | 1:0ec6c4c22623 | 400 | if (tempSensorflag==true) { |
seedteam20 | 1:0ec6c4c22623 | 401 | if (lightSensorflag==true) { |
seedteam20 | 1:0ec6c4c22623 | 402 | getLightReading(&lightData); |
seedteam20 | 1:0ec6c4c22623 | 403 | getTempHumid(&tempData,&humData); |
seedteam20 | 1:0ec6c4c22623 | 404 | sendData3(tempData,tempField,humData,humField,lightData,lightField); |
seedteam20 | 1:0ec6c4c22623 | 405 | sdWrite3(tempData,humData,lightData); |
seedteam20 | 1:0ec6c4c22623 | 406 | } else if (SomeotherSensorflag==true) { |
seedteam20 | 1:0ec6c4c22623 | 407 | |
seedteam20 | 1:0ec6c4c22623 | 408 | } |
seedteam20 | 1:0ec6c4c22623 | 409 | } else if (lightSensorflag==true) { |
seedteam20 | 1:0ec6c4c22623 | 410 | if (SomeotherSensorflag==true) { |
seedteam20 | 1:0ec6c4c22623 | 411 | getLightReading(&lightData); |
seedteam20 | 1:0ec6c4c22623 | 412 | |
seedteam20 | 1:0ec6c4c22623 | 413 | |
seedteam20 | 1:0ec6c4c22623 | 414 | } |
seedteam20 | 1:0ec6c4c22623 | 415 | } |
seedteam20 | 1:0ec6c4c22623 | 416 | } else { |
seedteam20 | 1:0ec6c4c22623 | 417 | if (tempSensorflag==true) { |
seedteam20 | 1:0ec6c4c22623 | 418 | getTempHumid(&tempData,&humData); |
seedteam20 | 1:0ec6c4c22623 | 419 | sendData2(tempData,tempField,humData,humField); |
seedteam20 | 1:0ec6c4c22623 | 420 | sdWrite2(tempData,humData); |
seedteam20 | 1:0ec6c4c22623 | 421 | } else if (lightSensorflag==true) { |
seedteam20 | 1:0ec6c4c22623 | 422 | getLightReading(&lightData); |
seedteam20 | 1:0ec6c4c22623 | 423 | sendData1(lightData,lightField); |
seedteam20 | 1:0ec6c4c22623 | 424 | sdWrite1(lightData); |
seedteam20 | 1:0ec6c4c22623 | 425 | } else if (SomeotherSensorflag==true) { |
seedteam20 | 1:0ec6c4c22623 | 426 | |
seedteam20 | 1:0ec6c4c22623 | 427 | } |
seedteam20 | 1:0ec6c4c22623 | 428 | } |
seedteam20 | 1:0ec6c4c22623 | 429 | wdt_sleep.sleep(BROADCAST_TIME); |
seedteam20 | 1:0ec6c4c22623 | 430 | |
seedteam20 | 1:0ec6c4c22623 | 431 | } |
seedteam20 | 1:0ec6c4c22623 | 432 | } |