stuff for me
This commit is contained in:
parent
9a29bb1c68
commit
0498770a49
15
monitor_mesh.py
Normal file → Executable file
15
monitor_mesh.py
Normal file → Executable file
@ -5,16 +5,18 @@ import pymysql
|
|||||||
import json
|
import json
|
||||||
|
|
||||||
# MySQL database configuration
|
# MySQL database configuration
|
||||||
servername = "localhost"
|
servername = "192.168.2.42"
|
||||||
username = "meshtastic"
|
username = "meshtastic"
|
||||||
password = "YOUR_PASSWORD"
|
password = "themeshywoman"
|
||||||
dbname = "meshtastic"
|
dbname = "meshtastic"
|
||||||
|
|
||||||
# MQTT broker configuration
|
# MQTT broker configuration
|
||||||
broker_address = "192.168.x.x"
|
mqttun="lorawan"
|
||||||
|
mqttpw="lorawanthing"
|
||||||
|
broker_address = "192.168.2.25"
|
||||||
broker_port = 1883
|
broker_port = 1883
|
||||||
client_id = "mesh-monitor-py"
|
client_id = "mesh-monitor-py"
|
||||||
topic = "Meshtastic/2/json/LongFast/!da5ed0a0" # replace !da5ed0a0 with your own Node ID
|
topic = "LoRa/2/json/LongFast/!e2e5bc7c" # replace !da5ed0a0 with your own Node ID
|
||||||
|
|
||||||
# You should not need to modify anything beyond here
|
# You should not need to modify anything beyond here
|
||||||
|
|
||||||
@ -72,7 +74,7 @@ def process_JSON(data, type):
|
|||||||
|
|
||||||
cursor.close()
|
cursor.close()
|
||||||
|
|
||||||
def on_connect(client, userdata, flags, rc):
|
def on_connect(client, userdata, flags, rc,properties):
|
||||||
client.subscribe(topic)
|
client.subscribe(topic)
|
||||||
|
|
||||||
def on_message(client, userdata, msg):
|
def on_message(client, userdata, msg):
|
||||||
@ -83,11 +85,12 @@ def on_message(client, userdata, msg):
|
|||||||
if data['type'] in known_types:
|
if data['type'] in known_types:
|
||||||
process_JSON(data, data['type'])
|
process_JSON(data, data['type'])
|
||||||
|
|
||||||
mqtt_client = mqtt.Client(client_id=client_id)
|
mqtt_client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)
|
||||||
mqtt_client.on_connect = on_connect
|
mqtt_client.on_connect = on_connect
|
||||||
mqtt_client.on_message = on_message
|
mqtt_client.on_message = on_message
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
mqtt_client.username_pw_set(mqttun,mqttpw)
|
||||||
mqtt_client.connect(broker_address, broker_port, 60)
|
mqtt_client.connect(broker_address, broker_port, 60)
|
||||||
mqtt_client.loop_forever()
|
mqtt_client.loop_forever()
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
|
|||||||
99
monitor_mesh_longmesh.py
Executable file
99
monitor_mesh_longmesh.py
Executable file
@ -0,0 +1,99 @@
|
|||||||
|
#!/usr/bin/python3
|
||||||
|
|
||||||
|
import paho.mqtt.client as mqtt
|
||||||
|
import pymysql
|
||||||
|
import json
|
||||||
|
|
||||||
|
# MySQL database configuration
|
||||||
|
servername = "192.168.2.42"
|
||||||
|
username = "meshtastic"
|
||||||
|
password = "themeshywoman"
|
||||||
|
dbname = "meshtasticlongmesh"
|
||||||
|
|
||||||
|
# MQTT broker configuration
|
||||||
|
mqttun="lorawan"
|
||||||
|
mqttpw="lorawanthing"
|
||||||
|
broker_address = "192.168.2.25"
|
||||||
|
broker_port = 1883
|
||||||
|
client_id = "mesh-monitor-py"
|
||||||
|
topic = "LoRa/2/json/LongMesh/!e2e5bc7c" # replace !da5ed0a0 with your own Node ID
|
||||||
|
|
||||||
|
# You should not need to modify anything beyond here
|
||||||
|
|
||||||
|
# Connect to MySQL database
|
||||||
|
conn = pymysql.connect(host=servername, user=username, password=password, database=dbname)
|
||||||
|
|
||||||
|
def process_JSON(data, type):
|
||||||
|
global conn
|
||||||
|
|
||||||
|
sql_columns = ""
|
||||||
|
sql_values = ""
|
||||||
|
params = []
|
||||||
|
|
||||||
|
# Build the SQL column names and values
|
||||||
|
for field, value in data.items():
|
||||||
|
if field != 'payload':
|
||||||
|
sql_columns += f"`{field}`,"
|
||||||
|
sql_values += "%s,"
|
||||||
|
params.append(value)
|
||||||
|
|
||||||
|
payload_value = data.get('payload')
|
||||||
|
if isinstance(payload_value, dict):
|
||||||
|
for field, value in payload_value.items():
|
||||||
|
if isinstance(value, list):
|
||||||
|
if field == 'neighbors':
|
||||||
|
# Extract neighbor information and join into a single string
|
||||||
|
field_str = '|'.join([f"node_id:{item['node_id']}:snr:{item['snr']}" for item in value])
|
||||||
|
else:
|
||||||
|
field_str = '|'.join(value) # Concatenate array elements into a string
|
||||||
|
sql_columns += f"`payload_{field}`,"
|
||||||
|
sql_values += "%s,"
|
||||||
|
params.append(field_str)
|
||||||
|
else:
|
||||||
|
sql_columns += f"`payload_{field}`,"
|
||||||
|
sql_values += "%s,"
|
||||||
|
params.append(value)
|
||||||
|
else:
|
||||||
|
# Handle the case where payload is not a dictionary with iterable values
|
||||||
|
sql_columns += "`payload_route`,"
|
||||||
|
sql_values += "%s,"
|
||||||
|
params.append(payload_value)
|
||||||
|
|
||||||
|
# Remove trailing commas
|
||||||
|
sql_columns = sql_columns.rstrip(",")
|
||||||
|
sql_values = sql_values.rstrip(",")
|
||||||
|
|
||||||
|
# Prepare the SQL statement
|
||||||
|
sql = f"INSERT INTO `{type}` ({sql_columns}) VALUES ({sql_values})"
|
||||||
|
cursor = conn.cursor()
|
||||||
|
cursor.execute(sql, params)
|
||||||
|
conn.commit()
|
||||||
|
|
||||||
|
print("New record created successfully")
|
||||||
|
print("")
|
||||||
|
|
||||||
|
cursor.close()
|
||||||
|
|
||||||
|
def on_connect(client, userdata, flags, rc,properties):
|
||||||
|
client.subscribe(topic)
|
||||||
|
|
||||||
|
def on_message(client, userdata, msg):
|
||||||
|
print(msg.payload.decode())
|
||||||
|
print("")
|
||||||
|
data = json.loads(msg.payload)
|
||||||
|
known_types = ['traceroute', 'telemetry', 'text', 'neighborinfo', 'nodeinfo', 'position']
|
||||||
|
if data['type'] in known_types:
|
||||||
|
process_JSON(data, data['type'])
|
||||||
|
|
||||||
|
mqtt_client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)
|
||||||
|
mqtt_client.on_connect = on_connect
|
||||||
|
mqtt_client.on_message = on_message
|
||||||
|
|
||||||
|
try:
|
||||||
|
mqtt_client.username_pw_set(username=mqttun,password=mqttpw)
|
||||||
|
mqtt_client.connect(broker_address, broker_port, 60)
|
||||||
|
mqtt_client.loop_forever()
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print("\nExiting...")
|
||||||
|
mqtt_client.disconnect()
|
||||||
|
conn.close()
|
||||||
Loading…
Reference in New Issue
Block a user