-
Notifications
You must be signed in to change notification settings - Fork 1
/
receiverMesh.py
executable file
·112 lines (88 loc) · 3.1 KB
/
receiverMesh.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
import time
import base64
import hashlib
import mysql.connector as mdb
import signal
import sys
import logging
import timeout_decorator
from RF24 import *
from struct import *
from config import *
logging.basicConfig(filename='/home/pi/sensorknoten/receiver.log', level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
irq_gpio_pin = None
radio = RF24(22, 0)
pipes = ["1Node", "2Node"]
DBconn = mdb.connect(**configRaspi)
queryCurs = DBconn.cursor()
def signal_handler(signal, frame):
logging.info('Closing db connection...')
DBconn.close()
sys.exit(0)
def setup():
radio.begin()
radio.payloadSize = 28
radio.enableDynamicPayloads()
radio.setAutoAck(1)
radio.setDataRate(RF24_250KBPS)
radio.setPALevel(RF24_PA_MAX)
radio.setChannel(90)
radio.setRetries(15, 15)
radio.setCRCLength(RF24_CRC_16)
radio.openWritingPipe(pipes[0])
radio.openReadingPipe(1, pipes[0])
# radio.maskIRQ(0,0,1)
radio.startListening()
def writeToDatabase(ID_hashed, originAddr, value, unit):
timeStamp = int(time.time())
queryCurs.execute('''INSERT IGNORE INTO messwerte (id,originAddr,value,unit,timestamp)
VALUES (%s,%s,%s,%s,%s)''', (ID_hashed, originAddr, value, unit, timeStamp))
DBconn.commit()
def genearteID_hashed(stationID, messageID, timeID):
str1 = str(stationID)
str2 = str(messageID)
str3 = str(timeID)
toHash = str1 + str2 + str3
hashed = hashlib.md5()
hashed.update(toHash)
return hashed.hexdigest()
def processData(stationID, messageID, timeID, originAddr, value, unit):
ID_hashed = genearteID_hashed(stationID, messageID, timeID)
writeToDatabase(ID_hashed, originAddr, value, unit)
def readRadio():
received_payload = radio.read(radio.payloadSize)
return received_payload
@timeout_decorator.timeout(5, use_signals=False)
def receive():
if radio.available():
while radio.available():
try:
receive_payload = readRadio()
anz_paddings = (len(receive_payload) % 3) + 1
if anz_paddings > 1:
receive_payload += b'=' * anz_paddings
decodedData = base64.b64decode(receive_payload)
if len(decodedData) == 20:
destinationAddr, originAddr, lastHopAddr, messageID, stationID, value, unit, timeID = unpack('<hhhhhfhL', decodedData)
logging.info('StationId: ' + str(stationID) + '\tMessageID: ' + str(messageID) + '\tValue: ' + str(round(value, 2)))
processData(stationID, messageID, timeID, originAddr, round(value, 2), unit)
return
except TypeError:
logging.error('Base64 Fehler')
except mdb.errors.InterfaceError:
logging.error('Mysql Fehler')
DBconn.reconnect(attempts=5, delay=5)
except StopIteration:
logging.error('Timeout - radio.read()')
except Exception as e:
logging.error(e)
return
setup()
radio.printDetails()
# signal.signal(signal.SIGINT, signal_handler)
while 1:
try:
receive()
except Exception as e:
logging.error(e)
time.sleep(1)