My Arduino Sketch is calling a python script that does some actions and queries on a database. I want to push a resulting single value back to the Arduino sketch.
What is the best way to do this?
My Arduino Sketch is calling a python script that does some actions and queries on a database. I want to push a resulting single value back to the Arduino sketch.
What is the best way to do this?
Process Class with Return Variable
http://www.ibuyopenwrt.com/index.php/2-uncategorised/208-process-class-with-return-variable
That looks like what I want.
What does the Python or PHP code look like to pass the variable?
#!/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()
my_variable=my_function()
print my_variable
http://www.ibuyopenwrt.com/index.php/8-yun-compatible/89-python-and-sqlite3
I tried doing something similar...
Arduino Code:
/* Yun specific */
#include <Bridge.h>
#include <Process.h>
#include <Console.h>
void setup() {
Bridge.begin();
Console.begin();
while(!Console);
Process p; // Create a process and call it "q"
p.begin("/mnt/sda1/test.py"); // log baseline flow values
while (p.running());
while (p.available()) {
int result = p.parseInt(); // look for an integer
Serial.println(result); // print the number as well
}
Console.println("Setup complete...");
}
void loop() {
// put your main code here, to run repeatedly:
}
Python Code:
#!/usr/bin/python
int = 1
print int
The output of the python code when called using ssh is:
1
...as expected
The output on the Console Monitor from the Arduino code is:
Setup complete...
The number one is not printed before the statement as is expected.
/* Yun specific */
#include <Process.h>
String result = "";
void setup() {
Bridge.begin();
Serial.begin(9600);
while (!Serial); // do nothing until the serial monitor is opened
Serial.println("Starting bridge...\n");
Process p; // Create a process and call it "q"
p.runShellCommand("/mnt/sda1/test.py"); // log baseline flow values
while (p.running());
while (p.available()) {
result = p.readString();
Serial.println(result.toInt()); // print the number as well
}
}
void loop() {
// put your main code here, to run repeatedly:
}
PERFECT!!!
Thanks.
lexical43:
My Arduino Sketch is calling a python script that does some actions and queries on a database. I want to push a resulting single value back to the Arduino sketch.What is the best way to do this?
@lexical43,
There is no "best way". Solutions are always based on what you are trying to do.
The solution that SonnyYu offers is a good general solution.
Best of luck.
Jesse
Now that I have python to arduino working. I am having issues with the arduino to python.
How do I pass a variable from arduino for use in the Python sketch.
My failed attempt:
Arduino:
/* Yun specific */
#include <Bridge.h>
#include <Process.h>
#include <Console.h>
String python_result = "";
void setup() {
Bridge.begin();
Console.begin();
while(!Console);
Console.println("Setup complete...");
}
void loop() {
python_result = "";
Process d; // Create a process and call it "d"
d.runShellCommand("/mnt/sda1/test.py"); // log data in the sql database
d.addParameter(String(12)); // Add the parameter to "zone"
while (d.running());
while (d.available())
{
python_result = d.readString();
Console.print("test.py results: "); Console.println(python_result);
}
}
Python code:
#!/usr/bin/python
from __future__ import division
import serial
import sqlite3 as sqlite
import sys
ard = serial.Serial(port,9600,timeout=5)
msg = ard.read(ard.inWaiting())
print (msg)