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. Aws://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 raisesRpcErroron 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 raisesPingErrorwith the samecodeas JSpingServer():NOT_READY,BUSY,TIMEOUT,CONNECTION_CLOSEDorINVALID_ARGUMENT.
4. What differs in Python
- Two clients:
iosignal.IOSignalis the asyncio client;iosignal.sync.IOSignalis a blocking one that runs asyncio on a background thread. Its handlers run on that thread: they may callsignal(), but not methods that wait such asready()orcall(). - Names: Python uses underscore names where JS uses camel case (
ping_serverforpingServer). 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,bytesis BINARY,dict,listandNoneare OBJECT, and several values are MJSON (MBA when bytes are among them). Data formats - Login: call
io.auth("dev1", "key")orio.auth("dev1.key")before connecting. A logged-inws://connection is encrypted with Boho (Ascon). Authentication - Reconnection: the client reconnects by itself and sends its subscriptions again. To finish, leave the
withblock or callclose()orstop(). - Not available: JS
echoandlink.
5. TCP and E2E
- A
cong://host:portaddress 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 receivedSignalwithe2eTrue opens withio.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
Deviceanswers#search, announces$panel,$view,$name,$ui,$stateand#notifyin 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 fromon_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()raisesTimeoutError, check the address and the firewall. Connection diagnosis - If messages do not show, check the tags and subscriptions. Delivery diagnosis
- Calling
ready()orcall()in a handler of the blocking client raisesRuntimeError. Wait outside handlers.