Making esp32 assign the right data to the right field

when i run both my esp32 and the arduino code, the values meant to move to a particular field mixes up when the is network error or any power issue

I'm so sorry to hear that. The only way anyone could help you with that, reading only that problem description, is if they were running the same code on the same processor.

What are the chances of that?

The problem might be in the code that you have not posted

Before posting it please Auto format it in the IDE and then post it here using code tags

Also where is this "field" that you speak of? Do you have some display connected, or...????

If you have a hardware setup, we need to know about that too.

the phreading, conductivity and turbidity do mixes up

Relays are involved. Probably more, some sensors. Please post a complete, accurate wiring diagram. Do not omit the power and ground wiring.

i dont have the set up here but the values do get to my api just that it mixes up sometimes

Then please wait and post the information when you have the setup there. I hope the "setup" includes documentation...

That is just a repetition of your problem, still brief and general in nature...

I was reading OP post and it disappeared. First time that happened.

It didn't disappear. It was deleted by the OP.

 #include <ArduinoJson.h>
#include <HTTPClient.h>
#include <WiFiMulti.h>

#define RXp2 16
#define TXp2 17
 
const char *AP_SSID = "Prince";
const char *AP_PWD = "prince32@";
  
HardwareSerial mySerial(2); // Use hardware serial port 2 (pins 16 and 17)
WiFiMulti wifiMulti;
bool isConnected = false; // Flag to track WiFi connection status
 
void setup() {
  Serial.begin(115200); //Serial communication initialization
  mySerial.begin(9600, SERIAL_8N1, RXp2, TXp2); // Initialize the software serial port

  //delay(4000);
  wifiMulti.addAP(AP_SSID, AP_PWD); 
   while (wifiMulti.run() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
  }
  
  Serial.println();
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  isConnected = true; // WiFi connection successful

}
  
 
void loop() {
  if (isConnected) {
    if (mySerial.available()) {
    String phReading = mySerial.readStringUntil('\n');
    phReading.trim();
    postDataToServer(phReading);
    }
    delay(5000);
    if (mySerial.available()) {
    String conductivityReading = mySerial.readStringUntil('\n');
    conductivityReading.trim();
    postDataToServer2(conductivityReading);
    }
    delay(5000);
    if (mySerial.available()) {
    String turbidityReading = mySerial.readStringUntil('\n');
    turbidityReading.trim();
    postDataToServer3(turbidityReading);
    }
    delay(200000);
  }
  else {
    // If not connected, try to reconnect
    Serial.println("WiFi connection lost. Reconnecting...");
    wifiMulti.addAP(AP_SSID, AP_PWD); 
    while (wifiMulti.run() != WL_CONNECTED) {
      delay(1000);
      Serial.print(".");
    }
    Serial.println("WiFi reconnected!");
    isConnected = true; // Update the connection status
  }
  //delay(100);
}
 
bool postDataToServer(String phValue) {
  // Block until we are able to connect to the WiFi access point
  if (wifiMulti.run() == WL_CONNECTED) { 
    Serial.println("Posting JSON data to server...");
    HTTPClient http;    
    http.begin("https://water-purity-checker/createph");  
    http.addHeader("Content-Type", "application/json");         
    StaticJsonDocument<200> doc;
    // Add values in the document
    String phStringValue = String(phValue);
    doc["ph"] = phStringValue;
    String requestBody;
    serializeJson(doc, requestBody); 
    int httpResponseCode = http.POST(requestBody);

    if(httpResponseCode>0){ 
      String response = http.getString();                       
      Serial.println(httpResponseCode);  

      Serial.println(response);
      return true;
    }
    else {
      Serial.println("Error occurred while sending HTTP POST");
      return false;   
    }   
  }
}

bool postDataToServer2(String conductivityValue){ 
  if (wifiMulti.run() == WL_CONNECTED) { 
    HTTPClient http;    
    http.begin("https://water-purity-checker/createconductivity");  
    http.addHeader("Content-Type", "application/json");         
    StaticJsonDocument<200> doc;
    // Add values in the document
    String conductivityStringValue = String(conductivityValue);
    doc["conductivity"] = conductivityStringValue;
    String requestBody;
    serializeJson(doc, requestBody); 
    int httpResponseCode = http.POST(requestBody);

    if(httpResponseCode>0){ 
      String response = http.getString();                       
      Serial.println(httpResponseCode);  

      Serial.println(response);
      return true;
    }
    else {
     
      Serial.println("Error occurred while sending HTTP POST");  
      return false; 
    }   
  }
}

