Home › Forums › Mayfly Data Logger › Water level monitor with a Mayfly. › Reply To: Water level monitor with a Mayfly.
Hi
It’s been a little while since I have had a chance to spend any time on this project but over the last few days I made some time.
Thank you to Danny Waz for your suggestions as they confirmed my suspicions and I have removed the TTL-232 from the Circuit and I am just inverting in code.
I currently have just fudged together all the code that has been provided in the Mayfly software page and have been able to get a file written to the sd card on the Mayfly that contains Sample Number, Battery volts, Temperature from RTC, Millimeters from Maxbotix 7360 Serial( I know this is not the best choice and will change in the next iteration), Year, Month, Day, Time.
I have attached the code below for anyone who may want to use it and to hopefully get some ideas on how to improve it, (I dont really know how to code and cant take any credit for the code good or bad)
Now that I have the data I want coming in,my next goal is to send it using a GPRSbee, any advice or help here would be really appreciated as it’s something I have never had any experience with.
Cheers
Rich
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 |
#include <Wire.h> #include <SPI.h> #include <SD.h> #include "Sodaq_DS3231.h" #include <SoftwareSerial.h> SoftwareSerial mySerial(5, -1,true); // RX, TX, true sets the inverter //Digital pin 12 is the MicroSD slave select pin on the Mayfly #define SD_SS_PIN 12 //The data log file #define FILE_NAME "DataLog.txt" //Data header (these lines get written to the beginning of a file when it's created) #define LOGGERNAME "Ultrasonic water level Datalogger" #define DATA_HEADER "Data set includes Sample Number, Battery volts, Temperature,Millimeters, Year, Month, Day, Time" int sampleinterval = 10; //time between samples, in seconds int State8 = LOW; int State9 = LOW; int samplenum = 1; // declare the variable "samplenum" and start with 1 int batteryPin = A6; // on the Mayfly board, pin A6 is connected to a resistor divider on the battery input int batterysenseValue = 0; // variable to store the value coming from the analogRead function float batteryvoltage; // the battery voltage as calculated by the formula below void setup() { //Initialise the serial connection Serial.begin(57600); pinMode(8, OUTPUT); pinMode(9, OUTPUT); Wire.begin(); rtc.begin(); mySerial.begin(9600);// set the data rate for the SoftwareSerial port //Initialise log file setupLogFile(); //Echo the data header to the serial connection Serial.println(DATA_HEADER); } void loop() { String dataRec = createDataRecord(); //Save the data record to the log file logData(dataRec); //Echo the data to the serial connection Serial.print(dataRec); //////////////////////////////// int index = 0; boolean startreading = false; char mm[6]; mm[4] = 0; mySerial.flush(); while(index < 4){ if(mySerial.available()){ byte incoming = mySerial.read(); if(incoming == 'R') { startreading = true; } else if(startreading) { mm[index++] = incoming; } } } //////////////////////////////////////// DateTime now = rtc.now(); //get the current date-time rtc.convertTemperature(); //convert current temperature into registers Serial.print(", "); Serial.print(rtc.getTemperature(),2); //read registers and display the temperature Serial.print(", "); Serial.print(mm); Serial.print(", "); Serial.print(now.year(), DEC); Serial.print(", "); Serial.print(now.month(), DEC); Serial.print(", "); Serial.print(now.date(), DEC); Serial.print(", "); Serial.print(now.hour(), DEC); Serial.print(':'); Serial.print(now.minute(), DEC); Serial.print(':'); Serial.print(now.second(), DEC); Serial.println(); delay(sampleinterval*1000); //multiply by 1000 to convert from milliseconds to seconds } void setupLogFile() { //Initialise the SD card if (!SD.begin(SD_SS_PIN)) { Serial.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.print(rec); //////////////////////////////// int index = 0; boolean startreading = false; char mm[6]; mm[4] = 0; mySerial.flush(); while(index < 4){ if(mySerial.available()){ byte incoming = mySerial.read(); if(incoming == 'R') { startreading = true; } else if(startreading) { mm[index++] = incoming; } } } //////////////////////////////////////// DateTime now = rtc.now(); //get the current date-time rtc.convertTemperature(); //convert current temperature into registers logFile.print(", "); logFile.print(rtc.getTemperature(),2); //read registers and display the temperature logFile.print(", "); logFile.print(mm); logFile.print(", "); logFile.print(now.year(), DEC); logFile.print(", "); logFile.print(now.month(), DEC); logFile.print(", "); logFile.print(now.date(), DEC); logFile.print(", "); logFile.print(now.hour(), DEC); logFile.print(':'); logFile.print(now.minute(), DEC); logFile.print(':'); logFile.print(now.second(), DEC); logFile.println(); //Close the file to save it logFile.close(); } String createDataRecord() { //Create a String type data record in csv format //SampleNumber, Battery String data = ""; data += samplenum; //creates a string called "data", put in the sample number data += ","; //adds a comma between values batterysenseValue = analogRead(batteryPin); // reads the analog voltage on the batteryPin, reported in bits batteryvoltage = (3.3/1023.) * 1.47 * batterysenseValue; // converts bits into volts (see batterytest sketch for more info) data += batteryvoltage; //adds the battery voltage to the data string samplenum++; //increment the sample number return data; } |