cancel
Showing results for 
Search instead for 
Did you mean: 

Sensor Data: Custom Java Adapter for Streaming Lite

former_member258450
Participant
0 Kudos

I now have a Streaming Lite project on my Raspberry Pi device. I have followed the blog and now have a similar Java adapter setup on my the device which writes events to the Streaming Lite program.

Q. 1. However, if I have to connect it to read the values from the sensors. Would it be ok if the sensors talk to a python script on the pi, and then when the python script is capable of displaying temperature sensor readings on its console, would I just have to change the command value to say "sudo python temp.py" for my Java program to capture the output of the Python script?

As shown in the blog post below:

For example, if cmd = “echo 90” is passed in, the function will return “90”. Additionally, if we had a python script called SensorPoll.py, we can pass in cmd = “sudo python SensorPoll.py”. This will return the output of the sensor being polled.

private static String exec(String cmd) throws IOException{ 

Process p = Runtime.getRuntime().exec(cmd); 

BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream())); 

String s = stdInput.readLine(); return s; 

}

Q.2. And, the line sudo temp.py is mentioned here (below). Am I getting this right?

String val_sensorId ="RaspberryPi";
String val_Temperature_Command = "echo 90";

Q. 3. Does the(current) java program expect only the "Temperature" value from the python console? If I have a python program which just prints numbers like:

97.788

78.567

56.786....

Will the (current) Java code be happy with this Python output and would it be capable of incorporating these temperature values as input to the Streaming Lite program?

Link to the tutorial: http://sapassets.edgesuite.net/sapcom/docs/2016/01/f61dbb2f-5c7c-0010-82c7-eda71af511fa.pdf

Kindly let me know your views.

Thanks you.

Accepted Solutions (1)

Accepted Solutions (1)

RobertWaywell
Product and Topic Expert
Product and Topic Expert
0 Kudos

1) Yes the sample code is set up to make an external call to "read" the sensor. In the sample code we simply execute an "echo 90" which will feed the value "90" into the adapter. You can replace the "echo" statement with the command line of a Python script or other executable that will return a single numeric value.

2) Yes, if you wanted to call "sudo python temp.py" then you would update the line "String val_Temperature_Command ="echo 90";" to replace the "echo 90" with the "sudo python temp.py"

3) Yes the expected output of the external call for this particular example is a single numeric value.

Keep in mind that this tutorial example has been kept simple on purpose, for example by only retrieving a single input value from the sensor. If your sensor generates multiple values as a single event or record, then you would build your custom adapter to retrieve the full event record. Similarly this example was built to make it easy to read input from a python script as many sensors available for the Pi are readily accessible from Python scripts, but if you prefer to read the sensor directly from your Java code then you can do that.

former_member258450
Participant
0 Kudos

Thanks, Robert!

Answers (0)