forked from brad28b/meshtastic-mqtt-mysql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor_mesh.php
138 lines (109 loc) · 3.72 KB
/
monitor_mesh.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/php8.2
<?php
// Include required libraries
require 'vendor/autoload.php';
// Import necessary classes
use PhpMqtt\Client\MqttClient;
// Enable async signals
pcntl_async_signals(true);
// MQTT server configuration
$server = '192.168.X.X'; // IP address or hostname of your MQTT server
$port = 1883; // Port number of your MQTT server
$clientId = 'mesh-monitor'; // this isn't really necessary
$meshtastic_node = ""; // Unique node ID of the Meshtastic radio (Example: !da5ed0a0)
// MQTT topic to subscribe to
$topic = 'Meshtastic/2/json/LongFast/' . $meshtastic_node;
// MySQL database configuration
$servername = "localhost";
$username = "YOUR_MYSQL_USERNAME";
$password = "YOUR_MYSQL_PASSWORD";
$dbname = "meshtastic";
// You should not need to modify anything beyond here
// Create MySQL connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check MySQL connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Define function to process JSON message into MySQL tables
function process_JSON($data, $type) {
global $conn;
$sql_columns = "";
$sql_values = "";
$params = array();
// Build the SQL column names and values
foreach ($data as $field => $value) {
if ($field !== 'payload') {
$sql_columns .= "`$field`,";
$sql_values .= "?,";
$params[] = $value;
}
}
foreach ($data['payload'] as $field => $value) {
if (is_array($value)) {
$field_str = '';
foreach ($value as $item) {
if (is_array($item)) {
$field_str .= "node_id:".$item['node_id'].":snr:".$item['snr']."|";
} else {
$field_str .= $item."|";
}
}
// Remove trailing '|'
$field_str = rtrim($field_str, '|');
$sql_columns .= "`payload_$field`,";
$sql_values .= "?,";
$params[] = $field_str;
} else {
$sql_columns .= "`payload_$field`,";
$sql_values .= "?,";
$params[] = $value;
}
}
// Remove trailing commas
$sql_columns = rtrim($sql_columns, ",");
$sql_values = rtrim($sql_values, ",");
// Prepare the SQL statement
$sql = "INSERT INTO `$type` ($sql_columns) VALUES ($sql_values)";
$stmt = $conn->prepare($sql);
if (!$stmt) {
echo "Error preparing statement: " . $conn->error;
return;
}
// Bind parameters
$types = str_repeat('s', count($params)); // Assuming all parameters are strings
$stmt->bind_param($types, ...$params);
// Execute SQL query
if ($stmt->execute()) {
echo "New record created successfully\n\n";
} else {
echo "Error executing statement: " . $stmt->error;
}
$stmt->close();
}
// Create MQTT client instance
$mqtt = new MqttClient($server, $port, $clientId);
// Handle interruption gracefully
pcntl_signal(SIGINT, function (int $signal, $info) use ($mqtt) {
$mqtt->interrupt();
});
// Connect to MQTT broker
$mqtt->connect();
// Subscribe to MQTT topic
$mqtt->subscribe($topic, function ($topic, $message, $retained, $matchedWildcards) use ($conn) {
// Decode JSON message
echo "$message\n\n";
$data = json_decode($message, TRUE);
// Check if this JSON is of a type we know about. If it is, send it to the process_JSON function
$known_types = array('traceroute', 'telemetry', 'text', 'neighborinfo', 'nodeinfo', 'position');
if (in_array($data['type'], $known_types)) {
process_JSON($data, $data['type']);
}
}, 0);
// Start MQTT event loop
$mqtt->loop(true);
// Disconnect from MQTT broker
$mqtt->disconnect();
// Close MySQL connection
$conn->close();
?>