Skip to content

Commit

Permalink
Added network connection to Meshtastic interface. Close #6
Browse files Browse the repository at this point in the history
  • Loading branch information
Mictronics committed Jul 18, 2024
1 parent d651ea4 commit a9a739b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ A Python client for use with Meshtastic devices. The client connects to a mesh r
## Usage

```bash
usage: meshtastic2hass [-h] --dev DEV --mqtt-host MQTT_HOST [--mqtt-port MQTT_PORT] --mqtt-user MQTT_USER --mqtt-password
MQTT_PASSWORD [--mqtt-topic-prefix MQTT_TOPIC_PREFIX] [--version]
usage: meshtastic2hass [-h] [--config CONFIG] [--dev DEV] [--mqtt-host MQTT_HOST] [--mqtt-port MQTT_PORT] [--mqtt-user MQTT_USER]
[--mqtt-password MQTT_PASSWORD] [--mqtt-topic-prefix MQTT_TOPIC_PREFIX] [--use-network USE_NETWORK]
[--hostname HOSTNAME] [--version]

Connects Meshtastic radios via MQTT to Home Assistant (Hass).

Expand All @@ -23,8 +24,11 @@ options:
--mqtt-password MQTT_PASSWORD
The MQTT broker password.
--mqtt-topic-prefix MQTT_TOPIC_PREFIX
The MQTT topic prefix.
--version show program's version number and exit
The MQTT topic prefix
--use-network USE_NETWORK
Use network connection to Meshtastic interface instead of serial
--hostname HOSTNAME Meshtastic interface network hostname or IP
--version show programs version number and exit
```
## Node Filter

Expand Down
6 changes: 6 additions & 0 deletions src/meshtastic2hass/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
# The device the Meshtastic device is connected to
device = "/dev/ttyUSB0"

# Use network connection to Meshtastic interface instead of serial
use_network = false

# Meshtastic interface network hostname or IP
hostname = ""

[mqtt]
# MQTT broker host name or IP
host = "localhost"
Expand Down
32 changes: 28 additions & 4 deletions src/meshtastic2hass/meshtastic2hass.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import meshtastic
import meshtastic.serial_interface
import meshtastic.tcp_interface
import paho.mqtt.client as mqttClient
import random
from globals import Globals
Expand All @@ -37,7 +38,7 @@
__author__ = "Michael Wolf aka Mictronics"
__copyright__ = "2024, (C) Michael Wolf"
__license__ = "GPL v3+"
__version__ = "1.0.12"
__version__ = "1.0.13"


def onReceiveTelemetry(packet, interface, topic=pub.AUTO_TOPIC):
Expand Down Expand Up @@ -416,6 +417,20 @@ def initArgParser():
required=False,
)

parser.add_argument(
"--use-network",
help="Use network connection to Meshtastic interface instead of serial",
default=False,
required=False,
)

parser.add_argument(
"--hostname",
help="Meshtastic interface network hostname or IP",
default=None,
required=False,
)

parser.set_defaults(deprecated=None)
parser.add_argument("--version", action="version", version=f"{__version__}")

Expand Down Expand Up @@ -485,16 +500,21 @@ def signal_handler(signal, frame):
args.mqtt_password = cfg.get("mqtt").get("password")
args.mqtt_host = cfg.get("mqtt").get("host")
args.mqtt_port = cfg.get("mqtt").get("port")
args.use_network = cfg.get("use_network")
args.hostname = cfg.get("hostname")
_globals.setFilterNodes(cfg.get("meshtastic").get("filter_nodes"))
else:
print(f"Error: configuration file {args.config} not found!")
sys.exit(1)

initMQTT()
try:
client = meshtastic.serial_interface.SerialInterface(
devPath=args.dev, noProto=False
)
if args.use_network and isinstance(args.hostname, str):
client = meshtastic.tcp_interface.TCPInterface(args.hostname , noProto=False)
else:
client = meshtastic.serial_interface.SerialInterface(
devPath=args.dev, noProto=False
)
except PermissionError as ex:
username = os.getlogin()
message = "Permission Error:\n"
Expand All @@ -508,6 +528,10 @@ def signal_handler(signal, frame):
print("Serial interface not found.")
print(f"Error was: {e}")
sys.exit(1)
except OSError as e:
print("Network interface not found.")
print(f"Error was: {e}")
sys.exit(1)

# We assume client is fully connected now
onConnected(client)
Expand Down

0 comments on commit a9a739b

Please sign in to comment.