Python communication problem

I'm working on a project where the computer will be communicating with 5-6 Arduino Nanos for data acquisition.

Overview:
I send and "x" to the Arduino and the Arduino samples A0-A7 and sends back the results of all ADC as a string. The computer then loads the results to a file and acts according to the results.

Problem:
When this is powered up, Python sends the 'x' and waits forever. The Arduinos never respond. BUT..... If I open the Arduino IDE and the terminal screen in it and send an 'x', each Arduino, they work perfectly. I close the Arduino IDE and run the Python program and it works perfectly as long as it's powered up. As soon as it's powered down and back up, Python will wait forever for a response.

I've tried using only one Nano and get the same results.

What is the Arduino IDE doing that the Python program isn't when it's first connected? I want this to run at boot up for data logging.

Welcome to the forum

Please post your Arduino code, using code tags when you do

The serial monitor waits for a time after opening the serial connection so the Arduino can be reset and load a fresh copy of the program. Your Python program probably does not do that wait.

Something to study and get ideas from
Robin's examples

JML's (more recent) example

Those look like some good examples to try. Thank you!

Here is the Python I'm using. It's still a work in progress:

#!/usr/bin/python3

'''
2024-04-28 Greenhouse controller
'''

print("\n2024-04-28 Greenhouse controller\n")


import datetime
from datetime import time
from datetime import timezone
import time
from datetime import timedelta
import serial
from pathlib import Path
from sys import platform as _platform
import sys
import os


print(_platform)
print()

# Addresses of the Arduino boards
#gh = ['/dev/ttyUSB0']  # Test
gh = ['/dev/ttyUSB0','/dev/ttyUSB1']

#path = str("/media/rob/DATA FILES/")
path = str("/media/rob/SHOPFILES/")  # Test


def wxserial(sergh):
    #print("wxserial module")  # Test
    wxdata = sergh.readline().decode('utf-8')
    sergh.flush()
    sergh.close()
    return (wxdata)


def timeSetup():
    y = []

    # Get current times
    now = datetime.datetime.now()
    day = now.strftime('%Y-%m-%d')
    y.append(day)
    
    now = now.strftime('%H:%M')
    y.append(now)
    
    return (y)


# Save data to file
def dataSave(y, pathWrite):
    fileWrite = str(pathWrite)
    f = open(fileWrite, 'a+')
    print(y, file=f)
    f.close()
    return ()


# Main program -------------------------------------------------------------------------

print("Starting main program\n")
time.sleep(2)  # Let the Aruino boot up


while 1:

    start = time.time()
    endtime = start + 10
    y = ""
    i = 0
    times = timeSetup()
    #print("times = ", times)

    for item in gh:
        y = ''
        y = y+(times[1]) 
        #print("y = ", y)  # Test
        
        print(gh[i])
        sergh = serial.Serial(gh[i])
        
        if times[1] == '05:00':
            sergh.write(b'1' + b'\n')
            print("Write 1")
        elif times[1] == '21:00':
            sergh.write(b'0')
            print("Write 0")
        else:
            sergh.write(b'x')
            print("Write x")
            
            
        sergh.reset_output_buffer()
        y = y + wxserial(sergh)
        y = y[:-1]
        #print("y = ", y)  # Test
        sergh.close()
        pathWrite = path + str('Test.csv')
        dataSave(y, pathWrite)
        i = i+1
        

    while time.time() < endtime:
        pass

    end = time.time()
    print("loop = ", end-start)
    #print()
    
    print("--------------------------------------------------------")
# ---------------------------------------------------------------------------




That worked! Thank you very much!

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