pololu / vl53l0x-arduino

Pololu Arduino library for VL53L0X time-of-flight distance sensor
https://www.pololu.com/product/2490
Other
346 stars 162 forks source link

Reading from pH sensor and dissolved oxygen sensor on same i2c bus #23

Closed srkmurd closed 6 years ago

srkmurd commented 6 years ago

I am trying to read from these sensors but only 1 sensor's reading is coming on the serial monitor on sending the 'R' character. the code is `#include //enable I2C.

define address 97 //default I2C ID number for EZO D.O. Circuit.

define address_pH 99 //default I2C ID number for EZO pH Circuit.

char computerdata[20]; //we make a 20 byte character array to hold incoming data from a pc/mac/other. byte received_from_computer = 0; //we need to know how many characters have been received. byte code = 0; //used to hold the I2C response code. char DO_data[20]; //we make a 20 byte character array to hold incoming data from the D.O. circuit. byte inchar = 0; //used as a 1 byte buffer to store inbound bytes from the D.O. Circuit. int time = 900; //used to change the delay needed depending on the command sent to the EZO Class D.O. Circuit. float DO_float; //float var used to hold the float value of the DO. char DO; //char pointer used in string parsing. char sat; //char pointer used in string parsing. float do_float; //float var used to hold the float value of the dissolved oxygen. float sat_float; //float var used to hold the float value of the saturation percentage.

char computerdata_pH[20]; //we make a 20 byte character array to hold incoming data from a pc/mac/other. byte received_from_computer_pH = 0; //we need to know how many characters have been received. byte code_pH = 0; //used to hold the I2C response code_pH. char pH_data[20]; //we make a 20-byte character array to hold incoming data from the pH circuit. byte in_char_pH = 0; //used as a 1 byte buffer to store inbound bytes from the pH Circuit. byte i = 0; //counter used for ph_data array. int time_pH = 900; //used to change the delay needed depending on the command sent to the EZO Class pH Circuit. float ph_float; //float var used to hold the float value of the pH.

void setup() //hardware initialization. { Serial.begin(9600); //enable serial port. Wire.begin(); //enable I2C port. }

