pythoncarduinoraspberry-piserial-communication

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


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"Res\n")

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


Solution

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