Send and receive temperature values via UART

I have a program that reads the temperature from the sensor and sends it via UART and the program receives the temperature value via UART as shown in the photo. Can you help me see? The received value is not the same as the actual temperature.


// ESP32 receives temperature from xiao nrf52840 sense 
HardwareSerial SerialUART(2); // Sử dụng cổng UART 1

void setup() {
  Serial.begin(9600);    // Initialize the Serial monitor for debugging
  Serial.println("Sketch Started");
  SerialUART.begin(9600);   // Initialize SerialUART for sending and receiving data
  Serial.println("SerialUART Started");
}

void loop() {
  // Nhận dữ liệu từ UART
  if (SerialUART.available()) {
    float receivedChar = SerialUART.read();
    Serial.print("Received: ");
    Serial.println(receivedChar);
  }
}


//send temperature via UART from seeed xiao nrf52840 sense to ESP32
#include "LSM6DS3.h"
#include "Wire.h"
#include <HardwareSerial.h>

LSM6DS3 myIMU(I2C_MODE, 0x6A);

void setup() {
  Serial.begin(9600);
  Serial1.begin(9600);
  while (!Serial1) {
    // Đợi cho Serial1 sẵn sàng
  }
  
  if (myIMU.begin() != 0) {
    Serial.println("Device error");
  } else {
    Serial.println("Device OK!");
  }
}

void loop() {
  float temperature = myIMU.readTempC();

  Serial.print("Degrees C1 = ");
  Serial.println(temperature, 4);

  if (Serial1.write(temperature) > 0) {
    // Gửi dữ liệu qua UART
    Serial1.println();
  } else {
    Serial.println("Error writing to Serial1");
  }

  delay(5000); // Chờ 5 giây trước khi lặp lại
}

Do not post images of codes and printouts, it is against forum rules.
Using the " < code > " tags in the toolbar, post your codes and printouts.

Your code is printing 3 numbers:
The first must be the correct temperature value;
The second is also a UART control character (13)
which means CR carriage return.
The third is actually a UART control character (10)
which stands for LF Line feed.

The equipment that sends the temperature is also sending these 2 control characters.

How should I fix the code? Please help me

i just need to display temperature value

Try this code for receive:

 

// ESP32 receives temperature from xiao nrf52840 sense
HardwareSerial SerialUART(2); // Sử dụng cổng UART 1

void setup() {
  Serial.begin(9600);    // Initialize the Serial monitor for debugging
  Serial.println("Sketch Started");
  SerialUART.begin(9600);   // Initialize SerialUART for sending and receiving data
  Serial.println("SerialUART Started");
}

void loop() {
  // Nhận dữ liệu từ UART
  if (SerialUART.available()) {
    char receivedChar = SerialUART.read();
    if (receivedChar != 0x0D or receivedChar != 0x0A ){
      Serial.print("Received: ");
      Serial.println(receivedChar);
    }
  }
}
1 Like

1. Connect ESP32-Sender with ESP32-Receiver using UART2 Port using this Table of wiring:

ESP32-Sender        ESP32-Receiver
U2_TXD (GPIO-17)    U2_RXD (GPIO-16)
U2_RXD (GPIO-16)     U2_TXD (GPIO-17)
GND                  GND

2. Connect your LSM6DS3 sensor with ESP32-Send as per following wiring Table:

ESP32-Sender         LSM6DS3
3.3V                 VCC
GND                  GND
SDA (GPIO-21)        SDA
SCL (GPIO-22)        SCL

3. Upload the following sketch (your one with some modifications) in ESP32-Sender. (uncompiled/untested)

//send temperature via UART from seeed xiao nrf52840 sense to ESP32
#include "LSM6DS3.h"
#include "Wire.h"

LSM6DS3 myIMU(I2C_MODE, 0x6A);

void setup() 
{
  Serial.begin(9600);
  Serial2.begin(9600);
  while (!Serial1) 
{
    // Đợi cho Serial1 sẵn sàng
  }
  
  if (myIMU.begin() != 0) 
  {
    Serial.println("Device error");
  } 
  else 
  {
    Serial.println("Device OK!");
  }
}

void loop() 
{
  float temperature = myIMU.readTempC();
  Serial.print("Degrees C1 = ");
  Serial.println(temperature, 4);
  //-----------------------------------------
  Serial2.println(temperature, 4);
  delay(1000); // Chờ 5 giây trước khi lặp lại
}

4. Upload the following sketch (your one with some modifications) in ESP32-Receier. (uncompiled/untested)

// ESP32 receives temperature from xiao nrf52840 sense 
char myData[15];

void setup() 
{
  Serial.begin(9600);    // Initialize the Serial monitor for debugging
  Serial.println("Sketch Started");
  Serial2.begin(9600);   // Initialize SerialUART for sending and receiving data
  Serial.println("Serial2UART Started");
}

void loop() 
{
  // Nhận dữ liệu từ UART
   byte n = Serial2.available();
   if(n != 0)
   {
       byte m = Serial2.readBytesUntil('\n', myData, sizeof(myData) - 1);
       myData[m] = '\0';     //insert null-character
       //Serial.println(myData);    //should appear Temp signal coming from Snder
       //----------------------------------------------------------------------------------------------------
       float temperature = atof(myData);
       Serial.println(temperature, 4);    //should appear Temp signal coming from Snder
   }  
}

5. Open Serial Monitor of ESP32-Receiver and chcek that the correct temperature value is appearing at 1-sec interval.

The receiving side, still receives 3 values at the same time: Received: 19.00
Received: 13.00
Received: 10.00
Received: 19.00
Received: 13.00
Received: 10.00

Your code only displays: 0.0000
0.0000

What do you see on the Serial Monitor of ESP32-Sender?

Are you compiling my sketches of post #6?

Your topic has been moved. Please do not post in "Uncategorized"; see Uncategorized - Arduino Forum.

yes, I compiled your sketches.

I have compiled both sketches of my post #6 without any errors. I am using 30-pin ESP32 Dev Module (Fig-1).


Figure-1

1 Like

Because I use seeed xiao nrf52840 sense instead of esp32 for sending. I changed serial2 to serial1 on your code and after that, result was correct

1 Like

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