How can I check the output of my code using Arduino IDE and ESP8266 when I want to display the data to a large 7-segment LED

serial.write does not exist (AFAIK). Serial.write() with an Uppercase S do exist (Google for what it does) but the one you want to talk about is uart.write().
uart.write() sends binary data one bit after the other. It doesn't care whether you do the math with dec or hex, the compiler allows for any representation.
Try

int mixedFormat  = 0x28 + 18 + '3'; // Should be 40 + 18 + 51 = 109 = 0x6D = 0b01101101 = 'm'
Serial.print("mixedFormat   (Hex): ");
Serial.println(mixedFormat   , HEX);    // 6D
Serial.print("mixedFormat   (Dec): ");
Serial.println(mixedFormat   , DEC);    // 109
Serial.print("mixedFormat   (Bin): ");
Serial.println(mixedFormat   , BIN);    // 1101101
Serial.print("mixedFormat   (Char): ");
Serial.write(mixedFormat);              // m

Same value, different display.
The thing you can do before hardware arrives is to set a Wokwi simulation up and tinker with its Logic Analyzer. Have fun !

1 Like

So grateful for being patient with me (sorry for the repetitive questions just needed some clarification for now).

I did try your code and now it's clearer that indeed it can have mixed format, I think I have to have a trial once I have the set up ready (have incomplete resources for now) to tell if the code works and will display the data.
Will surely update here about my progress and hopefully I can successfully display what I wanted. Again, thank you so much for your time and effort for explaining stuff, I've learned a lot :smiley:

So I tinkered with Wokwi myself and here it what I came up with : (LedSer485Disp V1 - Wokwi Arduino and ESP32 Simulator)
The program was enhanced to this (more explicit names, compatible Baudrates for better Logic Analyzer display) :

#include <SoftwareSerial.h>

#define DEBUG // Comment this out for release version to get rid of the debug print statements

#define LedDispSoftSerRxPin 11
#define LedDispSoftSerTxPin 12
#define LedDispSoftSerBaudRate 9600

SoftwareSerial LedDispSoftSer(LedDispSoftSerRxPin, LedDispSoftSerTxPin);

#define DE_PIN 10
#define RE_PIN 14

const int16_t interval = 1000; // interval (in milliseconds) between to measurements

// Define the data to send to the LED display
const uint8_t header   = 0x5A; // byte 0
const uint8_t address  = 0xFA; // byte 1
const uint8_t byte2    = 0x00;
const uint8_t byte3    = 0x00;
//    uint8_t byte4    = (data2 / 10000) % 100;
//    uint8_t byte5    = (data2 / 100) % 100;
//    uint8_t byte6    = data2 % 100;
//    uint8_t checksum = header + address + byte2 + byte3 + byte4 + byte5 + byte6;  // Keep only the lower 8 bits

#ifdef DEBUG
  void printPaddedUnsignedDecimal(int Value)
  {
    char buffer[6];
    sprintf(buffer, "%02u", Value);
    Serial.println(buffer);
  }
#endif

uint32_t GetData(int whateverNeeded){
  // do here what has to be done to get the data
  // you want to send to he LEDs
  // using the whateverNeeded parameter(s)
  int32_t data = 506070;
  return data;
}

void SendDataToLEDs(uint32_t data){
  // do here what has to be done to send the data
  
  uint8_t byte4 = (data / 10000) % 100;
  uint8_t byte5 = (data / 100) % 100;
  uint8_t byte6 = data % 100;
  uint8_t checksum = header + address + byte2 + byte3 + byte4 + byte5 + byte6;

  digitalWrite(DE_PIN, HIGH);
  digitalWrite(RE_PIN, HIGH);

  LedDispSoftSer.write(header);
  LedDispSoftSer.write(address);
  LedDispSoftSer.write(byte2);
  LedDispSoftSer.write(byte3);
  LedDispSoftSer.write(byte4);
  LedDispSoftSer.write(byte5);
  LedDispSoftSer.write(byte6);
  LedDispSoftSer.write(checksum);

  // Set the MAX485 to receive mode
  digitalWrite(DE_PIN, LOW);
  digitalWrite(RE_PIN, LOW);
  
  #ifdef DEBUG
    Serial.print("Header   : ");
    printPaddedUnsignedDecimal(header);

    Serial.print("address  : ");
    printPaddedUnsignedDecimal(address);

    Serial.print("byte2    : ");
    printPaddedUnsignedDecimal(byte2);

    Serial.print("byte3    : ");
    printPaddedUnsignedDecimal(byte3);

    Serial.print("byte4    : ");
    printPaddedUnsignedDecimal(byte4);

    Serial.print("byte5    : ");
    printPaddedUnsignedDecimal(byte5);

    Serial.print("byte6    : ");
    printPaddedUnsignedDecimal(byte6);

    Serial.print("checksum : ");
    printPaddedUnsignedDecimal(checksum);
    Serial.println();
  #endif
}

