Home › Forums › Mayfly Data Logger › Water level monitor with a Mayfly.
Tagged: mayfly, water level
- This topic has 12 replies, 4 voices, and was last updated 2017-07-21 at 1:12 AM by Richard.
-
AuthorPosts
-
-
2016-07-19 at 12:19 AM #1646
Hi
After all the brilliant support I have received from you all regarding my attempts to build this water level monitor using a stalker V3 and finding the shortfalls that came along with it I have just had 2 Mayfly data loggers arrive here in New Zealand and I am looking forward to getting them into the wild.
From reading the other forums here I am hoping you could give me a kick start as it seems I am plugging in with similar equipment to what you use for your projects.
To start with I want to use my Mayfly with a serial connection from a Maxbotix 7360 and then report data using a gprsbee with a sim 900 chip.
any chance you could help me get started with this, the coding side of these projects is quite daunting for me so anything would be a great help.
Cheers
Rich -
2016-07-23 at 12:47 AM #1672
Check out this ultrasonic sensor example I just posted. It shows how to read the serial output of a Maxbotix sensor. I like to use the MB7386 and MB7389 sensors because they have the TTL output that can be read directly by the Mayfly. The MB7360 you mentioned only has RS232 output, so you’ll need a RS232-to-TTL converter between the sensor and the Mayfly.
-
2016-07-24 at 6:27 PM #1675
Hi
As usual Thanks for the support, I have added an RS232-to-TTL converter inline between the board and the mayfly and have had a go with the code you posted with only average success I seemed to be getting every second measurement as a 0 reading and some unreliable repeat-ability.
As I was dubious about the cheap converter I had added to the circuit I decided to try some code that worked well as a software serial option and just turned off the software converter. It seemed to be really good so I’ll go with that. I’ll attach it below for anyone else that may be playing around with these things. I agree the other 2 sensors you mention would be better for the job but I guess I’ll make do with what I have for now.
The example below can be used with a RS232 or TTL just by changing the true/false statement. true if you are using an RS232, false for TTL.Arduino12345678910111213141516171819202122232425262728293031323334353637#include <SoftwareSerial.h>SoftwareSerial mySerial(5, -1,false); // RX, TX, true sets the invertervoid setup(){Serial.begin(57600); // Open serial communications and wait for port to open:mySerial.begin(9600); // set the data rate for the SoftwareSerial portdelay(50);}void loop() {int index = 0;boolean startreading = false;char mm[6];mySerial.flush();while(index < 4){if(mySerial.available()){byte incoming = mySerial.read();if(incoming == 'R') {startreading = true;} else if(startreading) {mm[index++] = incoming;}}}Serial.print("mm: ");Serial.print(mm);Serial.print(" ");Serial.println();//delay(500);} -
2016-07-25 at 11:50 AM #1676
According to the datasheet, the “RS232” models MB736x are 0-Vcc (typical RS232 is -13V to +13V). Of course, it will be inverted from TTL, but if you account for that in your softwareSerial constructor, I do not see why you should need an external converter (MAX232 or similar). In fact, the converter might cause problems if it’s shifting the logic levels on the sensor side. Disclaimer: I haven’t done this myself; I’ve only used the true-TTL MB7389 and the I2C versions. Feel free to correct me if you have experience with it.
On the subject of sensor selection for future reference, the 7369/7389 might be preferable for water surface detection because they report the “largest” return rather than the “nearest” return. I can attest that the MB7389 does a good job of ignoring small objects partially obstructing its field of view. If the sensor field of view is relatively clear it’s probably a non-issue.
-
2016-07-25 at 12:05 PM #1677
Pardon the somewhat off-topic response, but regarding reading serial data I have found this post extremely helpful. Both of the examples above hold the loop() hostage until the whole string comes in. That’s okay in a small, single-sensor sketch, but it is susceptible to (a) truncating the string if the arduino gets ahead of the incoming data and (b) getting hung in the serial.read() “while” loop. It’s more robust (although, admittedly, more complicated) to add a single byte per loop() iteration.
-
2016-08-23 at 9:32 PM #1714
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
RichArduino123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189#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 secondsint State8 = LOW;int State9 = LOW;int samplenum = 1; // declare the variable "samplenum" and start with 1int batteryPin = A6; // on the Mayfly board, pin A6 is connected to a resistor divider on the battery inputint batterysenseValue = 0; // variable to store the value coming from the analogRead functionfloat batteryvoltage; // the battery voltage as calculated by the formula belowvoid setup(){//Initialise the serial connectionSerial.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 filesetupLogFile();//Echo the data header to the serial connectionSerial.println(DATA_HEADER);}void loop(){String dataRec = createDataRecord();//Save the data record to the log filelogData(dataRec);//Echo the data to the serial connectionSerial.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-timertc.convertTemperature(); //convert current temperature into registersSerial.print(", ");Serial.print(rtc.getTemperature(),2); //read registers and display the temperatureSerial.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 cardif (!SD.begin(SD_SS_PIN)){Serial.println("Error: SD card failed to initialise or is missing.");//Hang// while (true);}//Check if the file already existsbool oldFile = SD.exists(FILE_NAME);//Open the file in write modeFile logFile = SD.open(FILE_NAME, FILE_WRITE);//Add header information if the file did not already existif (!oldFile){logFile.println(LOGGERNAME);logFile.println(DATA_HEADER);}//Close the file to save itlogFile.close();}void logData(String rec){//Re-open the fileFile logFile = SD.open(FILE_NAME, FILE_WRITE);//Write the CSV datalogFile.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-timertc.convertTemperature(); //convert current temperature into registerslogFile.print(", ");logFile.print(rtc.getTemperature(),2); //read registers and display the temperaturelogFile.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 itlogFile.close();}String createDataRecord(){//Create a String type data record in csv format//SampleNumber, BatteryString data = "";data += samplenum; //creates a string called "data", put in the sample numberdata += ","; //adds a comma between valuesbatterysenseValue = analogRead(batteryPin); // reads the analog voltage on the batteryPin, reported in bitsbatteryvoltage = (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 stringsamplenum++; //increment the sample numberreturn data;} -
2016-11-04 at 2:58 PM #1769
Dear all,
I have the same set-up of Maxbotix sonar with Mayfly as Steven. I did some trials first with my arduino and analog signal reading which was succesfull.
Now I trying the serial code available on the enviroDIY website, but readings are 0 mm. I hope following questions may help me to get succesfull measurements:
1) In the code the pin 5 is D5 of the mayfly?
2) The GND and 3.3V of the D pins is used?
3) To the pin 5 of the mayfly I connect the signal of output 2 (PulseWidth) of the maxbotix?Any other checks I can make to resolve this issue? Thanks in advance.
Have a nice weekend.
Arnoud
Attachments:
-
2016-11-06 at 1:00 AM #1771
ArnoudC, I’m not sure which Maxbotix sensor you’re using, but on the models we usually use, it’s pin 5 on the sensor that gets connected to the Mayfly digital pin (pin 5 in my example code above, but you can make it whatever you want). I think on most of the Maxbotix sensors, Pin 6 and 7 are the power and ground pins, and pin 5 is the serial TTL or RS232 output pin that you can easily read with the Mayfly using the capture code above. Sensor pin2 is the PWM output and pin3 is the analog signal, but I prefer the serial output. The code above only works for capturing the sensor serial output. Check the datasheet for your sensor model to verify that these are the correct pin numbers.
You can connect the sensor power and ground pins to any power or ground pin on the Mayfly or any other Arduino board. The Maxbotix sensors draw just a few milliamps of current so I usually just connect the sensor power pin to an available Mayfly digital pin and switch it high or low to control the on-off cycle of the sensor, which is handy for situations where the logger is programmed for sleeping between measurements. However, I have found that some of the sensors don’t like having their power pin connected to the switched 3.3v bus on the Mayfly. So if you’re running the Mayfly without sleeping, just connect the sensor power to the 3.3v constant bus. If you’re sleeping the Mayfly between sensor readings and you want to shut off power to the sensor, then use a digital pin to provide the power.
-
2016-11-06 at 7:03 PM #1772
Thanks for the quick reply. Indeed, I was probably erroneously connecting the PWM output (2) instead of the serial (5) to the Mayfly. I will give it a try this week and keep you informed. Thanks for the help…
-
2016-11-18 at 8:33 AM #1779
Dear all,
OK. I have correct distance readings right now of my sonar. Thanks for helping. Next step is to save the readings into the SD card. I tried the code of Rich abuve, but in my case it is not working. Steve, can I ´incorporate´ the sonar scetch into the´simple filé´ example posted on the forum? Any tips on how do that ? I was able to save analogue measurement values of temperatures with the mayfly earlier with the simple file code, but no clue on how and where to include the section of sonar.
Thanks in advance,
Many greetings from Asuncion, Paraguay
-
2016-11-30 at 12:12 PM #1824
ArnoudC, the second half of the Ultrasonic sensor example now has sample code for recording the data to a memory card and also puts the logger to sleep between readings.
-
2017-07-20 at 6:30 AM #2292
Hi
I tried to use this code but it wouldn’t compile due to the #include <Sodaq_PcInt_Mod.h>#include <SoftwareSerialMod.h> libraries not being found. I looked and dowloaded the entire package of libraries from the GITHUB page but still couldn’t find it. any ideas??
-
2017-07-21 at 1:12 AM #2293
Hi
Further to my last little input I have managed to get the code to compile and run on my MAYFLY with a MB7389 sonar by changing the #include <Sodaq_PcInt_Mod.h> to #include <Sodaq_PcInt_PCINT0.h> and
#include <SoftwareSerialMod.h> to #include <SoftwareSerial_PCINT12.h>
also a few lines down form there I changed
SoftwareSerialMod sonarSerial(11, -1); to SoftwareSerial sonarSerial(11, -1);
It all seems to be working fine after making these changes but wonder if I may be doing it wrong or potentially going to have problems.
Also the way I have it now seems to make the samples get taken every two minutes and I got the impression from the code it would be every minute, am I missing something??
-
-
-
-
AuthorPosts
- You must be logged in to reply to this topic.