more init

This commit is contained in:
Terra 2022-04-24 03:41:28 +02:00
parent 2355f290de
commit c30441a9f6
6 changed files with 134 additions and 2 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
__pycache__/

1
PyRH
View File

@ -1 +0,0 @@
/mnt/ssh/multinode/PyRH

1
RasPi
View File

@ -1 +0,0 @@
/mnt/ssh/multinode/RasPi

7
RasPi/Config.toml Normal file
View File

@ -0,0 +1,7 @@
[server]
address = 0x0001
secret_key = 0x2e29b257521dc792
[node]
address = 0x1FFF
secret_key = 0x7ed64cce5b5d8e85

71
RasPi/MultiNode.py Normal file
View File

@ -0,0 +1,71 @@
from enum import IntEnum
import struct
from pyblake2 import blake2s
import time
import toml
HASH_LENGTH = 8
with open("Config.toml", "r") as config_file:
config = toml.loads(config_file.read())
print(config)
devices = {}
class MessageType(IntEnum):
DeviceStatus = 1
SensorStatus = 2
def decode_packet(data):
packet_type = data[0]
# match packet_type:
# case MessageType.DeviceStatus:
if packet_type == MessageType.DeviceStatus:
return {"Battery voltage": struct.unpack('<f', data[1:5])[0]}
if packet_type == MessageType.SensorStatus:
channels_raw = struct.unpack('<H', data[1:3])[0]
channels = []
for i in range(16):
if (channels_raw >> i) & 1:
channels.append(i)
sensor_data = []
for i in range(len(channels)):
offset = i * 6
sensor_data.append({
"channel": channels[i],
"type": data[3 + offset],
"pin": data[4 + offset],
"value": struct.unpack('<f', data[5 + offset : 9 + offset])[0]
})
return sensor_data
def process_packet(payload):
rx_id = int.from_bytes(payload.message[0:2], byteorder="little")
tx_id = int.from_bytes(payload.message[2:4], byteorder="little")
msg_id = int.from_bytes(payload.message[4:8], byteorder="little")
length = payload.message[8]
data = payload.message[9: 9 + length]
data_hash = payload.message[9 + length: 9 + length + HASH_LENGTH]
if len(payload.message) != length + 9 + HASH_LENGTH:
print(
f"Invalid length! Expected {length + 9 + HASH_LENGTH} actual {len(payload.message)}")
return
hash_function = blake2s(key=0x0.to_bytes(8, "little"), digest_size=8)
hash_function.update(payload.message[: -HASH_LENGTH])
if hash_function.digest() != data_hash:
print(
f"Hash doesn't match! Expected {hash_function.digest()} got {data_hash}")
return
# print(f"{tx_id} #{msg_id}: {decode_packet(data):.3f} V, {payload.rssi} dB(?) RSSI, {payload.snr} dB(?) SNR {(time.clock_gettime_ns(0)) / 1e9}")
print(f"{tx_id} #{msg_id}: {data.hex()} {decode_packet(data)}, {payload.rssi} dB(?) RSSI, {payload.snr} dB(?) SNR {(time.clock_gettime_ns(0)) / 1e9}")

55
RasPi/RXTest.py Normal file
View File

@ -0,0 +1,55 @@
from pyLoraRFM9x import LoRa, ModemConfig
import MultiNode
#class MessageType(IntEnum):
# DeviceStatus = 1;
#
#def decode_packet(data):
# packet_type = data[0]
#
# #match packet_type:
# # case MessageType.DeviceStatus:
#
# if packet_type == MessageType.DeviceStatus:
# return struct.unpack('f', data[1:5])[0]
# This is our callback function that runs when a message is received
def on_recv(payload):
#print("From:", payload.header_from)
#print("Received:", payload.message)
#print("RSSI: {}; SNR: {}".format(payload.rssi, payload.snr))
MultiNode.process_packet(payload)
#print(payload.message.hex())
#rx_id = int.from_bytes(payload.message[0:2], byteorder="little")
#tx_id = int.from_bytes(payload.message[2:4], byteorder="little")
#msg_id = int.from_bytes(payload.message[4:8], byteorder="little")
#length = payload.message[8]
#data = payload.message[9 : 9 + length]
#data_hash = payload.message[9 + length : 9 + length + HASH_LENGTH]
#
#if len(payload.message) != length + 9 + HASH_LENGTH:
# print(f"Invalid length! Expected {length + 9 + HASH_LENGTH} actual {len(payload.message)}")
# return
#
#hash_function = blake2s(key=0x0.to_bytes(8, "little"), digest_size=8)
#hash_function.update(payload.message[: -HASH_LENGTH])
#
#if hash_function.digest() != data_hash:
# print(f"Hash doesn't match! Expected {hash_function.digest()} got {data_hash}")
# return
#print(f"Received {struct.unpack('f', data[1:])[0]:.3f} V from {tx_id} with destination {rx_id} and {payload.rssi} dB(?) RSSI and {payload.snr} dB(?) SNR")
#print(f"{tx_id} #{msg_id}: {decode_packet(data):.3f} V, {payload.rssi} dB(?) RSSI, {payload.snr} dB(?) SNR {(time.clock_gettime_ns(0) - start_time) / 1e9}")
# Use chip select 1. GPIO pin 5 will be used for interrupts and set reset pin to 25
# The address of this device will be set to 2
lora = LoRa(0, 25, 255, reset_pin = 22, modem_config=ModemConfig.Bw125Cr45Sf128, tx_power=14, freq=868, acks=False)#, receive_all=True)
lora.cad_timeout = 1
lora.on_recv = on_recv
lora.set_mode_rx()
import time
while True:
time.sleep(0.5)