This Community site is new! Please help us build a community around the JNIOR.
Sign up, and help share your knowledge. Please sign-up even if you do not plan to post as a sign of support.
If there is evidence of a demand we will continue to develop the content here.

Read temperature

You got ideas? Let's hear 'em. Here we can talk about your experiences programming on the JNIOR or items that you may wish to have INTEG assist you with.
Post Reply
kmcloutier
Posts: 31
Joined: Tue Sep 12, 2017 7:26 am

Read temperature

Post by kmcloutier » Tue Dec 12, 2017 1:07 pm

Sometimes you want to know the temperature in your applicaiton. You can use a temperature probe and the JNIOR.
package samplereadtemp;

import com.integpg.sensor.SensorPort;
import com.integpg.system.ArrayUtils;
import com.integpg.system.JANOS;
import java.io.IOException;

public class SampleReadTemp {

    public static void main(String[] args) {
        // read the registry key that holds the ID of the first temperature sensor
        String tempIdString = JANOS.getRegistryString("Externals/DeviceOrder/Type28_1", null);

        // error if the registry key is not defined.  This should automatically be defined by JANOS when the 
        // temperature device is present.
        if (null == tempIdString) throw new RuntimeException("No Temperature Device present");

        // convert the tempIdString to a long address
        long address = parseHexAsLong(tempIdString);

        try {
            // use the address and try to read the device block
            byte[] deviceBlock = SensorPort.readDeviceBlock(address);

            // convert the device block to a double value.  first we convert the read bytes to a long value.  then 
            //  convert the long value to a double value
            long longBits = ArrayUtils.getLong(deviceBlock, 0);
            double temperatureCelsius = Double.longBitsToDouble(longBits);
            System.out.printf("temp: %.2f C\r\n", temperatureCelsius);

            // convert to fahrenheit
            double temperatureFahrenheit = temperatureCelsius * 9 / 5.0 + 32;
            System.out.printf("temp: %.2f F\r\n", temperatureFahrenheit);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }



    public static long parseHexAsLong(String s) {
        if (s == null) return 0;
        int len = s.length();
        long l = Long.parseLong(s.substring(0, len - 8), 16);
        l <<= 32;
        l += Long.parseLong(s.substring(len - 8), 16);
        return l;
    }
}
I am a Senior Software Programmer at INTEG. You have questions and I have answers.

Post Reply