uint32_t data = 506070;

void setup() {
  Serial.begin(9600);

  // Set up the UART interface
  LedDispSoftSer.begin(LedDispSoftSerBaudRate);

  pinMode(DE_PIN, OUTPUT);
  pinMode(RE_PIN, OUTPUT);

  digitalWrite(DE_PIN, LOW);
  digitalWrite(RE_PIN, LOW);

  // Do the other settings here
  SendDataToLEDs(data);
}

void loop() {
  static uint64_t startTime = 0; // unsigned long "time keeper"
  if (millis() - startTime >= interval) {
    startTime += interval;

    // do relaxed things here
    // int whateverNeeded = 1; // represents the parameter(s) required by the GetData function
    // uint32_t data = GetData(whateverNeeded);
    // SendDataToLEDs(data);
  }
  
  if(Serial.available())     //if data available
  {
    String Message = Serial.readStringUntil('\n');
    Message.trim();              // remove any \r \n whitespace at the end of the String
    data = Message.toInt();
    SendDataToLEDs(data);
  }
}

and each time you stop the simulation, your browser will upload a "wokwi-logic.vcd" file to you computer. This is the record of the Logic Analyzer data. You'll need it for the next part.

But first, to know how to use any part (i.e. the Logic Analyzer) in Wokwi, click it and then click the question mark associated with it :
LogicAnalyzerQuestionMark
I say this because you will need to install PulseView for the next part, and its "simplified user manual" is in these help pages.

Here is a screen capture of PulseView display :

... that you can easily reproduce using this "LedSer485Disp V1.pvs" PulseView Session Setup file :

[D0]
name=LedTx
enabled=true
color=4279638298
conversion_type=0
conv_options=0

[D1]
name=DE_PIN
enabled=true
color=4287582722
conversion_type=0
conv_options=0

[D2]
name=ArduinoTx
enabled=true
color=4291559424
conversion_type=0
conv_options=0

[D3]
name=ArduinoRx
enabled=true
color=4294277376
conversion_type=0
conv_options=0

[decode_signal0]
name=LedHex
enabled=true
color=4281510450
conversion_type=0
conv_options=0
decoders=1
decoder0\id=uart
decoder0\visible=true
decoder0\options=1
decoder0\option0\name=baudrate
decoder0\option0\value=@ByteArray(\x80%\0\0\0\0\0\0)
decoder0\option0\type=x
decoder0\row0\visible=true
decoder0\row1\visible=true
decoder0\row2\visible=true
decoder0\row3\visible=true
decoder0\row4\visible=true
decoder0\row5\visible=true
decoder0\row6\visible=true
decoder0\row7\visible=true
decoder0\row8\visible=true
decoder0\row9\visible=true
decoder0\ann_class0\visible=true
decoder0\ann_class1\visible=true
decoder0\ann_class2\visible=true
decoder0\ann_class3\visible=true
decoder0\ann_class4\visible=true
decoder0\ann_class5\visible=true
decoder0\ann_class6\visible=true
decoder0\ann_class7\visible=true
decoder0\ann_class8\visible=true
decoder0\ann_class9\visible=true
decoder0\ann_class10\visible=true
decoder0\ann_class11\visible=true
decoder0\ann_class12\visible=true
decoder0\ann_class13\visible=true
decoder0\ann_class14\visible=true
decoder0\ann_class15\visible=true
decoder0\ann_class16\visible=true
decoder0\ann_class17\visible=true
channels=2
channel0\name=RX
channel0\initial_pin_state=2
channel1\name=TX
channel1\initial_pin_state=2
channel1\assigned_signal_name=Led
channel0\assigned_signal_name=LedTx

[D4]
name=RE_PIN
enabled=true
color=4293776384
conversion_type=0
conv_options=0

