QuICK
Posts: 8
Joined: Fri Sep 28, 2012 2:33 am

PHP & I2C

Fri Sep 28, 2012 2:49 am

Hello,
I've just bought myself a raspberry pi. I'm quite excited.
I've done assorted bits of embedded C programming & PHP, but never really got into linux (altho I can do a lot of the basics).

Anyway, what I'm wanting to do is use my raspi as a webserver to connect to complex I2C chips, so I can put a pretty (and intuitive) interface on them, rather than having an LCD with a couple of buttons to try and do some complex configurations.

So, how do I go about accessing the I2C? specifically from PHP. I would be looking to write & read to a slave's register (ie slave_addr, register_address), preferably multi-byte (but I guess I can cope if that isnt possible)

I've been searching & searching google for a solution.
The closest I have found is this:
http://www.robot-electronics.co.uk/foru ... ?f=4&t=351
but that only deals with writing

Code: Select all

Summary:
   $i2c  = fopen('/dev/i2c-0', 'w+');
   $wdata = chr($data);
   fseek ($i2c, $adr);
   fwrite($i2c, $wdata, 1);
   fclose ($i2c);
I've also read this: http://git.kernel.org/?p=linux/kernel/g ... -interface

which deals with the writing aspect, but wont translate to PHP, as php doesn't support ioctrl().
Has anyone achieved I2C coms from PHP?
or should I code a translator-type program that php can call (exec()) which handles the read/write?

it seems like such a neat way to create a nice interface for complex i2c chips. I guess the other way would be to create a hardware translater, and use the raspi UART to connect thru a microchip to the i2c device...

Anyone have any experience on this? any good reading?

QuICK
Posts: 8
Joined: Fri Sep 28, 2012 2:33 am

Re: PHP & I2C

Sat Sep 29, 2012 5:12 pm

Is it as easy as

Code: Select all

	$address = ($address & 0xFE) << 8 & $registry;
	$i2c = fopen("/dev/i2c-2", "w+b");
	fseek($i2c, $address)
	fwrite($i2c, $data, $length)
	fclose($i2c);
and

Code: Select all

	$address = ($address | 0x01) << 8 & $registry;
	$i2c = fopen("/dev/i2c-2", "w+b");
	fseek($i2c, $address)
	$rtn = fread($i2c, $length)
	fclose($i2c);
obviously with error catches & formatting

Gefrierbrand
Posts: 3
Joined: Sun Jun 09, 2013 1:00 pm

Re: PHP & I2C

Sun Jun 09, 2013 1:11 pm

Hi!

I may be a bit late to the party, but I struggle accessing I²C from withing PHP and can't find any useful information on it.
I searched on what seemed to be the entire internet an I thought someone here might be able to help.

Fist of all:
Your code does not work for me as PHP reports "PHP Warning: fseek(): stream does not support seeking "
I'm using the model B and I²C works if I use other tools outside php like i2cget.

Second of all:
could you explain why you shifted the bytes the way you did in your second post?

QuICK
Posts: 8
Joined: Fri Sep 28, 2012 2:33 am

Re: PHP & I2C

Mon Jun 10, 2013 2:09 pm

I never really got this to work.
I *think* it's because of the way i2c works
Raspi sends a message of address:registry in a 16 bit packet
So highest 8 bits are the chip address (7 bit) with the read/write bit set using the '& 0xFE' and '| 0x01'
And the lowest 8 bits are the registry to set ( I presume, although not tested, that you can then read/write consecutive bytes as per i2c standard).
FSeek was recommended for i2c (and peripherals) use in C, so I thought i'd try it in php, but as youve seen it doesnt work. After trying that, I couldn't find anything that replicated the behaviour of fseek... So it might have to be shell()

If you can make any progress on this, it would be amazing.
I kinda gave up on it after hitting this brick wall... Kinda gave up on raspi as well...
Maybe someone else has done the bit I couldn't get on the paste year.


Gefrierbrand
Posts: 3
Joined: Sun Jun 09, 2013 1:00 pm

Re: PHP & I2C

Mon Jun 10, 2013 3:39 pm

Well the linked code is just a fancy way to call the command-line tools, which is exactly what I'm trying to avoid because these don't give me the functionality I need (multibyte read/write).

Maybe I'll try to write my own C-Apps to get things done the way I need it and call them from a sane language like PHP :D
Anyway I'm going to keep you updated.

tbrianjones
Posts: 1
Joined: Wed Oct 23, 2013 7:29 am

Re: PHP & I2C

Wed Oct 23, 2013 6:02 pm

Here is a PHP I2C Library I started for use with Raspberry Pi. There is a core I2C class that can be extended to work with various peripherals. There are also some specific classes that extend the I2C class for some specific peripherals that are all mentioned in the readme.

https://github.com/tbrianjones/raspberry-pi-i2c-bus

medalist
Posts: 2
Joined: Thu Nov 20, 2014 6:08 pm

Re: PHP & I2C

Thu Nov 20, 2014 6:11 pm

Hey guys,

I wrote my own php extension so now I have some new functions in php, now the request time is some hundred times faster then the "exec" command.

If anybody is interrested, feel free to ask me ;)

BR
medalist

Gefrierbrand
Posts: 3
Joined: Sun Jun 09, 2013 1:00 pm

