Giter VIP home page Giter VIP logo

php-i2c-extension's Introduction

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.

$ 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.

$ 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

; 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:

php --ri php_i2c

You should see the following output:

php_i2c

Version => 0.8.0

Usage

The extension adds five function to the global scope:

  1. i2c_open
    This opens the device bus.
  2. i2c_select
    This selects an address of a connected chip.
  3. i2c_read i2c_read_byte i2c_read_2_bytes i2c_read_3_bytes i2c_read_4_bytes
    Reads data from the i2c bus.
  4. i2c_write i2c_write_byte i2c_write_2_bytes i2c_write_3_bytes i2c_write_4_bytes
    Writes data to the i2c bus
  5. i2c_close
    Closes the bus.

Example

I'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
$fd = i2c_open("/dev/i2c-1");
i2c_select($fd, 0x48);

for($e=0;$e<30;$e++) {
    // Read for 30 times the value between channel AIN_0 and GND, 4.096 V, 128 samples/s
    i2c_write($fd, 1, [0xc3, 0x85]);
    // or
    // i2c_write_2_bytes( 0x01c385 );
    
    // Wait for conversion completed
    usleep(9000);
    i2c_write($fd, 0);
    $data = i2c_read($fd, 2);

    $value = $data[0]*256 + $data[1];
    printf("Hex: 0x%02x%02x - Int: %d - Float, converted: %f V\n",
        $data[0], $data[1], $value, (float)$value*4.096/32768.0);

    usleep(500000);
}

i2c_close($fd);

Usage PHP

The package also contains a php wrapper class for i2c.

$ composer require tasoft/php-i2c-extension

Please note that the composer installation does not compile the extension! For compilation use the installation guide described before.

Now the same example can be rewritten as:

<?php
use TASoft\Bus\I2C;

$i2c = new I2C(0x48, 1);
for($e=0;$e<30;$e++) {
    // Read for 30 times the value between channel AIN_0 and GND, 4.096 V, 128 samples/s
    $i2c->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);
}

php-i2c-extension's People

Contributors

settermjd avatar tasoftch avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

php-i2c-extension's Issues

php 8.1

Hi
When I run make I get a lot of errors and it will not build.
Should this work on php 8.1?
And yes I did install php8.1-dev

Installation problem, unrecognized option --enable-i2c-php

Thanks for your great Library!

Unfortunately I run into some trouble while trying to install the library,

when I enter the command "./configure --enable-i2c-php" a long list of checks and stuff appears but it also writes

configure: WARNING: unrecognized options: --enable-i2c-php

What causes this problem, what could I do?

Using PHP 7.1 and Raspbian 10 (also tried with PHP7.3)

Edit: it works on PHP 7.3 even with that warning

i2c_open("/dev/i2c-0") returns false

I installed this software normally and set php.ini. In addition, I have given permissions on /dev/i2c-0.
However, i2c_open("/dev/i2c-0") returns false. Please tell me what you can think of.
If you change i2c.c as follows, it will succeed.

// int fd = open(devPath, O_RDWR);
int fd = open("/dev/i2c-0", O_RDWR);

Installation

Hello,

Pi 3B+, new install of PHP.
Following the install, when I try to run phpize, it get:
bash: phpize: command not found

And ./configure isn't a file listed.

Any suggestions?

Installation

Hello, I am able to get the extension installed and use it to communicate with a DAC module, however, it only works when using the VS Code built-in web server (Debian 10). When I try to use it with Apache, the device fails to load: "Uncaught Error: Call to undefined function i2c_open()..." I noticed that the debugging process is runnint as root whereas the Apache server is running as www-data. I tried changing the owner of /dev/i2c_1 to www-data:www-/*data, but still no joy.

Also, I am not able to install the PHP library with composer. It fails with: "Failed opening required 'tasoft/php-i2c-extension' (include_path='.:/usr/share/php')

I'm not sure how /usr/share/php is involved with this...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.