[decode_signal1]
name=LedDec
enabled=true
color=4286722730
conversion_type=0
conv_options=0
decoders=1
decoder0\id=uart
decoder0\visible=true
decoder0\options=2
decoder0\option0\name=baudrate
decoder0\option0\value=@ByteArray(\x80%\0\0\0\0\0\0)
decoder0\option0\type=x
decoder0\option1\name=format
decoder0\option1\value=@ByteArray(dec\0)
decoder0\option1\type=s
decoder0\row0\visible=true
decoder0\row1\visible=true
decoder0\row2\visible=true
decoder0\row3\visible=true
decoder0\row4\visible=true
decoder0\row5\visible=true
decoder0\row6\visible=true
decoder0\row7\visible=true
decoder0\row8\visible=true
decoder0\row9\visible=true
decoder0\ann_class0\visible=true
decoder0\ann_class1\visible=true
decoder0\ann_class2\visible=true
decoder0\ann_class3\visible=true
decoder0\ann_class4\visible=true
decoder0\ann_class5\visible=true
decoder0\ann_class6\visible=true
decoder0\ann_class7\visible=true
decoder0\ann_class8\visible=true
decoder0\ann_class9\visible=true
decoder0\ann_class10\visible=true
decoder0\ann_class11\visible=true
decoder0\ann_class12\visible=true
decoder0\ann_class13\visible=true
decoder0\ann_class14\visible=true
decoder0\ann_class15\visible=true
decoder0\ann_class16\visible=true
decoder0\ann_class17\visible=true
channels=2
channel0\name=RX
channel0\initial_pin_state=2
channel1\name=TX
channel1\initial_pin_state=2
channel1\assigned_signal_name=LedTx

[decode_signal2]
name=Arduino
enabled=true
color=4286753330
conversion_type=0
conv_options=0
decoders=1
decoder0\id=uart
decoder0\visible=true
decoder0\options=2
decoder0\option0\name=baudrate
decoder0\option0\value=@ByteArray(\x80%\0\0\0\0\0\0)
decoder0\option0\type=x
decoder0\option1\name=format
decoder0\row0\visible=true
decoder0\option1\value=@ByteArray(ascii\0)
decoder0\row1\visible=true
decoder0\option1\type=s
decoder0\row2\visible=true
decoder0\row3\visible=true
decoder0\row4\visible=true
decoder0\row5\visible=true
decoder0\row6\visible=true
decoder0\row7\visible=true
decoder0\row8\visible=true
decoder0\row9\visible=true
decoder0\ann_class0\visible=true
decoder0\ann_class1\visible=true
decoder0\ann_class2\visible=true
decoder0\ann_class3\visible=true
decoder0\ann_class4\visible=true
decoder0\ann_class5\visible=true
decoder0\ann_class6\visible=true
decoder0\ann_class7\visible=true
decoder0\ann_class8\visible=true
decoder0\ann_class9\visible=true
decoder0\ann_class10\visible=true
decoder0\ann_class11\visible=true
decoder0\ann_class12\visible=true
decoder0\ann_class13\visible=true
decoder0\ann_class14\visible=true
decoder0\ann_class15\visible=true
decoder0\ann_class16\visible=true
decoder0\ann_class17\visible=true
channels=2
channel0\name=RX
channel0\initial_pin_state=2
channel0\assigned_signal_name=ArduinoRx
channel1\name=TX
channel1\initial_pin_state=2
channel1\assigned_signal_name=ArduinoTx

[General]
decode_signals=3
views=1
meta_objs=0

[view0]
scale=2.048052186876791e-5
v_offset=2
splitter_state=@ByteArray(\0\0\0\xff\0\0\0\x1\0\0\0\x2\0\0\0\x64\0\0\x5]\x1\0\0\0\x1\x1\0\0\0\x1\0)
segment_display_mode=1
offset=22 serialization::archive 14 0 0 0 0 118355 1583068 42586483 38239017 22808928 3008420 -8 1 0 6
zero_offset=22 serialization::archive 14 0 0 0 0 0 0 0 0 0 0 0 0 0 6
D0\trace_height=26
D1\trace_height=26
D2\trace_height=26
D3\trace_height=26
D4\trace_height=26

clicking here :


You should be all set, enjoy

1 Like

Hi, apologies for the late reply. So I was not able to install Logic Analyzer and haven't tried to replicate your way. Thank you for the detailed answer, very much appreciated.

