Motion sensors are the eyes of any smart office or home automation system. But a raw PIR sensor that only toggles a light is a missed opportunity. By integrating a PIR motion sensor with the ASI Biont AI agent, you unlock context-aware automation: occupancy-based HVAC control, security alerts with AI analysis, and energy optimization that learns your patterns. This guide shows you exactly how to connect a PIR sensor (via an ESP32 or Arduino) to ASI Biont using MQTT or a COM port, with code examples you can copy today.
Why Connect a PIR Sensor to an AI Agent?
A standalone PIR sensor gives you a binary signal — motion or no motion. That's useful, but limited. When connected to ASI Biont, the AI agent can:
- Analyze patterns: detect occupancy trends over hours, days, and weeks.
- Trigger multi-step actions: turn off lights, adjust thermostat, send a Slack message — all from one motion event.
- Filter false positives: ignore pet movement or HVAC drafts using logic the AI writes on the fly.
- Log and report: generate daily occupancy reports in Excel or PDF without manual scripting.
Choosing the Right Connection Method
ASI Biont supports many protocols (see list above). For a PIR sensor, two methods are most practical:
| Method | Best For | Pros | Cons |
|---|---|---|---|
| MQTT | ESP32 with WiFi | Wireless, cloud-ready, easy to scale | Requires MQTT broker |
| COM port (Hardware Bridge) | Arduino wired to PC | Low latency, no network setup | Physical cable, limited range |
Both work seamlessly. Pick based on your hardware.
Real-World Scenario: Smart Office Occupancy Dashboard
The Problem: An open-plan office has 20 rooms. Lights and HVAC run full blast even when rooms are empty. Energy bills are high.
The Solution: Install an ESP32 with a PIR sensor in each room. Each ESP32 publishes motion status via MQTT to a central Mosquitto broker. ASI Biont subscribes to all topics, analyzes occupancy patterns, and automatically adjusts HVAC setpoints and lighting schedules. The AI also generates a weekly occupancy report and sends it to the facilities manager via email.
Step 1: Hardware Setup
- ESP32 DevKit V1
- HC-SR501 PIR sensor
- Wiring: PIR VCC → ESP32 3.3V, GND → GND, OUT → GPIO 4
- Power via USB or 5V adapter
Step 2: MicroPython Code for ESP32
from machine import Pin
import time
import network
from umqtt.simple import MQTTClient
# WiFi credentials
SSID = "Office_WiFi"
PASSWORD = "secure_password"
# MQTT broker (run Mosquitto on a Raspberry Pi or cloud)
BROKER = "192.168.1.100"
TOPIC_PUB = "office/room1/motion"
CLIENT_ID = "esp32_pir_room1"
# PIR sensor on GPIO 4
pir = Pin(4, Pin.IN)
# Connect WiFi
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(SSID, PASSWORD)
while not wlan.isconnected():
time.sleep(1)
# Connect MQTT
client = MQTTClient(CLIENT_ID, BROKER)
client.connect()
# Main loop
motion_state = 0
while True:
current = pir.value()
if current != motion_state:
motion_state = current
payload = "1" if current == 1 else "0"
client.publish(TOPIC_PUB, payload)
print(f"Published: {payload}")
time.sleep(0.5)
Step 3: Connect ASI Biont to the MQTT Broker
Now, in the ASI Biont chat, you simply type:
"Connect to MQTT broker at 192.168.1.100:1883, subscribe to office/+/motion, and log motion events to a CSV file. If any room has no motion for 30 minutes, send a Telegram alert to turn off HVAC."
The AI agent writes and runs this Python script using execute_python:
import paho.mqtt.client as mqtt
import csv
import json
from datetime import datetime
import time
BROKER = "192.168.1.100"
TOPIC = "office/+/motion"
last_motion = {} # room -> timestamp
def on_message(client, userdata, msg):
room = msg.topic.split("/")[1]
payload = msg.payload.decode()
now = datetime.now()
# Log to CSV
with open("motion_log.csv", "a", newline="") as f:
writer = csv.writer(f)
writer.writerow([now.isoformat(), room, payload])
if payload == "1":
last_motion[room] = now
# Check for inactivity
for r, t in last_motion.items():
if (now - t).total_seconds() > 1800: # 30 min
print(f"ALERT: No motion in {r} for 30 min — consider turning off HVAC.")
# Here you could call Telegram API via requests
client = mqtt.Client()
client.on_message = on_message
client.connect(BROKER, 1883, 60)
client.subscribe(TOPIC)
client.loop_forever()
Result: The AI runs this script in the cloud (sandbox), subscribes to the broker, and processes events in real time. No manual dashboard setup — just chat.
Alternative: Wired Arduino via COM Port
If you prefer a wired setup (e.g., for a single room), use an Arduino Uno connected via USB to a PC running bridge.py. The PIR output connects to digital pin 2.
Arduino sketch:
void setup() {
Serial.begin(115200);
pinMode(2, INPUT);
}
void loop() {
int motion = digitalRead(2);
if (motion == HIGH) {
Serial.println("MOTION_DETECTED");
} else {
Serial.println("MOTION_CLEAR");
}
delay(500);
}
On your PC (Windows/Linux/macOS), run:
pip install pyserial requests websockets
python bridge.py --token=YOUR_API_KEY --ports=COM3 --baud 115200
Then in ASI Biont chat:
"Connect to Arduino on COM3 at 115200 baud. Read serial data. When you see 'MOTION_DETECTED', log it and turn on a smart plug via its HTTP API at 192.168.1.50."
The AI uses the industrial_command tool with serial_write_and_read to communicate with the Arduino through the bridge.
Why This Approach Beats Traditional IoT Platforms
Traditional IoT platforms require you to:
- Set up dashboards
- Configure triggers in a UI
- Write custom backend logic
With ASI Biont, you just describe what you want in natural language. The AI agent:
- Chooses the right protocol (MQTT, COM, Modbus, etc.)
- Writes production-quality Python code using real libraries (paho-mqtt, pyserial, aiohttp)
- Handles errors, retries, and logging automatically
- Adapts the solution as your requirements change — just ask
Conclusion
Integrating a PIR motion sensor with ASI Biont transforms a simple binary input into an intelligent automation hub. Whether you use an ESP32 with MQTT for a wireless multi-room setup, or an Arduino over a COM port for a single device, the AI agent handles the heavy lifting. No coding skills required — just describe your scenario in the chat.
Ready to make your office or home smarter? Try the integration yourself at asibiont.com. Download bridge.py from the Devices page, connect your PIR sensor, and let the AI write the code for you.
Comments