Variable data serial communication

What I want is to send variables from Raspberry. For example. I want from Python
Y = 20

And in Arduino place
delay(Y)

(It's just an example for something I want to do, I want to input more variables from Raspberry to Arduino, the numbers will never be constant).

The serial input basics tutorial may be of interest.

Just be aware you are NOT sending integers with your Python code, but are assuming they are integers in your Arduino code. That won't work.

You need to ensure consistency between the two devices. The easiest way is use characters on both ends and then convert them to byte on the Arduino. Also look at the length of the data. You are sending and receiving only a single byte, but integers on the Arduino are two bytes long.

Paul

If you want to use serial to do multiple things you should think about implementing a structured serial message format that allows you to send all the forms of commands and data you might need.

Such structures often include "fields" in a serial stream like:

  • framing bytes/characters (indicates start and end of a message)
  • command
  • number of data bytes in message
  • data (if any)
  • a checksum term to check the message and framing is valid

For example:

Example serial structure:
SOH         ; 0x01 start of header (beginning of data frame)
CMD         ; command byte
NDATA       ; number of data bytes to follow (if no data, st to 0x00)
DATA0       ; data byte 0
.
.
.
DATAN       ; data byte N
CS          ; message checksum (LSB of sum of SOH to CS inclusive should be 0x00)
EOT         ; 0x04 end of transmission

Examples:
01 26 02 00 01 D6 04
|  |  |  |  |  |  +----------   end of transmission marker
|  |  |  |  |  +-------------   checksum term: 01+26+02+00+01+D6 == 0x0100; & 0xff == 00
|  |  |  |  +----------------   state of LED; 00=off, 01=on
|  |  |  +-------------------   LED number
|  |  +----------------------   number of data bytes to follow
|  +-------------------------   LED control command 0x26
+----------------------------   start of transmission marker

01 13 04 E0 93 04 00 71 04
|  |  |  |  |  |  |  |  +----   end of transmission marker
|  |  |  |  |  |  |  +-------   checksum term 01+13+04+E0+93+04+00+71 == 0x0200; & 0xff == 00
|  |  |  +--+--+--+----------   4-byte uint32_t delay(x) value 0x0493E0 (300000) in little-endian format
|  |  +----------------------   # of bytes in data field; 4 == sizeof( uint32_t)
|  +-------------------------   set delay command 0x13
+----------------------------   start of transmission marker

.
.
.

Paul_KD7HB:
Just be aware you are NOT sending integers with your Python code

Errr . . ?

groundFungus:
The serial input basics tutorial may be of interest.

And there is even an accompanying python demo

That does not make sense.

int pin 12;
int pin 13;

Are not a legal statements.

I already read that post that shows the basics and I don't see that it has what I'm looking for.

Then you need to read it again.

Hi,
Have you googled

communicating between arduino and raspberry pi

Tom... :slight_smile:

Here is an example of how to change variables from serial monitor. Make sure that the serial baud rate is 115200 and line endings are Both NL&CR. Uses the example #2 Receive with an end-marker from the serial input basics and the strtok() function (example #5 of the tutorial). All the information for this example is in the tutorial.

If this is not what you want, you will have to better explain your requirements.

const byte numChars = 12;
char receivedChars[numChars];   // an array to store the received data

boolean newData = false;

int pin12;
int pin13;

void setup()
{
   Serial.begin(115200);
   Serial.println("<Arduino is ready>");
   Serial.println("make sure line endings set to Both NL & CR");
   Serial.println("enter desired pin numbers separated by a comma");
   Serial.println("for example:  12,13\n\n");
}

void loop()
{
   recvWithEndMarker();
   if (newData)
   {
      //showNewData();
      parseData();
   }
}

void recvWithEndMarker()
{
   static byte ndx = 0;
   char endMarker = '\n';
   char rc;

   while (Serial.available() > 0 && newData == false)
   {
      rc = Serial.read();

      if (rc != endMarker)
      {
         receivedChars[ndx] = rc;
         ndx++;
         if (ndx >= numChars)
         {
            ndx = numChars - 1;
         }
      }
      else
      {
         receivedChars[ndx] = '\0'; // terminate the string
         ndx = 0;
         newData = true;
      }
   }
}

void showNewData()
{
   Serial.print("This just in ... ");
   Serial.println(receivedChars);
}

void parseData()
{
   char *strings[3];  
   char *ptr = NULL;
   byte index = 0;
   ptr = strtok(receivedChars, ",");
   while (ptr != NULL)
   {
      strings[index] = ptr;
      index++;
      ptr = strtok(NULL, ",");
   }
   pin12 = atoi(strings[0]);
   pin13 = atoi(strings[1]);
   Serial.print("pin12 = ");
   Serial.print(pin12);
   Serial.print("    pin13 = ");
   Serial.println(pin13);
   newData = false;
}

Why did you remove that post?

sterretje:
And there is even an accompanying python demo

I wrote a newer version Simple Python - Arduino demo that is more closely aligned with Serial Input Basics

...R