Home >Backend Development >PHP Tutorial >A practical approach to intelligent traffic monitoring and control using PHP and MQTT
Practical methods to implement intelligent traffic monitoring and control using PHP and MQTT
Introduction:
With the continuous development of Internet of Things technology, intelligent traffic monitoring and control has become an important need in modern society. This article will introduce how to use PHP and MQTT technology to implement practical methods of intelligent traffic monitoring and control, and provide corresponding code examples.
1. What is MQTT?
MQTT (Message Queuing Telemetry Transport) is a lightweight message transmission protocol suitable for communication over limited network bandwidth and unstable network connections. MQTT provides a publish/subscribe mode of communication, that is, the publisher of the message publishes the message to a broker, and then the subscriber receives the message by subscribing to the relevant topic.
2. Preparation work:
Before starting to write code, we need to do some preparation work:
sudo apt-get install php7.2-cli php7.2-mbstring php7.2-mysql php7.2-curl php7.2-xml php7. 2-zip php7.2-mysqli php7.2-mosquitto
sudo apt-get install mosquitto
flow
with the following fields:id
, device_id
, flow_rate
, timestamp
. 3. Traffic monitoring:
The following code example demonstrates how to use PHP to monitor traffic data and store it in the database.
<?php require('vendor/autoload.php'); use MosquittoClient; $mqttClient = new Client(); $mqttClient->onConnect(function() use ($mqttClient) { echo "Connected to MQTT broker "; $mqttClient->subscribe('flow-monitor/flow-data', 2); }); $mqttClient->onMessage(function($message) { $data = json_decode($message->payload); // 将数据存储到数据库中 $conn = new mysqli('localhost', 'username', 'password', 'database'); $query = "INSERT INTO flow (device_id, flow_rate, timestamp) VALUES ('$data->device_id', '$data->flow_rate', '$data->timestamp')"; $result = $conn->query($query); echo "Received flow data: device_id=$data->device_id, flow_rate=$data->flow_rate "; }); $mqttClient->connect('localhost', 1883); $mqttClient->loopForever(); ?>
In the above code, we created an MQTT client and subscribed to the flow-monitor/flow-data
topic. When a message arrives, we parse the received data into JSON format and store the data into the database.
4. Flow control:
The following code example demonstrates how to use PHP to control traffic and send control instructions to the MQTT broker.
<?php require('vendor/autoload.php'); use MosquittoClient; $mqttClient = new Client(); $mqttClient->onConnect(function() use ($mqttClient) { echo "Connected to MQTT broker "; // 向broker订阅控制指令的主题 $mqttClient->subscribe('flow-control/commands', 2); }); $mqttClient->onMessage(function($message) use ($mqttClient) { $command = $message->payload; // 根据收到的指令进行相应的流量控制 switch ($command) { case 'start': echo "Starting flow "; // 执行流量控制的相关代码 break; case 'stop': echo "Stopping flow "; // 执行流量控制的相关代码 break; default: echo "Invalid command "; break; } }); $mqttClient->connect('localhost', 1883); $mqttClient->loopForever(); ?>
In the above code, we created another MQTT client and subscribed to the flow-control/commands
topic. When a control instruction arrives, we perform corresponding flow control operations according to different instructions.
Conclusion:
This article introduces how to use PHP and MQTT to implement intelligent traffic monitoring and control with examples. By using the MQTT communication protocol and related PHP code, an intelligent traffic monitoring and control system can be realized, providing important help for traffic management in modern society.
Reference:
The above is the detailed content of A practical approach to intelligent traffic monitoring and control using PHP and MQTT. For more information, please follow other related articles on the PHP Chinese website!