Arduino <--> Raspberry PiCM4 Serial communication very slow

Hi together

I want to use a raspberry Pi to communicate to my ESP8266 D1 mini over Serial. Currently i have a steppermotor attached to my ESP8266. For test purposes, I just write a PWM frequency from my Raspberry Pi to the ESP8266 which is then used as "steps" for my steppermotor.

Everything works, but unfortunately if I write a new value to the ESP8266, the frequency changes roughly 1 second after my input. I think it has something to do with the buffer, but I am not sure if the problem is on the raspberry-side or on the ESP8266-side.

As a feedback, I just print the received values back to my raspberry over serial as a confirmation that the values change. If I write a value value of roughly less than 99999999, the feedback value is identical. Unfortunately, if I send for example "9582665835" as a value, I receive "992731243" as a feedback. It just happens if the value is very big and I think I made some mistakes in converting the value properly. Maybe someone knows also a solution for that.

Code on Raspberry-Side:

import serial
import time

SerialPort = serial.Serial('/dev/ttyUSB0', 500000, timeout=1)
cmd = 5000
cmd = str(cmd).encode()
SerialPort.write(cmd)

time.sleep(0.01)

DataReceived = SerialPort.readline().decode().strip()
print(DataReceived)

Code on ESP8266-side

#include <SPI.h>
#include <HighPowerStepperDriver.h>
HighPowerStepperDriver Driver;

const uint8_t CSPin = 15;
const uint8_t PWMPIN = 0;

int receivedRawValue = 0;

void setup()
{
  Serial.begin(500000);
  SPI.begin();
  Driver.setChipSelectPin(CSPin);
  pinMode(PWMPIN, OUTPUT);
  analogWrite(PWMPIN, 128);

  delay(1);

  Driver.resetSettings();
  Driver.clearStatus();

  Driver.setCurrentMilliamps36v4(250);

  Driver.setStepMode(HPSDStepMode::MicroStep8);

  Driver.enableDriver();
  Driver.setDirection(0);
}

void loop()
{
if (Serial.available() > 0) {
    receivedRawValue = Serial.parseInt(); // Lese die empfangene Zahl
   analogWriteFreq(receivedRawValue);
   Serial.println(receivedRawValue);
}
}


//https://arduino.esp8266.com/stable/package_esp8266com_index.json  

Why are you doing this with only ONE byte being received?

I moved your topic to an appropriate forum category @manuelambaum.

In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

Hi @manuelambaum ,

Welcome to the forum..
sounds like parseInt() is timing out waiting for the newline \n..
not sure of the sender language..
can you do something like??

SerialPort.write('\n')

just after or with the cmd, cmd+'\n' ??

good luck.. ~q

Integer overflow

9582665835 dec > 2 3B2B E06B hex
 992731243 dec >   3B2B E06B hex

An int on a ESP8266 is 32 bits. From above, 9582665835 does not fit in a 32 bit int so you loose the "2" resulting in 992731243.

You will need to change the type in the Arduino code to an int64_t and modify the parsing (I do not think that parseInt can handle 64 bit).

You can use SerialTransfer.h and pySerialTransfer to communicate between Python and your Arduino fast and easy.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.