Hirdetés

Új hozzászólás Aktív témák

  • D@reeo

    aktív tag

    Üdv
    Kezdek belebolondulni ebbe a hőmérős projectbe.
    Elméletileg minden a helyén van, de az adatbázisba mégse menti le mért értékeket.
    pollSensors.py (itt a 38. sorban kell a nonce = nonce.encode('utf-8'), különben hibát dob, de lehet, hogy pont e miatt nem menti le az adatbázisba)

    import requests
    import hashlib
    import time

    #Dont forget to fill in PASSWORD and URL TO saveTemp (twice) in this file

    sensorids = ["28-00000509be8b", "28-00000535993e"]
    avgtemperatures = []
    for sensor in range(len(sensorids)):
    temperatures = []
    for polltime in range(0,3):
    text = '';
    while text.split("\n")[0].find("YES") == -1:
    # Open the file that we viewed earlier so that python can see what is in it. Replace the serial number as before.
    tfile = open("/sys/bus/w1/devices/"+ sensorids[sensor] +"/w1_slave")
    # Read all of the text in the file.
    text = tfile.read()
    # Close the file now that the text has been read.
    tfile.close()
    time.sleep(1)

    # Split the text with new lines (\n) and select the second line.
    secondline = text.split("\n")[1]
    # Split the line into words, referring to the spaces, and select the 10th word (counting from 0).
    temperaturedata = secondline.split(" ")[9]
    # The first two characters are "t=", so get rid of those and convert the temperature from a string to a number.
    temperature = float(temperaturedata[2:])
    # Put the decimal point in the right place and display it.
    temperatures.append(temperature / 1000)

    avgtemperatures.append(sum(temperatures) / float(len(temperatures)))

    print avgtemperatures[0]
    print avgtemperatures[1]

    session = requests.Session()
    nonce = session.get(url='http://localhost/saveTemp.php?step=nonce').text
    nonce = nonce.encode('utf-8')
    response = hashlib.sha256(nonce + 'root' + str(avgtemperatures[0]) + str(avgtemperatures[1])).hexdigest()

    post_data = {'response':response, 'temp1':avgtemperatures[0], 'temp2': avgtemperatures[1]}

    post_request = session.post(url='http://localhost/saveTemp.php', data=post_data)

    if post_request.status_code == 200 :
    print post_request.text

    saveThemp.php
    <?php

    // set the defines here, and the mysqli info on line 24
    // make sure you have an temps table with floats for temp1 and temp2 and created_at (timestamp)
    // make sure you have an alerts table with floats for avgtemp1 and avgtemp2, open (boolean, default: true) and created_at (timestamp)

    define("PASSWORD","root");
    session_start();
    $mysqli = initDB();

    if($_GET['step'] == 'nonce') {
    getNonce();
    } else if (isset($_POST['response']) && isset($_POST['temp1']) && isset($_POST['temp2'])) {
    checkAuthenticationResponce();
    processEntry($mysqli);
    }

    $mysqli->close();

    function initDB() {
    $mysqli = new mysqli("localhost", "root", "root", "temp");
    /* check connection */
    if ($mysqli->connect_errno) {
    header("HTTP/1.0 500 Internal Server Error");
    exit();
    }
    return $mysqli;
    }

    function checkAuthenticationResponce() {
    if(!isset($_SESSION['tempNonce']) || hash('sha256', $_SESSION['tempNonce'] . PASSWORD . $_POST['temp1'] . $_POST['temp2']) != $_POST['response']) {
    header("HTTP/1.0 401 Authorization Required");
    exit;
    } else {
    unset($_SESSION['tempNonce']);
    }
    }

    function getNonce() {
    $_SESSION['tempNonce'] = hash('sha256', '1321421412412452354235325' . time());
    echo $_SESSION['tempNonce'];
    }


    function processEntry($mysqli) {

    $temp1 = floatval($_POST['temp1']);
    $temp2 = floatval($_POST['temp2']);

    $stmt = $mysqli->prepare("INSERT INTO temps (temp1, temp2) VALUES(?,?)");
    $stmt->bind_param('dd', $temp1, $temp2);

    if ($stmt->execute() === true) {
    echo "added";
    } else {
    header("HTTP/1.0 500 Internal Server Error");
    }
    }

    ?>

    Ha kézzel viszek be értékeket, akkor azokat kirajzolja a grafilonra. Ha kézzel futtatom a var/www/pollSensors.py-t, akkor kiírja a 2 értéket, de az adatbázisba nem menti le.
    mysql user: localhost,root, root

    Adatbázis szerkezet:
    -- phpMyAdmin SQL Dump
    -- version 3.4.11.1deb2+deb7u1
    -- http://www.phpmyadmin.net
    --
    -- Hoszt: localhost
    -- Létrehozás ideje: 2015. febr. 08. 19:38
    -- Szerver verzió: 5.5.41
    -- PHP verzió: 5.4.36-0+deb7u3

    SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
    SET time_zone = "+00:00";


    /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
    /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
    /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
    /*!40101 SET NAMES utf8 */;

    --
    -- Adatbázis: `temp`
    --

    -- --------------------------------------------------------

    --
    -- Tábla szerkezet: `temps`
    --

    CREATE TABLE IF NOT EXISTS `temps` (
    `temp1` float NOT NULL,
    `temp2` float NOT NULL,
    `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

    --
    -- A tábla adatainak kiíratása `temps`
    --

    INSERT INTO `temps` (`temp1`, `temp2`, `created_at`) VALUES
    (21, 11, '2015-02-08 12:59:35'),
    (41, 61, '2015-02-08 12:59:51');

    /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
    /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
    /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

    Mi a francnak nem menti le az adatbázisba az értékeket? Köszi

Új hozzászólás Aktív témák