-1

i wrote python script to read "Temperature/Humidity" data from sensor and it works fine and return result "23.0/38.0".

i need to put 23.0 in object member variable "temperature" and 38.0 in "Humidity" member variable then return the sensor object.

my problem is , the created web service return temperature and Humidity variable empty.

package raspberryws; 
import java.io.IOException;
import java.io.InputStream;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

@Path("DHTService")
public class DHTService {
    public DHTService() {
        super();
    }


@GET
@Produces(value = { "application/json" })
@Path("/readDHT")
public DHTSensor readDHT11() throws IOException {
    DHTSensor sensor = new DHTSensor();
    int ch;
    char y;
    String temp="" ;
    String humidity="" ;

    try {
        Process p =
            Runtime.getRuntime().exec("sudo python /projects/Adafruit_Python_DHT/examples/AdafruitDHT.py 11 4 ");
        // get an InputStream for the python stdout
        InputStream inStream = p.getInputStream();
        // read all stdout data one char at a time.
        while ((ch = inStream.read()) != -1) {
            System.out.println(ch + " ch");
            if ((int) ch == 47) {
                sensor.setTempreture(temp);
                humidity="";

            } else {
                if ((int) ch == 10) {
                    continue;
                }
                y = (char) ch;


                temp=temp+y;
                humidity=humidity+y;
                sensor.setHumidity(humidity);
            }

        }

    } catch (IOException e) {
        e.printStackTrace();
    }
    //   System.out.println(x);
    return sensor;  
}

}

  • There really isn't much error checking here to tell you what is going wrong - is the python part running? is your 'parsing' working? Why not just read in the whole output and then parse it? – pvg Dec 23 '16 at 23:13
  • yes the Python code is running , it's running on raspberry card . – Hany Talaat Dec 23 '16 at 23:18
  • if i print the returned string from python code it returns " 23.0/38.0" , but when i assign the value to sensor.temperatur and Humidity it return empty – Hany Talaat Dec 23 '16 at 23:20
  • That doesn't mean your invocation from java is running. Add some debugging statements or run in a debugger. And print out whatever you're getting from the invocation. – pvg Dec 23 '16 at 23:21
  • http://stackoverflow.com/questions/309424/read-convert-an-inputstream-to-a-string read that. And then parse the string instead of the incomprehensible code you have now. – pvg Dec 23 '16 at 23:25

1 Answers1

0

You can collect the output of the Python script using a BufferedReader to read the whole line of output in one go (assuming a single line is produced).

Then you can use String.split() to break the string into temperature and humidity components using / as the delimiter. Finally you can convert these strings to type float if required:

import java.io.*;

public class Test {
    public static void main(String[] args) {
        try {
            Process p = Runtime.getRuntime().exec("python test.py");
            BufferedReader fromChild = new BufferedReader(
                                         new InputStreamReader(p.getInputStream()));
            String data = fromChild.readLine();
            String[] measurement = data.split("/");
            System.out.println("Temperature: " + measurement[0]);
            System.out.println("Humidity: " + measurement[1]);

            float temperature = Float.parseFloat(measurement[0]);
            float humidity = Float.parseFloat(measurement[1]);
            System.out.println("Temperature (float): " + temperature);
            System.out.println("Humidity (float): " + humidity);
        }
        catch(Exception e) {
            System.out.println(e.getMessage());
        }
    }
}
mhawke
  • 75,264
  • 8
  • 92
  • 125
  • when i run on raspberry with test code , it works fine and return :Temperature: 38.0 Humidity: 16.0 Temperature (float): 38.0 Humidity (float): 16.0 but when i call the web service with same code , it raise java.lang.NullPointerException at the line " String[] measurement = data.split("/");" – Hany Talaat Dec 24 '16 at 08:41
  • You need to try to debug your code. Based on the exception the `data` string would appear to be null. Try to print out the value of `data` after you read the line from the child process. Are you even sure that the child process starts? How do you know? – mhawke Dec 24 '16 at 10:06
  • Yes i debug it by calling main method printing the result of python script , and it return correct values , but when i assign these values to object attributes and expose the class as webservice , it returns the object with null values – Hany Talaat Dec 24 '16 at 21:12