-
Notifications
You must be signed in to change notification settings - Fork 2
/
Meshtastic to Bitcoin Core.py
135 lines (115 loc) · 4.46 KB
/
Meshtastic to Bitcoin Core.py
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
#pip3 install requests, pyserial and meshtastic
#make sure you defind the devPath. search for it in the code and change it to your Meshtastic device's devPath (example COM5 or similar for Windows, /dev/ttyUSB0 or similiar for Linxu)
import requests
import json
import subprocess
import serial
import meshtastic
import meshtastic.serial_interface
from pubsub import pub
import time
import datetime
from decimal import Decimal
RECVSTR = ""
datainput2 = ""
MANUALBTCTRANS = ""
MANUALBTCTR = ""
BTCBROAD = ""
#-----------------------------------------------------------DEFINE FUNCTIONS -------------------------------------------
#LORA RADIO INITIALIZATION
def onReceive(packet, interface): # called when a packet arrives
global RECVSTR
global MANUALBTCTRANS
global MANUALBTCTR
global datainput2
RECVSTR = str(packet['decoded']['text'])
print("")
print(RECVSTR)
print("")
interface.sendText("rcvd..")
if "==" in RECVSTR:
interface.sendText("FIN!")
RECVSTR = RECVSTR.replace("+","")
RECVSTR = RECVSTR.replace("=","")
MANUALBTCTRANS += RECVSTR
MANUALBTCTR = MANUALBTCTRANS
print("")
print(MANUALBTCTR)
print("")
print("Length:"+str(len(MANUALBTCTR)))
if len(MANUALBTCTR) > 0:
try:
interface.sendText("Length:"+str(len(MANUALBTCTR)))
except Exception:
print(f"failed")
if "+" in RECVSTR:
interface.sendText("cont...")
RECVSTR = RECVSTR.replace("+","")
MANUALBTCTRANS += RECVSTR
print("")
print(MANUALBTCTRANS)
print("")
if len(MANUALBTCTRANS) > 0:
try:
interface.sendText("Length:"+str(len(MANUALBTCTRANS)))
except Exception:
print(f"failed")
if "-clear-" in RECVSTR:
BTCBROAD = ""
BTCSEND = ""
MANUALBTCTR = ""
MANUALBTCTRANS = ""
interface.sendText("Code cleared!")
if "-mempool-" in RECVSTR:
MEMPOOL = subprocess.getoutput("curl -sSL \"https://mempool.space/api/v1/fees/recommended\"")
MEMjson = json.loads(MEMPOOL)
MEMCHECK = str(MEMjson['halfHourFee'])+" Sats/vByte"
print(MEMCHECK)
interface.sendText(MEMCHECK)
def onConnection(interface, topic=pub.AUTO_TOPIC): # called when we (re)connect to the radio
# defaults to broadcast, specify a destination ID if you wish
interface.sendText("hello mesh")
print(f"Lora Online")
#TRY LORA RADIO CONNECTION
#try:
pub.subscribe(onReceive, "meshtastic.receive.text")
pub.subscribe(onConnection, "meshtastic.connection.established")
interface = meshtastic.serial_interface.SerialInterface(devPath='COM8') #you need to input your devPath (windows will be COM# like COM5, linux something like /dev/ttyUSB0
#except Exception:
# print(f"Lora not initialized")
#BEGIN MAIN LOOP WAITING FOR USER INPUT ------------------------------------------------------------
then = datetime.datetime.now()
while True:
time.sleep(0.25)
#CHECK FOR LORA COMMS
now = datetime.datetime.now()
if now.minute - then.minute > 5:
then = datetime.datetime.now()
try:
pub.subscribe(onReceive, "meshtastic.receive.text")
pub.subscribe(onConnection, "meshtastic.connection.established")
interface = meshtastic.serial_interface.SerialInterface(devPath='COM8') #YOU need to input your devPath (windows will be COM# like COM5, linux something like /dev/ttyUSB0
except Exception:
print(f"Lora comms not reestablished")
if len(MANUALBTCTR) > 20:
BTCSEND = "/whateverpathyouhave/bitcoin-cli sendrawtransaction "+MANUALBTCTR #YOU NEED TO SPECIFY CLI COMMAND PATH
interface.sendText("BTC Transaction Attempt Broadcast!")
try:
BTCBROAD = subprocess.getoutput(BTCSEND)
print(BTCBROAD)
if len(BTCBROAD) > 0:
SENDTHIS = BTCBROAD.strip()
if len(SENDTHIS) > 200:
interface.sendText(SENDTHIS[0:199])
else:
interface.sendText(SENDTHIS)
BTCBROAD = ""
BTCSEND = ""
MANUALBTCTR = ""
MANUALBTCTRANS = ""
except Exception:
BTCBROAD = ""
BTCSEND = ""
MANUALBTCTR = ""
MANUALBTCTRANS = ""
interface.sendText("Error! Message -clear- and Retry")