https://github.com/tasoftch/php-i2c-extension
A Zend Extension for php to grant access to an existing i2c bus on a linux device (Raspberry Pi and for example the ADS1x15)
https://github.com/tasoftch/php-i2c-extension
ads1015 ads1115 i2c-bus php-i2c raspberry-pi
Last synced: 12 days ago
JSON representation
A Zend Extension for php to grant access to an existing i2c bus on a linux device (Raspberry Pi and for example the ADS1x15)
- Host: GitHub
- URL: https://github.com/tasoftch/php-i2c-extension
- Owner: tasoftch
- Created: 2020-06-14T21:54:26.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2024-08-10T13:01:12.000Z (9 months ago)
- Last Synced: 2025-03-25T11:04:18.168Z (29 days ago)
- Topics: ads1015, ads1115, i2c-bus, php-i2c, raspberry-pi
- Language: C
- Homepage:
- Size: 41 KB
- Stars: 6
- Watchers: 1
- Forks: 3
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# The PHP i2c Extension
I've created this extension to get access to the i2c bus on my raspberry pi.
## Prerequisites
- The development module for your version of PHP, i.e, _php7.4-dev_, _php8.2-dev_.
## Installation
Change the configure file on line 5 (_php-config7.4_), to match your installed version of PHP.
Then, run the following commands to install the extension.```bin
$ cd ~
$ git clone https://github.com/tasoftch/php-i2c-extension.git
$ cd php-i2c-extension
$ phpize
$ ./configure --with-php-config=/usr/bin/php-config7.4 --enable-php-i2c
$ make
$ sudo make install
```Next, find the location of PHP's INI files on your computer by running the following command.
```bin
$ php --ini | grep -i "Configuration File.*Path"
```**Note:** If you're using Microsoft Windows, then run `php --ini` and look for the value of "Configuration File (php.ini) Path:".
You should see a directory path such as _/etc/php/7.4/cli_.
In that directory, create a new INI file named _20-i2c.ini_ with the following contents```ini
; configuration for php i2c module
extension=php_i2c
```Then, with the file created, test that the I2C extension is loaded by running the following command:
```bash
php --ri php_i2c
```You should see the following output:
```bash
php_i2c
Version => 0.8.0
```## Usage
The extension adds five function to the global scope:
1. ```i2c_open```
This opens the device bus.
1. ```i2c_select```
This selects an address of a connected chip.
1. ```i2c_read``` ```i2c_read_byte``` ```i2c_read_2_bytes``` ```i2c_read_3_bytes``` ```i2c_read_4_bytes```
Reads data from the i2c bus.
1. ```i2c_write``` ```i2c_write_byte``` ```i2c_write_2_bytes``` ```i2c_write_3_bytes``` ```i2c_write_4_bytes```
Writes data to the i2c bus
1. ```i2c_close```
Closes the bus.
### ExampleI've tested with a Raspberry Pi Model B 3+ and the Adafruit ADS1115 analog to digital converter.
It's default i2c address is 0x48.```php
write16(1, 0xC385);
// Wait for conversion completed
usleep(9000);
$value = $i2c->readRegister16(1);printf("Hex: 0x%04x - Int: %d - Float, converted: %f V\n",
$value, $value, (float)$value*4.096/32768.0);usleep(500000);
}
```