void loop() { //the main loop. byte i = 0; //counter used for DO_data array.

if (Serial.available() > 0) { //if data is holding in the serial buffer received_from_computer = Serial.readBytesUntil(13, computerdata, 20); //we read the data sent from the serial monitor(pc/mac/other) until we see a . We also count how many characters have been received. computerdata[received_fromcomputer] = 0; //stop the buffer from transmitting leftovers or garbage. / computerdata[0] = tolower(computerdata[0]); //we make sure the first char in the string is lower case./ if (computerdata[0] == 'c' || computerdata[0] == 'r')time = 900; //if a command has been sent to calibrate or take a reading we wait 600ms so that the circuit has time to take the reading. else time_ = 300; //if not 300ms will do

Wire.beginTransmission(address);                                      //call the circuit by its ID number.

Wire.write(computerdata);                                             //transmit the command that was sent through the serial port.

   if (strcmp(computerdata, "sleep") != 0) {  //if the command that has been sent is NOT the sleep command, wait the correct amount of time and request data.
  //if it is the sleep command, we do nothing. Issuing a sleep command and then requesting data will wake the D.O. circuit.

  delay(time_);                     //wait the correct amount of time for the circuit to complete its instruction.

  Wire.requestFrom(address, 20, 1); //call the circuit and request 20 bytes (this may be more than we need)

  code = Wire.read();               //the first byte is the response code, we read this separately.

  switch (code) {                   //switch case based on what the response code is.
    case 1:                         //decimal 1.
      Serial.println("Success");    //means the command was successful.
      break;                        //exits the switch case.

    case 2:                         //decimal 2.
      Serial.println("Failed");     //means the command has failed.
      break;                        //exits the switch case.

    case 254:                      //decimal 254.
      Serial.println("Pending");   //means the command has not yet been finished calculating.
      break;                       //exits the switch case.

    case 255:                      //decimal 255.
      Serial.println("No Data from do");   //means there is no further data to send.
      break;                       //exits the switch case.
  }
}
while (Wire.available()) {       //are there bytes to receive.
  in_char = Wire.read();         //receive a byte.
  DO_data[i] = in_char;          //load this byte into our array.
  i += 1;
  if (in_char == 0 || in_char_pH == 0) {            //if we see that we have been sent a null command.
    i = 0;                       //reset the counter i to 0.
    Wire.endTransmission();      //end the I2C data transmission.
    break;                       //exit the while loop.
    Serial.println(DO_data);
    delay(100);
    Wire.beginTransmission(address_pH);                                      //call the circuit by its ID number.
    Wire.write(computerdata_pH);                                             //transmit the command that was sent through the serial port.
    Wire.endTransmission();                                               //end the I2C data transmission.
    if (isDigit(DO_data[0])) {
      string_pars();                  //If the first char is a number we know it is a DO reading, lets parse the DO reading
    }
    else {                            //if it’s not a number
      Serial.println(DO_data);        //print the data.
      for (i = 0; i < 20; i++) {      //step through each char
        DO_data[i] = 0;               //set each one to 0 this clears the memory
      }
    }

    if (strcmp(computerdata_pH, "sleep") != 0) {  //if the command that has been sent is NOT the sleep command, wait the correct amount of time and request data.
      //if it is the sleep command, we do nothing. Issuing a sleep command and then requesting data will wake the D.O. circuit.
    }
  }
}

}

   if (Serial.available() > 0) {                                           //if data is holding in the serial buffer
received_from_computer = Serial.readBytesUntil(13, computerdata, 20); //we read the data sent from the serial monitor(pc/mac/other) until we see a <CR>. We also count how many characters have been received.
computerdata[received_from_computer] = 0;                             //stop the buffer from transmitting leftovers or garbage.
/* computerdata[0] = tolower(computerdata[0]);                           //we make sure the first char in the string is lower case.*/
if (computerdata[0] == 'c' || computerdata[0] == 'r')time_ = 900;     //if a command has been sent to calibrate or take a reading we wait 600ms so that the circuit has time to take the reading.
else time_ = 300;                                                     //if not 300ms will do

Wire.beginTransmission(address);                                      //call the circuit by its ID number.

Wire.write(computerdata);                                             //transmit the command that was sent through the serial port.

   delay(time_pH);                     //wait the correct amount of time for the circuit to complete its instruction.

   Wire.requestFrom(address_pH, 20, 1); //call the circuit and request 20 bytes (this may be more than we need)
      code_pH = Wire.read();               //the first byte is the response code, we read this separately.
      switch (code_pH) {                  //switch case based on what the response code is.
        case 1:                         //decimal 1.
          Serial.println("Success");    //means the command was successful.
          break;                        //exits the switch case.

        case 2:                         //decimal 2.
          Serial.println("Failed");     //means the command has failed.
          break;                        //exits the switch case.

        case 254:                      //decimal 254.
          Serial.println("Pending");   //means the command has not yet been finished calculating.
          break;                       //exits the switch case.

        case 255:                      //decimal 255.
          Serial.println("No Data from pH");   //means there is no further data to send.
          break;                       //exits the switch case.

      }

  while (Wire.available()) {         //are there bytes to receive.
  in_char_pH = Wire.read();           //receive a byte.
  pH_data[i] = in_char_pH;            //load this byte into our array.
  i += 1;                          //incur the counter for the array element.
  if (in_char_pH == 0) {              //if we see that we have been sent a null command.
    i = 0;                         //reset the counter i to 0.
    Wire.endTransmission();        //end the I2C data transmission.
    break;                         //exit the while loop.
  }
}
     Serial.println(pH_data);          //print the data.
    }

} void string_pars() { //this function will break up the CSV string into its 2 individual parts, DO and %sat. byte flag = 0; //this is used to indicate is a “,” was found in the string array byte i = 0; //counter used for DO_data array.

for (i = 0; i < 20; i++) {          //Step through each char
  if (DO_data[i] == ',') {          //do we see a ','
    flag = 1;                       //if so we set the var flag to 1 by doing this we can identify if the string being sent from the DO circuit is a CSV string containing tow values
  }
}

if (flag != 1) {                    //if we see the there WAS NOT a ‘,’ in the string array
  Serial.print("DO:");              //print the identifier
  Serial.println(DO_data);          //print the reading
}

if (flag == 1) {                    //if we see the there was a ‘,’ in the string array
  DO = strtok(DO_data, ",");        //let's pars the string at each comma
  sat = strtok(NULL, ",");          //let's pars the string at each comma
  Serial.print("DO:");              //print the identifier
  Serial.println(DO);               //print the reading
  Serial.print("Sat:");             //print the identifier
  Serial.println(sat);              //print the reading
  flag = 0;                         //reset the flag
}

/*                                //uncomment this section if you want to take the ASCII values and convert them into a floating point number.
  DO_float=atof(DO);
  sat_float=atof(sat);
*/

}` only 1 sensor ,dissolved oxygen sensor is able to respond as can also be seen on the green led blink on the sensor. can you please help on this issue.?

kevin-pololu commented 6 years ago

Hi,

This is a library for a VL53L0X distance sensor, and the GitHub issues are intended for tracking bugs and feature requests related to the library code. It doesn't look like you are using this library (I don't see any mention of the VL53L0X in your code), so this is not the right place for your question.

I would suggest asking for help either from the manufacturer of your sensor boards or from a more general community like the Arduino forum.

Kevin