I'm currently working on a small project with my Arduino Yún. I want to process the sensor data in python and send them in a message queue. I need to use Python3.
I cannot find a way to get the sensor data in my Python code.
I already tried to start the python process in my sketch and use the println
method of the process object but this doesn't work.
I also tried to use the BridgeClient
class from the Arduino Github but it won't work either.
Summarized: I can't find a way to send my measurements from my Arduino sketch to the my Python3 program on the Linux side of the Arudino Yún.
I don't think this is an extraordinary problem and I don't understand why there aren't any tutorials for this specific use-case.
The section below contains my Arduino sketch, which does the measurements.
#include <SparkFunHTU21D.h>
#include <Wire.h>
HTU21D myHumidity;
int trigPin = 11;
int echoPin = 12;
void setup() {
// put your setup code here, to run once:
pinMode(A1, INPUT);
Serial.begin(9600);
myHumidity.begin();
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
//Perform 5 SoilMoisture-measurements and get mean
float soil = meanSoilMoisture(A0, 5);
float humd = myHumidity.readHumidity();
float temp = myHumidity.readTemperature();
float distance = getDistance();
String dataString;
dataString += String(soil) + ", " + String(humd) + ", " + String(temp) + ", " + String(distance);
Serial.println(dataString);
delay(4000);
}
long getDistance() {
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT);
long duration = pulseIn(echoPin, HIGH);
// Convert the time into a distance
return (duration/2) / 29.1; // Divide by 29.1 or multiply by 0.0343
}
float meanSoilMoisture(int analogPin, int nMeasurements) {
float sum = 0;
for(int i = 0; i < nMeasurements; i ++) {
sum = sum + readVH400(analogPin);
delay(50);
}
return sum / nMeasurements;
}
//Perform Measurement
float readVH400(int analogPin) {
int sensor1DN = analogRead(analogPin);
float sensorVoltage = sensor1DN*(3.0 / 1023.0);
float VWC;
// Calculate VWC
if(sensorVoltage <= 1.1) {
VWC = 10*sensorVoltage-1;
} else if(sensorVoltage > 1.1 && sensorVoltage <= 1.3) {
VWC = 25*sensorVoltage-17.5;
} else if(sensorVoltage > 1.3 && sensorVoltage <= 1.82) {
VWC = 48.08*sensorVoltage-47.5;
} else if(sensorVoltage > 1.82) {
VWC = 26.32*sensorVoltage-7.89;
}
return(VWC);
}
I'd like to have a way to send dataString
to the linux side of the Arduino Yún and process it with Python3.
As explained earlier I've already tried out multiple solutions, e.g. start a new process and use the println
method of the process.
{...}
Process proc;
void setup() {
{...}
proc.begin("python3 /mnt/sda1/test.py");
p.runAsynchronously();
{...}
}
{...}
proc.println(dataString);
{...}
I've tried to read this dataString
in Python3 in different ways, e.g. I've used the input
method, because I read in a forum that I can handle such input like a keyboard input.
It didn't work and I didn't get an error, neither in Python nor in my sketch.
I've read different examples using the Bridge
library. So I set up the sketch:
{...}
bridge.put("data", dataString);
{...}
I'd like to try out the BridgeClient
on the python side but this library was written in Python2. I don't want to migrate the library to Python3 and I need to use Python3, so I didn't find a way to write Python code here.
I would highly appreciate any help from you.