Re: PHP & I2C

Fri Nov 21, 2014 12:48 am

I'm definately interested, but I won't be able to test it for quite some time, as I'm out of the country.

forest
Posts: 1
Joined: Mon Feb 03, 2014 8:42 pm

Re: PHP & I2C

Wed Jan 07, 2015 4:46 pm

medalist wrote:Hey guys,

I wrote my own php extension so now I have some new functions in php, now the request time is some hundred times faster then the "exec" command.

If anybody is interrested, feel free to ask me ;)

BR
medalist
I am interested, so I am asking you ....

JuniorIII
Posts: 1
Joined: Tue Jan 13, 2015 11:49 pm

Re: PHP & I2C

Tue Jan 13, 2015 11:55 pm

medalist wrote:Hey guys,

I wrote my own php extension so now I have some new functions in php, now the request time is some hundred times faster then the "exec" command.

If anybody is interrested, feel free to ask me ;)

BR
medalist
I would be interested in that. Thanks

Torsten1
Posts: 4
Joined: Wed Mar 11, 2015 4:16 pm

Re: PHP & I2C

Wed Mar 11, 2015 4:41 pm

medalist wrote:Hey guys,

I wrote my own php extension so now I have some new functions in php, now the request time is some hundred times faster then the "exec" command.

If anybody is interrested, feel free to ask me ;)

BR
medalist
I would also be interested in the extension . Please contact me. Thanks

Erwinnijmeijer
Posts: 1
Joined: Fri Mar 13, 2015 7:06 am

Re: PHP & I2C

Fri Mar 13, 2015 7:09 am

Medialist, i'm also interested in you php i2c extension.

mikelin17
Posts: 1
Joined: Fri Apr 03, 2015 7:19 am

Re: PHP & I2C

Fri Apr 03, 2015 7:20 am

i'm also interested in you php i2c extension.

TK

didier9
Posts: 1
Joined: Thu May 28, 2015 1:49 am

Re: PHP & I2C

Thu May 28, 2015 1:51 am

I would like a copy of the i2c class as well. I did not see any of the messages asking for one being answered, I hope the thread is not completely dead...

treii28
Posts: 77
Joined: Fri May 10, 2013 4:52 pm

Re: PHP & I2C

Tue Apr 26, 2016 4:49 pm

Also interested. Please contact me.

SteveGrant
Posts: 1
Joined: Fri May 06, 2016 10:59 pm

Re: PHP & I2C

Fri May 06, 2016 11:01 pm

Im very interested in your PHP extension as well; I'll donate to your cause!!

VBaczynski
Posts: 2
Joined: Mon Aug 22, 2016 6:49 pm

Re: PHP & I2C

Mon Aug 22, 2016 6:56 pm

I wrote a library (https://github.com/VBaczynski/phpi2c) and script for reading data from pressure and temperature sensor BMP085 (https://github.com/VBaczynski/bmp-sensor-reader), it works pretty fast:

Code: Select all

[email protected]:~# time php-cli bmpreader.php
Temperature: 27.7 C. Pressure: 752.09 mm Hg. Absolute altitude: 112m. Pressure at sea level: 762.25
real    0m 0.59s
user    0m 0.22s
sys     0m 0.36s
Maybe it will be useful for someone.

petekaner
Posts: 3
Joined: Sun Feb 24, 2013 12:09 pm

Re: PHP & I2C

Mon Jan 30, 2017 3:47 pm

Hello,

I don't know if its too late but I'd be very interested in this extension as well!

User avatar
[email protected]
Posts: 1979
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: PHP & I2C

Tue Jan 31, 2017 4:44 pm

If it's just the bmp085/bmp180 you're after and you have a recent build of wiringPi installed, (2.36+) then:

Code: Select all

gpio -x bmp180:100 aread 100
will give you the temperature in °C times 10. Reading 101 gives the pressure in mB times 10. It's reasonably quick:

Code: Select all

[email protected]:~/weather$ time gpio -x bmp180:100 aread 100
258

real	0m0.044s
user	0m0.000s
sys	0m0.010s
[email protected]:~/weather$ time gpio -x bmp180:100 aread 101
10039

real	0m0.045s
user	0m0.010s
sys	0m0.000s/
In a PHP program, you can call it with the backticks operator:

Code: Select all

  $temperature = `gpio -x bmp180:100 aread 100` / 10 ;
as far as I know the bmp180 is just a newer version of the bmp085, so it ought to work fine.

Personally, I don't think I'd do much I2C directly from PHP - I'd be writing a little shim program in C to return the result like the gpio program does above, then call it from a PHP program - only because C has better bit twiddling and arithmetic needed for some I2C devices.

-Gordon
--
Gordons projects: https://projects.drogon.net/

medalist
Posts: 2
Joined: Thu Nov 20, 2014 6:08 pm

Re: PHP & I2C

Wed Jan 17, 2018 5:02 pm

Hey guys,

I'm so sorry for not answering your posts. It has been some years and I'm not sure if anyone is still interested. I have to look for the extension in my old HDD. If I find it, I will post it here for sure.

Greetings
meda

Return to “General programming discussion”

Who is online

Users browsing this forum: No registered users and 3 guests