Home › Forums › Mayfly Data Logger › Connecting to the Internet › Reply To: Connecting to the Internet
2018-04-22 at 2:00 PM
#12257
Ok, so it is the code on my sensor/logger that needs to be changed. Do you by chance have any example code that I can look at to try and figure out where I need to go? The sensor is an ultrasonic sensor. I have attached the code I am using for the sensor. This code will change as I only want to send data one time per day.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 |
[code] #include <Wire.h> #include <avr/sleep.h> #include <avr/wdt.h> #include <SPI.h> #include <SD.h> #include <RTCTimer.h> #include <Sodaq_DS3231.h> #include <Sodaq_PcInt_PCINT0.h> #include <SoftwareSerial_PCINT12.h> const int SonarExcite = 10; SoftwareSerial sonarSerial(11, -1); boolean stringComplete = false; #define READ_DELAY 1 //RTC Timer RTCTimer timer; String dataRec = ""; int currentminute; long currentepochtime = 0; float boardtemp = 0.0; int batteryPin = A6; // select the input pin for the potentiometer int batterysenseValue = 0; // variable to store the value coming from the sensor float batteryvoltage; int range_mm; //RTC Interrupt pin #define RTC_PIN A7 #define RTC_INT_PERIOD EveryMinute #define SD_SS_PIN 12 //The data log file #define FILE_NAME "SonicLog.txt" //Data header #define LOGGERNAME "Ultrasonic Maxbotix Sensor Datalogger" #define DATA_HEADER "DateTime,Loggertime,BoardTemp,Battery_V,SonarRange_mm" void setup() { //Initialise the serial connection Serial1.begin(9600); sonarSerial.begin(9600); rtc.begin(); delay(100); pinMode(8, OUTPUT); pinMode(9, OUTPUT); pinMode(SonarExcite, OUTPUT); digitalWrite(SonarExcite, LOW); //pin 10 is the power pin for the ultrasonic sensor greenred4flash(); //blink the LEDs to show the board is on setupLogFile(); //Setup timer events setupTimer(); //Setup sleep mode setupSleep(); Serial1.println("Power On, running: ultrasonic_logger_example_1.ino"); } void loop() { //Update the timer timer.update(); if(currentminute % 2 == 0) //if the time ends in an even number, a sample will be taken. Can be changed to other intervals { digitalWrite(8, HIGH); dataRec = createDataRecord(); delay(500); digitalWrite(SonarExcite, HIGH); delay(1000); range_mm = SonarRead(); digitalWrite(SonarExcite, LOW); stringComplete = false; //Save the data record to the log file logData(dataRec); //Echo the data to the serial connection Serial1.println(); Serial1.print("Data Record: "); Serial1.println(dataRec); String dataRec = ""; digitalWrite(8, LOW); delay(500); } systemSleep(); } void showTime(uint32_t ts) { //Retrieve and display the current date/time String dateTime = getDateTime(); //Serial.println(dateTime); } void setupTimer() { //Schedule the wakeup every minute timer.every(READ_DELAY, showTime); //Instruct the RTCTimer how to get the current time reading timer.setNowCallback(getNow); } void wakeISR() { //Leave this blank } void setupSleep() { pinMode(RTC_PIN, INPUT_PULLUP); PcInt::attachInterrupt(RTC_PIN, wakeISR); //Setup the RTC in interrupt mode rtc.enableInterrupts(RTC_INT_PERIOD); //Set the sleep mode set_sleep_mode(SLEEP_MODE_PWR_DOWN); } void systemSleep() { //Wait until the serial ports have finished transmitting Serial1.flush(); Serial1.flush(); //The next timed interrupt will not be sent until this is cleared rtc.clearINTStatus(); //Disable ADC ADCSRA &= ~_BV(ADEN); //Sleep time noInterrupts(); sleep_enable(); interrupts(); sleep_cpu(); sleep_disable(); //Enbale ADC ADCSRA |= _BV(ADEN); } String getDateTime() { String dateTimeStr; //Create a DateTime object from the current time DateTime dt(rtc.makeDateTime(rtc.now().getEpoch())); currentepochtime = (dt.get()); //Unix time in seconds currentminute = (dt.minute()); //Convert it to a String dt.addToString(dateTimeStr); return dateTimeStr; } uint32_t getNow() { currentepochtime = rtc.now().getEpoch(); return currentepochtime; } void greenred4flash() { for (int i=1; i <= 4; i++){ digitalWrite(8, HIGH); digitalWrite(9, LOW); delay(50); digitalWrite(8, LOW); digitalWrite(9, HIGH); delay(50); } digitalWrite(9, LOW); } void setupLogFile() { //Initialise the SD card if (!SD.begin(SD_SS_PIN)) { Serial1.println("Error: SD card failed to initialise or is missing."); //Hang // while (true); } //Check if the file already exists bool oldFile = SD.exists(FILE_NAME); //Open the file in write mode File logFile = SD.open(FILE_NAME, FILE_WRITE); //Add header information if the file did not already exist if (!oldFile) { logFile.println(LOGGERNAME); logFile.println(DATA_HEADER); } //Close the file to save it logFile.close(); } void logData(String rec) { //Re-open the file File logFile = SD.open(FILE_NAME, FILE_WRITE); //Write the CSV data logFile.println(rec); //Close the file to save it logFile.close(); } String createDataRecord() { //Create a String type data record in csv format //TimeDate, Loggertime,Temp_DS, Diff1, Diff2, boardtemp String data = getDateTime(); data += ","; rtc.convertTemperature(); //convert current temperature into registers boardtemp = rtc.getTemperature(); //Read temperature sensor value batterysenseValue = analogRead(batteryPin); batteryvoltage = (3.3/1023.) * 1.47 * batterysenseValue; data += currentepochtime; data += ","; addFloatToString(data, boardtemp, 3, 1); //float data += ","; addFloatToString(data, batteryvoltage, 4, 2); return data; } static void addFloatToString(String & str, float val, char width, unsigned char precision) { char buffer[10]; dtostrf(val, width, precision, buffer); str += buffer; } int SonarRead() { int result; char inData[5]; //char array to read data into int index = 0; while (sonarSerial.read() != -1) {} while (stringComplete == false) { if (sonarSerial.available()) { char rByte = sonarSerial.read(); //read serial input for "R" to mark start of data if(rByte == 'R') { //Serial.println("rByte set"); while (index < 4) //read next three character for range from sensor { if (sonarSerial.available()) { inData[index] = sonarSerial.read(); //Serial.println(inData[index]); //Debug line index++; // Increment where to write next } } inData[index] = 0x00; //add a padding byte at end for atoi() function } rByte = 0; //reset the rByte ready for next reading index = 0; // Reset index ready for next reading stringComplete = true; // Set completion of read to true result = atoi(inData); // Changes string data into an integer for use } } dataRec += ","; dataRec += result; return result; } [/code] |