0

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 BridgeClienton 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.

  • Are you having problems with the python code or Arduino code? – VE7JRO May 30 at 19:15
  • I've written arduino code that does the measurement and python code that processes the measurement and pushes the data into the message queue. I'm having problems with the connection between these two sides. I just need to send the measurment data to the python code somehow - with python3. – raphael_mav May 31 at 11:06
  • Please show us the code you are using. If the problem is with the Arduino sketch, it's "on topic" and we can help you. If the problem is with the python code, it would be better suited for Stack Overflow. Here is a link to a Stack Overflow question that has a simple sketch and python script to test if communication between the two is working: stackoverflow.com/questions/24682354/arduino-python3-script/… – VE7JRO May 31 at 14:33
  • 1
    raphael, as per my comment earlier, we can not read your mind and sometimes you have to start at the basics to rule out possibilities. Did you try the tutorial at getting started with Yun???? If so, did that work? If not, what exactly did not work? When I exactly i mean what errors did you get? At what point did output stop? What happened jut before that and so on. Remember we can not see your environment, we can only rely on what you tell us and the problem might just be that you missed something. – GMc Jun 4 at 22:42
  • 2
    also, i note that you have not actually shown us anything yet... Please refer to How to ask a good question and create a Minimal, Complete, Verifiable example. Especially the MCVE link (i.e. the second one) – GMc Jun 4 at 22:44
-1

You might want to try the Process method, although, looking at your recent edits to the question, it seems like you already have.

Nevertheless, I was having difficulty getting my head around the problem when I first used my Yún, and I found the information below helped me understand how to get up and running. You do say that you've tried many solutions already, and maybe you've already seen this, but as you don't list which solutions you have already tried (apart from one), I thought it might be worth posting this link as an answer.

From this post to the thread on the Arduino forums, Yun Serial connection between Arduino and Linux

Accepting HTTP connections, as shown in the Bridge tutorial, is but one facet of the Bridge library. (And probably the least useful and least efficient facet.) It is intended for accepting incoming connections from the greater network, not necessarily the local Linux processor.

The Bridge library is a collection of different classes that perform a variety of operations, all over the serial link between the two processors, and all can be used at the same time. The Bridge uses a network port paradigm, not HTTP. Using ports gives it a little more overhead, but it allows multiple simultaneous "connections" between the sketch and Linux sides.

I suggest you take a look at the Process tutorial. It does what you want, and 95% of the time it is the only part of the Bridge library that I use. The example defines Process objects that are local to a function, which run simple commands that do a little bit different f processing, return a result, and then exit.

My projects typically define a global Process object which starts a Python script, and keeps the connection open. Once open, the sketch can write to the Process object at any time, and the Python script reads that data from STDIN. Python can write to STDOUT at any time, and the sketch can read it from the Process object. The Process class derives from the Stream class, just like Serial, so your sketch can use any of the read/write methods you would use for a serial port. On the Linux side, you are just reading/writing as you would to the console. By default, Pythons runs in a buffered mode: I've found that you need to add the -u option to the command line that runs your Python script, or add it to the hashbang at the beginning of your script.

There are those who disable the Bridge library, and use the serial port manually on both sides. This can be done, but I've not had to resort to that yet. The Process class does everything that a direct serial connection can do, and more: you can have multiple simultaneous Process connections going, and you can still use all of the other Bridge library features. If you disable the Bridge library, you get just the one connection stream. It would be a bit more efficient, but unless you are dealing with high amounts of data, I'll bet you don't need to do that.

My typical project has the sketch running as a rather dumb I/O processor, passing barely digested input data down though one or more Process objects to a Python script. The Linux side does all of the heavy processing and network communications, and then sends output data back up the sketch which outputs it to the pins.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.