But to give you an update, I have edited my code as I have read that it's better to use Serial hardware and software serial could cause a lot of trouble. Also, I have edited the checksum as I have realized that it's the LSB of the sum of bytes 1 to 6 but in my previous code above I only calculated the sum. The pin that I am using is the TXD pin of the ESP which is GPIO1 as seen on my declaration and I did not set a receive mode because I won't be needing any feedback from the LED.

const int TX_PIN = 1; // GPIO1

void setup() {
  Serial.begin(115200, SERIAL_8N1);
  pinMode(TX_PIN, OUTPUT);
}
void loop() {
  uint8_t header = 0xFF; // byte 0
  //set different address
  uint8_t address1 = 0xFA; // byte 1
  uint8_t address2 = 0xFB;
  uint8_t address3 = 0xFC;
  uint8_t address4 = 0xFD;
  uint8_t byte2= 0x10;
  uint8_t byte3= 0x12;
  uint8_t byte4= 0x34;
  uint8_t byte5= 0x56;
  uint8_t byte6= 0x00;

  //try to calculate the checksum and do (bitwise AND operation)
  uint16_t sum1 = address1 + byte2+ byte3+ byte4+ byte5+ byte6;
  uint8_t checksum1 = (sum1 & 0xFF);

  uint16_t sum2 = address2 + byte2+ byte3+ byte4+ byte5+ byte6;
  uint8_t checksum2 = (sum2 & 0xFF);

  uint16_t sum3 = address3 + byte2+ byte3+ byte4+ byte5+ byte6;
  uint8_t checksum3 = (sum3 & 0xFF);

  uint16_t sum4 = address4 + byte2+ byte3+ byte4+ byte5+ byte6;
  uint8_t checksum4 = (sum4 & 0xFF);

  uint8_t data1[] = {header, address1, byte2+ byte3+ byte4+ byte5+ byte6, checksum1};
  uint8_t data2[] = {header, address2, byte2+ byte3+ byte4+ byte5+ byte6, checksum2};
  uint8_t data3[] = {header, address3, byte2+ byte3+ byte4+ byte5+ byte6, checksum3};
  uint8_t data4[] = {header, address4, byte2+ byte3+ byte4+ byte5+ byte6, checksum4};

  digitalWrite(TX_PIN, HIGH); // set the RS485 transceiver to transmit mode
  // Serial.print(checksum1, HEX);

  Serial.write(data1, sizeof(data1)); // send data1 over serial
  Serial.write(data2, sizeof(data2));
  Serial.write(data3, sizeof(data3)); 
  Serial.write(data4, sizeof(data4)); 

  delay(100); // wait for "n" second before sending the next data
}

However, the LED displays a garbage number and is constantly blinking, are there any options where I could debug or see what the LED receives? As I have used wokwi and this code sends the correct data format base on the ASCII result and I just convert it to hex to visualize the result.

  • Declaring const int TX_PIN = 1; // GPIO1 and pinMode(TX_PIN, OUTPUT); is not necessary as Serial.begin() does it already (Serial - Arduino Reference).

  • Serial.begin(115200, SERIAL_8N1); : 8N1 is the default value. No need to use it there except to make things explicit. Serial.begin(115200); does the same thing.

  • Are you sure the baudrate is 115200 ? In post 1, it was 9600 and that would explain the garbage displayed by the LED

  //try to calculate the checksum and do (bitwise AND operation)
  uint16_t sum1 = address1 + byte2+ byte3+ byte4+ byte5+ byte6;
  uint8_t checksum1 = (sum1 & 0xFF);

Using a 16 bit intermediate variable (sum1) to do the sum and then truncating it in an 8 bit variable is not necessary : uint8_t checksum1 = address1 + byte2 + byte3 + byte4 + byte5 + byte6; will do the same.

  • digitalWrite(TX_PIN, HIGH); // set the RS485 transceiver to transmit mode : No, TX_PIN is the pin the data is transmitted on, not a TX_EN_PIN (Transmit Enable Pin). Declare and use an other pin for that.
  • uint8_t data1[] = {header, address1, byte2+ byte3+ byte4+ byte5+ byte6, checksum1}; : copy-paste typo here ! Replace the "+" symbols with commas. This is a second reason why you get garbage. It should be uint8_t data1[] = {header, address1, byte2, byte3, byte4, byte5, byte6, checksum1};
  • The size of data1[] is 8 bytes. You send 4 in a row, that's 32 bytes. At 115200 baud, it takes roughly 100µs per byte, 3.2ms total. delay(100); is OK but did you make the calculation ? At 9600 baud, it would be 1.054ms per byte, 34ms total. The best way to avoid the math but make sure data is fully transmitted is to use serial.flush() (Serial.flush() - Arduino Reference) and then add a relaxing delay (as you send data over and over again). BTW, the comment about delay is erroneous : it's not "n" second but "n" milliseconds.

