View Single Post
Old 11-16-15, 02:07 AM   #38
TechShop
FNG
 
Join Date: Jul 2015
Location: Washington
Posts: 71
Thanks: 8
Thanked 19 Times in 13 Posts
Default

Please see my previous post for the first half of this Arduino source code

Code:
void loop() {
  byte s, ScriptCommand;
  while (Serial.available() > 0) { // if a command was sent via the serial port, parse it and execute the appropriate action.
    ScriptCommand = Serial.parseInt();
    delay(1000); // wait for the rest of the command to fill the serial buffer.
    switch (ScriptCommand) {
      case 1: // dump contents of data file to serial port
        DumpLogFileToSerial();
        break;
      case 2: // add a timestamp to the record
        SensorList(true);
        SaveTimeStamp();
        break;
      case 3: // change sensor/logging interval
        if (Serial.available() > 0) {
          SetCheckInterval(byte(Serial.parseInt()));
        }
        break;
      case 4:
        if (Serial.available() > 1) {
          switch (Serial.parseInt()) {
            case 0:
              SensorList(true); // using the 4,0 command (WITH zero suffix) appends the sensor list to the log file.
              break;
            case 1:
              SetCheckActiveSensors(1, Serial.parseInt());  //enable a sensor by it's list number
              break;
            case 2:
              SetCheckActiveSensors(2, Serial.parseInt());  //disable a sensor by it's list number
              break;
            case 3:    // Enable all available sensors
              if (G_SensorQty > 0) {
                for (s = 0; s < G_SensorQty; s++) {
                  G_ActiveSensors[s] = true;
                }
              }
              break;
            case 4:    // Disable all available sensors
              if (G_SensorQty > 0) {
                for (s = 0; s < G_SensorQty; s++) {
                  G_ActiveSensors[s] = false; 
                }
              }            
              break;
            default:
              break;
          }
        }
        else {
          SensorList(false);  // using the 4 command (WITHOUT the zero suffix) does NOT append the sensor list to the log file.
        }
        break;
      case 6: // Pause logging
        logging = !logging;
        if (logging) Serial.println("\nLogging resumed.");
        break;
      case 9: // delete the file
        DeleteLogFile();
        break;
      default: //invalid command
        //do nothing
        break;
    }
    G_dString = ""; // reset the data output string if this is to be the first in the list of sensors
  }
  if (millis() >= G_TimePoint) {
    if (logging) {
      G_TimePoint = G_TimePoint + G_LogInterval * 1000;
      for (G_CurSensor = 1; G_CurSensor <= G_SensorQty;) {
        if (G_ActiveSensors[G_CurSensor - 1]) { //does the user want to log this sensor?
          ReadTempSensors(G_CurSensor - 1);
        }
        G_CurSensor++;
      }
      LogSensorData();
      G_CurSensor = 1;
    }
    else {
      G_TimePoint = G_TimePoint + 1000; // Use a 1 second interval when logging is suspended
      Serial.println("\nLogging suspended.  Send 6 to resume.");
      Serial.println("Interval = " + String(G_LogInterval));
      SensorList(true);
    }
  }
}

boolean ReadTempSensors(byte q) { //read data from sensor q from the array of addresses: G_AddressList[q]
  boolean SignBit;
  int TReading, Tc_100, Whole, Fract;
  byte i, HighByte, LowByte, data[12];
  ds.reset();
  ds.select(G_AddressList[q]);
  ds.write(0x44, 1);        // start conversion, with parasite power on at the end

  delay(800);     // maybe 750ms is enough, maybe not

  ds.reset();
  ds.select(G_AddressList[q]);
  ds.write(0xBE);         // Read Scratchpad

  for ( i = 0; i < 9; i++) {           // we need 9 bytes
    data[i] = ds.read();
  }
  //Serial.print(" CRC=");
  // Serial.println( OneWire::crc8( data, 8), HEX);
  LowByte = data[0];
  HighByte = data[1];
  TReading = (HighByte << 8) + LowByte;
  SignBit = TReading & 0x8000;  // test most sig bit
  if (SignBit) // negative
  {
    TReading = (TReading ^ 0xffff) + 1; // 2's comp
  }
  Tc_100 = (6 * TReading) + TReading / 4;    // multiply by (100 * 0.0625) or 6.25
  Whole = Tc_100 / 100;  // separate off the whole and fractional portions
  Fract = Tc_100 % 100;
  if (SignBit) // If its negative
  {
    G_dString = G_dString + "-" + (Tc_100 * .018 + 32) + ",";
  }
  else {
    G_dString = G_dString + (Tc_100 * .018 + 32) + ",";
  }
  return true;
}

void LogSensorData() {
  File LogFile = SD.open(LOGFILENAME, FILE_WRITE);
  if (LogFile) {
    LogFile.println(G_dString);  // This is where we actually write all of the sensor data to the file
    LogFile.close();
    // print to the serial port too:
    Serial.println(G_dString);
  }
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening " + String(LOGFILENAME));
  }
  G_dString = "";
  return;
}

