Problem using get() with python and BridgeClient

Hello!! I am trying to build a system that uses the Yun to control a greenhouse (fans/lights/pumps) and to relay sensor information to a database. I am using python and bridgeClient to get and put values from the python and arduino scripts, and have created a basic test to pull one value and send one value. I have put my script in a folder called "www" in the same folder as the arduino sketch, and it appears on the SD card when I upload the sketch wirelessly. The problem is that the get() within the arduino always returns nothing... I am not sure what is wrong because the code seems correct... :-[ please help!!!

Here is the python code:

import sys  

sys.path.insert(0, '/usr/lib/python2.7/bridge/') 
from bridgeclient import BridgeClient as bridgeclient
                                                     
value = bridgeclient()                         
                                                     
A0_State = value.get("A0")

value.put("Digital5", "155")

And here's the arduino main loop:

char D5_state[5];
//sending value to python
Bridge.put("A0", String(analogRead(A0)));

//pulling value from python
Bridge.get("Digital5", D5_state, 5);

int D5int = atoi(D5_state);

analogWrite(5, D5int);
delay(1000);

Great thanks for any help!!! :grinning: :grinning:

nate4495: The problem is that the get() within the arduino always returns nothing...

What do you mean by "nothing"? I assume you mean D5int is always zero?

What do you get if you print out the string D5_state? If there is something in there that atoi() doesn't understand, it will return zero.

You can also use http://arduino.local/data/get from a browser to peek at the stored values to see what, if anything, gets put into Digital5.

Also, has the Python script had a chance to actually run and put the value by the time you try to get it?

Yes, D5 is returning zero and D5_state is just empty when I try and print to the serial console. I tried running the python script within the arduino sketch by calling this

  Process p;
p.begin("python");
p.addParameter("/mnt/sd/arduino/www/BridgeClient_TEST/bridgeClient_python.py");
p.run();

but it didn't seem to do anything, and in other examples people did not include this.

That's very curious. I'm currently using Python to "put" a value that a sketch "get"s. My code looks virtually the same as yours (I'm using different names and buffer sizes, but the way the put and get calls are made is essentially the same.) I don't see why it shouldn't work.

Try using a browser to load:

http://arduino.local/data/get

That will list the current value all of the keys that have been "put" to the bridge. It will give us a clue as to what side it's on: if the D5_state key is not set, or has the wrong value, it's on the Python side. If it has the right value then it's on the sketch side.

Divide and conquer: first step is to figure out which side is acting up.

Hey I really appreciate your help on this :) it is a load off my shoulders haha

Here is what that address reads

{"value":{"A0":"292"},"response":"get"}

So it seems it can't even get the value?

It looks like the Python script is not putting the value. It's likely the Arduino side is working correctly, there just isn't anything there to read. To fully test the sketch, you can use a web browser to manually put a value, which then should be read by the sketch:

http://arduino.local/data/put/D5_state/123

Are you sure the Python script is being run? How are you launching it?

You do have a bit of a chicken/egg problem with the code, but it should still work as a proof of concept. The issue is that there is no synchronization between the Python and Arduino code. The saving grace is that the Arduino get/put calls are likely in a loop. (Is that correct?) so it will repeatedly put the A0 value, and repeatedly attempt to read the D5 value. When the Python code runs, it will get the last put A0 value, then put a D5 value. At that point the Arduino sketch should start returning the last put value.

YESS!!! I GOT IT!!! :smiley_cat:

All I had to do was change “sd” to “sda1” when I called my python script!!! Here is the call

Process p;
p.begin("python");
p.addParameter("/mnt/sda1/arduino/www/BridgeClient_TEST/bridgeClient_python.py");
p.run();

Thank you so much for your help. I cannot express how happy I am right now :smiley: :smiley:

One last thing, how can I check that the python is receiving the “A0” value that is being sent?

nate4495: One last thing, how can I check that the python is receiving the "A0" value that is being sent?

I can think of a few ways, in order of increasing complexity:

  • Add a print statement to the Python script to display the value, and run it from the command line so you can see the output
  • Add a print statement to the Python script to display the value, and use the Process class read() and available() functions on the Arduino side to see the output
  • Have the Python code write the A0 value to a file, then cat the file from the command line
  • Have the Python code put the value to a new key, use http://arduino.local/data/get to view it
  • Have the Python code put the value to a new key, then get it and display it in the Arduino sketch
  • Have the Python code do something else useful with the value, like open a socket to a computer and send it, act as a server so it can be accessed, implement a logging function, or...