Files
identity-micropython-tag/example_main.py

67 lines
1.9 KiB
Python

# ============================================================+
# Identity IoT — Example main.py for Pico W
# ============================================================+
# Copy to Pico W as main.py for standalone operation.
# Connects to WiFi, registers node, authenticates, prints JWT.
# ============================================================+
import network
import utime
from identity_iot import IdentityIoT
# ---- WiFi config ----
SSID = 'YOUR_SSID'
PASSWORD = 'YOUR_PASSWORD'
# ---- Backend config ----
BASE_URL = 'https://ap.parta.app/API/V1/identity_layer/identity_iot/index.php'
TENANT_ID = 1
NODE_TYPE = 'sensor' # sensor | reader | actuator | gateway
NODE_LABEL = 'pico-01'
# ---- Bootstrap ITEMS keys (from your tenant config) ----
COMM_KEY = b'pm4u5YahhYSZJwGNzpA7QeD0F0O8R8Eo' # 32 bytes
COMM_IV = b'DPUBMeD0Jc42ZbMM' # 16 bytes
def connect_wifi():
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(SSID, PASSWORD)
timeout = 15
while not wlan.isconnected() and timeout > 0:
utime.sleep(1)
timeout -= 1
if not wlan.isconnected():
raise Exception('WiFi connection failed')
print('WiFi connected:', wlan.ifconfig()[0])
def main():
connect_wifi()
identity = IdentityIoT(
base_url = BASE_URL,
node_type = NODE_TYPE,
node_label= NODE_LABEL,
tenant_id = TENANT_ID,
comm_key = COMM_KEY,
comm_iv = COMM_IV,
)
# Register (idempotent — safe to call every boot)
print('Registering node...')
reg = identity.ensure_registered()
print('Node:', reg.get('result'), '— uid:', reg.get('node_uid', '')[:16] + '...')
# Authenticate
print('Authenticating...')
token = identity.authenticate()
print('JWT:', token[:40] + '...')
# Node UID (hashed — safe to log)
print('Node UID:', identity.get_node_uid()[:16] + '...')
main()