Search code examples
arraysstringarduinoraspberry-pi3

Serial communication from raspi to multiple arduino's


I have a possible idiotic question, but I simple can't seem to find a good solution.

What I want: 2 arduino's collecting analog and digital signals, they save it as a string (each one separately) and when asked they send the data via Serial to the Raspberry Pi. The raspberry pi sends a signal (and clock data) to be able to match everything togheter (next stage).

Question: How to write a descent script to: 1 collect all data and save it (as a string or should I go to arrays?) in the arduino, secondly how to collect the data from the Raspi.

What I got: Arduino :

char dataStr[50];
void setup() {
    Serial.begin(9600);
    while (!Serial) {   
        ; 
    } 
void loop(){
    //read sensors
    //attach value to datastring with strcat(dataStr, Sensorvalue);
    dataStr= "1, 2, 3, 4, 5, 6, 7, 8, A0-TRPI-Empty, S;" //this part already works code A0 is arduino 1 and code A01 is arduino 2
    if (Serial.available() > 0) {
        String data = Serial.readStringUntil('\n');
        Serial.print( data); //clock signal coming from raspi
        Serial.println(dataStr); 
        dataStr[0] = 0;
    }

Raspberry Pi:

#!/usr/bin/env python3
import serial
import time
from datetime import datetime
now = datetime.now()

if __name__ == '__main__':
ser00 = serial.Serial('/dev/ttyACM0', 9600, timeout=10)
    ser00.reset_input_buffer()
    
    ser01 = serial.Serial('/dev/ttyACM1', 9600, timeout=10)
    ser01.reset_input_buffer()

    while True:
        
        line = ser00.readline().decode('utf-8').rstrip()
        if (line != ""):           
          d1 = now.strftime("%d/%m/%Y %H:%M:%S ")
          ser00.write(d1.encode('utf-8'))
          print(line)
            
        line = ser01.readline().decode('utf-8').rstrip()
        if (line != ""):
          d1 = now.strftime("%d/%m/%Y %H:%M:%S ")
          ser00.write(d1.encode('utf-8'))
          print(line)

As you see, I now manage to communicate, send a time to the arduino and receive time + the datastring. But those 2 arduino's would not work at the same speed, or would not collect data at the same speed (f.e. temperature of a closed enviroment is measured every minute, but the acceleration is measured as fast as possible to have the max values). Some background info, I'm trying to monitor my trailer and animals in it. And get a nice printout on how the drive was.

Right now I get this response:

> 22/02/2022 10:46:14 1, 2, 3, 4, 5, 6, 7, 8, A0-TRPI-Empty, S;
> 22/02/2022 10:46:14 1, 2, 3, 4, 5, 6, 7, 8, A01-TRPI-Empty, S;
> 22/02/2022 10:46:14 1, 2, 3, 4, 5, 6, 7, 8, A0-TRPI-Empty, S;
> 22/02/2022 10:46:14 1, 2, 3, 4, 5, 6, 7, 8, A01-TRPI-Empty, S;
> 22/02/2022 10:46:14 1, 2, 3, 4, 5, 6, 7, 8, A0-TRPI-Empty, S;
> 22/02/2022 10:46:14 1, 2, 3, 4, 5, 6, 7, 8, A01-TRPI-Empty, S;
> 22/02/2022 10:46:14 1, 2, 3, 4, 5, 6, 7, 8, A0-TRPI-Empty, S;
> 22/02/2022 10:46:14 1, 2, 3, 4, 5, 6, 7, 8, A01-TRPI-Empty, S;

I'm for sure making 100 rookie mistakes, I'm new to python. But If you menage to look past that easy rookie code, could you help me in understanding how I should create the wanted communication. A couple of directions I should look into and any obvious mistake that I missed are more then welcome!

Thanks in advance!!

Some extra info; I will eventually need more then 2 arduino's because I will add multiple camera's. I am using serial, because I am using wires anyway (I'm not gonna manage multiple batteries) so I thought this was the cleanest solution (plus lenght is +-5m max so I couldn't use I2C without extra hardware). Arduino is connected to raspberry via USB, currently using Arduino Uno but will switch to something smaller when finished. I am using the Raspberry Pi 3B+.


Solution

  • You can use the pyMultiSerial module in python3.

    pip3 install pyMultiSerial
    

    Check out the Examples provided in Examples folder here: https://github.com/SunitRaut/pyMultiSerial/tree/main/Examples

    This module will make it very easy for you to implement your desired application.