Last active
March 9, 2021 12:24
-
-
Save sybip/81355ecb359eef441aa69eb231863e9e to your computer and use it in GitHub Desktop.
30-second, 60-line demo of pyGT library (2021 version)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# Simple demo of the pyGT library - send a shout message via goTenna Mesh | |
import sys | |
# from https://github.com/sybip/pyGT | |
import gtdevice | |
from compatGTA import gtMakeGTABlobMsg | |
from gtapiobj import gtMakeAPIMsg | |
# One line of help | |
cliHelpText = "Syntax: " + sys.argv[0] + " MAC_ADDRESS" | |
if (len(sys.argv) < 2) or (len(sys.argv[1]) < 17): | |
print(cliHelpText) | |
sys.exit() | |
# Pick up MAC address from command line | |
MAC = sys.argv[1] | |
# Open Bluetooth connection | |
try: | |
gotenna = gtdevice.goTennaDev(MAC) | |
except: | |
sys.exit("Connection to %s FAILED, exiting" % MAC) | |
# Initialize device communication | |
if not gotenna.initialize(): | |
sys.exit("Initialization failed") | |
print("READY") | |
# Message parameters, edit as needed | |
fromGID = 20210301031337 # Fake a source GID, not important | |
fromTXT = "SYBIP" # Nickname shown under message | |
bodyTXT = ("Hello World from PyGT API!") | |
# ---------- no user data past this line ---------- | |
# Some constants, copied from gtdefs.py to keep things compact | |
APPID_GOTENNA_APP = 0x3fff # App ID of official goTenna app | |
OP_SENDMSG = 0x03 # Send a message (shout, direct, key exch etc) | |
MSG_CLASS_SHOUT = 2 # Message is a shout | |
# Build-a-Message Workshop just got a lot easier | |
msgBlob = gtMakeGTABlobMsg(bodyTXT, fromTXT) | |
msgPDU = gtMakeAPIMsg(msgBlob, MSG_CLASS_SHOUT, APPID_GOTENNA_APP, fromGID) | |
# Finally, feed the PDU to the API SENDMSG command | |
print("SENDING...") | |
r = gotenna.execute(OP_SENDMSG, msgPDU) | |
if r: | |
print("ALL OK" if r[0] == 0x40 else "FAILED") | |
else: | |
print("ERROR: request failed") | |
# All done, goodbye | |
print("DISCONNECTING") | |
gotenna.disconnect() | |
del gotenna | |
print("GOODBYE") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment