Created
May 22, 2015 19:59
-
-
Save kraftb/510be475b6c0f1d9bac0 to your computer and use it in GitHub Desktop.
Read out temperature from an LM75 via Arduino as I2C adapter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/php | |
<?php | |
define('CHAR_ERROR', chr(0x1B)); // <ESC> | |
define('CHAR_ESCAPE', chr(0x5C)); // '\' | |
define('CHAR_ESCAPED_ERROR', chr(0xB1)); | |
define('CHAR_ESCAPED_ESCAPE', chr(0xC5)); | |
define('LM75_ADDR', 0x48); | |
// exec('stty -F /dev/ttyACM0 clocal cread cs8 -parenb -cstopb raw'); | |
$fd = fopen('/dev/ttyACM0', 'r+b'); | |
sleep(1); | |
function readUnescape($fd, $length) { | |
$result = ''; | |
$escaped = FALSE; | |
while ($length) { | |
$data = fread($fd, 1); | |
if ($data === FALSE) { | |
throw new \Exception('Error reading device!'); | |
} | |
if ($data === CHAR_ERROR) { | |
throw new \Exception('Error signaled (1)'); | |
} | |
if ($escaped) { | |
switch ($data) { | |
case CHAR_ESCAPED_ERROR: | |
$result .= CHAR_ERROR; | |
break; | |
case CHAR_ESCAPED_ESCAPE: | |
$result .= CHAR_ESCAPE; | |
break; | |
default: | |
throw new \Exception('Invalid escaped character!'); | |
break; | |
} | |
$length--; | |
$escaped = FALSE; | |
} else { | |
switch ($data) { | |
case CHAR_ESCAPE: | |
$escaped = TRUE; | |
break; | |
default: | |
$result .= $data; | |
$length--; | |
break; | |
} | |
} | |
} | |
return $result; | |
} | |
function readReply($singleByte = FALSE) { | |
global $fd; | |
if ($singleByte) { | |
$num = 1; | |
} else { | |
$data = readUnescape($fd, 1); | |
$num = ord($data[0]); | |
if ($num === 0) { | |
return ''; | |
} | |
} | |
$data = readUnescape($fd, $num); | |
return $data; | |
} | |
function writeCommand($cmd, $data = '') { | |
global $fd; | |
fwrite($fd, substr($cmd, 0, 1), 1); | |
if (strlen($data)) { | |
fwrite($fd, $data, strlen($data)); | |
} | |
fflush($fd); | |
} | |
function identify() { | |
writeCommand('I'); | |
$res = readReply(); | |
echo $res."\n"; | |
if ($res !== 'Arduino I2C-to-USB 1.0') { | |
throw new \Exception('Invalid device'); | |
} | |
} | |
function temperatur() { | |
writeCommand('A', chr(LM75_ADDR)); | |
// writeCommand('a'); | |
// $res = readReply(TRUE); | |
// echo '0x'.dechex(ord($res))."\n"; | |
writeCommand('L', chr(0x02)); | |
// writeCommand('l'); | |
// $res = readReply(TRUE); | |
// echo '0x'.dechex(ord($res))."\n"; | |
writeCommand('R'); | |
$res = readReply(); | |
if (strlen($res) != 2) { | |
throw new \Exception('Invalid response from LM75!'); | |
} | |
$msb = ord(substr($res, 0, 1)); | |
$lsb = ord(substr($res, 1, 1)) & 0x80; | |
$lsb = $lsb >> 7; | |
$msb = ($msb << 1) | $lsb; | |
$temp = $msb * 0.5; | |
return $temp; | |
} | |
identify(); | |
while (TRUE) { | |
$temp = temperatur(); | |
printf("%.2f °C\n", $temp); | |
sleep(1); | |
} | |
fclose($fd); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment