How to Connect a PIR Motion Sensor to AI Agent ASI Biont: Smart Home Automation Without Coding

Introduction

A Passive Infrared (PIR) motion sensor is one of the simplest yet most powerful devices for smart home automation. It detects movement by measuring changes in infrared radiation — typically from a human body — and outputs a digital signal (HIGH/LOW). But a raw PIR sensor alone is limited: it can trigger a light or alarm at the hardware level, but it cannot distinguish between a person and a pet, send notifications to your phone, or coordinate with other smart devices like cameras and thermostats.

Connecting a PIR sensor to ASI Biont — an AI agent that writes and executes integration code on the fly — transforms this basic sensor into an intelligent motion detection system. With ASI Biont, you can:

  • Receive Telegram alerts when motion is detected
  • Automatically turn on lights via a smart plug or relay
  • Trigger camera recording via RTSP or HTTP API
  • Log motion events to a database for analytics
  • All without writing a single line of code manually — the AI writes the integration for you.

Why Integrate a PIR Sensor with an AI Agent?

Traditional PIR integrations require:
- Manual coding in Arduino IDE or MicroPython
- Setting up an MQTT broker and writing subscribers/publishers
- Debugging connection issues and handling edge cases
- Maintaining the code when requirements change

ASI Biont eliminates these barriers. You simply describe your device and desired outcome in natural language. The AI agent chooses the appropriate connection method (MQTT, GPIO via SSH, or COM port via Hardware Bridge), generates the Python code, and runs it in a sandboxed environment. If something goes wrong, you can ask the AI to fix it — no need to open an IDE.

Real Connection Methods Supported by ASI Biont

ASI Biont connects to PIR sensors through three primary methods, depending on your hardware setup:

Method Hardware Required How It Works Best For
MQTT ESP32/ESP8266 with MQTT firmware (e.g., ESPHome, Tasmota) Sensor publishes motion status to an MQTT topic; ASI Biont subscribes via paho-mqtt Wireless, cloud-based automation
SSH + GPIO Raspberry Pi, Orange Pi, or any Linux SBC with GPIO AI connects via SSH (paramiko), reads GPIO pin state, and executes commands on the Pi Local, low-latency control
COM port (Hardware Bridge) Arduino Uno/Nano, ESP32 via USB serial User runs bridge.py on PC; AI sends serial commands through industrial_command Direct wired connection, real-time control

All methods are supported by ASI Biont's execute_python tool, which has access to paho-mqtt, paramiko, and industrial_command (for bridge).

Concrete Use Case: PIR Sensor + ESP32 + MQTT + ASI Biont

Scenario

You have an ESP32 with a PIR sensor (e.g., HC-SR501) wired to GPIO 4. The ESP32 runs a simple MQTT publisher that sends "motion": "detected" or "motion": "clear" to topic home/sensor/pir. You want ASI Biont to:
1. Subscribe to that topic
2. When motion is detected, send a Telegram message to your phone
3. If motion is detected at night (after 8 PM), turn on a smart plug via HTTP API

Step-by-Step Implementation

1. ESP32 Code (MicroPython)

import network
import time
from machine import Pin
import ujson
from umqtt.simple import MQTTClient

# WiFi credentials
WIFI_SSID = "your_ssid"
WIFI_PASS = "your_password"

# MQTT broker
MQTT_BROKER = "192.168.1.100"
MQTT_TOPIC = b"home/sensor/pir"

# PIR sensor on GPIO 4
pir = Pin(4, Pin.IN)

# Connect to WiFi
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(WIFI_SSID, WIFI_PASS)
while not wlan.isconnected():
    time.sleep(0.5)

# Connect to MQTT
client = MQTTClient("esp32_pir", MQTT_BROKER)
client.connect()

# Main loop
last_state = pir.value()
while True:
    current_state = pir.value()
    if current_state != last_state:
        payload = ujson.dumps({"motion": "detected" if current_state == 1 else "clear"})
        client.publish(MQTT_TOPIC, payload)
        last_state = current_state
    time.sleep(0.1)

2. ASI Biont Integration (User Describes in Chat)

You open chat with ASI Biont and say:

"Connect to MQTT broker at 192.168.1.100, subscribe to topic 'home/sensor/pir'. When motion is detected, send a Telegram message to chat ID 123456789 using bot token 'xxxx'. Also, if the current hour is >= 20, turn on smart plug at http://192.168.1.200/cm?cmnd=Power%20ON."

ASI Biont generates and executes the following Python code (which runs in the sandbox with paho-mqtt and aiohttp):

import paho.mqtt.client as mqtt
import asyncio
import aiohttp
from datetime import datetime

TELEGRAM_BOT_TOKEN = "xxxx"
TELEGRAM_CHAT_ID = "123456789"
SMART_PLUG_URL = "http://192.168.1.200/cm?cmnd=Power%20ON"

def on_message(client, userdata, msg):
    payload = msg.payload.decode()
    if '"motion": "detected"' in payload:
        # Send Telegram notification
        asyncio.run(send_telegram("Motion detected!"))
        # Check if night time
        hour = datetime.now().hour
        if hour >= 20 or hour < 6:
            asyncio.run(turn_on_plug())

async def send_telegram(text):
    async with aiohttp.ClientSession() as session:
        url = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/sendMessage"
        data = {"chat_id": TELEGRAM_CHAT_ID, "text": text}
        async with session.post(url, json=data) as resp:
            return await resp.json()

async def turn_on_plug():
    async with aiohttp.ClientSession() as session:
        async with session.get(SMART_PLUG_URL) as resp:
            return await resp.text()

client = mqtt.Client()
client.on_message = on_message
client.connect("192.168.1.100", 1883, 60)
client.subscribe("home/sensor/pir")
client.loop_forever()

3. Result

  • The AI runs the script (note: sandbox timeout is 30 seconds, but MQTT loop_forever is allowed since it's event-driven)
  • Every motion event triggers a Telegram alert and, if it's nighttime, the smart plug turns on
  • You can ask the AI to add logging, change the plug URL, or send a different message — all by chatting

Alternative: Using Hardware Bridge with Arduino

If you prefer a wired connection (e.g., Arduino Uno connected via USB), you can use the Hardware Bridge method:

  1. Download bridge.py from ASI Biont
  2. Run on your PC: python bridge.py --token=YOUR_TOKEN --ports=COM3 --default-baud=9600
  3. In chat, tell ASI Biont: "Connect to Arduino on COM3 at 9600 baud. Read pin 2 digital value every second. When it goes HIGH, send me a Telegram message."

The AI will use industrial_command with protocol serial:// to read the pin status and act accordingly.

Why This Approach Is Superior

  • Zero coding required: The AI writes all integration code based on your description
  • Flexible: Change behavior simply by asking — no need to flash firmware or edit scripts
  • Secure: All AI-generated code runs in a sandbox with no access to your local filesystem
  • Fast: Integration takes seconds instead of hours
  • Scalable: Connect dozens of sensors and actuators, each with custom logic

Conclusion

A PIR motion sensor is a simple input device, but when combined with the AI agent ASI Biont, it becomes the brains of your smart home. Whether you use an ESP32 with MQTT, a Raspberry Pi with GPIO, or an Arduino via serial, ASI Biont connects in moments and lets you automate anything — from lighting to security — without writing code.

Ready to give your PIR sensor superpowers? Go to asibiont.com, describe your setup in the chat, and watch the AI build your integration in real time.

← All posts

Comments