Permalink
Browse files

Merge pull request #4 from sergejey/alpha

Alpha
  • Loading branch information...
sergejey committed Jan 28, 2019
2 parents 3ceff4d + b4b4fbb commit 9f4c879deac5c2ef959d606b4dd7960a68707ab8
@@ -0,0 +1,145 @@
<?php
if (!defined('HIGH')) define("HIGH", 1);
if (!defined('LOW')) define("LOW", 0);
class i2c_com extends stdClass
{
public $scl;
public $sda;
public $version;
public $url;
function __construct($url, $scl, $sda, $version = 1)
{
$this->scl = $scl;
$this->sda = $sda;
$this->url = $url;
$this->version = $version;
}
function i2c_request($data) {
$url = $this->url . $data;
//dprint($url,false);
return file_get_contents($this->url . $data);
}
function i2c_init()
{
$this->i2c_dir($this->scl, "OUT");
$this->i2c_dir($this->sda, "OUT");
$this->i2c_request("cmd=" . $this->scl . ":" . HIGH . ";" . $this->sda . ":" . HIGH);
}
function i2c_start()
{
$this->i2c_request("cmd=" . $this->sda . ":" . LOW . ";" . $this->scl . ":" . LOW);
}
function i2c_stop()
{
//file_get_contents(MD."cmd=".$this->scl.":".HIGH);
//file_get_contents(MD."cmd=".$this->scl.":".LOW);
$this->i2c_request("cmd=" . $this->sda . ":" . LOW . ";" . $this->scl . ":" . HIGH . ";" . $this->sda . ":" . HIGH);
}
function i2c_dir($port, $dir)
{
if ($dir == "OUT")
$dir = 1;
else
$dir = 0;
$this->i2c_request("pt=$port&dir=$dir");
}
function i2c_send($data)
{
if ($this->version == 2) {
//if ( $this->version == 2 )
$this->i2c_request("pt=" . $this->sda . "&i2c=" . hexdec($data) . "&scl=" . $this->scl . ":1;" . $this->scl . ":0;");
//file_get_contents(MD."pt=11&i2c=".hexdec($data)."&scl=10:1;10:0;");
} else {
$data_bin = decbin(hexdec($data));
$len = strlen($data_bin);
for ($i = 8; $i > $len; $i--)
$data_bin = "0" . $data_bin;
$old_bit = 3;
$cmd = "";
$cnt = 0;
for ($i = 0; $i < 8; $i++) {
// Вариант 1
//file_get_contents($this->url."cmd=".$this->sda.":".$data_bin[$i]);
//file_get_contents($this->url."cmd=".$this->scl.":".HIGH);
//file_get_contents($this->url."cmd=".$this->scl.":".LOW);
//file_get_contents($this->url."cmd=".$this->sda.":".LOW);
// Вариант 2
$this->i2c_request("cmd=" . $this->sda . ":" . $data_bin[$i] . ";" . $this->scl . ":" . HIGH . ";" . $this->scl . ":" . LOW . ";" . $this->sda . ":" . LOW);
/*
// Вариант 3 - отправляем сразу 2 бита
$cmd .= $this->sda.":".$data_bin[$i].";".$this->scl.":".HIGH.";".$this->scl.":".LOW.";".$this->sda.":".LOW.";";
$cnt++;
if ( $cnt == 2 )
{
file_get_contents($this->url."cmd=$cmd");
$cnt = 0;
$cmd = "";
}
*/
}
// Вариант 1
//i2c_dir($this->sda, "IN");
//file_get_contents($this->url."cmd=".$this->scl.":".HIGH);
//$ack = file_get_contents(MD."pt=".$this->sda."&cmd=get")."\n";
//file_get_contents($this->url."cmd=".$this->scl.":".LOW);
//echo $ack;
// Вариант 2
$this->i2c_request("pt=" . $this->sda . "&dir=0&cmd=" . $this->scl . ":" . HIGH . ";" . $this->scl . ":" . LOW);
$ack = $this->i2c_request("pt=" . $this->sda . "&cmd=get&dir=1");
return $ack;
}
}
function i2c_read($nack = 0)
{
$data_bits = "";
$bit = "";
$this->i2c_request("pt=" . $this->sda . "&dir=0&cmd=" . $this->scl . ":" . LOW);
//file_get_contents(MD."cmd=".$this->scl.":".LOW);
for ($i = 0; $i < 8; $i++) {
$this->i2c_request("cmd=" . $this->scl . ":" . HIGH);
$bit = $this->i2c_request("pt=" . $this->sda . "&cmd=get");
if ($bit == "ON")
$data_bits .= "0";
else
$data_bits .= "1";
$this->i2c_request("cmd=" . $this->scl . ":" . LOW);
}
$this->i2c_dir($this->sda, "OUT");
$this->i2c_request("cmd=" . $this->scl . ":" . HIGH);
if ($nack == 1)
$this->i2c_request("cmd=" . $this->sda . ":" . HIGH);
//file_get_contents(MD."cmd=".$this->scl.":".LOW);
//i2c_dir($this->sda, "IN");
$this->i2c_request("pt=" . $this->sda . "&dir=0&cmd=" . $this->scl . ":" . LOW);
//return dechex(bindec($data_bits))." - $data_bits\n";
return dechex(bindec($data_bits));
}
}
@@ -0,0 +1,21 @@
<?php
function hexdecs($hex)
{
// ignore non hex characters
$hex = preg_replace('/[^0-9A-Fa-f]/', '', $hex);
// converted decimal value:
$dec = hexdec($hex);
// maximum decimal value based on length of hex + 1:
// number of bits in hex number is 8 bits for each 2 hex -> max = 2^n
// use 'pow(2,n)' since '1 << n' is only for integers and therefore limited to integer size.
$max = pow(2, 4 * (strlen($hex) + (strlen($hex) % 2)));
// complement = maximum - converted hex:
$_dec = $max - $dec;
// if dec value is larger than its complement we have a negative value (first bit is set)
return $dec >= $_dec ? -$_dec : $dec;
}
@@ -0,0 +1,94 @@
<?php
function check_htu21d_crc($raw_data, $crc)
{
$remainder = $raw_data << 8;
//$remainder |= $crc; //Add on the check value
$divsor = 0x988000;
for ( $i = 0 ; $i < 16 ; $i++)
{
if ( $remainder & 1<<(23 - $i) )
$remainder ^= $divsor;
$divsor >>= 1;
}
//echo dechex($remainder)."\n";
return dechex($remainder);
}
function get_htu21d_temperature($i2c)
{
$i2c->i2c_init();
$i2c->i2c_start();
$i2c->i2c_send("80");
$i2c->i2c_send("E3");
$i2c->i2c_stop();
$i2c->i2c_start();
$i2c->i2c_send("81");
$msb = $i2c->i2c_read();
$lsb = $i2c->i2c_read();
$crc = $i2c->i2c_read(1);
$i2c->i2c_stop();
if ( strlen($msb) == 1 )
$msb = "0$msb";
if ( strlen($lsb) == 1 )
$lsb = "0$lsb";
//echo $msb.".".$lsb.".$crc\n";
$msb = hexdec($msb);
$lsb = hexdec($lsb);
//$raw_temp = ($msb << 8) | ($lsb & 0b11111100);
$raw_temp = ($msb << 8) | $lsb;
if ( check_htu21d_crc($raw_temp, $crc) != $crc )
return "CRC error - $crc";
else
{
$raw_temp &= 0xFFFC;
$temperature = number_format(round(-46.85 + 175.72 * ($raw_temp / 65536), 2), 2);
return $temperature;
}
}
function get_htu21d_humidity($i2c)
{
$i2c->i2c_init();
$i2c->i2c_start();
$i2c->i2c_send("80");
$i2c->i2c_send("E5");
$i2c->i2c_stop();
$i2c->i2c_start();
$i2c->i2c_send("81");
$msb = $i2c->i2c_read();
$lsb = $i2c->i2c_read();
$crc = $i2c->i2c_read(1);
$i2c->i2c_stop();
//$msb = "7C";
//$lsb = "80";
if ( strlen($msb) == 1 )
$msb = "0$msb";
if ( strlen($lsb) == 1 )
$lsb = "0$lsb";
//echo $msb.".".$lsb.".$crc\n";
$msb = hexdec($msb);
$lsb = hexdec($lsb);
$raw_hum = ($msb << 8) | $lsb;
if ( check_htu21d_crc($raw_hum, $crc) != $crc )
return "CRC error - $crc";
else
{
$raw_hum &= 0xFFFC;
$humidity = number_format(round(-6 + (125 * ($raw_hum / 65536)), 2), 2);
if ( $humidity > 100 )
$humidity = 100;
return $humidity;
}
}
@@ -1,4 +1,4 @@
<?
<?php
/*
* Copyright (c) 2016, Andrey_B
* http://ab-log.ru
@@ -10,8 +10,8 @@
Использует драйвер HTU21D и библиотеку I2C-PHP
*/
define("SCL", "8");
define("SDA", "9");
define("SCL", "30");
define("SDA", "31");
define("MD", "http://192.168.0.14/sec/?");
// Вариант реализации I2C: 1 - полностью программный; 2 - частично аппаратный (прошивка 3.43beta1 и выше)
@@ -1,4 +1,4 @@
<?
<?php
/*
* Copyright (c) 2016, Andrey_B
* http://ab-log.ru
@@ -1,4 +1,4 @@
<?
<?php
/*
* Copyright (c) 2016, Andrey_B
* http://ab-log.ru
@@ -716,6 +716,7 @@ function dbInstall($data)
megaddevices: IP varchar(255) NOT NULL DEFAULT ''
megaddevices: PASSWORD varchar(255) NOT NULL DEFAULT ''
megaddevices: ADDRESS int(3) NOT NULL DEFAULT '0'
megaddevices: I2C_VERSION int(1) NOT NULL DEFAULT '0'
megaddevices: UPDATE_PERIOD int(10) NOT NULL DEFAULT '0'
megaddevices: NEXT_UPDATE datetime
megaddevices: CONFIG text
@@ -44,6 +44,8 @@
global $update_period;
$rec['UPDATE_PERIOD'] = (int)$update_period;
$rec['I2C_VERSION']=gr('i2c_version','int');
$rec['NEXT_UPDATE'] = date('Y-m-d H:i:s', time() + $rec['UPDATE_PERIOD']);
}
@@ -220,7 +222,7 @@
$this->readValues($rec['ID']);
}
} else {
$properties = SQLSelect("SELECT * FROM megadproperties WHERE DEVICE_ID='" . $rec['ID'] . "' ORDER BY NUM, COMMAND_INDEX, TYPE");
$properties = SQLSelect("SELECT * FROM megadproperties WHERE DEVICE_ID='" . $rec['ID'] . "' ORDER BY NUM, COMMAND_INDEX, COMMAND");
$total = count($properties);
for ($i = 0; $i < $total; $i++) {
if ($properties[$i]['LINKED_OBJECT']!='') {
@@ -22,8 +22,7 @@
$mode=(int)$prop['MODE'];
$cmd=(int)$prop['COMMAND'];
*/
$current_prop=SQLSelectOne("SELECT * FROM megadproperties WHERE DEVICE_ID='".$record['ID']."' AND NUM='".$i."'");
$current_prop=SQLSelectOne("SELECT * FROM megadproperties WHERE DEVICE_ID='".$record['ID']."' AND NUM='".$i."' AND COMMAND_INDEX=0");
if ($states[$i] == 'ON') {
$cmd = array('NUM' => $i, 'VALUE' => 1, 'COMMAND' => 'output');
$commands[] = $cmd;
@@ -80,6 +79,29 @@
}
}
$i2c_properties=SQLSelect("SELECT * FROM megadproperties WHERE DEVICE_ID='".$record['ID']."' AND COMMAND LIKE 'i2c%' ORDER BY NUM");
if ($i2c_properties[0]['ID']) {
include_once(DIR_MODULES.$this->name.'/libs/i2c_com.class.php');
include_once(DIR_MODULES.$this->name.'/libs/i2c_functions.inc.php');
foreach($i2c_properties as $property) {
$scl=$property['NUM'];
$sda=$property['ADD_NUM'];
$i2c_com=new i2c_com('http://'.$record['IP'].'/'.$record['PASSWORD'].'/?',$scl,$sda,$record['I2C_VERSION']);
if ($property['COMMAND']=='i2c_htu21d') {
include_once(DIR_MODULES.$this->name.'/libs/i2c_htu21d.inc.php');
$temperature = get_htu21d_temperature($i2c_com);
if (is_numeric($temperature)) {
$commands[] = array('NUM' => $property['NUM'], 'COMMAND' => 'temperature','INDEX'=>1, 'VALUE' => $temperature);
$humidity = get_htu21d_humidity($i2c_com);
if (is_numeric($humidity)) {
$hum_compensated = round($humidity + (25 - $temperature) * -0.15,2);
$commands[] = array('NUM' => $property['NUM'], 'COMMAND' => 'humidity','INDEX'=>1, 'VALUE' => $hum_compensated);
}
}
}
}
}
if ($_GET['debug']) {
dprint($url."\n".$stateData,false);
dprint($commands);
@@ -74,6 +74,18 @@
<div class="col-lg-4"><input type="text" class="form-control" name="password" value="[#PASSWORD#]" ></div>
</div>

<div class="form-group">
<label class="col-lg-3 control-label">
I2C:
</label>
<div class="col-lg-4">
<select name="i2c_version" class="form-control">
<option value="1"[#if I2C_VERSION="1"#] selected[#endif#]>V1 (software)</option>
<option value="2"[#if I2C_VERSION="2"#] selected[#endif#]>V2</option>
</select>
</div>
</div>

<div class="form-group">
<label class="col-lg-3 control-label">
<#LANG_POLLING_PERIOD#>, <#LANG_SECONDS#>:
@@ -14,7 +14,7 @@

<div class="form-group">
<label class="col-lg-3 control-label"[#if ERR_PORT#] style="color:red;font-weight:bold"[#endif#]>
<#LANG_PORT#>[#if I2C="1"#] SLC[#endif#]:
<#LANG_PORT#>[#if I2C="1"#] SCL[#endif#]:
(*)
</label>
<div class="col-lg-4"><input type="text" class="form-control" name="num" value="[#PROPERTY_NUM#]" required></div>
@@ -67,8 +67,8 @@
<option value="humidity"[#if PROPERTY_COMMAND="humidity"#] selected[#endif#]>Humidity</option>
<option value="wiegand"[#if PROPERTY_COMMAND="wiegand"#] selected[#endif#]>Wiegand</option>
<option value="ibutton"[#if PROPERTY_COMMAND="ibutton"#] selected[#endif#]>IButton</option>
<option value="i2c"[#if PROPERTY_COMMAND="i2c"#] selected[#endif#]>I2C (coming soon)</option>
<option value="i2c_htu21d"[#if PROPERTY_COMMAND="i2c_htu21d"#] selected[#endif#]>I2C htu21d (coming soon)</option>
<option value="i2c"[#if PROPERTY_COMMAND="i2c"#] selected[#endif#]>I2C</option>
<option value="i2c_htu21d"[#if PROPERTY_COMMAND="i2c_htu21d"#] selected[#endif#]>I2C htu21d</option>
<option value="i2c_bmp180"[#if PROPERTY_COMMAND="i2c_bmp180"#] selected[#endif#]>I2C bmp180 (coming soon)</option>
<option value="i2c_tsl2591"[#if PROPERTY_COMMAND="i2c_tsl2591"#] selected[#endif#]>I2C tsl2591 (coming soon)</option>
<option value="i2c_ssd1306"[#if PROPERTY_COMMAND="i2c_ssd1306"#] selected[#endif#]>I2C ssd1306 (coming soon)</option>

0 comments on commit 9f4c879

Please sign in to comment.