

#include <SPI.h>
#include <SD.h>
//****************************导入使用传感器所需库文件******************************
//*****************************************************************************
//*****************************************************************************

#include "OneWire.h"
//*****************************************************************************
//*****************************************************************************
//*****************************************************************************



const int chipSelect = 4;

File dataFile;
//****************************设置传感器针脚，及必要参数****************************
//*****************************************************************************
//*****************************************************************************

int Temperature_GreenPin = 2; //DS18S20 Signal pin on digital 2
int PH_BluePin = 0;       //Ph sensor pin on analog 0
float temperature,phValue,ecValue;
//Temperature chip i/o
OneWire ds(Temperature_GreenPin);  // on digital pin 2


//*****************************************************************************
//*****************************************************************************
//*****************************************************************************


void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);

  Serial.print("Initializing SD card...");

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");


  // open the file in MicroSD card
  dataFile = SD.open("datalog.csv", FILE_WRITE);
  dataFile.println("This is a data log test code!");
  dataFile.close();
  
}

void loop() {
  
  // make a string for assembling the data to log:
  String dataString = "";

//**********************设定datastring内容**************************************
//*****************************************************************************
//*****************************************************************************

  
  temperature = readTemperature();
  phValue = readPh();

  dataString += String(temperature,10);
  dataString += ",";
  dataString += String(phValue,10);
  dataString += ",";


//*****************************************************************************
//*****************************************************************************
//*****************************************************************************

//************************************************************
/*
  static unsigned int value = 0;
  value = random(0,100);
  
  // read three sensors and append to the string:
  for (int i = 0; i < 3; i++) {
    dataString += String(value);
    if (i < 2) {
      dataString += ",";
    }
  }
*/
//************************************************************


  dataFile = SD.open("datalog.csv", FILE_WRITE);
  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println(dataString);
    dataFile.close();
    // print to the serial port too:
    Serial.println(dataString);
  }
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.csv");
  }

  delay(1000); // write data once per second
  
}


//*********************************实现获取传感器读数所需要的函数**********************
//*****************************************************************************
//*****************************************************************************

float readPh(){  
  int buf[10],temp;
  for(int i=0;i<10;i++)       //Get 10 sample value from the sensor for smooth the value
  { 
    buf[i]=analogRead(PH_BluePin);
    delay(10);
  }
  
  for(int i=0;i<9;i++)        //sort the analog from small to large
  {
    for(int j=i+1;j<10;j++)
    {
      if(buf[i]>buf[j])
      {
        temp=buf[i];
        buf[i]=buf[j];
        buf[j]=temp;
      }
    }
  }
  
  unsigned long int avgValue=0;
  for(int i=2;i<8;i++)                      //take the average value of 6 center sample
    avgValue+=buf[i];
  float phValue=(float)avgValue*5.0/1024/6; //convert the analog into millivolt
  phValue=3.5*phValue;                      //convert the millivolt into pH value
  
  return phValue;
}


float readTemperature(){
  //returns the temperature from one DS18S20 in DEG Celsius

  byte data[12];
  byte addr[8];

  if ( !ds.search(addr)) {
      //no more sensors on chain, reset search
      ds.reset_search();
      return -1000;
  }

  if ( OneWire::crc8( addr, 7) != addr[7]) {
      Serial.println("CRC is not valid!");
      return -1000;
  }

  if ( addr[0] != 0x10 && addr[0] != 0x28) {
      Serial.print("Device is not recognized");
      return -1000;
  }

  ds.reset();
  ds.select(addr);
  ds.write(0x44,1); // start conversion, with parasite power on at the end

  byte present = ds.reset();
  ds.select(addr);    
  ds.write(0xBE); // Read Scratchpad

  
  for (int i = 0; i < 9; i++) { // we need 9 bytes
    data[i] = ds.read();
  }
  
  ds.reset_search();
  
  byte MSB = data[1];
  byte LSB = data[0];

  float tempRead = ((MSB << 8) | LSB); //using two's compliment
  float TemperatureSum = tempRead / 16;
  
  return TemperatureSum;
  
}

//*****************************************************************************
//*****************************************************************************
//*****************************************************************************