Hi again, thank you for breaking down your corrections and suggestions.

With your first bullet, I opened the link and as what I have understood it is for Arduino boards and I am using ESP WROOM-02 I might sound dumb but I assume it is applicable to my microcontroller too? So I will follow your suggestion and get rid of the declaration.

  • Serial.begin(115200, SERIAL_8N1); : 8N1 is the default value. No need to use it there except to make things explicit.
    -- Yes I have read also that 8N1 is by default but I just tried including it in the parameter as I am not also sure but I will also edit it out to be 'Serial.begin(115200)'
    And actually I am still trying the code for both the 115200 and 9600 just to see as I am not sure at what baud rate the LED works (I am still trying to look for and confirm the baud rate of LED side).

With the calculation of checksum, I just want to explain base on the articles and forums I have read. Yes I first got the sum, and as what I understood the LSB of the sum is the last 2 digit of the sum that is why I did the bitwise AND operation. Just to breakdown and visualize my implementation:

Hex | Dec
0xFF | 255
0xFA | 250
0x10 | 16
0x12 | 18
0x34 | 52
0x56 | 86
0x00 | 0
SUM:
1A6 | 422

The sum of data1 byte1 to byte6 is 422 (decimal) or 1A6 (hex) and the LSB of the sum is the A6 am I right? So if 1A6 is stored in the sum, I will be using bitwise AND operation with 0xFF to get just the A6.
Sorry for the long explanation I just wanted to let you know how I tried to get the checksum.

You mentioned uint8_t checksum1 = address1+----+ byte6 will do the same (I will try to see this result on wokwi first to understand it better). Thanks for pointing it out.

I thought this code will just like trigger the TXD pin of the ESP to enable its transmission? I am confused with your usage of TX_EN_PIN, do I need it in my code too?

And with the typo, thanks for pointing it out too and yes I am aware that the delay is in milliseconds. I will be reading the documentation on serial.flush to understand it more.

Also to add, the LED need to accept 32 bytes of data (for 4 layers of LED is connected) and each LED receives 8 byte where data1 be displayed on LED1, so on and so forth.

As you didn't post any diagram nor any list of the hardware you intend to use, we can only make assumptions. In Post #1, I can read :

    const int DE_PIN = 10; 
    const int RE_PIN = 14;  

What is the purpose of these 2 pins ? The way you use them and without explanations, I assume you need receive and transmit enable pins. If you don't, don't digitalWrite(TX_PIN, HIGH) either, Serial.write() will "do the right stuff" for you. Simply remove any TX_PIN related code.

I'm sorry for not making it clear, but I will be only using the TXD pin of the ESP

I am only sending data and I don't need to receive feedback from transceiver, that is why I will be using just the TXD pin for transmitting or sending the data to the transceiver.

