/* * This sketch is to monitor the helium recover bag height * using an ultrasonic sensor suspended above the bag. * Rev 5 has a LED level indicator. */ // Setup for SD card datalogging #include // Files and variables for sms and email #include #include String startTime; String alarmTime; // Time marker for Bauer ON status String alertTime; // Time marker for SMS messages int flagBauer = 0; // Flag for Bauer on (1) and off (0) const int thresholdTimeOn = 300; // Time (in seconds) before Bauer ON status written const int thresholdTimeSMS = 900; // Time (in seconds) before SMS message is sent const int thresholdDistanceOn = 110; // Distance (in centimeters) before compressor starts const int thresholdDistanceOff = 200; // Distance (in centimeters) before compressor stops const int thresholdDistanceSMS = 90; // Distance (in centimeters) before SMS text is sent long hourcount = 0; // Setup pins for ultrasonic sensor const int trigPin = 9; const int echoPin = 10; // Setup pin for relay for compressor control const int bagPin = 8; // Setup pins for LED Level Indicator const int ledPin1 = 13; const int ledPin2 = 12; const int ledPin3 = 11; const int ledPin4 = 7; const int ledPin5 = 6; const int ledPin6 = 5; const int ledPin7 = 4; const int ledPin8 = 3; const int ledPin9 = 2; int ledFrac; void setup() { // Initialize bridge for linux module Bridge.begin(); // Initialize serial communication at 9600 bits per second Serial.begin(9600); // Starts the serial communication FileSystem.begin(); while (!SerialUSB); // Wait for serial port to connect // Initialize the ultrasonic sensor pins pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output pinMode(echoPin, INPUT); // Sets the echoPin as an Input // Initialize the relay pin pinMode(bagPin, OUTPUT); digitalWrite(bagPin, HIGH); // Deactivate Relay // Initialize the LED pins pinMode(ledPin1, OUTPUT); pinMode(ledPin2, OUTPUT); pinMode(ledPin3, OUTPUT); pinMode(ledPin4, OUTPUT); pinMode(ledPin5, OUTPUT); pinMode(ledPin6, OUTPUT); pinMode(ledPin7, OUTPUT); pinMode(ledPin8, OUTPUT); pinMode(ledPin9, OUTPUT); digitalWrite(ledPin1,LOW); digitalWrite(ledPin2,LOW); digitalWrite(ledPin3,LOW); digitalWrite(ledPin4,LOW); digitalWrite(ledPin5,LOW); digitalWrite(ledPin6,LOW); digitalWrite(ledPin7,LOW); digitalWrite(ledPin8,LOW); digitalWrite(ledPin9,LOW); // Initialize the alarm time and report startup startTime += getEpochTime(); SerialUSB.print("System online at epoch "); SerialUSB.println(startTime); writeLog(hourcount,"STARTUP",startTime,0); alarmTime = startTime; alertTime = startTime; } void loop() { int measureDistance; String measureTime; measureDistance = getDistance(); // Safety to remove large errant measurements if (measureDistance > 300) { measureDistance = 150; } measureTime += getEpochTime(); // Prints the distance on the Serial Monitor Serial.print("Epoch: "); Serial.print(measureTime); Serial.print(" Distance: "); Serial.print(measureDistance); Serial.print(" LED Frac: "); Serial.println(ledFrac); // Write to logfile every hour past start if (measureTime.toInt() > startTime.toInt() + hourcount*3600) { writeLog(hourcount,"MEASURE",measureTime,measureDistance); hourcount++; } /* * If the distance is lower than the threshold, set the bag relay to run the * compressor. Otherwise, set the bag relay to keep the compressor at idle. */ if (measureDistance < thresholdDistanceOn) { digitalWrite(bagPin, LOW); // Activate the relay, and start the compressor // Change Bauer flag to denote compressor is ON flagBauer = 1; // Write to logfile writeLog(hourcount,"BAUERON",measureTime,measureDistance); // Reset the alarm time alarmTime = measureTime; } else if (measureDistance > thresholdDistanceOff) { digitalWrite(bagPin, HIGH); // Deactivate the relay, and stop the compressor // Change Bauer flag to denote compressor is OFF flagBauer = 0; } else { if ((measureTime.toInt() > alarmTime.toInt() + thresholdTimeOn) && (flagBauer == 1)) { // Write to logfile writeLog(hourcount,"BAUERON",measureTime,measureDistance); // Reset the alarm time alarmTime = measureTime; } } /* * If the distance is lower than the threshold, and the thresholdTime has * passed since the last SMS message. Then send a SMS message as a warning. */ if ((measureDistance < thresholdDistanceSMS) && (measureTime.toInt() > alertTime.toInt() + thresholdTimeSMS)) { /* // Send the SMS text message Process p; p.runShellCommand("/usr/bin/python /mnt/sda1/python_sms_wylie"); //sms Wylie */ // Write to logfile writeLog(hourcount,"WARNING",measureTime,measureDistance); // Reset the alert time alertTime = measureTime; } ledFrac = (thresholdDistanceOff-measureDistance)*100/(thresholdDistanceOff-thresholdDistanceOn); // Turn OFF LED Level Indicator Lights if (ledFrac < 98) { digitalWrite(ledPin1,LOW); } if (ledFrac < 90) { digitalWrite(ledPin2,LOW); } if (ledFrac < 80) { digitalWrite(ledPin3,LOW); } if (ledFrac < 70) { digitalWrite(ledPin4,LOW); } if (ledFrac < 60) { digitalWrite(ledPin5,LOW); } if (ledFrac < 50) { digitalWrite(ledPin6,LOW); } if (ledFrac < 40) { digitalWrite(ledPin7,LOW); } if (ledFrac < 20) { digitalWrite(ledPin8,LOW); } if (ledFrac < 10) { digitalWrite(ledPin9,LOW); } // Turn ON LED Level Indicator Lights if (ledFrac > 98) { digitalWrite(ledPin1,HIGH); } if (ledFrac > 90) { digitalWrite(ledPin2,HIGH); } if (ledFrac > 80) { digitalWrite(ledPin3,HIGH); } if (ledFrac > 70) { digitalWrite(ledPin4,HIGH); } if (ledFrac > 60) { digitalWrite(ledPin5,HIGH); } if (ledFrac > 50) { digitalWrite(ledPin6,HIGH); } if (ledFrac > 40) { digitalWrite(ledPin7,HIGH); } if (ledFrac > 20) { digitalWrite(ledPin8,HIGH); } if (ledFrac > 10) { digitalWrite(ledPin9,HIGH); } // Delay Between Measurements delay(5000); } // This function returns a string with the epoch time String getEpochTime() { String result; Process time; // Use the date command line utility to get the date and the time // The additional parameter is used to set the format time.begin("date"); time.addParameter("+%s"); // parameters: epoch time.run(); // run the command // Read the output of the command while (time.available() > 0) { char c = time.read(); if (c != '\n') { result += c; } } return result; } /* * This function returns the distance in centimeters * The Ultrasonic Sensor HC-SR04 code is by Dejan Nedelkovski, * www.HowToMechatronics.com */ int getDistance() { // Define sensor variables long duration; int distance; // Clears the trigPin digitalWrite(trigPin, LOW); delayMicroseconds(2); // Sets the trigPin on HIGH state for 10 micro seconds digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); // Reads the echoPin, returns the sound wave travel time in microseconds duration = pulseIn(echoPin, HIGH); // Calculating the distance in centimeters distance= duration*0.034/2; return distance; } /* * This function writes the count, status, epoch, and distance to a log file */ void writeLog (int entryCount, String systemStatus, String recordTime, int recordDistance) { // Open the log file File dataFile = FileSystem.open("/mnt/sda1/datalog.csv", FILE_APPEND); // if the log file is available, write to it if (dataFile) { dataFile.print(entryCount); dataFile.print(","); dataFile.print(systemStatus); dataFile.print(","); dataFile.print(recordTime); dataFile.print(","); dataFile.print(recordDistance); dataFile.print("\n"); dataFile.close(); } // If the file isn't open, pop up an error: else { SerialUSB.println("error opening datalog.csv"); } }