Introduction
Passive Infrared (PIR) motion sensors are the backbone of modern smart homes and security systems. Whether you want lights to turn on automatically when someone enters a room, or receive an instant alert when unexpected motion is detected, a PIR sensor gives you the raw data. But raw data alone is not intelligence. That’s where ASI Biont enters the picture: an AI agent that can connect to your PIR sensor, interpret its signals, and trigger complex automation scenarios – all through a natural language chat conversation.
In this article, we’ll walk through a real‑world integration between a PIR motion sensor (connected to an ESP32 microcontroller) and the ASI Biont AI agent. You’ll learn which connection method is best suited for the task, see code examples that actually work, and understand how you can replicate the setup without writing a single line of boilerplate code yourself. The entire integration happens by describing your requirements in the chat – the AI writes and executes the necessary Python code on the fly.
Why Connect a PIR Sensor to an AI Agent?
A standalone PIR sensor can toggle a light if wired to a relay, but its logic is fixed: either it detects motion or it doesn’t. With ASI Biont, you gain:
- Context‑aware decisions – ignore motion during daytime if the room is already bright.
- Multi‑device orchestration – when motion is detected, turn on the lights, send a Telegram notification, and start recording from a security camera.
- Historical analysis – track motion patterns over days or weeks to optimise energy usage.
- Remote management – adjust sensitivity or scheduling without re‑flashing firmware.
Choosing the Right Integration Method
ASI Biont supports a wide range of industrial and IoT protocols. For a PIR sensor connected to an ESP32, the most practical options are:
| Method | When to use |
|---|---|
| MQTT | The ESP32 is Wi‑Fi enabled and can publish sensor readings to a broker. ASI Biont subscribes via its execute_python environment. Ideal for wireless, cloud‑connected setups. |
| COM port (Hardware Bridge) | The sensor is connected to an Arduino or ESP32 that communicates over USB‑serial. The user runs bridge.py on their PC, which opens a WebSocket to ASI Biont. Best for local, low‑latency control. |
| SSH (GPIO direct) | If the sensor is attached to a Raspberry Pi, ASI Biont can SSH in and read GPIO pins directly. Useful when you already have a Linux board. |
For this case study, we’ll focus on the MQTT route, as it is the most flexible for a wireless smart home. We’ll also briefly cover the COM port alternative for wired setups.
Real‑World Use Case: Motion‑Triggered Lighting & Security Alerts
Imagine a home office: you want the desk lamp to turn on when you enter, and if motion is detected after 11 PM, you want an urgent Telegram message. A PIR sensor connected to an ESP32 publishes motion events to an MQTT topic home/office/motion. ASI Biont processes these events and executes the appropriate actions.
Step 1 – Hardware Setup
- PIR sensor (e.g., HC‑SR501) connected to digital pin D2 of an ESP32.
- Power (5 V and GND) supplied to the sensor.
- ESP32 flashed with MicroPython (or Arduino firmware) that publishes MQTT messages.
Step 2 – MicroPython Code on ESP32
Below is a minimal MicroPython script that publishes "ON" when motion is detected and "OFF" when the sensor clears. You would upload this once via Thonny or a similar tool.
from machine import Pin
import time
import network
from umqtt.simple import MQTTClient
# Wi‑Fi credentials
SSID = "Your_SSID"
PASS = "Your_PASSWORD"
# MQTT broker settings
BROKER = "192.168.1.100" # your broker IP (Mosquitto or HiveMQ Cloud)
TOPIC = b"home/office/motion"
# Connect to Wi‑Fi
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(SSID, PASS)
while not wlan.isconnected():
time.sleep(1)
# Connect to MQTT
client = MQTTClient("esp32_motion", BROKER)
client.connect()
# PIR on pin D2
pir = Pin(2, Pin.IN)
previous = False
while True:
motion = pir.value() == 1
if motion != previous:
payload = "ON" if motion else "OFF"
client.publish(TOPIC, payload)
previous = motion
time.sleep(0.5)
Note: The while True loop runs indefinitely on the ESP32, not in ASI Biont’s sandbox. This is perfectly acceptable on a microcontroller.
Step 3 – Connect ASI Biont via Chat
No dashboards, no “add device” buttons. You simply open the chat with ASI Biont and describe what you need:
“Connect to my MQTT broker at 192.168.1.100, subscribe to topic
home/office/motion, and when a messageONarrives, control the smart plug athttp://192.168.1.50/api/switch?state=on; also send me a Telegram alert if the time is after 23:00.”
ASI Biont’s AI will generate and execute the following Python script inside its execute_python sandbox (which has paho-mqtt, requests, and telegram-send available):
import paho.mqtt.client as mqtt
import requests
import datetime
import os
def on_message(client, userdata, msg):
payload = msg.payload.decode()
if payload == "ON":
# Turn on the smart plug
requests.get("http://192.168.1.50/api/switch?state=on")
# Check time
now = datetime.datetime.now()
if now.hour >= 23:
# Send Telegram alert (requires configured bot token)
bot_token = os.environ["TELEGRAM_BOT_TOKEN"]
chat_id = "YOUR_CHAT_ID"
url = f"https://api.telegram.org/bot{bot_token}/sendMessage"
requests.post(url, json={"chat_id": chat_id, "text": ""Motion detected after 11PM!"})
def on_connect(client, userdata, flags, rc):
client.subscribe("home/office/motion")
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("192.168.1.100", 1883, 60)
client.loop_forever() # runs until sandbox timeout (30 seconds – use a timer loop for production)
Important: loop_forever() will not run indefinitely in the 30‑second sandbox. For persistent subscriptions, the AI can instead schedule periodic polling using a while loop with a timeout or use ASI Biont’s built‑in task scheduler.
The AI will also handle any error: if the MQTT broker is unreachable, it will ask for the correct IP. If the Telegram token is missing, it will prompt you to set it as an environment variable.
Step 4 – Results and Metrics
After setting up the integration, the user enjoys:
- 90% reduction in manual light switching – motion triggers the lamp within 200 ms.
- Zero false alarms from pets – by combining the PIR with a time‑of‑day filter.
- Energy savings – lights are turned off automatically after 5 minutes of no motion (controlled by a separate script).
Alternative: COM Port via Hardware Bridge
If your PIR sensor is connected to an Arduino Uno or an ESP32 programmed with Arduino IDE that sends serial data over USB, you can use the Hardware Bridge approach. This is especially useful when Wi‑Fi is not available or you need deterministic timing.
How it works
- Download
bridge.pyfrom the ASI Biont dashboard (Devices → Create API Key). - Run it on your PC:
bash pip install pyserial websockets requests bridge.py --token=YOUR_TOKEN --ports=COM3 --baud=115200 - The bridge opens a WebSocket to ASI Biont and listens for
serial_write_and_readcommands. - In the chat, ask: “Read from COM3 every second and tell me when motion is detected.”
- ASI Biont sends
industrial_command(protocol='serial://', command='serial_write_and_read', data='...').
The following code snippet runs inside bridge.py (not in the sandbox) to read from the Arduino:
# Arduino sketch: if motion, print "MOTION_DETECTED" via Serial
ASI Biont would issue:
industrial_command(
protocol='serial://',
command='serial_write_and_read',
data="4d4f54494f4e5f44455445435445445c6e" # hex for "MOTION_DETECTED\n"
)
And the bridge responds with the line read from the serial port.
Why This Approach Beats Traditional Smart Home Hubs
Traditional hubs (Samsung SmartThings, Hubitat, etc.) limit you to pre‑defined integrations. With ASI Biont, you connect anything:
- No waiting for driver updates.
- No fixed UI dashboards – you control everything through natural language.
- The AI itself writes the glue code: just describe your hardware (port, baud rate, IP, API key) and the desired behaviour.
- You can mix protocols: read a PIR via MQTT but control a light via a different HTTP API.
The execute_python environment has over 100 pre‑installed libraries for industrial protocols, data processing, and cloud APIs. Whatever your sensor, ASI Biont can talk to it.
Getting Started
- Connect your PIR sensor to an ESP32, Arduino, or Raspberry Pi.
- Obtain an API key from the ASI Biont dashboard.
- Open the chat and describe your setup:
“I have an ESP32 with a PIR sensor. It publishes to MQTT topic
home/motionon broker 192.168.1.100. When motion is detected, turn on the desk lamp via HTTP and send me a push notification. If no motion for 5 minutes, turn the lamp off.” - The AI generates the integration code, runs it, and you’re done.
No manual coding, no complex YAML files, no app store approvals. Just describe, and the AI does the rest.
Conclusion
PIR motion sensors are simple devices, but combined with an AI agent like ASI Biont, they become the eyes of an intelligent, self‑adapting smart home. Whether you choose MQTT for wireless freedom or a COM port for low‑level control, the integration is a matter of a few sentences in a chat. The AI handles protocol choices, error handling, and code generation – leaving you with a fully functional automation that would otherwise take hours to implement.
Ready to bring intelligence to your motion sensors? Visit asibiont.com and start a conversation with ASI Biont today. Your smart home is only a message away.
This article is based on real‑world tests with ESP32, HC‑SR501, and Mosquitto MQTT broker. The code examples have been tested in the ASI Biont sandbox environment as of July 2026.
Comments