Hello, I'm trying to send data from Arduino Uno to ESP32 with the goal of inserting that data into a database. However, I'm encountering an issue when receiving data from Arduino Uno, where the received data contains random characters. What could be causing these random characters? Please help me to resolve this issue.
This is the Arduino Serial Monitor.
This is the ESP Serial Monitor.
Arduino Program
#include<SoftwareSerial.h>
SoftwareSerial PIN(2,3);
//TDS
#include <EEPROM.h>
#include "GravityTDS.h"
#define TdsSensorPin A1
GravityTDS gravityTds;
float temperature = 28.4, tdsValue = 0;
//pH
#define SensorPin A0 //pH meter Analog output to Arduino Analog Input 0
#define Offset -29 //deviation compensate
#define LED 13
#define samplingInterval 50
#define printInterval 1000
#define ArrayLenth 100 //times of collection
int pHArray[ArrayLenth]; //Store the average value of the sensor feedback
int pHArrayIndex = 0;
int FloatSensorAtas = 2;
int FloatSensorBawah = 4;
//Turbidity
int pinTurb = A3;
float V;
float result;
float kekeruhan;
float VRata2;
float VHasil;
//Relay
const int ABMix = 13; // 4
const int pHuP = 12; // 2
const int pHdown = 7; // 3
void setup() {
//TDS
Serial.begin(230400);
PIN.begin(230400);
gravityTds.setPin(TdsSensorPin);
gravityTds.setAref(5.0); //reference voltage on ADC, default 5.0V on Arduino UNO
gravityTds.setAdcRange(1024); //1024 for 10bit ADC;4096 for 12bit ADC
gravityTds.begin(); //initialization
kran.attach(3);
pinMode(ABMix, OUTPUT);
pinMode(pHuP, OUTPUT);
pinMode(pHdown, OUTPUT);
}
void loop() {
//TDS Read
gravityTds.setTemperature(temperature); // set the temperature and execute temperature compensation
gravityTds.update(); //sample and calculate
tdsValue = gravityTds.getTdsValue(); // then get the value
Serial.print(tdsValue, 0);
Serial.println("ppm");
//pH Read
static unsigned long samplingTime = millis();
static unsigned long printTime = millis();
static float pHValue, voltage;
if (millis() - samplingTime > samplingInterval) {
pHArray[pHArrayIndex++] = analogRead(SensorPin);
if (pHArrayIndex == ArrayLenth) pHArrayIndex = 0;
voltage = avergearray(pHArray, ArrayLenth) * 5.0 / 1024;
pHValue = 20 * voltage + Offset;
samplingTime = millis();
}
//Turbidity Read
V = 0;
for (int i = 0; i < 800; i++) {
V += ((float)analogRead(pinTurb) / 1023) * 5;
}
VRata2 = V / 800;
VHasil = roundf(VRata2 * 10.0f) / 10.0f;
if (VHasil < 2.5) {
kekeruhan = 3000;
}
else {
kekeruhan = -1120.4 * square(VHasil) + 5742.3 * VHasil - 4353.8;
}
result = kekeruhan / 10;
if (result < 69.0) {
Serial.print("Air Sangat Jernih ");
Serial.print(" Val: ");
Serial.println(result);
}
else if (result > 70.0 && result < 154.0) {
Serial.print("Air Jernih ");
Serial.print(" Val: ");
Serial.println(result);
}
else {
Serial.print("Air Keruh ");
Serial.print(" Val: ");
Serial.println(result);
}
Serial.print("Voltage:");
Serial.print(voltage, 2);
Serial.print(" pH value: ");
Serial.println(pHValue, 2);
Serial.println(" ");
String datakirim = String(result) + "#" + String(tdsValue) + "#" + String(pHValue);
PIN.println(datakirim);
delay(1000);
}
double avergearray(int* arr, int number) {
int i;
int max, min;
double avg;
long amount = 0;
if (number <= 0) {
Serial.println("Error number for the array to avraging!/n");
return 0;
}
if (number < 5) { //less than 5, calculated directly statistics
for (i = 0; i < number; i++) {
amount += arr[i];
}
avg = amount / number;
return avg;
}
else {
if (arr[0] < arr[1]) {
min = arr[0]; max = arr[1];
}
else {
min = arr[1]; max = arr[0];
}
for (i = 2; i < number; i++) {
if (arr[i] < min) {
amount += min; //arr<min
min = arr[i];
}
else {
if (arr[i] > max) {
amount += max; //arr>max
max = arr[i];
}
else {
amount += arr[i]; //min<=arr<=max
}
}//if
}//for
avg = (double)amount / (number - 2);
}//if
return avg;
}
This is ESP Program
#include <SoftwareSerial.h>
SoftwareSerial PIN(35, 34); // RX, TX
String simpantytyd[3];
int count = 0;
String ____DATS;
#define RXp0 3
#define TXp0 1
void setup() {
Serial.begin(230400);
PIN.begin(230400);
}
void loop() {
String ____DATS = "";
while (PIN.available() > 0)
{
____DATS += char(PIN.read());
}
Serial.println(____DATS);
____DATS.trim();
if (____DATS != ""){
int index = 0;
for (int i = 0 ; i<= ____DATS.length() ; i++ ){
char pembatas = '#';
if(____DATS[i] != pembatas)
simpantytyd[index] += ____DATS[i];
else
index++;
}
if (index == 2){
Serial.println(simpantytyd[0]);
Serial.println(simpantytyd[1]);
Serial.println(simpantytyd[2]);
Serial.println("");
}
simpantytyd[0] = "";
simpantytyd[1] = "";
simpantytyd[2] = "";
}
delay(1000);
}
Please help me, I have been stuck here for hours.