Problem with Process.available()

Hello to everybody,

I met A Strange bug:

I need to run a process for a script in Python that read a file json, and then returns a string to the sketch:

This is the part of the sketch with the process:

Process Call_Python_Sync;    

Call_Python_Sync.begin("python");  

Call_Python_Sync.addParameter("/script/python_sync.py");

for (int i = 0; i < packet.length; i++) 
  { 
   Call_Python_Sync.addParameter(String(packet.data[i],HEX));
   Serial.print(String(packet.data[i],HEX));
   Serial.print(" ");
  }

Call_Python_Sync.addParameter(String(rssi,DEC));
Call_Python_Sync.addParameter(String(lqi,DEC));
Serial.println(" ");

Call_Python_Sync.run();

while (Call_Python_Sync.available()>0) 
{
   char c = Call_Python_Sync.read();
   Serial.print (c);
   comando=comando+c;
 }
  
  comando.trim();
  Serial.print ("Comando: ");
  Serial.println (comando);
}

The script Python works fine (when I start the script from Linino it returs the right string)

BUT

When the script is called by process, it returns nothing…

I understand that the problem (problem only if I call the file python by the sketch) is when I read a file json…

If I insert a (print “Hello” ) inside the python file before reading the json file The sketch returns the String “Hello”
Ther isn’t error reading the json file…
I think that the Bridge closes when I read the json file…

How is it possible ?

This is the part of the python script with the file json call:

def Parse_JSON():
	global temp_voluta
	global temp_sensore_ble
	
	with open('comandavalvole.json') as data_file:    
		data = json.load(data_file)
	
	print (data[MC_VALVOLA]["stato"])
	print (data[MC_VALVOLA]["temp_voluta"])
		
	temp_voluta = data[MC_VALVOLA]["temp_voluta"]
	print temp_voluta

Parse_JSON()


temperatura = 0 
 
temperatura =  int(temp_voluta)
print ("La temperatura di soglia è: "+ str(temperatura))
		

if (temperatura >= (TempValvola + 5)): #dovrebbe essere più alto di 5 gradi
	comando = "accendi"
elif (temperatura <= (TempValvola - 5)): #dovrebbe essere più basso di 5 gradi
	comando = "spegni" 
else:
	comando = "nulla"


print comando

Thank you for any help

Yes, your Python script is probably crashing when it opens the JSON file.

This is a common situation: the code run by the Process call runs properly when you manually run it from the command line, but fails when run from the Process object. The reason? You are using a relative file name (just the name, no path information.) When you run the code from the command line, your current directory is the same as the file's location, so it has no trouble finding the file. But when you run it from a Process object within a sketch, the current directory is a system directory, not the same directory as the file. The result is that it can't open the file and the program crashes.

The solution is to use an absolute path to the file, something like "/mnt/sda1/data/comandavalvole.json"

When developing your code, you not only need to make sure it can run properly from the command line, but also run from any directory. Once you can run it from anywhere, then you can try running it from a Process object in your sketch, and you can be reasonably sure that it will work.

Great !!!!
That was the problem !!!!!

Because the script python and the file json were on the same directory, I was thinking that there is no problem to write the local path....... but there isn't....

Thank you very much !!!!!