forked from mateusomattos/loraLTA
-
Notifications
You must be signed in to change notification settings - Fork 1
/
send_position.py
87 lines (65 loc) · 2.23 KB
/
send_position.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
import socket
import time
import serial
import pynmea2
from datetime import datetime
"""
To use, download the app share GPS and create a USB connection using adb and tcp forward.
Run: adb forward tcp:20175 tcp:50000
"""
#
# sudo chmod a+rw /dev/ttyUSB0
class LoraEndDevice:
def __init__(self):
self.loraSerial = serial.Serial()
self.loraSerial.port = '/dev/ttyUSB0'
self.loraSerial.baudrate = 115200
self.loraSerial.bytesize = 8
self.loraSerial.parity='N'
self.loraSerial.stopbits=1
self.loraSerial.timeout=2
self.loraSerial.rtscts=False
self.loraSerial.xonxoff=False
self.lastAtCmdRx = ''
def setPortCom(self, newPort):
self.loraSerial.port = newPort
def openSerialPort(self):
self.loraSerial.open()
def closeSerialPort(self):
self.loraSerial.close()
def sendCmdAt(self,cmd):
if self.loraSerial.is_open:
self.loraSerial.write(cmd.encode())
else:
print('It\'s not possible to communicate with LoRa module!')
def getAtAnswer(self):
self.lastAtCmdRx = self.loraSerial.read(100)
def printLstAnswer(self):
print(self.lastAtCmdRx.decode('UTF-8'))
def sendMessage(self, msg):
msg = '{}\r\n'.format(msg)
self.sendCmdAt(msg)
self.getAtAnswer()
endDevice = LoraEndDevice()
endDevice.openSerialPort()
delayBetweenPkt_sec = 3*60
HOST = 'localhost' # The server's hostname or IP address
PORT = 20175 # The port used by the server
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT))
id=0
while 1:
now = datetime.now()
time = now.strftime("%H:%M:%S")
data = s.recv(1024).decode("utf-8")
position = data.splitlines(0)[0]
latitude = pynmea2.parse(position).lat
longitude = pynmea2.parse(position).lon
altitude = pynmea2.parse(position).altitude
data_to_send = '[{}] Id.:{}, Lat.: {}, Lon.: {}, Alt.:{}'.format(time, id, latitude, longitude, altitude)
print(data_to_send)
endDevice.sendMessage('AT')
endDevice.printLstAnswer()
id = id+1
time.sleep(delayBetweenPkt_sec)
endDevice.closeSerialPort()