void setup() {
  Serial.begin(115200);
}
void loop() {
  uint8_t header = 0xFF; // byte 0
  //set different address
  uint8_t address1 = 0xFA; // byte 1
  uint8_t address2 = 0xFB;
  uint8_t address3 = 0xFC;
  uint8_t address4 = 0xFD;
  uint8_t byte2= 0x10;
  uint8_t byte3= 0x12;
  uint8_t byte4= 0x34;
  uint8_t byte5= 0x56;
  uint8_t byte6= 0x00;

  //try to calculate the checksum and do (bitwise AND operation)
  uint16_t sum1 = address1 + byte2+ byte3+ byte4+ byte5+ byte6;
  uint8_t checksum1 = (sum1 & 0xFF);

  uint16_t sum2 = address2 + byte2+ byte3+ byte4+ byte5+ byte6;
  uint8_t checksum2 = (sum2 & 0xFF);

  uint16_t sum3 = address3 + byte2+ byte3+ byte4+ byte5+ byte6;
  uint8_t checksum3 = (sum3 & 0xFF);

  uint16_t sum4 = address4 + byte2+ byte3+ byte4+ byte5+ byte6;
  uint8_t checksum4 = (sum4 & 0xFF);

  uint8_t data1[] = {header, address1, byte2, byte3, byte4, byte5, byte6, checksum1};
  uint8_t data2[] = {header, address2, byte2, byte3, byte4, byte5, byte6, checksum2};
  uint8_t data3[] = {header, address3, byte2, byte3, byte4, byte5, byte6, checksum3};
  uint8_t data4[] = {header, address4, byte2, byte3, byte4, byte5, byte6, checksum4};

  Serial.write(data1, sizeof(data1)); // send data1 over serial
  Serial.write(data2, sizeof(data2));
  Serial.write(data3, sizeof(data3)); 
  Serial.write(data4, sizeof(data4)); 

  delay(100); // wait for "n" milliseconds before sending the next data
}

So in this code I removed the declaration of TX pin, the pinMode, and also the digitalWrite.

Did you try it ? What is the result ?

What about the rest of my comments on post #33 ? (Apart from the sum1 thing if you prefer this way)

Thinking it over, are you sure you want to use serial to talk to your LEDs ? It is the link you already use for the programmer/debugger AFAIK.

I can't try it out yet as I don't have the ckts with me right now to test. But If I exclude the line pinMode(TX_PIN, OUTPUT); from the code, the RS485 protocol requires the use of a direction control pin to set the transceiver to transmit mode. If I don't set the direction control pin to output mode using pinMode(), the transceiver may not be able to properly transmit the data over the RS485 bus. I have read that while Serial.begin() does set up the TX pin for communication with the UART interface, it does not necessarily set it up for use with the RS485 protocol.

  • This means that there is still a need for pinMode right?

[quote="Etienne_74, post:33, topic:1116513"]
The size of data1[] is 8 bytes. You send 4 in a row, that's 32 bytes. At 115200 baud, it takes roughly 100µs per byte, 3.2ms total. delay(100); is OK but did you make the calculation ?

-- I am trying to read about this I did not make any calculation on this.

With your last sentence, I can't really figure out what you mean by the link you already use for the programmer/debugger. But yes I think I have to use serial as the LEDs is using RS485 serial communication.

