|
| 1 | + |
| 2 | +///Sketch written by Jeff Sadler /// |
| 3 | + |
| 4 | +#include <SD.h> |
| 5 | +const int chipSelect = 10; |
| 6 | + |
| 7 | +/*Note:This code is used for Arduino 1.0 or later*/ |
| 8 | +//serial communication to Sim900 |
| 9 | +#include <SoftwareSerial.h> |
| 10 | +SoftwareSerial Sim900Serial(2, 3); |
| 11 | + |
| 12 | +#include <SDI12.h> |
| 13 | +#define DATAPIN 9 // change to the proper pin |
| 14 | +SDI12 mySDI12(DATAPIN); |
| 15 | + |
| 16 | +//define variables for HydroServer Lite |
| 17 | +byte SourceID = 6; |
| 18 | +byte SiteID = 84; |
| 19 | +byte VarID1 = 10; |
| 20 | +byte VarID2 = 11; |
| 21 | +String url = "http://worldwater.byu.edu/jefftest.php"; |
| 22 | + |
| 23 | +//define variables |
| 24 | +char character; |
| 25 | + |
| 26 | +//String Var1 = "55.55"; |
| 27 | +//String Var2 = "66.66"; |
| 28 | + |
| 29 | +byte minInt = 0; |
| 30 | +byte secInt = 0; |
| 31 | +byte hourInt = 0; |
| 32 | +/*byte yearInt = 0; |
| 33 | +byte monthInt=0; |
| 34 | +byte dayInt=0;*/ |
| 35 | + |
| 36 | +char tempBuf[3]; |
| 37 | + |
| 38 | +byte recTime = 30; |
| 39 | +byte recIntv = 45; |
| 40 | + |
| 41 | +char dateTimeBuf[25]; |
| 42 | + |
| 43 | +int count = 0; |
| 44 | + |
| 45 | +void setup() |
| 46 | +{ |
| 47 | + Sim900Serial.begin(115200); // the GPRS baud rate |
| 48 | + delay(500); |
| 49 | + Sim900Serial.println("AT+IPR=19200"); |
| 50 | + delay(500); |
| 51 | + Sim900Serial.begin(19200); // the GPRS baud rate |
| 52 | + delay(1000); |
| 53 | + Serial.begin(9600); // the Hardware serial rate |
| 54 | + pinMode(10, OUTPUT); //set pinmode 10 as output for SD card |
| 55 | + if (!SD.begin(chipSelect)) { |
| 56 | + Serial.println("Card failed, or not present"); |
| 57 | + //don't do anything more: |
| 58 | + } |
| 59 | + else {Serial.println("card initialized.");} |
| 60 | + |
| 61 | + mySDI12.begin(); |
| 62 | + |
| 63 | + timestamp(); |
| 64 | + timestamp(); |
| 65 | + powerUp(); |
| 66 | + recTime = secInt + 5; |
| 67 | + if(recTime >= 60){ |
| 68 | + recTime = recTime-60; |
| 69 | + } |
| 70 | +} |
| 71 | +void loop() |
| 72 | +{ |
| 73 | + //begin each loop with timestamp |
| 74 | + timestamp(); |
| 75 | + Serial.println(dateTimeBuf); |
| 76 | + |
| 77 | + //take measurement when minutes is recTime (e.g. 15) |
| 78 | + if (secInt==recTime){ |
| 79 | + recTime += recIntv; |
| 80 | + sd(); |
| 81 | + |
| 82 | + if(recTime >= 60){ |
| 83 | + recTime = recTime-60; |
| 84 | + } |
| 85 | + } |
| 86 | + if (Serial.available()) |
| 87 | + switch(Serial.read()) |
| 88 | + { |
| 89 | + case 'm': |
| 90 | + sd(); |
| 91 | + break; |
| 92 | + |
| 93 | + } |
| 94 | + |
| 95 | + |
| 96 | + |
| 97 | + |
| 98 | + delay (300); |
| 99 | + |
| 100 | +} |
| 101 | + |
| 102 | +//used to adjust time and date for realtime clock on sim900 |
| 103 | +/*void Clock(){ |
| 104 | + Sim900Serial.println("AT"); |
| 105 | + delay(100); |
| 106 | + ShowSerialData(); |
| 107 | + Sim900Serial.println("AT+CCLK=\"14/03/25,10:04:30-24\""); |
| 108 | + delay(100); |
| 109 | + ShowSerialData(); |
| 110 | +}*/ |
| 111 | + |
| 112 | +//powers down sim900 |
| 113 | +void powerDown(){ |
| 114 | + Sim900Serial.println("AT"); |
| 115 | + delay(100); |
| 116 | + ShowSerialData(); |
| 117 | + Sim900Serial.println("AT+CFUN=0"); |
| 118 | + delay(100); |
| 119 | + ShowSerialData(); |
| 120 | +} |
| 121 | + |
| 122 | +//powers up sim900 |
| 123 | +void powerUp(){ |
| 124 | + Sim900Serial.println("AT"); |
| 125 | + delay(100); |
| 126 | + ShowSerialData(); |
| 127 | + Sim900Serial.println("AT+CFUN=1"); |
| 128 | + delay(100); |
| 129 | + ShowSerialData(); |
| 130 | +} |
| 131 | +void test(){ |
| 132 | + Serial.println("test"); |
| 133 | +} |
| 134 | +//function returns timestamp |
| 135 | +void timestamp(){ |
| 136 | + String content = ""; |
| 137 | + Sim900Serial.print("AT + CCLK?\r"); |
| 138 | + delay(100); |
| 139 | + while (Sim900Serial.available()){ |
| 140 | + character = Sim900Serial.read(); |
| 141 | + content.concat(character); |
| 142 | + content.trim();} |
| 143 | + |
| 144 | + int slashPosition = content.lastIndexOf("/"); |
| 145 | + String dateTime = content.substring(slashPosition -5,slashPosition +12); |
| 146 | + String temp = ""; |
| 147 | + |
| 148 | + temp = content.substring(slashPosition +4,slashPosition +6); |
| 149 | + temp.toCharArray(tempBuf,3); |
| 150 | + hourInt = atoi (tempBuf); |
| 151 | + |
| 152 | + temp = content.substring(slashPosition +7,slashPosition +9); |
| 153 | + temp.toCharArray(tempBuf,3); |
| 154 | + minInt = atoi (tempBuf); |
| 155 | + |
| 156 | + temp = content.substring(slashPosition +10,slashPosition +12); |
| 157 | + temp.toCharArray(tempBuf,3); |
| 158 | + secInt = atoi (tempBuf); |
| 159 | + |
| 160 | + dateTime.toCharArray(dateTimeBuf, 25); |
| 161 | + |
| 162 | +} |
| 163 | + |
| 164 | + |
| 165 | +//writes all values to sd card |
| 166 | +void sd(){ |
| 167 | + String str = sensors(); |
| 168 | + |
| 169 | + int comPos = str.indexOf(","); |
| 170 | + int comPos1 = str.indexOf(",",comPos+1); |
| 171 | + String Var1 = str.substring(comPos+1, comPos1); |
| 172 | + |
| 173 | + int comPos2 = str.indexOf(",",comPos1+1); |
| 174 | + int comPos3 = str.indexOf(",",comPos2+1); |
| 175 | + String Var2 = str.substring(comPos2+1, comPos3); |
| 176 | + |
| 177 | + Serial.println(Var1); |
| 178 | + Serial.println(Var2); |
| 179 | + |
| 180 | + File dataFile = SD.open("test.txt", FILE_WRITE); |
| 181 | + if (dataFile){ //do this part each time |
| 182 | + dataFile.print(dateTimeBuf); |
| 183 | + dataFile.print(","); |
| 184 | + dataFile.print(Var1); |
| 185 | + dataFile.print(","); |
| 186 | + dataFile.print(Var2); |
| 187 | + dataFile.print(";"); |
| 188 | + dataFile.close(); |
| 189 | + Serial.println ("SD Success"); |
| 190 | + count ++; |
| 191 | + Serial.print("count: "); |
| 192 | + Serial.println(count); |
| 193 | +} |
| 194 | + else{ |
| 195 | + Serial.println("error with SD card"); |
| 196 | + } |
| 197 | + if(count == 4){ |
| 198 | + String dataString = ""; |
| 199 | + File dataFile = SD.open("test.txt", FILE_READ); |
| 200 | + if (dataFile) { |
| 201 | + int i=0; |
| 202 | + |
| 203 | + dataFile.seek(dataFile.size()-264); |
| 204 | + Serial.println(dataFile.position()); |
| 205 | + while (dataFile.available()&& dataFile.position()<dataFile.size()) { |
| 206 | + char c = dataFile.read(); |
| 207 | + dataString += c; |
| 208 | + } |
| 209 | + dataFile.close(); |
| 210 | + } |
| 211 | + else { |
| 212 | + // if the file didn't open, print an error: |
| 213 | + Serial.println("error opening test.txt"); |
| 214 | + } |
| 215 | + Serial.println(dataString); |
| 216 | + SubmitHttpRequest(dataString); |
| 217 | + count = 0; |
| 218 | + } |
| 219 | +} |
| 220 | + |
| 221 | +///SubmitHttpRequest() |
| 222 | +///this function is submit a http request |
| 223 | +///attention:the time of delay is very important, it must be set enough |
| 224 | +void SubmitHttpRequest(String dataString) |
| 225 | +{ |
| 226 | + Sim900Serial.println("AT+CSQ"); |
| 227 | + delay(100); |
| 228 | + |
| 229 | + ShowSerialData();// this code is to show the data from gprs shield, in order to easily see the process of how the gprs shield submit a http request, and the following is for this purpose too. |
| 230 | + |
| 231 | + Sim900Serial.println("AT+CGATT?"); |
| 232 | + delay(100); |
| 233 | + |
| 234 | + ShowSerialData(); |
| 235 | + |
| 236 | + Sim900Serial.println("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"");//setting the SAPBR, the connection type is using gprs |
| 237 | + delay(1000); |
| 238 | + |
| 239 | + ShowSerialData(); |
| 240 | + |
| 241 | + Sim900Serial.println("AT+SAPBR=3,1,\"APN\",\"CMNET\"");//setting the APN, the second need you fill in your local apn server |
| 242 | + delay(4000); |
| 243 | + |
| 244 | + ShowSerialData(); |
| 245 | + |
| 246 | + Sim900Serial.println("AT+SAPBR=1,1");//setting the SAPBR, for detail you can refer to the AT slashnd mamual |
| 247 | + delay(2000); |
| 248 | + |
| 249 | + ShowSerialData(); |
| 250 | + |
| 251 | + Sim900Serial.println("AT+HTTPINIT"); //init the HTTP request |
| 252 | + |
| 253 | + delay(2000); |
| 254 | + ShowSerialData(); |
| 255 | + |
| 256 | + Sim900Serial.print("AT+HTTPPARA=\"URL\",\"http://worldwater.byu.edu/jefftest.php?info=");// setting the httppara, the second parameter is the website you want to access |
| 257 | + Sim900Serial.print(SourceID); |
| 258 | + Sim900Serial.print(","); |
| 259 | + Sim900Serial.print(SiteID); |
| 260 | + Sim900Serial.print(","); |
| 261 | + Sim900Serial.print(VarID1); |
| 262 | + Sim900Serial.print(","); |
| 263 | + Sim900Serial.print(VarID2); |
| 264 | + |
| 265 | + Sim900Serial.print("&readings="); |
| 266 | + Sim900Serial.print(dataString); |
| 267 | + Sim900Serial.println("\""); |
| 268 | + |
| 269 | + delay(1000); |
| 270 | + ShowSerialData(); |
| 271 | + |
| 272 | + Sim900Serial.println("AT+HTTPACTION=0");//submit the request |
| 273 | + delay(10000);//the delay is very important, the delay time is base on the return from the website, if the return datas are very large, the time required longer. |
| 274 | + //while(!Sim900Serial.available()); |
| 275 | + |
| 276 | + ShowSerialData(); |
| 277 | + |
| 278 | + Sim900Serial.println("AT+HTTPREAD");// read the data from the website you access |
| 279 | + delay(300); |
| 280 | + |
| 281 | + ShowSerialData(); |
| 282 | + |
| 283 | + Sim900Serial.println(""); |
| 284 | + delay(100); |
| 285 | +} |
| 286 | + |
| 287 | + |
| 288 | + |
| 289 | +void ShowSerialData() |
| 290 | +{ |
| 291 | + while(Sim900Serial.available()!=0) |
| 292 | + Serial.write(Sim900Serial.read()); |
| 293 | +} |
| 294 | + |
| 295 | +String sensors(){ |
| 296 | + mySDI12.sendCommand("0M7!"); |
| 297 | + delay(300); // wait a while for a response |
| 298 | + String waitStr = bufferToStr(); |
| 299 | + String waitTrim = waitStr.substring(0,3); |
| 300 | + int waitInt = waitTrim.toInt()*1000; |
| 301 | + delay(waitInt); // print again in three seconds |
| 302 | + mySDI12.sendCommand("0D0!"); |
| 303 | + delay(300); // wait a while for a response |
| 304 | + String mStr = bufferToStr(); |
| 305 | + return(mStr); |
| 306 | +} |
| 307 | + |
| 308 | +String bufferToStr(){ |
| 309 | + String buffer = ""; |
| 310 | + mySDI12.read(); // consume address |
| 311 | + |
| 312 | + while(mySDI12.available()){ |
| 313 | + char c = mySDI12.read(); |
| 314 | + if(c == '+' || c == '-'){ |
| 315 | + buffer += ','; |
| 316 | + if(c == '-') buffer += '-'; |
| 317 | + } |
| 318 | + else { |
| 319 | + buffer += c; |
| 320 | + } |
| 321 | + delay(100); |
| 322 | + } |
| 323 | + return(buffer); |
| 324 | +} |
| 325 | + |
| 326 | + |
| 327 | + |
| 328 | + |
| 329 | + |
| 330 | + |
| 331 | + |
| 332 | + |
| 333 | + |
| 334 | + |
| 335 | + |
| 336 | + |
| 337 | + |
0 commit comments