Kod: Markera allt
cat /mnt/1wire/10.F6877C010800/temperature
oh nej men, det behövs inte. PHP är server side. Du skapar nya/uppdaterade grafer/inmatningar till mysql genom schemalagt cron job t.ex var 5:e min.Bergalot skrev:Patrik, om jag skall göra ett script i php, som är webbaserat. Förutsätter inte detta att jag måste ha igång en webläsare för kontinuerlig loggning?
Kod: Markera allt
#!/bin/sh
#
# Reads temperature and stores them in a mysql database
#
# Database layout
# CREATE TABLE `owfs`.`owfs` (
# `sensor` VARCHAR( 15 ) NOT NULL ,
# `timestamp` DATETIME NOT NULL ,
# `value` VARCHAR( 10 ) NOT NULL
# ) ENGINE = InnoDB;
#
# The user needs INSERT privileges on the owfs database
# GRANT INSERT ON `owfs`. * TO 'owfs'@'localhost' IDENTIFIED BY 'secretpassword';
#
OWFS="/mnt/owfs/uncached"
LOG="/tmp/owfslog"
MYSQL_USER="owfs"
MYSQL_PASS="secretpassword"
MYSQL_DB="owfs"
MYSQL_TABLE="owfs"
#############################################################
# Nothing more to change below
CAT=$(which cat)
DATE=$(which date)
EGREP=$(which egrep)
LS=$(which ls)
MYSQL=$(which mysql)
# Functions
log()
{
msg=$1
echo "`date` - ${msg}" >> ${LOG}
}
for a in `$LS /mnt/owfs/uncached/|$EGREP ^[[:digit:]]`
do
if [ -f ${OWFS}/${a}/temperature ] ;
then
temp=$($CAT ${OWFS}/${a}/temperature)
echo "
INSERT INTO \`${MYSQL_DB}\`.\`${MYSQL_TABLE}\` (
\`sensor\` ,
\`timestamp\` ,
\`value\`
)
VALUES (
'${a}', '`$DATE +"%Y-%m-%d %T"`', '${temp}'
);" | $MYSQL -u ${MYSQL_USER} --password=${MYSQL_PASS} ${MYSQL_TABLE}
else
log "$a is not a temperature sensor, skipping"
fi
done
Kod: Markera allt
<?php
include_once ("/var/www/mysql_settings.php");
# Path to owfs
$owfs_path = "/mnt/1wire";
# Connect to the database and select a db
mysql_connect ($mysqlHost, $mysqlUsername, $mysqlPassword);
mysql_select_db ($mysqlDatabase);
# Get serial from the database
$sensor_q = "SELECT serial FROM tempsensors ORDER by serial";
$sensor_r = @mysql_query ($sensor_q);
if (@mysql_num_rows ($sensor_r) > 0)
{
while ($_sensor = @mysql_fetch_object ($sensor_r))
{
$temp = exec ("/bin/cat $owfs_path/$_sensor->serial/temperature");
$date = date ("Y-m-d H:i:s");
$save_temp_q = "INSERT INTO temps (sensor_serial, temp_c, date)";
$save_temp_q .= " VALUES ('$_sensor->serial', '$temp', '$date')";
$save_temp_r = @mysql_query ($save_temp_q);
}
} else {
echo "No sensors found!<br />";
echo "Cowardly bailing out...<p>";
echo @mysql_error();
}
?>
Kod: Markera allt
*/5 * * * * /usr/bin/php /Library/WebServer/SSLDocuments/read_temp_to_mysql.php