bool postDataToServer3(String turbidityValue){ 
  if (wifiMulti.run() == WL_CONNECTED) { 
    HTTPClient http;    
    http.begin("https://water-purity-checker/createturbidity");  
    http.addHeader("Content-Type", "application/json");         
    StaticJsonDocument<200> doc;
    // Add values in the document
    String turbidityStringValue = String(turbidityValue);
    doc["turbidity"] = turbidityStringValue;
    String requestBody;
    serializeJson(doc, requestBody); 
    int httpResponseCode = http.POST(requestBody);

    if(httpResponseCode>0){ 
      String response = http.getString();                       
      Serial.println(httpResponseCode);  

      Serial.println(response);
      return true;
    }
    else {
     
      Serial.println("Error occurred while sending HTTP POST");   
      return false;
    }   
  }
}
#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 3); // RX, TX pins for serial communication with ESP32

float calibration_value = 26-0.05;
int pHValue =0;
unsigned long int avgval;
int buffer_arr[10],temp;
int sensorPin = A2;

const int tdsSensorPin = A1;
const float tdsCalibrationFactor = 0.5;
int tdsValue =0;

float ph_act;
// Pin definitions
const int ultrasonicTriggerPin = 5;
const int ultrasonicEchoPin = 6;
const int relayPin = 4;

// Sensor thresholds
const int waterLevelThreshold = 70;
void setup() {
  // put your setup code here, to run once:
  pinMode(ultrasonicTriggerPin, OUTPUT);
  pinMode(ultrasonicEchoPin, INPUT);
  pinMode(relayPin, OUTPUT);

  digitalWrite(relayPin, LOW); // Initially turn off the relay

  Serial.begin(9600);
  mySerial.begin(9600);

}

void loop() {
  // put your main code here, to run repeatedly:
  int waterLevel = getWaterLevel();

  // Check water level
  //if ((pHValue >=4 && pHValue <=9) && tdsValue < 190 ) {
    if(waterLevel <= 5 ){
    digitalWrite(relayPin, HIGH);
   // Serial.println("Water level is sufficient!");
    }
     
  //}
  else {
    digitalWrite(relayPin, LOW);
    //Serial.println("Water level is low!");
  }
  // Display water level visually
  for (int i = 0; i < waterLevelThreshold; i++) {
    if (i < waterLevel) {
      Serial.print("|");
    } else {
      Serial.print("-");
    }
  }
  Serial.println();

  delay(700); // Adjust the delay as per your requirements

  for(int i=0;i<10;i++){
    buffer_arr[i]=analogRead(A0);
    delay(30);
  }
  for(int i=0;i<9;i++){
    for(int j=i+1;j<10;j++){
      if(buffer_arr[i]>buffer_arr[j]){
        temp=buffer_arr[i];
        buffer_arr[i]=buffer_arr[j];
        buffer_arr[j]=temp;
      }
    }
  }
  avgval=0;
  for(int i=2;i<8;i++)
  avgval+=buffer_arr[i];
  float volt=(float)avgval*5.0/1024/6;
  ph_act = -5.70 * volt + calibration_value;
  float pHValue = constrain(ph_act, 0, 14);

  //Serial.println("pH val: ");
  String finalpHValue = String(pHValue);
  mySerial.println(finalpHValue);
  //mySerial.println();
  delay(5000); 

  // Reading the analog value from the TDS sensor
  int tdsRawValue = analogRead(tdsSensorPin);

  // Convert the raw analog value to TDS in ppm using the calibration factor
  float tdsValue = tdsRawValue * tdsCalibrationFactor;

  // Print the TDS value to the serial monitor
  //Serial.print("TDS Value (ppm): ");
  String result = String(tdsValue);
  mySerial.println(result);

  delay(5000);

  // Check water quality based on TDS value
  if (tdsValue <25 && pHValue >=6.2 && pHValue <= 7.5 ) {
    mySerial.println("Excellent");
  } else if (tdsValue >=25  && tdsValue <= 85 && pHValue >=6.2 && pHValue <= 7.5 ) {
    mySerial.println("Good");
  }else if (tdsValue >=85  && tdsValue <=190 && pHValue >=5.0 && pHValue <= 9 ) {
    mySerial.println("Acceptable");
  }  else {
    mySerial.println("Poor");
  }

 

  delay(1000); // Adjust the delay as per your requirements
}

// Function to measure water level using ultrasonic sensor
int getWaterLevel() {
  digitalWrite(ultrasonicTriggerPin, LOW);
  delayMicroseconds(2);
  digitalWrite(ultrasonicTriggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(ultrasonicTriggerPin, LOW);

  long duration = pulseIn(ultrasonicEchoPin, HIGH);
  int waterLevel = duration * 0.034 / 2; // Calculate distance based on speed of sound

  return waterLevel;
}


Same thing to me.

@chipismith this is becoming tiresome. We already have your code. It's the hardware information, and a more detailed problem description that is missing.

sorry. i was correcting some mistake

problem description:
when i both the esp32 and the arduino code, the values meant for ph reading sometimes gets to conductivity reading and conductivity reading gets to turbidity and turbidity gets to ph reading

...in serial output?

yes

Related to power application/cycling? Network issue?

when i power the arduino before esp32 and sometimes network do contribute also