void DumpLogFileToSerial() {
  File LogFile = SD.open(LOGFILENAME, FILE_READ);
  if (LogFile) {
    Serial.println("\n<<<READING DATA FILE>>>");
    while (LogFile.available()) {
      Serial.write(LogFile.read());
    }
    LogFile.close();
    Serial.println("<<<END OF DATA FILE>>>");
    Serial.println("\nResume logging task");
    return;
  }
}

void SaveTimeStamp() {
  byte hours, minutes, seconds;
  boolean badstamp = true;
  if (Serial.available() >= 3) { //check to see if a time was sent
    hours = Serial.parseInt();
    minutes = Serial.parseInt();
    seconds = Serial.parseInt();
    badstamp = false;
    if (hours > 24 || hours < 0) {
      Serial.println("Invalid time format.  Hours out of range.");
      Serial.println("Hours must be 0 - 23.");
      badstamp = true;
    }
    if (minutes > 59 || minutes < 0) {
      Serial.println("Invalid time format.  Minutes out of range.");
      Serial.println("Minutes must be 0 - 59.");
      badstamp = true;
    }
    if (seconds > 59 || seconds < 0) {
      Serial.println("Invalid time format.  Seconds out of range.");
      Serial.println("Seconds must be 0 - 59.");
      badstamp = true;
    }
    if (!badstamp) {
      File LogFile = SD.open(LOGFILENAME, FILE_WRITE);
      if (LogFile) {
        G_dString = "TIME:" + String(hours) + ":" + String(minutes) + ":" + String(seconds);
        if (LogFile.println(G_dString)) {
          G_TimePoint = millis();
          Serial.println("Timestamp saved:  " + G_dString);
        }
        else {
          Serial.println("Timestamp failed.");
          return;
        }
        LogFile.close();
        return;
      }
      else {
        Serial.println("FILE NOT AVAILABLE");
        return;
      }
    }
    else {
      return;
    }
  }
  else {
    Serial.println("Invalid timestamp or none provided.");
    Serial.println("hours = " + String(hours) + " Minutes = " + String(minutes) + " seconds = " + String(seconds));
  }
  return;
}

void SetCheckInterval(byte tempint) {
  if (tempint > 4) {
    G_TimePoint = G_TimePoint - G_LogInterval * 1000;
    G_LogInterval = tempint;
    G_TimePoint = G_TimePoint + G_LogInterval * 1000;
    Serial.println("Interval changed to: " + String(G_LogInterval));
    File LogFile = SD.open(LOGFILENAME, FILE_WRITE);
    if (LogFile) {
      LogFile.println("INTERVAL = " + String(G_LogInterval));
      LogFile.close();
    }
  }
  else if (tempint < 5) {
    Serial.println("Interval must be 5 seconds or larger.");
    Serial.println("Interval = " + String(G_LogInterval));
  }
  return;
}

void SensorList(boolean w) {// present a list of available sensors
  byte i, sc;
  File LogFile = SD.open(LOGFILENAME, FILE_WRITE);
  for (sc = 0; sc < G_SensorQty; sc++) {
    delay(20);
    Serial.print(String(sc + 1) + ") ");
    if (w) {
      if (LogFile) LogFile.print(String(sc + 1) + ") ");
    }
    for ( i = 0; i < 8; i++) {
      Serial.print(G_AddressList[sc][i], HEX);
      if (w) {
        if (LogFile) LogFile.print(G_AddressList[sc][i], HEX);
        delay(20);
      }
    }
    if (G_ActiveSensors[sc]) { // Is this sensor being logged?
      Serial.println(" - Logging");   // Notify the user that this sensor is being logged.
      if (w) {
        if (LogFile) LogFile.println(" - Logging");
      }
    }
    else {
      Serial.println(" - Ignored");   // Notify the user that this sensor is not being logged.
      if (w) {
        if (LogFile) LogFile.println(" - Ignored");
      }
    }
  }
  if (LogFile) {
    delay(20);
    LogFile.close();
  }
  Serial.println("");
  return;
}

int FillAddressList(int howmanysensorstoadd) {// fill an array with a list of sensor addresses
  byte i,  s = 0;
  byte addr[8];
  ds.reset_search();
  while ( ds.search(addr) && s < howmanysensorstoadd) {
    for ( i = 0; i < 8; i++) {
      G_AddressList[s][i] = addr[i];
    }
    s++; // increment the count for the next sensor (if there is one)
  }
  return s;
}

void SetCheckActiveSensors(byte c, byte s) {
  if (s > 0 && s <= G_SensorQty) {  // only do something if the sensor number is in the correct range
    if (c == 1) { // User sent the command to enable a sensor
      G_ActiveSensors[s - 1] = true; // enable the sensor by setting it true
    }
    if (c == 2) { // User sent the command to disable a sensor
      G_ActiveSensors[s - 1] = false;   // disable the sensor by setting it false
    }
  }
  else {
    Serial.println("Unknown sensor referenced in command");
  }
  SensorList(true);
  return;
}

void DeleteLogFile() {
  SD.remove(LOGFILENAME);
  Serial.println("FILE DELETED");
  return;
}
TechShop is offline   Reply With Quote