sudo update-alternatives --config java
File w1devDir = new File("/sys/bus/w1/devices");
if ( w1devDir.exists() && w1devDir.canRead() && w1devDir.isDirectory() ) {
String[] dirnames = w1devDir.list();
if ( dirnames!=null && dirnames.length>0 ) {
List<File> dirs = null;
for ( String dirname : dirnames ) {
if ( !dirname.startsWith("28-") ) continue;
File dir = new File( w1devDir, dirname );
if ( dir.isDirectory() && dir.canRead() ) {
if ( dirs==null ) dirs = new ArrayList<>(dirnames.length);
dirs.add( dir );
}
}
if ( dirs!=null ) {
for ( File dir : dirs ) {
Log.info( "Reading termosensor %s...", dir.getName() );
File file = new File( dirs.get(0), "w1_slave" );
// An example of file content:
// 87 01 4b 46 7f ff 09 10 48 : crc=48 YES
// 87 01 4b 46 7f ff 09 10 48 t=24437
//BufferedReader reader = FileUtils.openReader( file, StringUtils.ASCII );
BufferedReader reader = null;
try {
reader = new BufferedReader( new InputStreamReader(new FileInputStream(file),"US-ASCII") );
String line = reader.readLine();
if ( line==null ) break;
Log.info( "%s", line );
if ( line.endsWith(" YES") ) {
line = reader.readLine();
if ( line==null ) break;
Log.info( "%s", line );
if ( line.length()>29 && line.regionMatches(26," t=",0,3) ) {
int t = StringUtils.extractInteger( line, 29 );
int f = t*9/5+32000;
int k = t+273;
Log.info( "t = %d mC, %d mF, %d mK", t, f, k );
}
}
} catch ( Throwable e ) {
Log.error( e );
} finally {
try {
reader.close();
} catch ( Throwable e ) {
// ignore
}
}
} //for
}
}
But you have to execute the following before starting your java application (for example, you can put it into /etc/rc.local):
sudo modprobe w1-gpio
sudo modprobe w1-therm
File w1devDir = new File("/sys/bus/w1/devices");
if ( w1devDir.exists() && w1devDir.canRead() && w1devDir.isDirectory() ) {
String[] dirnames = w1devDir.list();
if ( dirnames!=null && dirnames.length>0 ) {
List<File> dirs = null;
for ( String dirname : dirnames ) {
if ( !dirname.startsWith("28-") ) continue;
File dir = new File( w1devDir, dirname );
if ( dir.isDirectory() && dir.canRead() ) {
if ( dirs==null ) dirs = new ArrayList<>(dirnames.length);
dirs.add( dir );
}
}
if ( dirs!=null ) {
for ( File dir : dirs ) {
Log.info( "Reading termosensor %s...", dir.getName() );
File file = new File( dirs.get(0), "w1_slave" );
// An example of file content:
// 87 01 4b 46 7f ff 09 10 48 : crc=48 YES
// 87 01 4b 46 7f ff 09 10 48 t=24437
//BufferedReader reader = FileUtils.openReader( file, StringUtils.ASCII );
BufferedReader reader = null;
try {
reader = new BufferedReader( new InputStreamReader(new FileInputStream(file),"US-ASCII") );
String line = reader.readLine();
if ( line!=null ) {
Log.info( "%s", line );
if ( line.endsWith(" YES") ) {
line = reader.readLine();
if ( line!=null ) {
...