-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathSHT30.java
More file actions
executable file
·45 lines (39 loc) · 1.58 KB
/
SHT30.java
File metadata and controls
executable file
·45 lines (39 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Distributed with a free-will license.
// Use it any way you want, profit or free, provided it fits in the licenses of its associated works.
// SHT30
// This code is designed to work with the SHT30_I2CS I2C Mini Module available from ControlEverything.com.
// https://www.controleverything.com/content/Humidity?sku=SHT30_I2CS#tabs-0-product_tabset-2
import com.pi4j.io.i2c.I2CBus;
import com.pi4j.io.i2c.I2CDevice;
import com.pi4j.io.i2c.I2CFactory;
import java.io.IOException;
public class SHT30
{
public static void main(String args[]) throws Exception
{
// Create I2C bus
I2CBus Bus = I2CFactory.getInstance(I2CBus.BUS_1);
// Get I2C device, SHT30 I2C address is 0x44(68)
I2CDevice device = Bus.getDevice(0x44);
// Send measurement command
// High repeatability measurement
byte[] config = new byte[2];
config[0] = 0x2C;
config[1] = 0x06;
device.write(config, 0, 2);
Thread.sleep(500);
// Read 6 bytes of data
// Temp msb, Temp lsb, Temp CRC, Humididty msb, Humidity lsb, Humidity CRC
byte[] data = new byte[6];
device.read(data, 0, 6);
// Convert the data
int temp = ((data[0] & 0xFF) * 256) + (data[1] & 0xFF);
double cTemp = -45 + (175 * temp / 65535.0);
double fTemp = -49 + (315 * temp / 65535.0);
double humidity = 100 * (((data[3] & 0xFF) * 256) + (data[4] & 0xFF)) / 65535.0;
// Output data to screen
System.out.printf("Relative Humidity : %.2f %%RH %n", humidity);
System.out.printf("Temperature in Celsius : %.2f C %n", cTemp);
System.out.printf("Temperature in Fahrenheit : %.2f F %n", fTemp);
}
}