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.
// 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);
}
}
}
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