Parsing The Data from Rasberry Pi to Arduino for controlling 8 Relay

Question:

My project Bidirectional Communicate between Arduino and Rasberry Pi for implementing many AC Applications, sofar the read from Arduino to Rasberry Pi has been solved

The only Problem now is parsing the data receving from the Rasberry Pi to the Arduino Mega, i have follow others guidance but it seem to be a little stuck, currently i had not used the char Parsing method like Robin2 Example 5 from [ Parsing Serial Data and separating it into variables] But using the String lib SubString method to count the byte for getting the data

In Rasberry Pi i have the code like the below:

# yes i could remove the semicolons, i did that for various method testing
Res = re1 +','+ re2 +','+ re3 +','+ re4 +','+ re5 +','+ re6 +','+ re7 +','+ re8
ser.write(b"Resn")

it will output a series like re1onn,re2off,re3on….re8off

Which i have the following code from the Arduino side

// all the variable has been defined above
 if (Serial.available()) 
  {
    command = Serial.readStringUntil('n');
    command.trim();
    if (command.substring(0,8) == "re1onn") {
      digitalWrite(re1, LOW);
    } else if (command.substring(0,8) == "re1off") {
      digitalWrite(re1, HIGH);
    }
    if (command.substring(10,18) == "re2on") {
      digitalWrite(re2, LOW);
    } else if (command.substring(10,18) == "re2off") {
      digitalWrite(re2, HIGH);
    }

Can u guys check my method?

Merry Xmas everyone <3

Asked By: Kyle-Toof

||

Answers:

You may send strings like 11111111, 01101111, … etc. where 1 means on and 0 means off and their position represents the relay number.

Answered By: Muhammadyusuf

Thank you for your help, i have solved the problem. Below are the Bug&Debug for this article

on Thonny (RasbianOS Python IDE) i have make some mistake on the ser.write

res = re1 + re2
ser.write(b"resn")

I have print out "resn" instead of "10" so i have corrected it

res = re1 + re2 +"n"
ser.write(res.encode())

Then on the ArduinoIDE below are the debug code for it to receive the Serial command from Rasperry Pi

/*Variables define*/
String command;
String command1;
String command2;
/*Serial receive and on/off function*/
 if (Serial.available()) {
    command = Serial.readStringUntil('n');
    command.trim();
    // just seperate those 2 String command and we good to go

    // get String at position 0 and 1, which are re1 & re2 from Ras
    command1 = command.charAt(0);
    command2 = command.charAt(1);
    
    // compare the String which we get from above
     if (command1.equals("0")) {
      digitalWrite(Re1, HIGH);
    }
    else if (command1.equals("1")) 
    {
      digitalWrite(Re1, LOW);
    }
    if (command2.equals("0")) {
      digitalWrite(Re2, HIGH);
    }
    else if (command2.equals("1")) 
    {
      digitalWrite(Re2, LOW);
    }

Merry Chirstmas everyone <3

Answered By: Kyle-Toof