Call a function in Pyhton from Arduino Mega 2560

Hi, I am new here. I'm badly wanna ask this thing cause I already did my research but I can't still get it. So I'm hoping that you can help me with this.

My project is to put an image processing which is face recognition in the barcode system. The barcode system is made up in Arduino languages while the image processing codes made up in Python using the software Pycharm.

The barcode system codes are being uploaded on Arduino Mega 2560. And my plan is to connect it to Raspberry Pi 3 model B and upload my python codes in it to do an image processing. While I am searching a while ago, I found out that there is a way to connect the Arduino to Python. I saw some explanation about it but I can't still get it.

My question is how can I call 1 or more function from Arduino Mega 2560 to Python? Can you give me an example on how can I do it using the given codes down here?

The codes down here are some part of the barcode system codes. Thabk you in advance! Have a good day!

.....
void loop() {
  now = rtc.now();
  ethernetFunc();     //Ethernet function.Receives command to download csv file.
  checkDate();        //Check fo date changes.
  readBarcode();      //Read the barcode scanner
  checkSD();          //Check if sd is inserted
}

//Check if the file already exist.------------------------------------------------------------------------------------------------------------------------------------
void checkCSV(){     
  //Serial.println("Checking csv.");
  showLCD("Checking csv.");
  myStatus("Checking csv.");
  if(SD.exists(fileName())){        //Check if the current csv is saved.                 
    //Serial.print("CSV exist.");
    showLCD("CSV exist.");
    myStatus("CSV exist.");
    } else{                        //Create new csv file if not present
      createFile();                //Call createFile() function
      }
  }
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------

//Function for CSV file creation--------------------------------------------------------------------------------------------------------------------------------------
void createFile(){                                                       
  //Serial.println("Creating CSV.");
  showLCD("Creating CSV.");
  myStatus("Creating CSV.");
  myFile = SD.open(fileName(),FILE_WRITE);          //Create the csv file.Open the file if already exist.
  if(myFile){                                       //If the file successfully open or created.
    myFile.close();                                 //Close the file (Very Important)
    //Serial.println("CSV created.");
    showLCD("CSV created");                         //LCD print CSV created
    myStatus("CSV created");                        //Write CSV created in status.txt
    } else{
        //Serial.println("ERROR CSV");             //If the creation of csv file is unsuccessful.
        showLCD("ERROR CSV");                       //Show ERROR CSV in LCD
      }
  }

.......

You can'r directly call a Python function from an Arduino. But the Arduino can send a message to the Python program which it can use to decide which function to call.

Start by getting communications working with this Simple Python - Arduino demo . Then add on the extra features that you need.

The Python code should work on Windows if you edit it to use the Windows style of COM ports.

...R

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