1. Home
  2. All Topics
  3. Software Topics
  4. How exactly does the integration into FHEM work?

How exactly does the integration into FHEM work?

The air-Q can be integrated into FHEM in two different ways: Either you use a configuration file to send the measurement data to the MQTT server integrated into FHEM (if it has been activated), or you can retrieve the data using a PHP script and decrypt it afterward. Both options are described below, making it easy for anyone already using FHEM as a smart home system to perform this.

Retrieve data via PHP script (no MQTT required)

  • Create the file getdata.php in any folder.
  • Fill the file as follows using Notepad++ or a similar program (e.g., VSCode), and adjust lines 2 and 3 according to your settings on the air-Q:
<?php
$password = "Ihr Gerätekennwort"; // config: set your air-Q password here
$airq = "http://IP-Adresse des air-Qs/data";  // config: use your own ip-adress

header('Access-Control-Allow-Origin: *'); 

function decrypt($msgb64,$password)
{
    $airqpass = $password;
	if (strlen($airqpass) < 32) {
		for ($i = strlen($airqpass); $i < 32; $i++) {
			$airqpass = $airqpass . '0';
		}
	} else {
		if (strlen($airqpass) > 32) {
			$airqpass = substr($airqpass,0,32);
		}
	}

	$key = utf8_encode ($airqpass);
//	$cyphertext = base64_decode ($msgb64);
//	But with verly long messages there could be some problems in base64_decode
	$decoded = "";
	for ($i=0; $i < ceil(strlen($msgb64)/256); $i++)
	   $decoded = $decoded . base64_decode(substr($msgb64,$i*256,256));
	$cyphertext = $decoded;
	$iv = substr($cyphertext,0,16);
	$cyphertext = substr($cyphertext,16,strlen($cyphertext));
	$decrypted = openssl_decrypt($cyphertext, 'aes-256-cbc', $key, OPENSSL_RAW_DATA, $iv);
	return utf8_encode($decrypted);
}

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_URL, $airq); 
$output = curl_exec($ch);
header('Content-Type: application/json');
echo decrypt(json_decode($output, true)["content"],$password);

?>
  • This PHP script can now be called within FHEM using the HTTPMOD function:
define airq_01_http HTTPMOD http://127.0.0.1/airq/getdata 300
attr airq_01_http enforceGoodReadingNames 1
attr airq_01_http extractAllJSON 2

This works only with the IP 127.0.0.1 if FHEM and the PHP script are on the same server.

Note: If the error Error: Call to undefined function curl_init() occurs, it may be necessary to install PHP and PHP-Curl after the fact: sudo apt-get install curl sudo apt-get install phpX-curl

Sending data to the integrated MQTT server of FHEM

  • Create the file “userconfig.json” on your PC and populate it using an editor (e.g., Notepad++ or VSCode) as follows:
{
  "mqtt": {
    "device_id": "Ihre_selbsterdachte_Geraetenummer",
    "broker_URL": "192.168.x.y",
    "user": "Ihr_Nutzername",
    "password": "Ihr_Passwort",
    "port": 1883,
    "topic": "Ihr_Topic",
    "qos": 1,
    "keepalive": 10000,
    "averages": true,
    "delay": 120,
    "retain": false,
    "ssl": false
  }
}
  • Afterwards, you can enter your personal configuration settings in lines 3 to 14 and copy/move the file to the root directory of the SD card in your air-Q.

Here is a guide and introduction to the MQTT service within FHEM: https://wiki.fhem.de/wiki/MQTT\

Example configuration files with enabled SSL are available upon request

Was this article helpful?

Related Articles

Sie benötigen Hilfe?
Sie können die gesuchte Antwort nicht finden? Kontaktieren Sie uns gern!
KONTAKT