diff --git a/meshtastic/__init__.py b/meshtastic/__init__.py index 8708b62..5b15ce6 100644 --- a/meshtastic/__init__.py +++ b/meshtastic/__init__.py @@ -267,6 +267,19 @@ class MeshInterface: s = base64.urlsafe_b64encode(bytes).decode('ascii') return f"https://www.meshtastic.org/c/#{s}" + def setURL(self, url, write=True): + """Set mesh network URL""" + if self.radioConfig == None: + raise Exception("No RadioConfig has been read") + + # URLs are of the form https://www.meshtastic.org/c/#{base64_channel_settings} + # Split on '/#' to find the base64 encoded channel settings + splitURL = url.split("/#") + decodedURL = base64.urlsafe_b64decode(splitURL[-1]) + self.radioConfig.channel_settings.ParseFromString(decodedURL) + if write: + self.writeConfig() + def _generatePacketId(self): """Get a new unique packet ID""" if self.currentPacketId is None: diff --git a/meshtastic/__main__.py b/meshtastic/__main__.py index 3f0c99a..c79654e 100644 --- a/meshtastic/__main__.py +++ b/meshtastic/__main__.py @@ -180,11 +180,7 @@ def onConnected(interface): # Handle set URL if args.seturl: - # URLs are of the form https://www.meshtastic.org/c/#{base64_channel_settings} - # Split on '/#' to find the base64 encoded channel settings - splitURL = args.seturl.split("/#") - bytes = base64.urlsafe_b64decode(splitURL[-1]) - interface.radioConfig.channel_settings.ParseFromString(bytes) + interface.setURL(args.seturl, False) print("Writing modified preferences to device") interface.writeConfig()