https://github.com/megamarco833/mqtt-meterN
dovete ovviamente cambiare gli indirizzi degli idx (esempio):
Codice: Seleziona tutto
$ID_prod = 4;
$ID_prel = 5;
$ID_autoc = 6;
$ID_f1 = 7;
$ID_f23 = 8;
$ID_imm = 9;
$ID_cons = 10;
$ID_boiler = 130;
ed ovviamente adattare la configurazione:
Codice: Seleziona tutto
while (true) {
if (file_exists('/dev/shm/mN_LIVEMEMORY.json') && file_exists('/dev/shm/mN_ILIVEMEMORY.json') && file_exists('/dev/shm/mN_MEMORY.json')) {
$data_mN_LIVEM = file_get_contents('/dev/shm/mN_LIVEMEMORY.json');
$data_mN_ILIVEM = file_get_contents('/dev/shm/mN_ILIVEMEMORY.json');
$data_mN_MEMORY = file_get_contents('/dev/shm/mN_MEMORY.json');
$memarray_mN_LIVEM = json_decode($data_mN_LIVEM, true);
$memarray_mN_ILIVEM = json_decode($data_mN_ILIVEM, true);
$memarray_mN_MEMORY = json_decode($data_mN_MEMORY, true);
$prod_KWH = $memarray_mN_MEMORY["Last2"]; //Last2 = Produzione Wh
$cons_KWH = $memarray_mN_MEMORY["Last1"]; //Last1 = Consumi Wh
$consumiW = $memarray_mN_LIVEM["Consumi1"]; //Consumi1 = consumi W
$prodW = $memarray_mN_LIVEM["Produzione2"]; //Produzinoe2 = produzione W
$prel_KWH = $memarray_mN_MEMORY["Last3"]; //Last3 = Prelievi Wh
$prelW = $memarray_mN_LIVEM["Prelievi3"]; //Prelievi2 = prelievi W
$imm_KWH = $memarray_mN_MEMORY["Last4"];
$immW = $memarray_mN_LIVEM["Immissioni4"];
$auto_KWH = $memarray_mN_MEMORY["Last5"];
$autoW = $memarray_mN_LIVEM["Autoconsumo5"];
$f1_KWH = $memarray_mN_MEMORY["Last8"];
$f1W = $memarray_mN_LIVEM["PrelieviF18"];
$f23_KWH = $memarray_mN_MEMORY["Last9"];
$f23W = $memarray_mN_LIVEM["PrelieviF239"];
$boiler_KWH = $memarray_mN_MEMORY["Last12"];
$boilerW = $memarray_mN_LIVEM["Boiler12"];
$temp = $memarray_mN_LIVEM["temperatura6"];
$humi = $memarray_mN_LIVEM["Umidità7"];
$V = $memarray_mN_ILIVEM["Voltage1"]; //Voltage1 = Volt
$A = $memarray_mN_ILIVEM["Corrente2"]; //Corrente2 = Ampere
$h2o = $memarray_mN_ILIVEM["ACQUA7"] * 1000; //ACQUA7 = acqua in m3
modificare la linea17 mettendo il proprio indirizzo IP del broker mqtt:
Codice: Seleziona tutto
$CMD = "timeout --kill-after=15s 10s mosquitto_pub -d -h '192.168.0.105' -t 'domoticz/in' -m '$msg'";
nel mio caso 192.168.0.105
ultimo passaggio creare con systemd il servizio per eseguirlo ogni X secondi:
un esempio funzionante è questo:
Codice: Seleziona tutto
sudo nano /etc/systemd/system/mqtt_energy.service
Codice: Seleziona tutto
[Unit]
Description=mqtt_energy
Requires=network.target
After=network.target nginx.service php-fpm.service
[Service]
Type=oneshot
ExecStart=/usr/bin/php /var/www/comapps/mqtt_energy.php
#ExecStartPre=/bin/sleep 30
[Install]
WantedBy=default.target
Codice: Seleziona tutto
sudo systemctl enable mqtt_energy.service
Codice: Seleziona tutto
sudo systemctl start mqtt_energy.service