0

Does anybody know how to run a python script from C?

Basically, I'm trying to make it so that when I press a button connected to my arduino, my arduino, which is connected to my raspberry pi 3B+, will run a python script which takes a photo.

Does anybody know how to pull this off? Also, is there any simpler, easier way to do it? I don't know how to use the GPIO on raspberry pi.

Alec
  • 8,529
  • 8
  • 37
  • 63
  • Possible duplicate of [How do you call Python code from C code?](https://stackoverflow.com/questions/1056051/how-do-you-call-python-code-from-c-code) – Vlam May 22 '19 at 01:29
  • 1
    Are you trying to call python on the pi from C code on the Arduino, to call python on the pi from C code on the pi, or run python code on the Arduino from C code also on the Arduino? – lowtex May 22 '19 at 01:43

2 Answers2

0

Provided that you have python installed you can just use the system() function and just call python directly, if you want more control like capturing program's output or giving input, and more interaction popen() is available for Arduino.

Otherwise if you can link to the python library just use PyRun_SimpleString(code);. The previous link has an example of how you need to setup the environment before actually calling this function.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
0

system() allows you to call anything in python. This link goes into more depth if you want to interact with input or output.

A really simple way to run any python script is to wrap it in a shell script and invoke it from within your C application

For more info, view the docs: (1, 2)

Alec
  • 8,529
  • 8
  • 37
  • 63