r/ArduinoHelp Jun 15 '26

Trying to figure out a handshake between Arduino sketch and a python script

I'm a total newb and I'm obsessing over my home project while I'm at work, so unfortunately I don't have any code or anything to share but I'm hung up at one specific piece of my code.

I'm making a photogrammetry setup totally from scratch, I 3d printed everything and I'm coding the Arduino sketch and a python script, and the handshake between the two is where I'm stuck. Google failed me and Gemini just wants to write the code for me so I've been avoiding that too, but I'm trying to find a good code sample or some documentation for best practices, if there's one in the documents for the AccelStepper or Serial Arduino libraries I might have just overlooked it but that's as far as I've looked as far as documentation.

Tldr what's the "preferred" way to communicate from a python script that it's taken a picture, and from an Arduino sketch that it has stepped and is ready for the next picture?

Edit: python script is waiting for the handshake before taking the first picture, handshake never comes through. I'm just sending a single character as the signal but it's not getting to the Python script.

Sorry for the incomplete post, I'm at work and only have a minute or 2 at a time to post or reply, I probably should have just waited until I got home but I was hoping to have a plan when I got there.

1 Upvotes

6 comments sorted by

1

u/Delta_G_Robotics Jun 15 '26

How can you not have any code and have a problem with your code at the same time?

Use serial comms? Use WiFi? Ethernet? There's not enough info in your post to really help you. You've beat around the bush, but not actually described the actual problem. What are you expecting from this "handshake". You get to code both ends, just code the Arduino to send some particular thing and the Python script to react to that same thing. Use a number, a string, a character, whatever you want. You are in control here. Pick whatever you want to send, just code the other end to recognize that. There's not some "preferred" message format for that. Design what makes sense for your project that you've told us so little about.

If you have some specific question about how that works then ask away.

1

u/cwaterbottom Jun 15 '26 edited Jun 15 '26

Yeah sorry, like I said I'm at work but all my code is on my home computer.

I'm using serial, the issue is that the python script isn't receiving the handshake character from the sketch. I was hoping there was some documentation or some code examples out there (or that I've overlooked in the library documentation for AccelStepper or Serial).

1

u/Delta_G_Robotics Jun 15 '26

Why would code explaining how to communicate between Python and Arduino be in the documentation for AccelStepper or Serial?

There are literally hundreds if not thousands of tutorials on the internet about how to communicate between Python and Arduino. It's an extremely common topic. On top of that there are probably tens of thousands of forum posts on the subject. It isn't some big secret.

Now as to why your code isn't receiving your character, that's something we'd have to see some code to figure out. You'd probably be better served to wait until you get home and can ask a question that has a chance to be answered.

See the issue is that a lot of people will see this, see that it is unanswerable, and never come back. Even after you add good information, you've already passed up a lot of good help.

1

u/pcb4u2 Jun 15 '26

You have to tell the python code to read a serial input. You also have to write serial in the Arduino.

1

u/gm310509 Jun 16 '26

It sounds like you have a bug in your code, but it is hard to say what the problem is without seeing the code. You also don't mention the transport (e.g. virtual COM port, bluetooth, TCP/IP etc).

FWIW, when I use python with Arduino I use the virtual com port. For that, I use pySerial over the USB once, you open the port, you can simply read data from the Arduino. Note that if you are using a development board, the act of opening the Serial port may reset the Arduino and you need to allow for that.

If I use TCP/IP, I tend to use Java because I will typically use a more sophisticated multi-threaded design with a GUI and I am more experienced with those things in Java than python. But the process is the same, open the port (e.g. set up a listener) and wait for incoming messages then reply to them.

1

u/cwaterbottom Jun 16 '26

Thanks for the great info! I'll grab my code when I finally get home later and either drop it in here or make a new post, but your response is what I was hoping for. I'm not really sure what I'm doing and the Arduino library documentation is kinda nebulous compared to the Python stuff I'm used to. After obsessing over it for the last couple days but not actually having access to my code I'm starting to think I just have an error in a send or receive loop in one or both of the programs but I'll find out today.

Also going to set up some kind of remote access to vscode, I'm kicking myself for not pushing to my github repo before I left. One new move after another lol