Introduction
ESP-NOW is a wireless communication protocol developed by Espressif for ESP32 and ESP8266 microcontrollers. Unlike Wi-Fi or Bluetooth, ESP-NOW does not require a central router or access point. Devices communicate directly using a peer-to-peer connection, sending small packets (up to 250 bytes) with microsecond latency. This makes ESP-NOW ideal for sensor networks, remote control, and industrial IoT where reliability and speed matter more than bandwidth.
But what happens when you connect an ESP-NOW network to an AI agent? You get a system that not only relays sensor data but also analyzes it, predicts failures, and sends commands based on natural language instructions. This article shows you how to integrate an ESP32 running ESP-NOW with ASI Biont, using real code examples and step-by-step instructions.
Why Integrate ESP-NOW with an AI Agent?
Traditional ESP-NOW setups require manual coding for every logic branch: if temperature > 30°C, turn on fan; if motion detected, send alert. An AI agent like ASI Biont removes this burden. You describe what you want in plain English — "monitor the temperature every 10 seconds and alert me if it spikes" — and the AI writes the integration code, connects to your hardware, and runs the logic. No firmware updates, no recompilation.
Connection Method: MQTT Bridge via ESP-NOW Gateway
The ESP32 nodes themselves do not have direct internet access. To connect them to ASI Biont, we use an ESP32 gateway that both communicates with ESP-NOW peers and connects to the internet via Wi-Fi. The gateway publishes sensor data to an MQTT broker (Mosquitto) and subscribes to command topics. ASI Biont, running in the cloud, uses the MQTT protocol to read and write data.
This architecture combines the low-power, low-latency benefits of ESP-NOW with the cloud connectivity of MQTT. The AI agent in ASI Biont writes a Python script using paho-mqtt that subscribes to the sensor topic and publishes commands.
Step-by-Step Integration Guide
1. Hardware Setup
You need:
- Two ESP32 boards (one as sensor node, one as gateway)
- DHT22 temperature/humidity sensor (or any I2C/analog sensor)
- Breadboard and jumper wires
Connect DHT22 to the sensor node:
- VCC → 3.3V
- GND → GND
- DATA → GPIO4
2. ESP-NOW Sensor Node Firmware (MicroPython)
This code runs on the battery-powered sensor node. It reads the DHT22 every 30 seconds and sends the data via ESP-NOW to the gateway.
# sensor_node.py
import network
import espnow
import dht
from machine import Pin
import time
# Initialize Wi-Fi in station mode (required for ESP-NOW)
sta = network.WLAN(network.STA_IF)
sta.active(True)
# Initialize ESP-NOW
e = espnow.ESPNow()
e.active(True)
# Gateway MAC address (replace with your gateway's MAC)
gateway_mac = b'\x24\x0a\xc4\x12\x34\x56'
e.add_peer(gateway_mac)
# Sensor setup
sensor = dht.DHT22(Pin(4))
while True:
try:
sensor.measure()
temp = sensor.temperature()
hum = sensor.humidity()
# Format: "T:25.5,H:60.2"
msg = f"T:{temp:.1f},H:{hum:.1f}"
e.send(gateway_mac, msg, True)
print("Sent:", msg)
except Exception as ex:
print("Error:", ex)
time.sleep(30)
3. ESP-NOW Gateway Firmware (MicroPython)
The gateway receives ESP-NOW packets from multiple sensor nodes and publishes them to an MQTT broker.
# gateway.py
import network
import espnow
import time
from umqtt.simple import MQTTClient
# Wi-Fi credentials
WIFI_SSID = "your_ssid"
WIFI_PASS = "your_password"
# MQTT broker (Mosquitto or HiveMQ)
MQTT_BROKER = "broker.hivemq.com"
MQTT_TOPIC_SENSORS = "home/espnow/sensors"
# Connect to Wi-Fi
sta = network.WLAN(network.STA_IF)
sta.active(True)
sta.connect(WIFI_SSID, WIFI_PASS)
while not sta.isconnected():
time.sleep(0.5)
print("Wi-Fi connected")
# Initialize ESP-NOW
e = espnow.ESPNow()
e.active(True)
print("ESP-NOW ready. MAC:", sta.config('mac'))
# Connect to MQTT
client = MQTTClient("esp32_gateway", MQTT_BROKER)
client.connect()
print("MQTT connected")
while True:
host, msg = e.recv() # blocking wait
if msg:
print(f"Received from {host}: {msg}")
client.publish(MQTT_TOPIC_SENSORS, msg)
4. ASI Biont Integration (via Chat)
In the ASI Biont chat, describe your setup:
"Connect to MQTT broker at broker.hivemq.com, subscribe to topic home/espnow/sensors, parse temperature and humidity values, and alert me if temperature exceeds 35°C or falls below 10°C. Also publish commands to topic home/espnow/commands/1 to turn on LED on the sensor node."
ASI Biont will generate and execute a Python script like this:
import paho.mqtt.client as mqtt
import json
import time
BROKER = "broker.hivemq.com"
TOPIC_SENSORS = "home/espnow/sensors"
TOPIC_COMMANDS = "home/espnow/commands/1"
def on_message(client, userdata, msg):
payload = msg.payload.decode()
print(f"Received: {payload}")
# Parse "T:25.5,H:60.2"
try:
parts = payload.split(',')
temp = float(parts[0].split(':')[1])
hum = float(parts[1].split(':')[1])
print(f"Temperature: {temp}°C, Humidity: {hum}%")
if temp > 35:
print("ALERT: Temperature exceeds 35°C!")
# Publish command to turn on LED
client.publish(TOPIC_COMMANDS, "LED_ON")
elif temp < 10:
print("ALERT: Temperature below 10°C!")
client.publish(TOPIC_COMMANDS, "LED_ON")
else:
client.publish(TOPIC_COMMANDS, "LED_OFF")
except Exception as e:
print(f"Parse error: {e}")
client = mqtt.Client()
client.on_message = on_message
client.connect(BROKER, 1883, 60)
client.subscribe(TOPIC_SENSORS)
client.loop_forever()
This script runs in the ASI Biont sandbox (execute_python) and continuously monitors the MQTT stream. The user does not write any of this — the AI generates and executes it based on the chat description.
Real-World Scenarios
Scenario 1: Agricultural Greenhouse Monitoring
A farmer deploys 20 ESP32 nodes with DHT22 and soil moisture sensors across a greenhouse. Each node sends data via ESP-NOW to a central gateway. The AI agent monitors all sensors, detects when soil moisture drops below 30%, and activates irrigation valves via MQTT commands to ESP32-controlled relays. The farmer simply asks: "Keep soil moisture between 30% and 60% and alert me if temperature exceeds 40°C."
Scenario 2: Industrial Machine Vibration Monitoring
An ESP32 with an ADXL345 accelerometer is attached to a motor. It sends vibration data via ESP-NOW. The AI agent runs FFT analysis on the data (using numpy in execute_python) and predicts bearing failure 48 hours before it happens. The maintenance team receives a Telegram alert.
Scenario 3: Smart Home Multi-Room Control
Each room has an ESP32 with a PIR motion sensor and relay for lights. ESP-NOW nodes communicate directly — no Wi-Fi needed. The AI agent learns occupancy patterns and optimizes lighting schedules. The user says: "Turn off lights in rooms that have been empty for 10 minutes." The AI sends MQTT commands to the gateway, which forwards them via ESP-NOW.
Why This Approach Works
| Method | Latency | Power | Range | Cloud Required |
|---|---|---|---|---|
| ESP-NOW only | < 1 ms | Very low | ~200m | No |
| MQTT + Wi-Fi | 50-200 ms | High | Internet | Yes |
| ESP-NOW + MQTT gateway | 10-50 ms | Low | Internet | Yes (gateway only) |
The combination gives you the best of both: sensor nodes run on batteries for months, while the gateway handles cloud connectivity. The AI agent sits in the cloud and orchestrates everything.
How ASI Biont Makes It Effortless
You don't need to write Python scripts, set up MQTT clients, or parse protocols. ASI Biont connects to any device through execute_python — the AI itself writes the integration code for each device. Just describe in the chat:
- Which device to connect to (ESP-NOW gateway via MQTT)
- Connection parameters (broker address, port, topics)
- What you want to automate (alerts, commands, data logging)
The AI generates the code using paho-mqtt, executes it in the sandbox, and handles the entire data pipeline. No dashboard panels, no "add device" buttons — everything happens through conversation.
Limitations and Considerations
- ESP-NOW packet size is limited to 250 bytes. For larger payloads, split data across multiple packets.
- The gateway must be within ESP-NOW range of all sensor nodes (typically 200m line-of-sight).
- MQTT broker must be reachable from the gateway. For local-only setups, run Mosquitto on a Raspberry Pi.
- The ASI Biont sandbox has a 30-second timeout for scripts. For long-running monitoring, use the MQTT callback pattern (loop_forever is allowed).
Conclusion
ESP-NOW is a powerful protocol for low-power, low-latency IoT networks. When combined with an AI agent like ASI Biont, it becomes a platform for intelligent automation — not just data relay, but real-time analysis, predictive maintenance, and natural-language control. The integration requires no manual coding: you describe your needs, and the AI writes and runs the code.
Ready to connect your ESP32s to an AI brain? Try the integration at asibiont.com. No setup, no coding — just describe what you want to automate.
Comments