IoT and devices (pyzed.iot)
Services that talk to devices: boards on a serial port, MQTT brokers, WebSockets, UDP and TCP
sockets, Modbus PLCs and meters, Bluetooth LE gadgets and what answers on the local network.
They work like the services: handlers go in on_<event>=, the
connection runs in a thread of its own and events reach the handlers on the app’s thread. In
Pyzed Design they are the components of the IoT category. See the
IoT guide.
board = pz.SerialPort(match="arduino", baud_rate=115200, on_line=self.reading)board.connect()
mqtt = pz.MqttClient("broker.local", subscribe="home/#", on_message=self.received)mqtt.connect()mqtt.publish("home/lamp/set", "on")Every connection has connect(), disconnect(), connected, running and the events
connected, disconnected and failed; with reconnect (milliseconds) it comes back by itself.
Serial ports
Section titled “Serial ports”SerialPort
Section titled “SerialPort”class SerialPortA serial or USB serial port (Arduino, ESP32, Pico, GPS…): port names it ("COM3", "/dev/ttyUSB0") or, empty, the first one match names ("arduino", "2341:0043"; empty: the first USB port). Raises data (bytes) and line (text); write(), send_line(), reset() (DTR), set_baud_rate(). SerialPort.ports(match) lists the ports. Opens the device again when it comes back (reconnect, 1000 ms by default).
SerialWatcher
Section titled “SerialWatcher”class SerialWatcherNotices serial devices being plugged in and unplugged, every interval milliseconds: plugged and unplugged with a PortInfo (name, kind, vid, pid, usb_id, manufacturer, product, description), change with the list; ports is the last list.
Messages and sockets
Section titled “Messages and sockets”MqttClient
Section titled “MqttClient”class MqttClientAn MQTT 3.1.1 client (Mosquitto, Home Assistant, EMQX…): publish(topic, payload, qos, retain), subscribe(topic), unsubscribe() and on_topic(pattern, handler) with the wildcards + and #. Each message raises message with an MqttMessage (topic, payload, text, value, json()); last keeps the last one of each topic. TLS, logins, QoS 0, 1 and 2, a last will, and the subscriptions kept after every reconnection (2000 ms by default).
WebSocket
Section titled “WebSocket”class WebSocketA WebSocket client (ws://, wss://): send() text, bytes or JSON; each message raises message (text, bytes or, with json=True, the parsed data). headers, protocols, a ping every ping_interval milliseconds.
UdpSocket
Section titled “UdpSocket”class UdpSocketSends and receives UDP datagrams: start() listens on port, each one raises message with a UdpMessage (data, text, json(), host, port); send(data, host, port), reply(message, data), broadcast and multicast_group.
TcpClient
Section titled “TcpClient”class TcpClientA TCP connection (optionally TLS) to a device or server: raises data and line; write() and send_line().
Industrial
Section titled “Industrial”ModbusClient
Section titled “ModbusClient”class ModbusClientA Modbus client over TCP (host) or RTU (serial_port, RS-485): read_coils, read_discrete_inputs, read_holding_registers, read_input_registers, read(table, address, format) ("float32", "int32"…), write_coil, write_register, write_coils, write_registers and write(address, value, format). poll lines (name table address [format] [count]) are read every interval milliseconds and raise update with a dict. Raises ModbusError when the device refuses or does not answer.
Discovery and Bluetooth
Section titled “Discovery and Bluetooth”DeviceDiscovery
Section titled “DeviceDiscovery”class DeviceDiscoveryFinds the devices of the local network with mDNS/Bonjour (service: "_http._tcp", "_esphomelib._tcp"…; empty: all) and SSDP/UPnP: scan(), start(); raises found and lost with a Device (name, host, port, address, kind, service, hostname, properties, url), change and done. pyzed.iot.resolve_local("esp32.local") finds one address.
BluetoothScanner
Section titled “BluetoothScanner”class BluetoothScannerFinds Bluetooth LE devices nearby for timeout milliseconds, filtered by match (name or address) and services: scan(), start(), devices (nearest first); raises found with a BleDevice (address, name, rssi, uuids, manufacturer_data), change, done and failed. Needs pip install "pyzed-ui[bluetooth]" (bleak).
BluetoothDevice
Section titled “BluetoothDevice”class BluetoothDeviceA connection to one Bluetooth LE device by address or name: read(uuid), write(uuid, data, response), subscribe(uuid), services(); the characteristics in notify raise notification with a BleNotification (uuid, data, text, value). UUIDs can be short ("2a37") or names ("heart_rate_measurement", "battery_level", "nordic_uart_tx").