Execute Python script from Arduino code (Process) [solved]

hi,
I have problem executing Python script from arduino code.

Python script do_p.py is located in /mnt/sda1/arduino/www/test.
It is inserting data to sqlite database by accepting script parameters ie. ./do_p.py 11.0 76.0
Script is working well under linux (adds record to table in database with values given as arguments).
Python script do_p.py

#!/usr/bin/python
import sqlite3 as lite
import sys
from sys import argv

script,Temp,Wilg = argv
con = lite.connect('test1.db')
with con:
        cur = con.cursor()
        cur.execute("INSERT INTO tablica VALUES(null,?,?)",(Temp,Wilg))
        con.commit()
con.close()

Using arduino "Process" i try to run above script but without success...I tried runShellCommand() and begin-run with various configurations
Arduino sketch

#include <Bridge.h>
#include <Process.h>
void setup() 
{
Bridge.begin();
}

void loop() {
 
Process p;
//p.runShellCommand("python /mnt/sda1/arduino/www/test/do_p.py 12.0 76.0");

p.begin("python");
p.addParameter("\"/mnt/sda1/arduino/www/test/do_p.py 12.0 76.0\"");
p.run();
}

Could somebody point me proper syntax to execute this Python script?
Another thing is that finally I want Script arguments be passed as arduino variables I belive something symilar to this:

 p.runShellCommand("python /mnt/sda1/arduino/www/test/d.py $i $j");

Hi,

I've executed some python scripts by similar way that your statement line commented and I haven't had problems. Even with passing parameters in the same way to you.
Make sure to give 755 permissions in /mnt/sda1/arduino/www/test folder.

Arturo.

hi,
I got it working with fixed values in python script. Changing line pointing to database in do_p.py fixed everything

#!/usr/bin/python
import sqlite3 as lite
import sys

con = lite.connect('/mnt/sda1/arduino/www/test/test1.db')
with con:
        cur = con.cursor()
        cur.execute("INSERT INTO tablica VALUES(null,111,222)"
        con.commit()
con.close()

However I still don't know how can I pass value to Python script in Arduino sketch.

int i = 111;
int j = 112;
Process p;
p.runShellCommand("python /mnt/sda1/arduino/www/test/do_p.py $i $j");

it's not working (Python script from my first post). Any help what should I put instead of $i $j?

You do not need to declare these integer variables for passing to the python script call through Process.
I would make it by passing the parameters without '$' symbols. Then, you should parse these values of variables into the python script to do the db query.

Regards.

Arturo.

Install software:

opkg update
opkg install sqlite3-cli
opkg install  python-sqlite3

Create DB:

sqlite3 /mnt/sda1/test1.db
sqlite> create table tablica(id integer, Temp,Wilg, primary key(id asc));
sqlite> INSERT INTO tablica (Temp,Wilg) VALUES (12, 30);
sqlite> INSERT INTO tablica (Temp,Wilg) VALUES (12, 30);
sqlite> select * from tablica;
1|12|30
2|12|30
sqlite>.quit

Create python file:

nano /mnt/sda1/do_p.py
#!/usr/bin/python
import sqlite3 as sqlite
import sys
script,Temp,Wilg = sys.argv
con = sqlite.connect('/mnt/sda1/test1.db')
cur = con.cursor()
cur.execute('''INSERT INTO tablica (Temp,Wilg) VALUES(?,?)''',(Temp,Wilg))
con.commit()
con.close()
chmod 755  /mnt/sda1/do_p.py

Testing python file:

/mnt/sda1/do_p.py  100 200
sqlite3 /mnt/sda1/test1.db
sqlite> select * from tablica;
.quit

Arduino code:

#include <Process.h>

void setup() {
  // Initialize Bridge
  Bridge.begin();
  // Initialize Serial
  Serial.begin(9600);
  // Wait until a Serial Monitor is connected.
  while (!Serial);
}

void loop() {
  // Do nothing here.
  Process p;		// Create a process and call it "p"
  p.begin("/mnt/sda1/do_p.py");	// Process that launch the "do_p.py" command
  p.addParameter("500"); // Add the parameter to "Temp"
  p.addParameter("1000"); // Add the parameter to "Wilg"
  p.run();		// Run the process and wait for its termination

  // Print arduino logo over the Serial
  // A process output can be read with the stream methods
  while (p.available() > 0) {
    char c = p.read();
    Serial.print(c);
  }
  // Ensure the last bit of data is sent.
  Serial.flush();
  delay(10000);
}

+1 to passing parameters with "addParameter"

Thank You. It's working now. :smiley: