Hi guys, im new on this stuff, so dont blame me. I have used Chatgpt to make this code work on my arduino uno R4 Wifi, which also prints correct data. The program is set to send and read data from IR transmitter/receiver thru the TX and RX on the UNO R4 and D4 and D5 on my esp32
I am receiving correct numbers in the serial monitor but i want to make it work properly
The command needs to be send in 300 baud 7E2
and data received in 1200 baud 7E2
this is the ESP32 version (Arduino Nano ESP32) that reads the data, but with garbage as well
what am i doing wrong ?
#include <HardwareSerial.h>
#define PIN_LED LED_BUILTIN
#define KAMTIMEOUT 5000
#define BUFFER_SIZE 1500
#define RX_PIN D4 // Adjust for your specific board
#define TX_PIN D5
char rxdata[BUFFER_SIZE];
byte numRequest = 1;
char requests[] = {'1'}; // Changed to char to hold ASCII characters
void setup() {
pinMode(PIN_LED, OUTPUT);
digitalWrite(PIN_LED, LOW);
Serial.begin(1200); // Use for debugging, keep at this baud rate
Serial1.begin(300, SERIAL_7E2, RX_PIN, TX_PIN); // Start Serial1 for sending with 8N1 configuration for ASCII
delay(15000); // Initial delay
}
void loop() {
for (int i = 0; i < numRequest; i++) {
kamReadReq(requests[i]);
delay(15000); // Delay between requests
}
}
void kamReadReq(char req) { // Parameter type changed to char
// Configure Serial1 for sending the request at 300 baud
Serial1.begin(300, SERIAL_7E2); // Change to 8N1 configuration
Serial1.print("/#"); // Using print for ASCII characters
Serial1.print(req); // Using print for ASCII character
Serial1.print('\r'); // Carriage return in ASCII
Serial1.flush(); // Ensure command is sent
// Switch Serial1 to receiving configuration at 1200 baud
Serial1.begin(1200, SERIAL_7E2); // Keep 8N1 configuration for receiving
unsigned short rxnum = kamReceive();
// Debug output
Serial.begin(1200);
if (rxnum != 0) {
Serial.println("Received response:");
for (int i = 0; i < rxnum; i++) {
Serial.print(rxdata[i]); // Print as is, as data is now ASCII
}
Serial.println();
}
}
unsigned short kamReceive() {
unsigned long rxindex = 0;
unsigned long starttime = millis();
int r = -1; // Changed to int to handle possible -1 return value from read()
while (r != '\r') { // Use CR ('\r') as the end-of-message delimiter
if (millis() - starttime > KAMTIMEOUT) {
return rxindex;
}
if (rxindex >= BUFFER_SIZE) {
return rxindex;
}
if (Serial1.available() > 0) {
r = Serial1.read();
if (r != -1) { // Only store if read() did not return -1
rxdata[rxindex++] = r;
starttime = millis(); // Reset the timeout timer on each byte received
}
}
}
return rxindex;
}
the print looks like this (every numbers except some of the zeroes and garbage symbols are relevant data)
14:47:54.225 -> 0�9�93��0�30����0����5��0000000�0000000�0000000�0000000�0000000�0000000�00000�3��
14:48:16.235 -> Received response:
14:48:16.235 -> 0�9�93��0�30����0����5��0000000�0000000�0000000�0000000�0000000�0000000�00000�3��
14:48:38.257 -> Received response:
14:48:38.257 -> 0�9�93��0�30����0����5��0000000�0000000�0000000�0000000�0000000�0000000�00000�3��
14:49:00.234 -> Received response:
14:49:00.234 -> 0�9�93��0�30����0����5��0000000�0000000�0000000�0000000�0000000�0000000�00000�3��
the working code from my uno r4 wifi looks like this
#define PIN_LED 13
#define KAMTIMEOUT 5000
#define BUFFER_SIZE 1500
byte rxdata[BUFFER_SIZE];
byte numRequest = 1;
byte requests[]={'1'};
void setup () {
pinMode(PIN_LED, OUTPUT);
digitalWrite(PIN_LED, 0);
delay(15000);
}
void loop () {
for(int i =0; i<numRequest; i++){
kamReadReq(requests[i]);
delay(15000);
}
}
float kamReadReq(byte req) {
byte sendmsg[] = { '/', '#', req, 0x0D, 0x0A};
int rxnum=0;
byte single = 0;
for (int i = 0; i < BUFFER_SIZE; i++) {
rxdata[i]= 8;
}
Serial1.begin(300);
for (int i = 0; i < 5; i++) {
Serial1.write(sendmsg[i]);
}
Serial1.end();
Serial1.flush();
Serial1.begin(1200, SERIAL_7E2);
rxnum = kamReceive();
Serial1.end();
Serial1.flush();
Serial.begin(1200);
if(rxnum != 0) {
Serial.println("");
Serial.print("Writing: ");
Serial.print("kWh: ");
for(int i = 0; i < 7; i++) {
Serial.print((char)(rxdata[i]));
}
Serial.print(", m3: ");
for(int i = 7; i < 15; i++) {
Serial.print((char)(rxdata[i]));
}
Serial.println();
}
Serial.flush();
Serial.end();
return 0;
}
unsigned short kamReceive() {
unsigned long rxindex = 0;
unsigned long starttime = millis();
byte r=0;
while(r != 0x03){
if(millis()-starttime > KAMTIMEOUT) {
return rxindex;
}
if(rxindex>=BUFFER_SIZE){
return rxindex;
}
while(Serial1.available()>0) {
r = Serial1.read();
rxdata[rxindex] = r;
rxindex++;
starttime = millis();
}
}
return rxindex;
}