Communication problem between esp8266 and UNO

Hello everyone,

I have a communication issue between an Arduino Uno and an ESP8266 connected to the Arduino Cloud.

The Uno measures several variables (temperature, conductivity, pH, as well as 4 liquid levels). It also regulates pH and conductivity by receiving a setpoint for each from the ESP8266 via a Dashboard.

The ESP8266 receives all the values and displays them on a Dashboard, and the user can change the setpoints and so on.

When the communication (serial via pins 1 and 2) is one-way, it works fine. However, as soon as it needs to be bidirectional, I don't receive anything.

I feel like I've tried everything and I'm a bit lost.

Here are my codes:

ESP8266 :

#include "thingProperties.h"

void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  delay(1500); 
  
  consigne_ph = 8.0; // Consigne de pH
  consigne_conductivite = 2000.0; // Consigne de conductivité


  initProperties();
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
}

void loop() {
  ArduinoCloud.update();
  // Envoyer les valeurs de consigne pH et conductivité à l'autre Arduino
  Serial.print(consigne_ph);
  Serial.print(",");
  Serial.print(consigne_conductivite);
  Serial.println();

  // Attendre la réponse de l'autre Arduino
  while (Serial.available() == 0) {
    delay(10);
  }

  // Lire les valeurs renvoyées par l'autre Arduino
  String data = Serial.readStringUntil('\n');
  int separatorIndex = data.indexOf(',');
  if (separatorIndex != -1) {
    float ph = data.substring(0, separatorIndex).toFloat();
    float conductivite = data.substring(separatorIndex + 1).toFloat();
    float temperature = data.substring(separatorIndex + 1, data.indexOf(',', separatorIndex + 1)).toFloat();
    bool niveau_A = data.substring(data.indexOf(',', separatorIndex + 1) + 1, data.indexOf(',', separatorIndex + 1 + 1)).toInt();
    bool niveau_B = data.substring(data.indexOf(',', separatorIndex + 1 + 1) + 1, data.indexOf(',', separatorIndex + 1 + 2)).toInt();
    bool niveau_C = data.substring(data.indexOf(',', separatorIndex + 1 + 2) + 1, data.indexOf(',', separatorIndex + 1 + 3)).toInt();
    bool niveau_D = data.substring(data.indexOf(',', separatorIndex + 1 + 3) + 1).toInt();

  }

  delay(10000);
}


/*
  Since ConsignePh is READ_WRITE variable, onConsignePhChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onConsignePhChange()  {
  // Add your code here to act upon ConsignePh change
}

/*
  Since ConsigneConductivite is READ_WRITE variable, onConsigneConductiviteChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onConsigneConductiviteChange()  {
  // Add your code here to act upon ConsigneConductivite change
}

Arduino UNO :

float consigne_ph; // Consigne de pH reçue
float consigne_conductivite; // Consigne de conductivité reçue

void setup() {
  Serial.begin(9600);
}

void loop() {
  if (Serial.available() > 0) {
    // Lire les valeurs de consigne de pH et conductivité envoyées par l'autre Arduino
    String data = Serial.readStringUntil('\n');
    int separatorIndex = data.indexOf(',');
    if (separatorIndex != -1) {
      consigne_ph = data.substring(0, separatorIndex).toFloat();
      consigne_conductivite = data.substring(separatorIndex + 1).toFloat();
    }

    // Traiter les données et simuler les valeurs de température et niveaux
    float ph = consigne_ph+0.5;
    float conductivite = consigne_conductivite+500;
    float temperature = 25.0; // Simulation de la valeur de température
    bool niveau_A = random(0, 2); // Générer une valeur aléatoire pour le niveau A (0 ou 1)
    bool niveau_B = random(0, 2); // Générer une valeur aléatoire pour le niveau B (0 ou 1)
    bool niveau_C = random(0, 2); // Générer une valeur aléatoire pour le niveau C (0 ou 1)
    bool niveau_D = random(0, 2); // Générer une valeur aléatoire pour le niveau D (0 ou 1)
    
    // Envoyer les valeurs de pH, conductivité, température et niveaux
    Serial.print(ph);
    Serial.print(",");
    Serial.print(conductivite);
    Serial.print(",");
    Serial.print(temperature);
    Serial.print(",");
    Serial.print(niveau_A);
    Serial.print(",");
    Serial.print(niveau_B);
    Serial.print(",");
    Serial.print(niveau_C);
    Serial.print(",");
    Serial.println(niveau_D);
    delay(5000);
  }
}

Thank you very much !

so now, what is on Serial? the computer or the esp8266?

I guess luck has a bit to do with that. You are coding for hardware serial, which is fine, but it uses pins 0,1.

I might also point out that luck may have been in your favour too. ESP is strictly 3.3v and you will need a voltage divider 1k /2k on Uno Tx

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.