bluerhinos / phpMQTT

a simple php class to connect/publish/subscribe to a MQTT broker
Other
765 stars 461 forks source link

> I am restarting this project so having a clear out of old issues (If you believe this is still valid please reopen) #106

Open EricMears opened 3 years ago

EricMears commented 3 years ago

I am restarting this project so having a clear out of old issues (If you believe this is still valid please reopen)

Originally posted by @bluerhinos in https://github.com/bluerhinos/phpMQTT/issues/65#issuecomment-615832024

Not sure how to 're-open' but hopefully this will achieve same objective.

I recently moved to the Octopus Agile electricity tariff and am using a Hildebrand 'Glow Stick' and their 'Bright' app as a portable IHD. Recent comments on the Glowmarkt Forum persuaded me to try & get (almost) live readings from my meter with MQTT.

Process works (after a fashion) with Python but I'm very new to Python so not very confident with it. I can download their JSON messages OK with Python but am having a few minor problems.

I've been using PHP for many years and thought I'd be a lot happier working in that environment. If I can download the JSON string, I should be able to extract what I want and save it to a database without too much effort.

I found the phpMQTT package and have downloaded it. The subscribeAndWaitForMessage script sounds exactly what I need to get my data and I should be able to edit the procmsg function to do what I want.

However, when running subscribeAndWaitForMessage.php I get the following errors :-

[26-Aug-2020 11:40:21 UTC] PHP Warning: stream_socket_client(): unable to connect to tcp://glowmqtt.energyhive.com:8883 (Connection refused) in /home/eric9095/public_html/Glow/phpMQTT.php on line 150 and [26-Aug-2020 11:40:21 UTC] Error:stream_socket_create() 111, Connection refused

I expect second is a logical consequence of first ?

I've tried switching between ports 1883 & 8883 but error persists. I saw a suggestion somewhere that it might be Firewall related but turning that off doesn't help. Nor does pausing my antivirus package.

Would welcome any other suggestions.

Incidentally, I had initial problems with it not working under php v5 but spotted a note saying that it needed v7.2 or above. When I upgraded to v7.4 I had a load more error messages from functions using curly brackets but dropping back to v7.2 cured them

EricMears commented 3 years ago

Still getting nowhere with trying to call MQTT from php

I have however made some progress with Python. I can download the JSON payload with this script GetMeterReading_Red.py.txt

Logically speaking, I ought to have been able to decode the payload in the same script then file the results. Alas, whenever I try that the script stops working ! (I did say I wasn't awfully strong on Python !) But you'll notice that script ended with me storing the payload in a .json file and I can then mess about with it in a second Python script. filread.py.txt

Purists will note that I haven't attempted to decode the JSON from a 'roadmap' but have stuck to a Q&D method of finding the right 'slug' of data with string functions.

Rather than filing it locally, I've chosen to add it to a database on a website I run on behalf of alumni of one of the schools I once attended. The last line of the previous script sends it there and this php script loads it into the dB

UpdateMreads.php.txt

Finally (I doubt if it really will be !) I'm using TaskShedule to run this batchfile every half hour ReadAndFile.bat.txt

So at the moment I'm constructing a database table that will have half-hourly readings from my 'smart' meter. Next step will probably be to import Agile price data for each half hour so that I can calculate my own daily bills. I haven't given up hope of being able to do the whole thing in php eventually.