edge-iot
Edge computing, IoT protocols, and embedded systems integration
What this skill does
# Edge Computing & IoT
## Overview
Building applications for edge devices, IoT protocols, and embedded systems integration.
---
## MQTT Protocol
### Broker Setup (Mosquitto)
```yaml
# docker-compose.yml
services:
mosquitto:
image: eclipse-mosquitto:2
ports:
- "1883:1883"
- "9001:9001"
volumes:
- ./mosquitto.conf:/mosquitto/config/mosquitto.conf
- mosquitto_data:/mosquitto/data
- mosquitto_log:/mosquitto/log
volumes:
mosquitto_data:
mosquitto_log:
```
```conf
# mosquitto.conf
listener 1883
listener 9001
protocol websockets
allow_anonymous false
password_file /mosquitto/config/passwd
persistence true
persistence_location /mosquitto/data/
log_dest file /mosquitto/log/mosquitto.log
```
### Node.js MQTT Client
```typescript
import mqtt from 'mqtt';
class MQTTClient {
private client: mqtt.MqttClient;
private subscriptions = new Map<string, Set<Function>>();
constructor(brokerUrl: string, options?: mqtt.IClientOptions) {
this.client = mqtt.connect(brokerUrl, {
clientId: `node_${Math.random().toString(16).slice(2, 10)}`,
clean: true,
reconnectPeriod: 5000,
...options,
});
this.client.on('connect', () => {
console.log('MQTT connected');
// Resubscribe to all topics
this.subscriptions.forEach((_, topic) => {
this.client.subscribe(topic);
});
});
this.client.on('message', (topic, payload) => {
const handlers = this.getMatchingHandlers(topic);
const message = this.parsePayload(payload);
handlers.forEach(handler => handler(topic, message));
});
this.client.on('error', (error) => {
console.error('MQTT error:', error);
});
}
subscribe(topic: string, handler: (topic: string, message: any) => void) {
if (!this.subscriptions.has(topic)) {
this.subscriptions.set(topic, new Set());
this.client.subscribe(topic);
}
this.subscriptions.get(topic)!.add(handler);
return () => {
this.subscriptions.get(topic)?.delete(handler);
if (this.subscriptions.get(topic)?.size === 0) {
this.subscriptions.delete(topic);
this.client.unsubscribe(topic);
}
};
}
publish(topic: string, message: any, options?: mqtt.IClientPublishOptions) {
const payload = typeof message === 'string'
? message
: JSON.stringify(message);
this.client.publish(topic, payload, {
qos: 1,
...options,
});
}
private getMatchingHandlers(topic: string): Set<Function> {
const handlers = new Set<Function>();
this.subscriptions.forEach((topicHandlers, pattern) => {
if (this.topicMatches(pattern, topic)) {
topicHandlers.forEach(h => handlers.add(h));
}
});
return handlers;
}
private topicMatches(pattern: string, topic: string): boolean {
const patternParts = pattern.split('/');
const topicParts = topic.split('/');
for (let i = 0; i < patternParts.length; i++) {
if (patternParts[i] === '#') return true;
if (patternParts[i] === '+') continue;
if (patternParts[i] !== topicParts[i]) return false;
}
return patternParts.length === topicParts.length;
}
private parsePayload(payload: Buffer): any {
const str = payload.toString();
try {
return JSON.parse(str);
} catch {
return str;
}
}
disconnect() {
this.client.end();
}
}
// Usage
const mqtt = new MQTTClient('mqtt://localhost:1883', {
username: 'user',
password: 'pass',
});
// Subscribe to device telemetry
mqtt.subscribe('devices/+/telemetry', (topic, data) => {
const deviceId = topic.split('/')[1];
console.log(`Device ${deviceId}:`, data);
});
// Subscribe to all events from a device
mqtt.subscribe('devices/sensor-001/#', (topic, data) => {
console.log(`${topic}:`, data);
});
// Publish command to device
mqtt.publish('devices/sensor-001/commands', {
action: 'reboot',
timestamp: Date.now(),
});
```
### Device Simulator
```typescript
class DeviceSimulator {
private mqtt: MQTTClient;
private deviceId: string;
private interval: NodeJS.Timeout | null = null;
constructor(deviceId: string, brokerUrl: string) {
this.deviceId = deviceId;
this.mqtt = new MQTTClient(brokerUrl);
// Subscribe to commands
this.mqtt.subscribe(`devices/${deviceId}/commands`, (_, command) => {
this.handleCommand(command);
});
}
start(intervalMs = 5000) {
this.interval = setInterval(() => {
this.sendTelemetry();
}, intervalMs);
}
stop() {
if (this.interval) {
clearInterval(this.interval);
this.interval = null;
}
}
private sendTelemetry() {
const telemetry = {
temperature: 20 + Math.random() * 10,
humidity: 40 + Math.random() * 20,
pressure: 1013 + Math.random() * 10,
battery: 85 + Math.random() * 15,
timestamp: Date.now(),
};
this.mqtt.publish(`devices/${this.deviceId}/telemetry`, telemetry);
}
private handleCommand(command: any) {
console.log(`Received command:`, command);
switch (command.action) {
case 'reboot':
this.mqtt.publish(`devices/${this.deviceId}/status`, {
status: 'rebooting',
timestamp: Date.now(),
});
break;
case 'update_config':
// Update local config
break;
}
}
}
```
---
## Edge Functions
### Cloudflare Workers for IoT
```typescript
// Edge function to process IoT data
export default {
async fetch(request: Request, env: Env): Promise<Response> {
const url = new URL(request.url);
// Device telemetry ingestion
if (url.pathname === '/ingest' && request.method === 'POST') {
const data = await request.json();
const deviceId = request.headers.get('X-Device-ID');
// Validate device
const device = await env.KV.get(`device:${deviceId}`);
if (!device) {
return new Response('Unauthorized', { status: 401 });
}
// Process at edge
const processed = processData(data);
// Store in Durable Object for aggregation
const aggregator = env.AGGREGATOR.get(
env.AGGREGATOR.idFromName(deviceId)
);
await aggregator.fetch(request.url, {
method: 'POST',
body: JSON.stringify(processed),
});
// Forward to origin if needed
if (processed.alert) {
await fetch('https://api.example.com/alerts', {
method: 'POST',
body: JSON.stringify({
deviceId,
alert: processed.alert,
}),
});
}
return new Response('OK');
}
return new Response('Not Found', { status: 404 });
},
};
function processData(data: any) {
// Edge processing logic
const alert = data.temperature > 30 ? 'HIGH_TEMP' : null;
return {
...data,
processedAt: Date.now(),
alert,
};
}
// Durable Object for stateful aggregation
export class DeviceAggregator {
private state: DurableObjectState;
private readings: any[] = [];
constructor(state: DurableObjectState) {
this.state = state;
}
async fetch(request: Request): Promise<Response> {
const data = await request.json();
this.readings.push(data);
// Keep last 100 readings
if (this.readings.length > 100) {
this.readings.shift();
}
// Compute aggregates
const aggregates = {
avgTemperature: this.average('temperature'),
avgHumidity: this.average('humidity'),
count: this.readings.length,
};
await this.state.storage.put('aggregates', aggregates);
return new Response(JSON.stringify(aggregates));
}
private average(field: string): number {
const values = this.readings
.map(r => r[field])
.filter(v => typeof v === 'number');
return values.reduce((a, b) => a + b, 0) / values.length;
}
}
```
---
## Embedded Systems (ESP32/Arduino)
### ESP32 with MicroPython
```python
# boot.py - WiFi connection
import network
import time
deRelated in General
modeling-omnistudio-epc-catalog
IncludedSalesforce Industries CME EPC product-modeling skill for Product2-based catalog creation. Use when creating EPC products, configuring product attributes, building offer bundles with Product Child Items, or reviewing EPC DataPack JSON metadata for product catalog changes. TRIGGER when: user creates or updates Product2 EPC records, AttributeAssignment payloads, AttributeMetadata/AttributeDefaultValues, Offer bundles, or ProductChildItem relationships. DO NOT TRIGGER when: designing OmniScripts/FlexCards/Integration Procedures (use building-omnistudio-omniscript, building-omnistudio-flexcard, or building-omnistudio-integration-procedure), implementing Apex business logic (use generating-apex), or troubleshooting deployment pipelines (use deploying-metadata).
relationship-science-coach
IncludedUse this skill for direct, practical adult relationship coaching: couples conflict, repair, trust, marriage, dating, flirting, attachment patterns, emotional connection, sex, desire differences, eroticism, kink negotiation, affection, love languages, breakups, and long-term passion. Draw on Gottman, EFT and Hold Me Tight, attachment science, modern sex research, Perel, Nagoski, Kerner, Schnarch, Love and Stosny, and flexible love-language tools. Be concrete and low-hedge. Redirect only for imminent danger, abuse, coercive control, minors, non-consent, self-harm, stalking, or medical/legal/psychiatric decisions.
building-sf-integrations
IncludedSalesforce integration architecture and runtime plumbing with 120-point scoring. Use this skill to set up Named Credentials, External Credentials, External Services, REST/SOAP callout patterns, Platform Events, and Change Data Capture. TRIGGER when: user sets up Named Credentials, External Services, REST/SOAP callouts, Platform Events, CDC, or touches .namedCredential-meta.xml files. DO NOT TRIGGER when: Connected App/OAuth config (use configuring-connected-apps), Apex-only logic (use generating-apex), or data import/export (use handling-sf-data).
venue-templates
IncludedAccess comprehensive LaTeX templates, formatting requirements, and submission guidelines for major scientific publication venues (Nature, Science, PLOS, IEEE, ACM), academic conferences (NeurIPS, ICML, CVPR, CHI), research posters, and grant proposals (NSF, NIH, DOE, DARPA). This skill should be used when preparing manuscripts for journal submission, conference papers, research posters, or grant proposals and need venue-specific formatting requirements and templates.
let-fate-decide
IncludedDraws the 12 Houses of the Zodiac Tarot spread to inject entropy into planning when prompts are vague, ambiguous, or casually delegated. Interprets the spread to guide next steps. Use when the user says 'let fate decide', 'YOLO', 'whatever', 'idk', or other nonchalant phrases, makes Yu-Gi-Oh references, or when you are about to arbitrarily pick between multiple reasonable approaches. Prefer over ask-questions-if-underspecified when the user's tone is casual or playful rather than precision-seeking.
net-ops
IncludedCross-platform network troubleshooting (Windows, macOS, Linux) via local or remote shell. Use for: DNS broken, can't resolve hostnames, nslookup/dig works but apps fail, NRPT, WFP, scutil, /etc/resolver, systemd-resolved, /etc/resolv.conf, NetworkManager, VPN DNS leak residue (ProtonVPN/Mullvad/WireGuard/AnyConnect), AV/firewall blocking DNS or DoH, Tailscale DNS interaction, intermittent connectivity, remote diagnostics over SSH.