Creating IOT temperature sensor app in Java

It’s time for the Internet of Things (ioT) Thanksgiving Special. This time we are going to work on a special Do-It-Yourself project to create an Internet of Things temperature probe to connect your Turkey Day turkey to the Internet by writing a Thanksgiving Day Java Embedded app for your Raspberry Pi

Here is the parts list:

1 Vernier Go!Temp USB Temperature Probe
1 Uncooked Turkey
1 Raspberry Pi (not Pumpkin Pie)
1 Roll thermal reflective tape

Temperature Probe and it is getting really close now to Turkey Day, so you want kick your Internet of Things (IoT) Turkey Tweeter project into high gear now.
First, we need to test the temperature probe before sticking it into unknown places, namely our delicious IoT bird on Thanksgiving. So, take your Go!Temp USB temperature probe and plug it into your Raspberry Pi device, just like in this photo.

Now here is the program which will read the data from temperature probe and find the current temperature of the area where the probe has been kept

public class TurkeyTweeter {
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        Date date;
        FileInputStream fis = null;
        DataInputStream dis = null;
        byte   b[];
        double tempavg, c, f;
        int    samplecount=0, sequence=0, temp1=0, temp2=0, temp3=0;
        final double VERNIER_SCALING_FACTOR=126.74;
        final double VERNIER_CALIBRATION_OFFSET=5.4;
        b = new byte[8];
        // Loop to keep reading temperature
        while (true) {
            int available;
            try {
                fis = new FileInputStream("/dev/ldusb0");
                dis = new DataInputStream(fis);
            } catch (FileNotFoundException fnfe) {
                System.out.println("Cannot find temp sensor");
                fnfe.printStackTrace();
                System.exit(-1);
            }
            // Read 8 bytes from Vernier Go!Temp USB probe
            //   Format:
            //     Byte 0:   Sample Count
            //     Byte 1:   Sequence Index
            //     Byte 2-3: First temp sample
            //     Byte 4-5: Second temp sample
            //     Byte 6-7: Third temp sample
            try {
                if (dis != null) {
                    available = dis.read(b, 0, 8);
                    samplecount = b[0];
                    sequence = b[1];
                    temp1 = b[2] + b[3] * 256;
                    temp2 = b[4] + b[5] * 256;
                    temp3 = b[6] + b[7] * 256;
                }
            } catch (IOException ioe1) {
                System.out.println("Unable to get data from temp sensor");
                ioe1.printStackTrace();
            }
            tempavg = (temp1 + temp2 + temp3) / 3.0;
            c = tempavg / VERNIER_SCALING_FACTOR - VERNIER_CALIBRATION_OFFSET;
            // Convert from Fahrenheit to Celcius
            f = ((9.0/5.0) * c) + 32.0;
            double temperature = roundDouble(f);
            date = Calendar.getInstance().getTime();
            Format formatter = new SimpleDateFormat("E MMM d kk:mm:ss");
            String timedateString = formatter.format(date);
            System.out.println(timedateString+"  "+temperature);
            try {
                if (dis != null)
                    dis.close();
                if (fis != null)
                    fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ie) {
                ie.printStackTrace();
            }
        }
    }
   public static double roundDouble(double value) {
        double result = value * 100;
        result = Math.round(result);
        result = result / 100;
        return(result);
   }
}

Uday Ogra

Connect with me at http://facebook.com/tendulkarogra and lets have some healthy discussion :)

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *