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.


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: bit.ly/2sDmXPl
DHT11 with Raspberry Pi using JAVA: bit.ly/2serqaJ
Github Repo:bit.ly/2sLFXMi
Code: bit.ly/2sGcV0O
Schematic: bit.ly/2sGdKXv
Download Pi4J Library: bit.ly/2j32blF
JAVA Application on Raspberry Pi: bit.ly/2hO2f84
DHT12 Datasheet: bit.ly/2sDmXPl

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

Twitter: twitter.com/geekybikash
GIT: github.com/oksbwn
Facebook: 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: bit.ly/2d8pHge

Bikash Panda
Catch Me On

Bikash Panda

Blogger / Embedded System Developer at WGLabz
A techie, tinkerer and tech lover, who loves to blog and feels everyone can learn tech provided they have the right attitude towards learning and passion. By profession, I am an IOT developer working in Smart Home/ Smart Grid domain.
Bikash Panda
Catch Me On

Latest posts by Bikash Panda (see all)

Related posts