Douple i2C is not working

mcmuff
Posts: 1
Joined: Thu Jul 11, 2024 2:07 pm

Douple i2C is not working

Postby mcmuff » Thu Jul 11, 2024 2:18 pm

Hello everyone,

II have been trying for days to activate both I2c ports of the ESP32 controller.

The setup:
An Adafruit HUZZAH32 ESP32 Feather is connected to 3 sensors (temperature, EC, pH) and 3 pumps (i2C-controlled) via the standard I2C port.

A second I2C port (PIN 32 and 14) is connected to a Raspberry PI. The Raspberry Pi should be able to read the values from the sensors and forward commands to control the pumps.

All addresses are recognised on the regular port (pumps+sensors). The values of the sensors are also output via the serial monitor).

The problem: I cannot establish a connection between the Raspberry and the ESP controller.

Here is the code. Does anyone have any idea where the problem might lie?


Code: Select all

#include <Wire.h>
#include <Ezo_i2c_util.h>
#include <Ezo_i2c.h>
#include <sequencer4.h>

#define SDA_PIN_1 23
#define SCL_PIN_1 22
#define SDA_PIN_2 32
#define SCL_PIN_2 14
#define I2C_SLAVE_ADDRESS 0x45

Ezo_board PH = Ezo_board(99, "PH");    // PH circuit object
Ezo_board EC = Ezo_board(100, "EC");   // EC circuit object
Ezo_board RTD = Ezo_board(102, "RTD"); // RTD circuit object

Ezo_board PMP1 = Ezo_board(56, "PMP1");
Ezo_board PMP2 = Ezo_board(57, "PMP2");
Ezo_board PMP3 = Ezo_board(58, "PMP3");

Ezo_board device_list[] = {PH, EC, RTD};
Ezo_board *default_board = &device_list[0]; 

const uint8_t device_list_len = sizeof(device_list) / sizeof(device_list[0]);

const int EN_PH = 12;
const int EN_EC = 27;
const int EN_RTD = 15;
const int EN_AUX = 33;

const unsigned long reading_delay = 1000;
unsigned int poll_delay = 2000 - reading_delay * 2 - 300;

void step1();
void step2();
void step3();
void step4();
Sequencer4 Seq(&step1, reading_delay, &step2, 300, &step3, reading_delay, &step4, poll_delay);

void setup() {
  pinMode(EN_PH, OUTPUT);
  pinMode(EN_EC, OUTPUT);
  pinMode(EN_RTD, OUTPUT);
  pinMode(EN_AUX, OUTPUT);
  
  // Setze die Enable-Pins auf HIGH, um die Sensoren zu aktivieren
  digitalWrite(EN_PH, LOW);
  digitalWrite(EN_EC, LOW);
  digitalWrite(EN_RTD, HIGH);
  digitalWrite(EN_AUX, HIGH);
  
  Serial.begin(115200);

  Wire.begin(SDA_PIN_1, SCL_PIN_1);
  Wire1.begin(SDA_PIN_2, SCL_PIN_2);
  Wire1.setClock(100000);

  Seq.reset();
  Serial.println("Setup complete");
}

void loop() {
  Seq.run();
  handlePixtendCommunication();
}

void step1() {
  Serial.println("Sending RTD read command");
  RTD.send_read_cmd();
}

void step2() {
  Serial.println("Receiving RTD reading");
  receive_and_print_reading(RTD);

  if ((RTD.get_error() == Ezo_board::SUCCESS) && (RTD.get_last_received_reading() > -1000.0)) {
    Serial.print("RTD reading valid: ");
    Serial.println(RTD.get_last_received_reading());
    PH.send_cmd_with_num("T,", RTD.get_last_received_reading());
    EC.send_cmd_with_num("T,", RTD.get_last_received_reading());
  } else {
    Serial.println("RTD reading invalid, sending default temp");
    PH.send_cmd_with_num("T,", 25.0);
    EC.send_cmd_with_num("T,", 25.0);
  }
}

void step3() {
  Serial.println("Sending PH and EC read commands");
  PH.send_read_cmd();
  EC.send_read_cmd();
}

void step4() {
  Serial.println("Receiving PH and EC readings");
  receive_and_print_reading(PH);
  if (PH.get_error() == Ezo_board::SUCCESS) {
    Serial.print("PH: ");
    Serial.print(PH.get_last_received_reading());
  } else {
    Serial.println("PH reading failed");
  }
  Serial.print("  ");
  receive_and_print_reading(EC);
  if (EC.get_error() == Ezo_board::SUCCESS) {
    Serial.print("EC: ");
    Serial.print(EC.get_last_received_reading());
  } else {
    Serial.println("EC reading failed");
  }
  Serial.print("  ");
  Serial.print("Temp: ");
  Serial.print(RTD.get_last_received_reading());
  Serial.println();
}

void handlePixtendCommunication() {
  Wire1.requestFrom(I2C_SLAVE_ADDRESS, 1);
  while (Wire1.available()) {
    char command = Wire1.read();
    Serial.print("Received command: ");
    Serial.println(command);

    switch(command) {
      case '1':
        PMP1.send_cmd("d,1");
        break;
      case '2':
        PMP1.send_cmd("d,0");
        break;
      case '3':
        PMP2.send_cmd("d,1");
        break;
      case '4':
        PMP2.send_cmd("d,0");
        break;
      case '5':
        PMP3.send_cmd("d,1");
        break;
      case '6':
        PMP3.send_cmd("d,0");
        break;
      default:
        Serial.println("Unrecognized command");
        break;
    }
  }

  float sensor_data[3] = {PH.get_last_received_reading(), EC.get_last_received_reading(), RTD.get_last_received_reading()};
  Wire1.beginTransmission(I2C_SLAVE_ADDRESS);
  Wire1.write((uint8_t*)sensor_data, sizeof(sensor_data));
  Wire1.endTransmission();
}

Who is online

Users browsing this forum: Google [Bot] and 21 guests