PHP code example of dept-of-scrapyard-robotics / ahtx0

1. Go to this page and download the library: Download dept-of-scrapyard-robotics/ahtx0 library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

dept-of-scrapyard-robotics / ahtx0 example snippets



use DeptOfScrapyardRobotics\Sensors\AHTx0\AHT20\AHT20;
use DeptOfScrapyardRobotics\Sensors\AHTx0\Enums\AHTx0I2CAddress;

return [
    'boards' => [
        'aht20-native' => [
            'class_name' => AHT20::class,
            'connection' => [
                'driver' => 'native',
            ],
            'startup' => [
                'i2c' => [
                    'chip_device' => 1,
                    'slave_address' => AHTx0I2CAddress::DEFAULT->value,
                ],
            ],
        ],
        'aht20-usb' => [
            'class_name' => AHT20::class,
            'connection' => [
                'driver' => 'usb',
            ],
            'startup' => [
                'i2c' => [
                    'chip_device' => 'ft232h',
                    'slave_address' => AHTx0I2CAddress::DEFAULT->value,
                ],
            ],
        ],
    ],
];


use RealityInterface\Sensors\Applied\Environmental\RelativeHumiditySensor;

$ahtx0 = RelativeHumiditySensor::using('aht20-native');

$humidity = $ahtx0->getHumidity();


use DeptOfScrapyardRobotics\Sensors\AHTx0\AHT20\AHT20;
use DeptOfScrapyardRobotics\Sensors\AHTx0\Enums\AHTx0I2CAddress;

$native_i2c_sensor = AHT20::connection('native')
    ->i2c(1, AHTx0I2CAddress::DEFAULT->value)
    ->create()
    
$temp_c = $native_i2c_sensor->temperature;
$rh = $native_i2c_sensor->humidity;


use DeptOfScrapyardRobotics\Sensors\AHTx0\AHT20\AHT20;
use DeptOfScrapyardRobotics\Sensors\AHTx0\Enums\AHTx0I2CAddress;

$usb_i2c_sensor = AHT20::connection('usb')
    ->i2c('ft232h', AHTx0I2CAddress::DEFAULT->value)
    ->create()
    
$temp_c = $usb_i2c_sensor->temperature;
$rh = $usb_i2c_sensor->humidity;

use DeptOfScrapyardRobotics\Sensors\AHTx0\AHT10\AHT10;
use DeptOfScrapyardRobotics\Sensors\AHTx0\AHT20\AHT20;
use DeptOfScrapyardRobotics\Sensors\AHTx0\AHT30\AHT30;
use DeptOfScrapyardRobotics\Sensors\AHTx0\Enums\AHTx0I2CAddress;

$aht10 = AHT10::connection('native')
    ->i2c(1, AHTx0I2CAddress::DEFAULT->value)
    ->create();

$aht20 = AHT20::connection('native')
    ->i2c(1, AHTx0I2CAddress::DEFAULT->value)
    ->create();

$aht30 = AHT30::connection('native')
    ->i2c(1, AHTx0I2CAddress::DEFAULT->value)
    ->create();

$humidity = $aht20->relative_humidity;
$temperature = $aht20->temperature;
$status = $aht20->status;


use DeptOfScrapyardRobotics\Sensors\AHTx0\AHT20\AHT20;
use DeptOfScrapyardRobotics\Sensors\AHTx0\Enums\AHTx0I2CAddress;
use RealityInterface\Sensors\Applied\Environmental\RelativeHumiditySensor;

$usb_sensor = AHT20::connection('usb')
    ->i2c('ft232h', AHTx0I2CAddress::DEFAULT->value)
    ->create();

$rh_sensor = RelativeHumiditySensor::as($usb_sensor);

$humidity = $rh_sensor->getHumidity();



use RealityInterface\Sensors\Applied\Environmental\RelativeHumiditySensor;

$ahtx0 = RelativeHumiditySensor::using('aht20-native');

// Simulated out-of-scope event measuring both sensors at the same time
$current_humidity = $ahtx0->getHumidity();
$reference_humidity = (float) "your-reference-rh";

// Add the values from your calibration session here.
$ahtx0 = $ahtx0->calibrate($reference_humidity, $current_humidity);

// The output of getHumidity() will now be adjusted with your compensation factor
// computed from the calibration.
$adjusted_humidity = $ahtx0->getHumidity();