Arduino reset issue on serial connection with Raspberry PI

I have written a sketch to control 2 relays and take input from a flowmeter. The code is initiated using serial communication with Raspberry PI 4B.
When I upload the sketch with raspberry pi connected with serial usb cable, for the first code run in pi, it resets arduino for the first time and starts running normally from second run onwards. But in case the power is shut for both devices and restarted, when i trigger the code run in raspberry pi, it will reset arduino for first, second, third...and every run, until I reupload the code on sketch again.
Can anyone suggest what am I missing here? sharing pi/arduino codes for reference.

Sketch:

#include <stdlib.h>
#include <string.h>
								 
#define relay_valve	7 // valve relay
#define relay_pump	8 // pump relay  

float liquidFlow;
float mltodispense;
String dispensestatus;

void setup() 
{
    pinMode(relay_valve, OUTPUT);
    pinMode(relay_pump, OUTPUT);
    
    liquidFlow = 0;
    Serial.begin(9600);  //baudrate
    Serial.flush();
    attachInterrupt(1, pulse, RISING);  //DIGITAL Pin 3: Interrupt 0
    dispensestatus = "not_dispensing";
}

void loop() 
{  
  if (dispensestatus == "not_dispensing")
  {
    readserialinput(); //check if pi has asked
  } else if (dispensestatus == "to_dispense")
  {
    
    digitalWrite(relay_valve, 1); // Valve Relay On
    delay(500);
    digitalWrite(relay_pump, 1); // Pump Relay On
    
    delay(50);
    
    liquidFlow = 0;
    delay(5000);

    digitalWrite(relay_pump, 0); // pump Relay off
    delay(500);
    digitalWrite(relay_valve, 0); // Valve Relay off
    delay(500);
}

void pulse()   //measure the quantity of square wave
{
  liquidFlow += 1.0 / 150.0; // 150 pulses=1L
}

void readserialinput()   //measure the quantity of square wave
{ 
  if (Serial.available() > 0) {
    String data = Serial.readStringUntil('\n');
    char line[256];
    char *subString;
    char *stringtomatch;
    char *secondPart;
    
    strcpy(line, data.c_str());
    subString = strtok(line, "_");
    stringtomatch = "dispensenow";
    Serial.println(data);
    if (strcmp(subString, stringtomatch) == 0)
    {
      mltodispense = atof(strtok(NULL, "_"));
      dispensestatus = "to_dispense";
      liquidFlow = 0;
    } else
    {
      Serial.print("not_dispensing");        
    }
  }
}

Raspberry Pi code:

import serial
import time
import sys

float_oil_to_dispense_ml = "dispensenow_200"

if __name__ == '__main__':
    ser = serial.Serial('/dev/ttyACM0', 9600, timeout=1)
    time.sleep(2)
    ser.reset_input_buffer()
    ser.reset_output_buffer()
    
    
    ser.write(str.encode(float_oil_to_dispense_ml + '\n'))
    
    while True:
        line = ser.readline().decode('utf-8')
        if line != "":
            print(line)

for many arduinos, opening the Serial line resets the Arduino.

see Arduino Playground - DisablingAutoResetOnSerialConnection

but reseting the arduino can be a good thing possibly. At least your PC code knows the device is starting "fresh". just give it time to boot before sending commands

Thanks and yes...I probably don't want to avoid it getting reset. But the confusion is, if I upload the sketch via pi, it only resets the first time and then it doesn't after the first run. But post rebooting both devices, it resets for every run via pi.

Probably the port does not get closed

Okay. Is there a way to keep the port opened post the first run pls?

The OS and apps manage the port.

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