Skip to main content

Get started with Python

Connect Python scripts to an IOSignal server for automation, data collection, bridges to other systems and virtual devices. The same iosignal package also runs on MicroPython boards (ESP32, ESP32-C3, ESP32-S3). If you are new, read core concepts in five minutes first. Tags, delivery types and the home channel are the same in every environment.

Source: github.com/remocons/iosignal-python · Package: pypi.org/project/iosignal

1. Install​

Python 3.10 or later is required. The only dependency is websockets. This page follows 0.2.5 (changes in versions and verification).

pip install iosignal

2. First connection​

The blocking client is the simplest for scripts. When it runs, the message it sends comes back through the channel, and any other program listening on the channel gets it too.

from iosignal.sync import IOSignal

with IOSignal("wss://io.iosignal.net/ws") as io:
print("CID", io.ready()) # returns the CID once connected
inbox = io.listen("room#chat") # without a handler: a queue.Queue
io.signal("room#chat", "hello")
msg = inbox.get(timeout=5)
print(msg.tag, msg.text)
print(io.ping_server(), "ms") # round trip to the server

A tag with #, such as room#chat, is a channel. Messages sent to the same tag from the web lab or the CLI client arrive too.

3. asyncio​

import asyncio
from iosignal import IOSignal

async def main():
io = IOSignal("wss://io.iosignal.net/ws")
io.listen("room#chat", lambda s: print(s.tag, s.args))
io.on_event("state", print)
async with io:
await io.ready()
io.signal("room#chat", "value", 42, b"\x01")
await asyncio.sleep(10)

asyncio.run(main())
  • Sending (signal, subscribe, unsubscribe, iam) does not wait. It returns False without a connection. A ws:// connection that logs in sends nothing before it is READY and returns False, so nothing goes out unencrypted.
  • Handlers are functions or coroutines and receive a Signal (tag, type, data, text, args, is_direct).
  • Events: state, ready, message, direct, server_signal, iam, pong, over_size, error
  • await io.call(target, topic, *args) calls a service attached to the server (server services) and raises RpcError on failure: "not connected" without a connection, "over size" when the message is over the size limit.
  • await io.ping_server(timeout=3.0) returns the round trip to the server in milliseconds. On failure it raises PingError with the same code as JS pingServer(): NOT_READY, BUSY, TIMEOUT, CONNECTION_CLOSED or INVALID_ARGUMENT.

4. What differs in Python​

  • Two clients: iosignal.IOSignal is the asyncio client; iosignal.sync.IOSignal is a blocking one that runs asyncio on a background thread. Its handlers run on that thread: they may call signal(), but not methods that wait such as ready() or call().
  • Names: Python uses underscore names where JS uses camel case (ping_server for pingServer). Timeouts are in seconds, not milliseconds.
  • Values and payloads: signal(tag, ...) picks the same payload types as JS from the values. Text and numbers are TEXT, bytes is BINARY, dict, list and None are OBJECT, and several values are MJSON (MBA when bytes are among them). Data formats
  • Login: call io.auth("dev1", "key") or io.auth("dev1.key") before connecting. A logged-in ws:// connection is encrypted with Boho (Ascon). Authentication
  • Reconnection: the client reconnects by itself and sends its subscriptions again. To finish, leave the with block or call close() or stop().
  • Not available: JS echo and link.

5. TCP and E2E​

  • A cong://host:port address connects over TCP, the same link Arduino boards use. Logged in, it is encrypted with Boho (Ascon), and a TCP switcher redirect is followed. Example: IOSignal("cong://io.iosignal.net:55488"). To choose between them, see Transports: WebSocket and CongSocket.
  • io.signal_e2e(tag, data, key) seals data with a key the server never sees (logged-in connections only). A received Signal with e2e True opens with io.decrypt_e2e(sig.data, key), which returns None for a wrong key or a forgery.

6. Making and finding devices​

A few lines make a device that follows the device rules. It appears as a card in app.iosignal.net and remocon.kr on the same public IP (home channel), where it can be controlled.

from iosignal.sync import IOSignal

with IOSignal("wss://io.iosignal.net/ws") as io:
lamp = io.device("Python lamp:Living room", type="dimmer.1", state={"on": "0", "level": "60"})
io.ready()
io.run(lambda: lamp.set(level=80)) # change device state on the client's thread

for d in io.discover(): # devices on the same home channel
print(d.id, d.name, d.category, d.ui.type, d.fields)
io.command(d, "toggle")
  • A Device answers #search, announces $panel, $view, $name, $ui, $state and #notify in that order, and answers every command with $state.
  • Standard commands (on, off, toggle, all=, key=value) are handled for you. To handle one yourself, return True from on_command(cmd, device).
  • discover() subscribes to tags only during the search and then removes only what it added. Tags your program already listens to stay.
  • Examples: virtual_lamp.py (one lamp), virtual_home.py (five devices)

7. MicroPython boards​

Install the same package on an ESP32 board to make the board a device and to work with it through a remote REPL (anonymous boards from the same router; from another network with remocon.kr device keys and login, or your own server). See MicroPython boards.

To start from a browser only, use python.iosignal.net (web IDE), the USB setup page (flashes MicroPython, then puts the package, Wi-Fi and keys on the board) and the guide. Members can also open it from the remocon.kr IDE and the REPL button on board cards.

8. When stuck​

  • If ready() raises TimeoutError, check the address and the firewall. Connection diagnosis
  • If messages do not show, check the tags and subscriptions. Delivery diagnosis
  • Calling ready() or call() in a handler of the blocking client raises RuntimeError. Wait outside handlers.