DHT12 Interfacing with Raspberry Pi using Pi4J

DHT12

DHT12 Interfacing with Raspberry Pi using Pi4JThis video around raspberry pi showing how to interface DHT12 humidity and temperature sensor with raspberry pi by using JAVA nad Pi4j. DHT12 its a temperature sensor with a 1-Wire and I2C interface to connect with external controllers and having 1-Wire interface makes it compatible with the older DHT11 sensor but with a higher accuracy. For this tutorial, we are going to use the I2C interface of the sensor and to interact with it, we will be using JAVA. One issue with the DHT12 I2C interface is that the device address is not changeable which limits the multiple sensor interfacing to the same I2C bus as we can’t have diff address for each sensor.


[adrotate banner=”6″]

Code:

package in.weargenius.main;

import com.pi4j.io.i2c.I2CBus;
import com.pi4j.io.i2c.I2CDevice;
import com.pi4j.io.i2c.I2CFactory;

public class Main {

    private static I2CDevice   dev = null;
    static boolean x =false;
	public static void main(String[] args) throws Exception{
		System.out.println("DHT12 Pi4j Example.");
        I2CBus bus = I2CFactory.getInstance(I2CBus.BUS_1); //

        dev = bus.getDevice(0x5c); //Address for MCp23017 change if A0,A1,A2 are connected to diff potenrial
        byte data[] = new byte[5];
        dev.read(0x00,data, 0,5);
        if(x){
	     	System.out.println(data[0]); //Humidity
	     	System.out.println(data[1]); //HUmidity decimal places
	     	System.out.println(data[2]); //Temperature
	     	System.out.println(data[3]); //Temperature decimal places
	     	System.out.println(data[4]); // Checksum = data[0]+data[1]+data[2]+data[3]
        }
     	
     	if(data[4]==(data[0]+data[1]+data[2]+data[3])){

         	double humidity=Double.parseDouble(data[0]+"."+data[1]);
         	double temperature=Double.parseDouble(data[2]+"."+data[3]);
         	System.out.println("humidity is : "+humidity+" %RH");
         	System.out.println("Temperature is : "+temperature+" °C");
     	}
     	else
     		System.out.println("Error in communication.");
	}
	
   
}

Links :
Intro Video Credit: http://bit.ly/2sDmXPl
DHT11 with Raspberry Pi using JAVA: http://bit.ly/2serqaJ
Github Repo:http://bit.ly/2sLFXMi
Code: http://bit.ly/2sGcV0O
Schematic: http://bit.ly/2sGdKXv
Download Pi4J Library: http://bit.ly/2j32blF
JAVA Application on Raspberry Pi: http://bit.ly/2hO2f84
DHT12 Datasheet: http://bit.ly/2sDmXPl

Java Application on Pi Playlist: http://bit.ly/2eB1O2K
___________________________________________
Catch Me On:

Twitter: https://twitter.com/geekybikash
GIT: https://github.com/oksbwn
Facebook: http://www.facebook.com/geekybikash
_______________________________________________________________

You can Subscribe on YouTube by clicking this link to show your support and be updated with the latest video on the channel like this.

Subscribe: http://bit.ly/2d8pHge

Related posts

Leave a Comment