Closed swilson86 closed 5 years ago
Has anyone got this software running on a Raspberry Pi under Stretch?
I have and it's working fine for a few months now, I installed it using pecl
.
magkopian: Thanks, that gives me hope. I've been working on this for 2 days. A couple of things are driving me crazy. There is no mosquitto.so file anywhere on my Pi. I understand I might have to create it with the phpize command which I managed to install. I don't see how to create that file. Apparently I need to be in a folder perhaps named libmosquitto-dev which does not exist.
When I finish this and get everything going I will email the author an updated help page so everyone will know in the future. I appreciate any clues you can give me. Thanks!
It's not abandoned, although I've not had a lot of time to go through the issues recently. It does work though, and I've run it quite happily on Raspbian. The installation should look something like that in the README, but here are more specific instructions, assuming you've cloned this repo into a directory called Mosquitto-PHP
. I don't have a Pi to hand right now so the package names and paths are based on that in Ubuntu but I believe they should be the same:
sudo apt-get install libmosquitto-dev php7.0-dev
cd Mosquitto-PHP
phpize
make
sudo make install
Then you'll need to make a file called something like /etc/php/7.0/mods-available/mosquitto.ini
, wth the following contents:
extension=mosquitto.so
and finally do
phpenmod mosquitto
With some luck you should then be able to see Mosquitto mentioned in the output of php -i
. There won't be a file called mosquitto.so
anywhere on your Pi until you've run make
, and it won't be in the correct place for PHP to find it until you run sudo make install
.
Hope that helps.
@swilson86 The way I personally installed it was simply by running pecl install Mosquitto-alpha
as root. For that to work though, you need to make sure that libmosquitto-dev
and php7.0-dev
packages are installed on your system. And of course after the installation is done you need to add extension=mosquitto.so
in your php.ini
.
A thing to note though, is that if you're running PHP from the CLI then the php.ini
file is different from the one when PHP used as an Apache module for example. One is located on /etc/php/7.0/apache2/php.ini
and the other on /etc/php/7.0/cli/php.ini
. If you just edited the former you may have the mosquitto
extension available on the CLI but you won't be able to use it with PHP on the web.
To see if the mosquitto
PHP extension is available on CLI you can run php -m | grep mosquitto
. As for the web, you can use the phpinfo()
function inside a temporary PHP script and run it from your web browser. Also, if you are using Apache don't forget to restart the service after editing your php.ini
file.
mgdm: After 60 hours I have it working just now based upon your message above. magkopian: I was unable to use pecl install as it had errors. Anyway, as promised I will document this after testing again on a fresh Raspberry Pi to ensure I have the documentation correct. THANK YOU BOTH !!
Hmmm. Strange thing happened. I am able to publish but not subscribe. I was checking magkopian's theory of the 2 php.ini files. I only had the line "mosquitto.so" in the CLI version of php.ini and not in the apache2/php.ini version. Interesting that publish worked. I entered that line in the apache2/php.ini and rebooted the Pi and same results... publish okay, but subscribe not. Just for fun I commented out the mosquitto.so lines in both files and rebooted the Pi. Surprise... I could still publish but not subscribe.
Conclusion so far? Placing the mosquitto.so line in the Dynamic Extensions section of php.ini has zero effect! I will investigate further. Ideas welcomed!
As promised, some preliminary documention appears below within a file that an end user can run to test their system. This works okay on one of my Raspberry Pi's running Stretch.
On the code below I cannot get the line that is remarked out working. The line is //$client->publish($topic, $message, 1, true);
I see of no way to pass variables for the topic and payload. Mosquitto-PHP ignores it. That problem would make the extension useless to most people as I must pass the topic/payload in real time. Are there errors perhaps in my documentation or something I have overlooked? Thanking you in advance!
/* Php needs an extension to work with Mosquitto.
1) Assumption: you already have php, mosquitto, and apache2 installed on your pi. This page was tested on a Pi running Stretch (the latest OS) and PHP version 7.0.32
2) Copy this page to your Apache2 area which is usually: /var/www/html If your Mosquitto server needs a user/pass then modify those lines below.
3) Start your browser and go to http://localhost/testMQTT.php (the name of this file) If the extension exists, this page will publish a test message. If not it will tell you that the extension is not installed.
4) To install the Mosquitto-PHP extension, download all the files from https://github.com/mgdm/Mosquitto-PHP and unzip them in the downloads folder to make things easier. Everything will decompress in a folder named Mosquitto-PHP-master which needs to be cut and paste directly below mods-available which is usually at:
/etc/php/7.0/mods-available
I like to use a file manager running as administrator to cut and paste, ie:
gksudo File Manager. If you unzip everything in the correct place you will end up with a folder named
/etc/php/7.0/mods-available/Mosquitto-PHP-master
Go to that folder from a
command prompt with the following command:
cd /etc/php/7.0/mods-available/Mosquitto-PHP-master
We need the mosquitto dev library particularly for the phpize command. To see if you already have that command type phpize -v at the command prompt. If not, install as follows:
sudo apt install php-dev
sudo phpize
sudo ./configure
sudo make install
Ignore any message that says something about libtool --finish
5) There are two php.ini files that need a modification. One is for Apache2 and the
other is for the Php Command Line Interface (CLI). For Apache2 the php.ini file is usually
at: /etc/php/7.0/apache2
and for php it should be at /etc/php/7.0/cli
Edit each of those files using something like 'sudo leafpad php.ini' or your favorite editor and do a search for the Dynamic Extensions area. At the end of that section copy and paste the following 3 lines, and save each file when done:
; See https://github.com/mgdm/Mosquitto-PHP
; Allows php to use MQTT with Mosquitto
extension=mosquitto.so
7) Reboot the Pi when done and test with Step 3 above. If in a production environment and you prefer not to reboot the Pi, search on Google how to stop/restart PHP and Apache2.
*/
if (!extension_loaded('Mosquitto')) {
echo "Mosquitto-PHP extension not loaded. Unable to continue.<br />";
die();
}else{
echo "The Mosquitto-PHP extension seems to be working!<br />";
}
// Edit your Mosquitto info in the next 4 lines.
$user = 'yourusername';
$pass = 'yourpass';
$broker = 'localhost';
$port = 1883;
$topic = 'Test/foo/bar';
$message = 'Test message at ' . date("Y-m-d H:i:s");
$client = new Mosquitto\Client();
$client->setCredentials($user, $pass);
$client->connect($broker, $port);
$client->onConnect(function() use ($client) {
//$client->publish($topic, $message, 1, true);
$client->publish('Test/foo/bar', 'Test message at ' . date("Y-m-d H:i:s"), 1, true);
$client->disconnect();
});
$client->loopForever();
echo 'Finished<br />';
echo 'Topic: ' . $topic . '<br />';
echo 'Message: ' . $message . '<br />';
Hi, I edited that slightly to try and fix the syntax highlighting, but I don't think it worked.
I see of no way to pass variables for the topic and payload. Mosquitto-PHP ignores it.
I think what's happening here is that you're getting stung by the way PHP's anonymous functions work. Although you're defining $message
like so:
$message = 'Test message at ' . date("Y-m-d H:i:s");
$topic
and $message
don't exist inside the anonymous function used for onConnect()
, because they're not in the use
statement. Try changing it to match this:
$client->onConnect(function() use ($client, $topic ,$message) {
$client->publish($topic, $message, 1, true);
$client->disconnect();
});
See http://php.net/manual/en/functions.anonymous.php for more detail on that, particularly "Example #3 Inheriting variables from the parent scope".
I'd also suggest that you don't want to put the source code for the extension under cd /etc/php/7.0/mods-available/
. Keep it in your home directory instead.
Yes! You were correct. It is now working on both of my Pi's. Here is the final documentation and the test code inserted. I hope that this saves someone in the future the hours upon hours of agony and frustration. Again THANKS to both of you. Feel free to modify any of my documentation and use it where you see fit. Next I will investigate subscribing and will do a write up!
<?php
/* Php needs an extension to work with Mosquitto. This page will test if the extension
is installed on a Raspberry Pi and if so, it will publish a message.
1) Assumption: you already have php, mosquitto, and apache2 installed on your pi. The
code below was tested on a Raspberry Pi running Stretch (the latest OS) and PHP version 7.0.32
2) Copy the code to your Apache2 area which is usually: /var/www/html
If your Mosquitto server needs a user/pass then modify those lines of code. Save the file as
testMQTT.php
3) Start your browser and go to http://localhost/testMQTT.php
If the extension exists, the page will publish the test message. If not
it will tell you that the extension is not installed. I use MQTTfx running
on a Windows 10 computer to monitor what's going on in Mosquitto. MQTT Spy is another
handy alternative. Both of those will let you subscribe and publish messages to do
testing.
4) To install the Mosquitto-PHP extension, download all the files from
https://github.com/mgdm/Mosquitto-PHP and unzip them in the downloads folder
to make things easier. If you used Chromium browser it will normally download
to the folder /home/pi/Downloads
Everything will decompress in a folder named
Mosquitto-PHP-master which needs to be cut and paste directly below your
home folder which is usually at (type pwd after opening a terminal window
if you want to see what your home folder is. Normally it is):
/home/pi
I like to use a file manager running as administrator to extract (unzip), cut and paste, ie:
gksudo File Manager. If you unzip everything in the correct place you will end up with a folder named
/home/pi/Mosquitto-PHP-master
Go to that folder from a command prompt with the following command:
cd /home/pi/Mosquitto-PHP-master
We need the mosquitto dev library particularly for the phpize command. To see
if you already have that command type phpize -v at the command prompt. If not,
install as follows:
sudo apt install php-dev
Now that phpize is installed, continue with the following commands at a command prompt while
in the /home/pi/Mosquitto-PHP-master folder:
sudo phpize
sudo ./configure
sudo make
sudo make install
Ignore any message that says something about libtool --finish
5) There are two php.ini files that need a modification. One is for Apache2 and the
other is for the Php Command Line Interface (CLI). For Apache2 the php.ini file is usually
at: /etc/php/7.0/apache2 and for php it should be at /etc/php/7.0/cli
Edit each of those files using something like 'sudo leafpad php.ini' or your favorite
editor and do a search for the Dynamic Extensions area. At the end of that section copy
and paste the following 3 lines, and save each file when done:
; See https://github.com/mgdm/Mosquitto-PHP
; Allows php to use MQTT with Mosquitto
extension=mosquitto.so
7) Reboot the Pi when done and test with Step 3 above. If in a production environment
and you prefer not to reboot the Pi, search on Google how to stop/restart PHP and Apache2.
*/
?>
<?php
// This tests for the extension
if (!extension_loaded('Mosquitto')) {
echo "Mosquitto-PHP extension not loaded. Unable to continue.<br />";
die();
}else{
echo "The Mosquitto-PHP extension seems to be working!<br />";
}
// Edit your Mosquitto info in the next 4 lines.
$User = 'enteryourusername';
$Pass = 'enteryourpassword';
$Broker = 'localhost';
$Port = 1883;
// Edit your MQTT message info in the next 4 lines.
$Topic = 'Test/foo/bar'; // Must conform to MQTT standards
$Payload = 'Test message at ' . date("Y-m-d H:i:s"); // The actual message that is sent
$QoS = 1; // Options are 0,1 and 2
$Retained = true; // Options are false or true
// You should not need to change anything below here
$client = new Mosquitto\Client();
$client->setCredentials($User, $Pass);
$client->connect($Broker, $Port);
$client->onConnect(function() use ($client, $Topic ,$Payload, $QoS, $Retained) {
$client->publish($Topic, $Payload, $QoS, $Retained);
$client->disconnect();
});
$client->loopForever();
echo 'Finished<br />';
echo 'Topic: ' . $Topic . '<br />';
echo 'Payload: ' . $Payload . '<br />';
?>
I don't see any answers to issues in a very long time. I'm suspecting this project has been abandoned. Has anyone got this software running on a Raspberry Pi under Stretch?