(Apart from the sum1 thing if you prefer this way)
If I used this code `

uint8_t checksum1 = address1 + byte2 + byte3 + byte4 + byte5 + byte6;

and the sum exceeds 255, won't it produce incorrect checksum due to overflow?

While if I use this

uint16_t sum1 = address1 + byte2+ byte3+ byte4+ byte5+ byte6;
uint8_t checksum1 = (sum1 & 0xFF);

this will mask out the LSB of the sum.

Though I tried to see the result of the code using the two and seem they have the same output.

serial is the link used to communicate between the Arduino and your computer. It is used each time you upload a sketch and use println to debug it.

Of course you need a serial port for the RS485 serial communication with the LEDs but using this one (serial) is tricky.

From what I can read on the ESP-WROOM-02 datasheet (i've never used it), you can use a second hardware port on pin GPIO2/UART1_TXD.

I suppose you can declare it this way
Serial2.begin(9600, SERIAL_8N1);

If the compiler complains about "Serial2", follow its suggestion (which should be Serial1).

1 Like

No, the checksum will be correct, verify with the simulator using an ESP32.
And don't forget to read post #40.

1 Like

pinMode doesn't set the direction of the transmission, the RS485 transceiver in your schematic does that but you went the hardware way, connecting both DE and /RE to 3.3V. To be able to control the direction of the transmission (which you don't AFAIK) you need to connect those pins to an output on the Arduino.

pinMode(pin, mode) only tells the Arduino if the pin is an output or an input (pinMode() - Arduino Reference). Nothing to do with the direction of the transmission.

Thanks, will try to run this code and will let you know if it works.

Yeah I did see that using this gives the same result if I used bitwise and operator. I'll just use this one instead.

#include <HardwareSerial.h>
#define TXPIN 2

HardwareSerial RS485(TXPIN);

void setup() {
  Serial.begin(115200);
  RS485.begin(9600, SERIAL_8N1);
}

void loop() {
  uint8_t header = 0xFF; // byte 0
  //set different address
  uint8_t address1 = 0xFA; // byte 1
  uint8_t address2 = 0xFB;
  uint8_t address3 = 0xFC;
  uint8_t address4 = 0xFD;
  uint8_t byte2= 0x10;
  uint8_t byte3= 0x12;
  uint8_t byte4= 0x34;
  uint8_t byte5= 0x56;
  uint8_t byte6= 0x00;

  uint8_t checksum1= address1 + byte2 + byte3 + byte4 + byte5 + byte6;
  uint8_t checksum2 = address2 + byte2 + byte3 + byte4 + byte5 + byte6;
  uint8_t checksum3 = address3 + byte2 + byte3 + byte4 + byte5 + byte6;
  uint8_t checksum4 = address4 + byte2 + byte3 + byte4 + byte5 + byte6;
  
  uint8_t data1[] = {header, address1, byte2, byte3, byte4, byte5, byte6, checksum1};
  uint8_t data2[] = {header, address2, byte2, byte3, byte4, byte5, byte6, checksum2};
  uint8_t data3[] = {header, address3, byte2, byte3, byte4, byte5, byte6, checksum3};
  uint8_t data4[] = {header, address4, byte2, byte3, byte4, byte5, byte6, checksum4};

  RS485.write(data1, sizeof(data1));
  RS485.flush(); // Wait for all bytes to be sent

  RS485.write(data2, sizeof(data2));
  RS485.flush(); // Wait for all bytes to be sent

  RS485.write(data3, sizeof(data3));
  RS485.flush(); // Wait for all bytes to be sent

  RS485.write(data4, sizeof(data4));
  RS485.flush(); // Wait for all bytes to be sent

  delay(1000); // wait for 1 second before sending the next batch
}

Is this correct? I wanted to try first doing it like sending 1 data at a time but I also wanted to send 32bytes in one go just to check if maybe it will work that way.

Will this second option of code might work too?

void setup() {
  Serial.begin(115200);
  Serial1.begin(9600, SERIAL_8N1, GPIO_NUM_2); // using UART1 on pin 2 only
}

  uint8_t checksum1= address1 + byte2 + byte3 + byte4 + byte5 + byte6;
  uint8_t checksum2 = address2 + byte2 + byte3 + byte4 + byte5 + byte6;
  uint8_t checksum3 = address3 + byte2 + byte3 + byte4 + byte5 + byte6;
  uint8_t checksum4 = address4 + byte2 + byte3 + byte4 + byte5 + byte6;

void loop() {
  uint8_t data[32] = {
    0xFF, 0xFA, 0x10, 0x12, 0x34, 0x56, 0x00, checksum1,
    0xFF, 0xFB, 0x10, 0x12, 0x34, 0x56, 0x00, checksum2,
    0xFF, 0xFC, 0x10, 0x12, 0x34, 0x56, 0x00, checksum3,
    0xFF, 0xFD, 0x10, 0x12, 0x34, 0x56, 0x00, checksum4,
  };
  
  Serial1.write(data, sizeof(data));
  Serial1.flush(); // wait until all data has been sent
  delay(100); // wait for 100 millisecond before sending the next batch
}

I will be using Serial1 because I have read it from here Reference — ESP8266 Arduino Core 3.1.2 documentation

Hi, so I have tried to read the output of my ESP using the Serial Terminal "PuTTY" and I have seen this output: ÿú4Vÿû4V§ÿü4V¨ÿý4V©ÿú4V which in hex is FF FA 34 56 FF FB 34 56 A7 FF FC 34 56 A8 FF FD 34 56 A9 FF FA 34 56

  • From this, I can see that the 0x10 (byte2) and 0x12 (byte3) is not read/seen in the terminal.

put

As compared to the wokwi result, ÿú4V¦ÿû4V§ÿü4V¨ÿý4V© (spaces here are special characters) which if converted to hex is FF FA 10 12 34 56 A6 FF FB 10 12 34 56 A7 FF FC 10 12 34 56 A8 FF FD 10 12 34 56 A9
wok

Right now, I am stuck in this part as I don't know why is the byte 2 and byte 3 not sent as seen in the terminal of putty.

Maybe try to send each byte individually and, if missing again, try to send the missing bytes only and see what happens...

Can you log the received data to make sure they are not received. Maybe they are received but not displayed for whatever reason...

Or simply connect to your LEDs are watch the result...