0

I would like some advices and help concerning a IoT stack. This is the stack I have imagined:

  • Arduino, the IoT device
  • Broker, doesn't matter for now, I will use a free public broker for testing.
  • A Server storing the data.

Since I already have a Web Host from Siteground, either apache2 or nginx. I wanted to know if it is possible to connect the it as a Subscriber to the broker to received the data and store it into its database.

For now I have discovered the php/mqtt library, and mock a publisher and a subscriber with this guide.

<?php
// Subscriber
require('vendor/autoload.php');

use \PhpMqtt\Client\MqttClient;
use \PhpMqtt\Client\ConnectionSettings;

$server   = 'broker.emqx.io';
$port     = 1883;
$clientId = rand(5, 15);
$username = 'emqx_user';
$password = null;
$clean_session = false;

$connectionSettings  = new ConnectionSettings();
$connectionSettings
  ->setUsername($username)
  ->setPassword(null)
  ->setKeepAliveInterval(60)
  ->setLastWillTopic('emqx/test/last-will')
  ->setLastWillMessage('client disconnect')
  ->setLastWillQualityOfService(0);


$mqtt = new MqttClient($server, $port, $clientId);
pcntl_signal(SIGINT, function (int $signal, $info) use ($mqtt) {
    $mqtt->interrupt();
});
$mqtt->connect($connectionSettings, $clean_session);
printf("client connected\n");

$mqtt->subscribe('emfqx/test', function ($topic, $message) {
    printf("Received message on topic [%s]: %s\n", $topic, $message);
}, 0);

$mqtt->loop(true);
$mqtt->disconnect();

How can I make my server run this script? Can it be run like a REST API? If it's feasible, can I also host a web application on the same server to display the data from the database?

Furthermore, I would also like to know if using the same server as the broker is possible? With Mosquitto, for example?

Actually I did not found any ressource explaining how to deploy this, more precisely I am looking for the connection between the broker and the 'cloud platform', preferably in php.

1 Answer 1

2

Without knowing more about your specific hosting plan it's hard to say if they will allow you to run a broker on the same account. If it only allow access to web based protocols e.g. http/https because it is behind a hostname based proxy then you will not normally be able to access a broker running on the same machine/vm.

If you are going to run your own broker you are going to need a hosting plan with a dedicated IP address.

PHP is not a great choice for a subscriber as scripts tend to only run when a page is requested (to render the response) which doesn't fit well with the concept of the MQTT subscribing client needing to run all the time. While it is possible to kick off a PHP script as a separate background task I suggest you look at an alternative and have a independent application to process messages and insert them into the database and just use the PHP to render the frontend.

2
  • Thanks, good to know. Do you know what kind of "independent application" I could us to fill the database? May 6, 2022 at 9:30
  • You might consider using a Raspberry Pi as your testing framework locally to start with. This eliminates the encryption issues, and then once you get it working, you could migrate that to the hosted platform. AWS is well suited to this with Route66 domain name, fixed IP and easy install of nginx, letsencrypt and mosquitto. Jul 3, 2022 at 11:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.