Hello everyone,
This is a quite basic question that has challenged me so far. I read the Nano ESP32 documentation and also posts related to that in this forum; But nothing led to an answer (solution) for me.
I am trying migrate from Arduino UNO to Nano, and a piece of the firmware, which is the serial communication, is not working. Simply explanation: I have a CO2 Data logger and I am trying to send a command to it to read it's values. ( It works perfectly with Arduino UNO)
Here is the simplified code : ( IT accepts two pins for Tx, Rx and then configure them in setup, and then send communication function send the command to device and tries to read values from it.) (this code with the usage of softwareSerial.h is working with no issue on UNO)
// Define software serial pins
const byte SOFT_RX = 44; // RX on Arduino to TX on device
const byte SOFT_TX = 43; // TX on Arduino to RX on device
bool checkConnection() {
const char* testCommand = "I\r";
Serial1.print(testCommand);
delay(1000);
if (Serial1.available()) {
while (Serial1.available()) {
Serial1.read();
}
Serial.println("Connection check successful.");
return true;
} else {
Serial.println("Connection check failed.");
return false;
}
}
// commands : D, I, M
void sendCommand(const char* command) {
Serial.print("Sending command: ");
Serial.println(command);
int attempts = 0;
const int maxAttempts = 10;
bool receivedResponse = false;
while (attempts < maxAttempts && !receivedResponse) {
// Clear any previous data
while (Serial1.available()) {
Serial1.read();
}
delay(50 + attempts); // Small delay before sending command + change the frequency
Serial1.print(command);
delay(1500); // Wait for response
// Read and print the response
String response = "";
while (Serial1.available()) {
char incomingByte = Serial1.read();
response += incomingByte;
Serial.print("Received byte: ");
Serial.println(incomingByte, HEX);
}
if (response.length() > 0) {
receivedResponse = true;
Serial.print("Response for command ");
Serial.print(command);
Serial.println(":");
Serial.println(response);
Serial.print("Response for command ");
Serial.print(command);
Serial.println(":");
for (int i = 0; i < response.length(); i++) {
Serial.print("0x");
Serial.print(response[i], HEX); // Print each character in hex
Serial.print(" "); // Add space for readability
}
Serial.println(); // Move to the next line after printing all characters
} else {
Serial.println("No response received, retrying...");
attempts++;
}
}
if (!receivedResponse) {
Serial.println("Failed to receive a response after 10 attempts.");
}
}
void setup() {
Serial.begin(9600);
Serial1.begin(9600, SERIAL_8N1, SOFT_RX, SOFT_TX);
while (!Serial) {
; // Wait for serial port to connect
}
delay(2000);
Serial.println("Enter commands to send to the device:");
}
void loop() {
if (Serial.available()) {
String command = Serial.readStringUntil('\n'); // Read the command from the Serial Monitor
command.trim(); // Remove any leading/trailing whitespace
command += "\r"; // Add carriage return to the command
delay(50); // Small delay before sending command
sendCommand(command.c_str()); // Send the command
delay(50); // Small delay after sending command
}
}
For the
const byte SOFT_RX = 44; // RX on Arduino to TX on device
const byte SOFT_TX = 43; // TX on Arduino to RX on device
part, I've already tried other pins such as 5, 6 (D2, D3) - 0,1 - 1,2 - etc. But I guess that I am doing something wrong more that pin specification.
ALSO, I've notice that this Arduino has voltage of 3.3v on its pins, and since UNO has 5v, can it be the reason? The SERIAL wire from the device as Tx, Rx, and GND.
Thanks in advance for your help