diff --git a/programs/network-test.py b/programs/network-test.py new file mode 100644 index 0000000000000000000000000000000000000000..cc34a967754cbfd40073a7db58e0100bcf363c0e --- /dev/null +++ b/programs/network-test.py @@ -0,0 +1,77 @@ +import sys + +from umnp.devices import DEVICE_TYPE_TEST +from umnp.microcontroller.devices.network.ethernet_w5500 import EthernetW5500 +from umnp.microcontroller.measurementdevice import MeasurementDevice +from umnp.microcontroller.tasks.periodictask import PeriodicTask +from umnp.proto.data_message import DataMessage + +if sys.implementation.name == "micropython": + # noinspection PyUnresolvedReferences + import machine + import gc + + # noinspection PyUnresolvedReferences + import uasyncio as asyncio +else: + from umnp.microcontroller.umock import machine + +# SPI PARAMETERS +SPI_BAUD = 2_000_000 +SPI_MOSI = machine.Pin(19) +SPI_MISO = machine.Pin(16) +SPI_SCK = machine.Pin(18) # serial clock + +# I2C PARAMETERS +I2C_SCL = machine.Pin(4) # serial clock +I2C_SDA = machine.Pin(5) # serial data + +# ETHERNET PARAMETERS +ETH_CS = machine.Pin(17) # chip select +ETH_RST = machine.Pin(20) # reset + +ETH_IP = "192.168.0.33" # IP of microcontroller +ETH_SUBNET = "255.255.255.0" # ... +ETH_GATEWAY = "192.168.0.1" # ... +ETH_DNS = "192.168.0.2" # technically not necessary +ETH_USE_DHCP = False + + +async def send(device: MeasurementDevice): + comm = device.communicator + + data = "hello" + + if comm.network_error: + await device.reset_network() + + if not comm.network_error: + msg = DataMessage(data, device.identifier_raw, device.device_type) + await comm.send_message(msg) + msg = None + + gc.collect() + + +async def main(): + # configure network + device = MeasurementDevice(device_type=DEVICE_TYPE_TEST) + spi = device.add_spi(0, SPI_BAUD, mosi=SPI_MOSI, miso=SPI_MISO, sck=SPI_SCK) + dev_mac = device.generated_mac_raw() + ether = EthernetW5500(spi, ETH_CS, ETH_RST, dev_mac, ETH_USE_DHCP) + ether.set_network(ETH_IP, ETH_SUBNET, ETH_GATEWAY, ETH_DNS) + device.add_network_adapter(ether) + + comm = device.create_communicator() + + comm.add_task( + PeriodicTask(send, None, 1000, device), + "send", + ) + comm.add_task(PeriodicTask(device.send_life_sign, None, 5000), "life_sign") + + asyncio.run(comm.start()) + + +if __name__ == "__main__": + asyncio.run(main()) diff --git a/umnp-daq.py b/umnp-daq.py index 341035277809b5c8c9130e51cc852098a771a8c3..5a45367fa4ab9e18e510c9ef7fae5327e52fc3a8 100644 --- a/umnp-daq.py +++ b/umnp-daq.py @@ -13,7 +13,7 @@ sock.bind(("0.0.0.0", DEFAULT_UMNP_DATA_IN_PORT)) def main(): files = FileDAQ() - print("receive-time,sender-id,T,rH,p,T(p)") + # print("receive-time,sender-id,T,rH,p,T(p)") while True: data, addr = sock.recvfrom(2048) diff --git a/umnp/daq/file_daq.py b/umnp/daq/file_daq.py index c768f70c4ba75e81e9626c00eee5b25731a79eb4..2d06f74844764569de0c316c4f3735c030d50199 100644 --- a/umnp/daq/file_daq.py +++ b/umnp/daq/file_daq.py @@ -84,5 +84,5 @@ class FileDAQ: msg_type = clean(msg.get_message_type_string) f.write(f"{when},{msg.sender_id},{msg_type},{payload}\n") else: - f.write(msg.payload + "\n") + f.write(msg.payload() + "\n") f.flush() diff --git a/umnp/devices/__init__.py b/umnp/devices/__init__.py index 63fa0dae66e3e0805f51e577fd74f94e4780b598..622e4968b249674e3b7b82c83939e34718958a7a 100644 --- a/umnp/devices/__init__.py +++ b/umnp/devices/__init__.py @@ -1,8 +1,13 @@ DEVICE_TYPE_UNKNOWN = 0 DEVICE_TYPE_RHTP = 1 +DEVICE_TYPE_TEST = 777 -DEVICE_TYPE_NAMES = {DEVICE_TYPE_RHTP: "RHTP", DEVICE_TYPE_UNKNOWN: "unknown"} +DEVICE_TYPE_NAMES = { + DEVICE_TYPE_RHTP: "RHTP", + DEVICE_TYPE_UNKNOWN: "unknown", + DEVICE_TYPE_TEST: "test device", +} def get_device_type_name(device_type: int) -> str: diff --git a/umnp/proto/headers.py b/umnp/proto/headers.py index 3441dd1a10d917e952b0001f7c60897dea660261..c1c0e1df5f6688622172cfdfd8c49ec80631d9d7 100644 --- a/umnp/proto/headers.py +++ b/umnp/proto/headers.py @@ -1,6 +1,7 @@ -from umnp.devices import DEVICE_TYPE_RHTP +from umnp.devices import DEVICE_TYPE_RHTP, DEVICE_TYPE_TEST from umnp.proto.messagetype import MessageType DEVICE_HEADERS = { - DEVICE_TYPE_RHTP: {MessageType.MSG_DEVICE_DATA: ["T", "rH", "p", "T(p)"]} + DEVICE_TYPE_RHTP: {MessageType.MSG_DEVICE_DATA: ["T", "rH", "p", "T(p)"]}, + DEVICE_TYPE_TEST: {MessageType.MSG_DEVICE_DATA: ["message"]}, }