Skip to content

Instantly share code, notes, and snippets.

@unreal4u
Created August 28, 2018 21:00
Show Gist options
  • Save unreal4u/8104d8680056656123a84b0c68a88b67 to your computer and use it in GitHub Desktop.
Save unreal4u/8104d8680056656123a84b0c68a88b67 to your computer and use it in GitHub Desktop.
Reads out a DHT22 sensor and publishes the data to a MQTT broker
# Raspberry Pi Tips & Tricks - https://raspberrytips.nl
import Adafruit_DHT
humidity, temperature = Adafruit_DHT.read_retry(Adafruit_DHT.AM2302, 2)
#print temperature
humidity = round(humidity, 2)
temperature = round(temperature, 2)
if humidity is not None and temperature is not None:
print '{0:0.1f}|{1:0.1f}'.format(temperature, humidity)
else:
print ''
<?php
use Monolog\Handler\RotatingFileHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use unreal4u\MQTT\Application\Message;
use unreal4u\MQTT\Client;
use unreal4u\MQTT\Protocol\Connect;
use unreal4u\MQTT\Protocol\Connect\Parameters;
use unreal4u\MQTT\Protocol\Publish;
chdir(__DIR__ . '/../');
include 'vendor/autoload.php';
$uniqId = substr(uniqid('fbs' . mt_rand(0, 255), true), -8);
$logger = new Logger('fanBaseroomSensors');
$logger->pushHandler(new StreamHandler('php://stdout', Logger::DEBUG));
$logger->pushHandler(new RotatingFileHandler('logs/fanBaseroomSensors.log', 14, Logger::DEBUG));
$logger->info('Program start, reading python output', ['uniqId' => $uniqId]);
$parts = explode('|', shell_exec('/usr/bin/python readout.py'));
$temperature = (float)$parts[0];
$humidity = (float)trim($parts[1]);
$logger->debug('Readout ready', ['temperature' => $temperature, 'humidity' => $humidity]);
if ($humidity < 100) {
$connectionParameters = new Parameters('fbs-' . $uniqId, '192.168.1.1');
$connectionParameters->setUsername('xxxx');
$connectionParameters->setPassword('xxxx');
$connect = new Connect();
$connect->setConnectionParameters($connectionParameters);
$client = new Client();
$client->sendData($connect);
if ($client->isConnected()) {
$logger->debug('Connected to broker, constructing and sending message');
$temperatureMessage = new Message();
$temperatureMessage->setTopicName('sensors/temperature/kelder');
$temperatureMessage->setPayload((float)$parts[0]);
$temperatureMessage->setRetainFlag(true);
$humidityMessage = new Message();
$humidityMessage->setTopicName('sensors/humidity/kelder');
$humidityMessage->setPayload((float)trim($parts[1]));
$humidityMessage->setRetainFlag(true);
$publish = new Publish();
$publish->setMessage($temperatureMessage);
$client->sendData($publish);
$logger->debug('Published', ['temperature' => (float)$parts[0]]);
$publish->setMessage($humidityMessage);
$client->sendData($publish);
$logger->debug('Published', ['humidity' => (float)trim($parts[1])]);
} else {
$logger->critical('Could not connect to MQTT broker!');
}
} else {
$logger->warning('Values fall outside scope of acceptable values');
}
$logger->info('Program stop');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment