Sending Variables via the Bridge from Python to Sketch on Arduino Yun

Hi, I am trying to run a simple sketch that uses some output variables from Python, but I can’t seem to get the Python side to “put” a variable through to the sketch side. The Python and sketch codes are based on this forum discussion.

The Python code looks like this:

import sys    
sys.path.insert(0, '/usr/lib/python2.7/bridge/') 

from bridgeclient import BridgeClient as bridgeclient

value = bridgeclient()                              
   
d_high = 85   
   
value.put("d_high",(d_high))

And the sketch looks like this:

#include <Process.h>
#include <Bridge.h>

char d_high[4];

void setup() {
  Bridge.begin();
}

void loop() {
  Process p;
  p.runShellCommand("python /mnt/sda1/arduino/Python.py"); //Run the Python Code to generate values
    
  Bridge.get("d_high",d_high,4);  //get value from Bridge and store
  int d_high_int = atoi(d_high);  //convert ASCII to integer
  Serial.println(d_high_int);  //print result to serial monitor to check
  delay(5000);  //wait 5 seconds and update
}

I am able to “put” and “get” the proper data when I put a discrete value in: value.put(“d_high”,‘70’), but if I try to reference a variable (as shown), the sketch output only returns 0. I’m sure it’s something very simple and I’ve tried to format the variable on the Python side 20 different ways, but I still can’t get a meaningful value to exit the sketch, unless I use a discrete value, so I know that data is being passed through the Bridge.

Thanks for any help!

As an update, I moved the Python script into a "www" folder as suggested by this forum post.

When I call http://local.arduino/data/get when I use a discrete value, it returns this:

{"value":{"d_high":"-70.5"},"response":"get"}

And my previous examples returns this:

nil socket, 146 Connection refused

Which I take to mean that a processing error happened and that no values are stored.

I've looked high and low for more information on the bridgeclient value.put function, but I can't find additional examples or resources. Maybe calling a variable into value.put isn't an option? Or I'm missing something. I hope it's the latter.

I was able to create a workaround by creating a new python script that basically wrote all of these variables to another python script that the sketch now calls. It's not the most elegant solution, but it seems to be working and I'm getting the data that I want!

Now it goes like this:

Sketch processes two python scripts.

1st python script get data from web and writes a new file (using f.write) with all of the current variables values embedded in a value.put("key",'web data' format.

The second file just contains the data without having to call for a variable and looks like the python script listed in my first question.

Now I can use all of the variables in my sketch using the Bridge.get function. The number values are converted to integers and the character strings are left alone.

I keep self posting in the hopes that this may be of benefit to someone that finds this in the future. I can post additional information if someone has questions.

doktorinjh: I am able to "put" and "get" the proper data when I put a discrete value in: value.put("d_high",'70'), but if I try to reference a variable (as shown), the sketch output only returns 0.

In your "discrete" example (I think the word you're looking for is "literal") you are putting a string value to the Bridge. When you are referencing a variable, you are putting an integer value to the Bridge. The Bridge ONLY supports strings, that's why your variable attempt is not working.

Try this, it should work:

d_high = "85"
   
value.put("d_high",(d_high))

That makes sense, thanks for the additional information.

I tried to make that example simple, but my actual script has the variable being called from a parsed JSON string from online, so the variable isn’t static. There’s probably an easier method, but everything seems to be working at the moment.

doktorinjh: so the variable isn't static.

Static vs dynamic is not important - character string vs numerical value is.