Internet of Things (IoT) Thanksgiving Special: Turkey Tweeter (Part 3)

OK, sports fans. You've got your Vernier Go!Temp USB probe connected. It looks good with lsusb and you can see the /dev/ldusb0 device in your Raspberry Pi Linux shell.

So, how do you write a Java SE Embedded app to read in the turkey temp values. Well, as with most things, you search the Web and you can find how it was done previously in other non-Java inferior programming languages. ;-) Here's a great example in Python on the finninday.net site.

See: finniday.net Go!Teamp example in Python

It shows the reverse engineered byte format of the data coming over USB from the Vernier Go!Temp probe. Booyah! That's what we need to write a Java SE Embedded app. And, here it is...

/**
 *
 * @author hinkmond
 * Copyright © 2013 Oracle and/or its affiliates. All rights reserved.
 */
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);
   }
}

Compile using javac, lather, rinse, repeat. And, here's the output running on the RPi (NOTE: Remember, you must run as root to access the /dev/ldusb0 device):

pi@raspberrypi ~ $ sudo java -jar TurkeyTweeter.jar
Thu Nov 21 16:42:59  71.59
Thu Nov 21 16:43:00  72.39
Thu Nov 21 16:43:01  72.39
Thu Nov 21 16:43:02  72.39
Thu Nov 21 16:43:03  72.39
Thu Nov 21 16:43:04  72.39
Thu Nov 21 16:43:05  72.39
Thu Nov 21 16:43:06  72.39
Thu Nov 21 16:43:07  72.39
Thu Nov 21 16:43:08  72.39
Thu Nov 21 16:43:09  72.39
...
As you can see, it's 72 degrees Fahrenheit in my office. The turkey won't be that temperature roasting in the oven on Thanksgiving, but we have now confirmed this part of the Turkey Tweeter works. Exciting, isn't it? :-) Next up, we will write the Java code to tweet out the values of our poor turkey as it cooks... (Yeah, poor turkey until it's inside my tummy. Then, it's yummy turkey!)

See the full series on the steps to this cool demo:
Internet of Things (IoT) Thanksgiving Special: Turkey Tweeter (Part 1)
Internet of Things (IoT) Thanksgiving Special: Turkey Tweeter (Part 2)
Internet of Things (IoT) Thanksgiving Special: Turkey Tweeter (Part 3)
Internet of Things (IoT) Thanksgiving Special: Turkey Tweeter (Part 4)
Internet of Things (IoT) Thanksgiving Special: Turkey Tweeter (Part 5)

Comments:

Great series, Hinkmonk, and Happy Thanksgiving! By the way, will a future Turkey Tweeter post feature a JavaFX UI for delicious turkey visualization as it's roasting? :-)

Regards,
Jim Weaver

Posted by James Weaver on November 22, 2013 at 06:36 AM PST #

Thanks, Jim! That's a good suggestion about a future JavaFX UI for the Turkey Tweeter! (I can see some type of scenegraph with a cartoon tom turkey...) :-)

Posted by Hinkmond Wong on November 22, 2013 at 10:10 AM PST #

This is a great idea and I'm trying to follow your "turkey recipe" but when I try to compile this code I get "cannot find symbol...@ 10 times -- starting with FileInputStream. Should there be some import statements at the beginning?

Any advice?

Thanks

Posted by guest on November 26, 2013 at 07:27 PM PST #

Sorry about that! I left out the import lines for brevity in my blog post. Here they all are:

-----

// For USB Device Reading
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

// For Tweeting
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.CookiePolicy;
import java.net.URL;
import java.net.URLEncoder;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import javax.net.ssl.HttpsURLConnection;

-----

Let me know if you still see problems after adding these.

Thanks,
Hinkmond

Posted by Hinkmond Wong on November 26, 2013 at 09:56 PM PST #

Ok, that part worked but now it looks like I have an issue with the Vernier Therm.

1) I ran the first test and it was found on port 7 (I'm using a USB hub):
Bus 001 Device 007: ID 08f7:0002 Vernier EasyTemp/Go!Temp
Bus 001 Device 008: ID 040b:2000 Weltrend Semiconductor

2) Then I run the next test and get the following -- this is not the date of the rpi system, so maybe this is a problem:
pi@raspberrypi ~ $ ls -l /dev/ldusb0
crw------- 1 root root 180, 176 Jan 1 1970 /dev/ldusb0

3) When I run the java file it appears it is not connecting to the therm:
pi@raspberrypi ~ $ java TurkeyTweeter
Cannot find temp sensor
java.io.FileNotFoundException: /dev/ldusb0 (Permission denied)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:146)
at java.io.FileInputStream.<init>(FileInputStream.java:101)
at TurkeyTweeter.main(TurkeyTweeter.java:49)

Thanks!

Posted by guest on November 27, 2013 at 04:50 AM PST #

I missed your instruction regarding doing this as the root user, and now it works!

For those wanting to follow along. I did the following:

1) In terminal: "sudo -i"
2) cd /pi/home
3) javac TurkeyTweeter
4) java TurkeyTweeter

Result
Wed Nov 27 12:57:20 64.92
Wed Nov 27 12:57:21 64.92
Wed Nov 27 12:57:22 64.92
Wed Nov 27 12:57:23 64.89
Wed Nov 27 12:57:24 64.89
and on and on

Posted by guest on November 27, 2013 at 04:58 AM PST #

Post a Comment:
Comments are closed for this entry.
About

Hinkmond Wong's blog on making Machine to Machine (M2M) and the Incredible Internet of Things (IoT) smarter with Java Embedded Technologies

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
2
3
4
5
6
7
8
9
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today