summaryrefslogtreecommitdiff
path: root/localwebsite/classes/Si7021dClient.php
blob: b9764482f9bbaeb3df8f53631eace5b3a74a1bd4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php

class Si7021dClient extends MySimpleSocketClient {

    public string $name;
    public float $temp;
    public float $humidity;

    /**
     * @throws Exception
     */
    public function __construct(string $host, int $port, string $name) {
        parent::__construct($host, $port);
        $this->name = $name;

        socket_set_timeout($this->sock, 3);
    }

    public function readSensor(): void {
        $this->send('read');

        $data = jsonDecode($this->recv());

        $temp = round((float)$data['temp'], 3);
        $hum = round((float)$data['humidity'], 3);

        $this->temp = $temp;
        $this->humidity = $hum;
    }

}