Best communication for arduino -pi?

Hi,
I have an arduino mega and raspberry pi 4.

I am retrieving information from my arduino with a serial communication reading it throught python, but I am having problems with it because sometimes I get part of the buffer memory for example: If my arduino print "Hello 213", I retrieve "Hello 23"

I was thinking to use a better way to communicate each device like a wifi module and MQTT, but I'm seeing it a little unnecessary because both devices are soo close, any suggest are welcome.

Thanks!

I'd be inclined to try to get serial working. As described (i.e. dropping characters in the middle of transmission) I doubt that the issue is on the Arduino - it blocks when its buffer is full.

So you might have a wiring issue, but I'd expect corrupt characters then, not just dropping them.

Based on what you observe, I'd take a look at your Python code. Maybe make a simpler version that just echos what it reads to the terminal.

Do you get "Hello 23" or "Hello 21" (3 missing)?

Your code (Arduino or Pi) might very well be the culprit.

Program uploads to your Mega are over serial, are they reliable ?

Bear in mind , the pi runs at 3.3v - you need to level shift the transmitt signal from the Arduino

Código Python:

#!/usr/bin/python

# Importamos la libreira de PySerial
import serial

# Abrimos el puerto del arduino a 115200
PuertoSerie = serial.Serial('/dev/ttyACM0', 115200)
# Creamos un buble sin fin
while True:
 # leemos hasta que encontarmos el final de linea
 sArduino = PuertoSerie.readline()
 # Mostramos el valor leido y eliminamos el salto de linea del final
 print sArduino.rstrip('\n')

Código Arduino:

#include <OneWire.h>
#include <DallasTemperature.h>

// All DS18B20 Sensors are connected to pin 8 on the Arduino
#define ONE_WIRE_BUS 30

// Creating a oneWire instance(object)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire object reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);

int numberOfDevices; //To store number of sensor connected

DeviceAddress tempDeviceAddress; // Variable to store a single sensor address

/*----------------------------------------------------------------*/

void setup(void) {
 
  Serial.begin(9600);
  sensors.begin();
  
  // Get the number of sensors connected to the the wire( digital pin 4)
  numberOfDevices = sensors.getDeviceCount();
  

  // Loop through each sensor and print out address

}//End of void setup()

/*----------------------------------------------------------------*/

void loop(void) { 
  
  sensors.requestTemperatures(); // Send the command to get temperatures from all sensors.
  
  // Loop through each device, print out temperature one by one
  for(int i=0; i<numberOfDevices; i++) {
    
    // Search the wire for address and store the address in tempDeviceAddress
    if(sensors.getAddress(tempDeviceAddress, i)){

      // Print the temperature
      float tempC = sensors.getTempC(tempDeviceAddress); //Temperature in degree celsius
      Serial.print("T");
      Serial.print(i);
      Serial.print(": ");
      Serial.println(tempC);
    }   
    
  }// End of for loop
  
  delay(5000);
}

/*----------------------------------------------------------------*/

// function to print a sensor address
void printAddress(DeviceAddress deviceAddress) {
  
  for (uint8_t i = 0; i < 8; i++) {
    
    if (deviceAddress[i] < 16) 
      Serial.print("0");
      Serial.print(deviceAddress[i], HEX);
      
  }//End of for loop
  
}

II retrieve “T1: #temp” OR "T0 #temp: " from arduino with python and node red and with that I put it into a graph node, the problem is that sometimes, I get “T0T1: 20.12”, this problem have
1 in 10 chance to happen.

Regards

I already change at the same baudrate, that is not the problem

Are you sending a sentence or just the data.

Just the data “hello 1234”

As a sentence “<hello 1234>”

The difference being is that a < alerts the RPi that new info is in coming begin collecting info and the > means the end of new info, send the received info to the parser and get ready for more new info.

And use a level shifter.

If you use a sentence and you receive a “h” but did not get a < to start the sentence then ignore data. If received a > then clear parse buffer.

Idahowalker:
Are you sending a sentence or just the data.

Just the data “hello 1234”

As a sentence “<hello 1234>”

The difference being is that a < alerts the RPi that new info is in coming begin collecting info and the > means the end of new info, send the received info to the parser and get ready for more new info.

And use a level shifter.

If you use a sentence and you receive a “h” but did not get a < to start the sentence then ignore data. If received a > then clear parse buffer.

Hi Idahowalker, that is for post-receiving processing purpose? I’m using: if "T1: " is not complete, then ignore the rest as an validation process, but I don’t know if that is what u want to mean.

Serial.write() improve my data connection?

Regards and thanks you all for answer.

This Simple Python - Arduino demo may be of interest.

...R

Robin2:
This Simple Python - Arduino demo may be of interest.

...R

Hi Robin, I will take a look, thanks!