1. Go to this page and download the library: Download microscrap/i2c 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/ */
microscrap / i2c example snippets
use Microscrap\Bindings\I2C\DataObjects\I2CBus;
$bus = i2c_open('/dev/i2c-1', 0x38);
if (is_null($bus)) {
exit("Failed to open bus or bind slave address\n");
}
// Soft init/calibration command.
i2c_write($bus, pack('C*', 0xBE, 0x08, 0x00));
usleep(20_000);
// Trigger a measurement and wait for the typical 80 ms conversion time.
i2c_write($bus, pack('C*', 0xAC, 0x33, 0x00));
usleep(80_000);
// Read back 7 status+data bytes.
$raw = i2c_read($bus, 7);
$bytes = array_values(unpack('C7', $raw));
$rawHumidity = (($bytes[1] << 12) | ($bytes[2] << 4) | ($bytes[3] >> 4)) & 0xFFFFF;
$rawTemp = ((($bytes[3] & 0x0F) << 16) | ($bytes[4] << 8) | $bytes[5]) & 0xFFFFF;
printf(
"Temp: %.2f C RH: %.2f %%\n",
($rawTemp / 1048576.0) * 200.0 - 50.0,
($rawHumidity / 1048576.0) * 100.0,
);
i2c_close($bus);
final readonly class I2CBus {
public int $fd; // open file descriptor
public string $path; // device path (/dev/i2c-1)
public int $addr; // currently bound 7-bit slave address
}
use Microscrap\Bindings\I2C\Enums\I2CFuncFlag;
$caps = i2c_get_funcs($bus);
if ($caps & I2CFuncFlag::I2C_FUNC->value) {
echo "Plain I2C messages supported\n";
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.