I've been playing with an Atlas scientific EZO pH probe attached to my esp32 by I2C. The device works fully using the Arduino wire command. However using the esp-idf I have only been successful at getting commands to be received by the device. I believe the problem to be with the processing delay required by the EZO device of 300ms but I'm unclear as to how to add in a read delay into my I2C command sequence.
- printf("\r\nStarting command sequence\r\n");
- i2c_cmd_handle_t cmd = i2c_cmd_link_create();
- //begin building command sequence
- i2c_master_start(cmd);
- i2c_master_write_byte(cmd, (99 << 1) | I2C_MASTER_WRITE, true);
- //
- for(int num_of_char_in_cmd_str = 0; num_of_char_in_cmd_str < count;num_of_char_in_cmd_str++ ){
- i2c_master_write(cmd,&input_characters[num_of_char_in_cmd_str], 1,false);
- printf("\r\nThe value of input is: %c\r\n",(char)input_characters[num_of_char_in_cmd_str]);
- }
- //Now we switch to read mode and build instructions for receiving
- i2c_master_write_byte(cmd, (99 << 1)| I2C_MASTER_READ,1);
- for(int i=0; i<20;i++){
- i2c_master_read_byte(cmd, &data_byte[i], I2C_MASTER_ACK);
- }
- i2c_master_stop(cmd);
- //Send command set to device
- esp_err_t ret = i2c_master_cmd_begin(I2C_NUM_1, cmd, 1000 / portTICK_RATE_MS);
- if(ret == ESP_OK){
- printf( "i2c succes during read: %s", esp_err_to_name(ret));
- printf("\r\n-> Data read from EZO pH Probe\r\n");
- for(int i = 0; i < 20; i++){
- printf("\r\n Bytes received %u\r\n",data_byte[i]);
- }
- }
- if(ret != ESP_OK){
- printf( "i2c error during read: %s", esp_err_to_name(ret));
- }
- i2c_cmd_link_delete(cmd);