🏠 Smart Home / IoT
Typ: REST + MQTT + ESP32
Auth: API Keys / Local
Status: ✅ DIY + Commercial
Übersicht
Smart Home Integration für: - Türschlösser (ESP32 DIY) - Sensoren (Temperatur, Luftqualität) - Schalter und Steckdosen - Heizungssteuerung
ESP32 DIY (Eigenbau)
Hardware Stack
| Komponente | Verwendung |
|---|---|
| ESP32-S3 | Controller |
| ESPHome | Firmware |
| MQTT | Kommunikation |
| Home Assistant | Optional Hub |
ESPHome Beispiel
# esp32-door-lock.yaml
esphome:
name: door-lock-wohnung-3a
platform: ESP32
board: esp32-s3-devkitc-1
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
mqtt:
broker: 192.168.25.10
username: !secret mqtt_user
password: !secret mqtt_password
topic_prefix: property/wohnung-3a
# Türschloss Servo
servo:
- id: lock_servo
pin: GPIO18
lock:
- platform: template
name: "Haustür WE 3A"
id: front_door_lock
lock_action:
- servo.write:
id: lock_servo
level: 0%
unlock_action:
- servo.write:
id: lock_servo
level: 100%
# Türsensor
binary_sensor:
- platform: gpio
pin: GPIO19
name: "Tür Status"
device_class: door
# NFC Reader (PN532)
pn532_i2c:
i2c_id: bus_a
on_tag:
then:
- if:
condition:
lambda: 'return x == "04:A3:B2:C1:D0:E9";' # Authorized tag
then:
- lock.unlock: front_door_lock
- mqtt.publish:
topic: property/wohnung-3a/access
payload: '{"status":"granted","tag":"${x}"}'
MQTT Topics
property/{unit}/lock/state → locked | unlocked
property/{unit}/lock/command → LOCK | UNLOCK
property/{unit}/door/state → open | closed
property/{unit}/access → { status, tag, timestamp }
property/{unit}/sensors/temp → 21.5
property/{unit}/sensors/humidity → 65
SonicJS MQTT Bridge
// src/integrations/mqtt-bridge.ts
import mqtt from 'mqtt';
interface MQTTConfig {
broker: string;
username?: string;
password?: string;
topics: string[];
}
export class MQTTBridge {
private client: mqtt.MqttClient;
constructor(config: MQTTConfig) {
this.client = mqtt.connect(config.broker, {
username: config.username,
password: config.password
});
this.client.on('connect', () => {
config.topics.forEach(topic => {
this.client.subscribe(topic);
});
});
}
async unlock(unitId: string): Promise<void> {
this.client.publish(`property/${unitId}/lock/command`, 'UNLOCK');
}
async lock(unitId: string): Promise<void> {
this.client.publish(`property/${unitId}/lock/command`, 'LOCK');
}
onAccessEvent(callback: (event: AccessEvent) => void): void {
this.client.on('message', (topic, message) => {
if (topic.includes('/access')) {
callback(JSON.parse(message.toString()));
}
});
}
}
API Endpoints
| Methode | Endpunkt | Beschreibung | Cache TTL |
|---|---|---|---|
GET |
/api/iot/devices |
Alle Geräte | 30s |
GET |
/api/iot/devices/:id |
Gerätestatus | 5s |
POST |
/api/iot/devices/:id/command |
Befehl senden | - |
GET |
/api/iot/properties/:id/sensors |
Sensordaten | 10s |
GET |
/api/iot/access-logs |
Zugangs-Logs | 1min |
POST |
/api/iot/access/grant |
Zugang gewähren | - |
DELETE |
/api/iot/access/:tagId |
Zugang entziehen | - |
Shelly Integration (Optional)
// Shelly Cloud API
const SHELLY_API = 'https://shelly-eu-api.shelly.cloud';
async function getShellyDevices(authKey: string) {
const response = await fetch(`${SHELLY_API}/device/all_status`, {
headers: { 'Authorization': `Bearer ${authKey}` }
});
return response.json();
}
async function toggleSwitch(deviceId: string, channel: number, on: boolean) {
await fetch(`${SHELLY_API}/device/relay/control`, {
method: 'POST',
headers: { 'Authorization': `Bearer ${authKey}` },
body: JSON.stringify({
id: deviceId,
channel,
turn: on ? 'on' : 'off'
})
});
}
Umgebungsvariablen
# MQTT Broker
MQTT_BROKER="mqtt://192.168.25.10:1883"
MQTT_USER="homeassistant"
MQTT_PASSWORD="secret"
# Shelly (optional)
SHELLY_AUTH_KEY=""
SHELLY_SERVER_URI="https://shelly-eu-api.shelly.cloud"
# ESP32 OTA
ESP32_OTA_PASSWORD="secret"
Sicherheit
Türschlösser
- MQTT mit TLS verschlüsseln
- NFC-Tags mit Rolling Codes
- Fallback: Physischer Schlüssel
- Audit-Log für alle Zugriffe
ESP32 DIY • ESPHome • MQTT • Shelly optional