1. Integrasi Multiple Sensor
Dalam sistem IoT nyata, kita biasanya perlu mengintegrasikan beberapa sensor untuk mendapatkan data yang komprehensif. Setiap sensor memiliki karakteristik dan interface yang berbeda.
Challenge Integrasi Sensor
- Interface Variability: Digital vs Analog, I2C vs SPI vs UART
- Data Rate: Sampling rate yang berbeda-beda
- Power Management: Konsumsi daya yang bervariasi
- Data Correlation: Mensinkronisasi data dari berbagai sumber
- Error Handling: Menangani sensor failure gracefully
Jenis-jenis Sensor dan Interface
Temperature Sensor
DHT22, LM35, DS18B20
- Interface: Digital (OneWire), Analog
- Range: -40°C to 80°C
- Accuracy: ±0.5°C
- Power: 3-5V, 1-2.5mA
Humidity Sensor
DHT22, SHT31, BME280
- Interface: I2C, Digital
- Range: 0-100% RH
- Accuracy: ±2% RH
- Power: 3.3V, 1.5μA (sleep)
Motion Sensor
PIR, Ultrasonic, Radar
- Interface: Digital Output
- Range: Up to 7 meters
- Detection: Human movement
- Power: 3-5V, 65μA (PIR)
Gas/Air Quality
MQ series, CCS811
- Interface: Analog, I2C
- Gases: CO, CO2, VOC
- Warm-up: 1-3 minutes
- Power: 5V, 150mA (heater)
2. Workflow Integrasi Sensor
1
Sensor Selection
Pilih sensor berdasarkan requirements project
2
Interface Design
Rancang interface dan wiring diagram
3
Driver Development
Buat library dan driver untuk setiap sensor
4
Data Acquisition
Implementasi sampling dan data collection
5
Data Processing
Filtering, calibration, dan sensor fusion
sensor_integration.ino - Multi-Sensor Reading
#include
#include
#include
#include
// Sensor objects
Adafruit_BME280 bme;
DHT dht(DHTPIN, DHT22);
// Sensor data structure
struct SensorData {
float temperature;
float humidity;
float pressure;
int airQuality;
bool motionDetected;
unsigned long timestamp;
};
SensorData currentData;
void setup() {
Serial.begin(115200);
// Initialize BME280
if (!bme.begin(0x76)) {
Serial.println("BME280 not found!");
}
// Initialize DHT22
dht.begin();
// Initialize motion sensor
pinMode(MOTION_PIN, INPUT);
// Initialize air quality sensor
pinMode(AIR_QUALITY_PIN, INPUT);
}
void readAllSensors() {
currentData.timestamp = millis();
// Read from BME280
currentData.temperature = bme.readTemperature();
currentData.humidity = bme.readHumidity();
currentData.pressure = bme.readPressure() / 100.0F;
// Read from DHT22 (backup)
float dhtTemp = dht.readTemperature();
float dhtHumidity = dht.readHumidity();
// Sensor fusion - average readings
if (!isnan(dhtTemp)) {
currentData.temperature = (currentData.temperature + dhtTemp) / 2;
}
if (!isnan(dhtHumidity)) {
currentData.humidity = (currentData.humidity + dhtHumidity) / 2;
}
// Read motion sensor
currentData.motionDetected = digitalRead(MOTION_PIN);
// Read air quality (analog)
currentData.airQuality = analogRead(AIR_QUALITY_PIN);
}
void loop() {
readAllSensors();
sendDataToCloud();
delay(10000); // Read every 10 seconds
}
IoT Integration Lab
Simulasi integrasi multiple sensor dalam sistem IoT
Workspace
ESP32
WiFi + Bluetooth
Component Library
DHT22
Temp & Humidity
BME280
Pressure Sensor
PIR Sensor
Motion Detection
MQ-135
Air Quality
Relay Module
Switch Control
LCD Display
16x2 I2C
3. Data Flow & Processing Pipeline
IoT Data Pipeline
Sensor Layer
Data Acquisition
→
Processing
Filter & Calibrate
→
Aggregation
Data Fusion
→
Cloud
Storage & Analytics
→
Application
Dashboard & Control
data_processing.cpp - Sensor Fusion
class SensorFusion {
private:
float tempSamples[10];
int sampleCount;
public:
SensorFusion() : sampleCount(0) {}
float movingAverage(float newSample) {
// Shift samples
for(int i = 8; i >= 0; i--) {
tempSamples[i+1] = tempSamples[i];
}
tempSamples[0] = newSample;
if(sampleCount < 10) sampleCount++;
// Calculate average
float sum = 0;
for(int i = 0; i < sampleCount; i++) {
sum += tempSamples[i];
}
return sum / sampleCount;
}
bool detectAnomaly(float current, float previous, float threshold) {
return abs(current - previous) > threshold;
}
float calibrateTemperature(float rawTemp, float calibrationOffset) {
return rawTemp + calibrationOffset;
}
};
// Usage example
SensorFusion fusion;
void processSensorData() {
float rawTemp = readTemperatureSensor();
float calibratedTemp = fusion.calibrateTemperature(rawTemp, 0.5);
float smoothedTemp = fusion.movingAverage(calibratedTemp);
if(fusion.detectAnomaly(smoothedTemp, previousTemp, 2.0)) {
Serial.println("Temperature anomaly detected!");
}
previousTemp = smoothedTemp;
}
4. Power Management untuk IoT Devices
Power Optimization Strategies
- Sleep Modes: Deep sleep, light sleep, modem sleep
- Duty Cycling: Aktifkan sensor hanya saat diperlukan
- Sensor Selection: Pilih sensor low-power
- Communication Optimization: Minimize data transmission
- Power Gating: Matikan power ke sensor yang tidak digunakan
power_management.ino - Deep Sleep Implementation
#include
#define uS_TO_S_FACTOR 1000000
#define SLEEP_DURATION 300 // 5 minutes in seconds
RTC_DATA_ATTR int bootCount = 0;
void setup() {
Serial.begin(115200);
delay(1000);
bootCount++;
Serial.println("Boot number: " + String(bootCount));
// Read sensors
readAllSensors();
sendDataToCloud();
// Configure wakeup sources
esp_sleep_enable_timer_wakeup(SLEEP_DURATION * uS_TO_S_FACTOR);
// Configure GPIO wakeup (optional)
esp_sleep_enable_ext0_wakeup(GPIO_NUM_33, 1);
Serial.println("Going to sleep now");
delay(1000);
// Enter deep sleep
esp_deep_sleep_start();
}
void loop() {
// This will never be reached
}
IoT Project Planner
Rencanakan sistem IoT lengkap dengan integrasi multiple sensor
Project Requirements
System Architecture
dalam detik
Implementation Checklist
- Design system architecture diagram
- Select appropriate sensors and components
- Create wiring diagram and PCB layout
- Develop sensor drivers and calibration routines
- Implement data processing and fusion algorithms
- Set up cloud connectivity and data storage
- Create web/mobile dashboard for monitoring
- Implement power management strategies
- Test and validate system performance
- Document the complete project