Bachelors Program in Computer Science Engineering
Internet of Things
•
Bachelor
IoT Data Engineering, Analytics and AI
Introduction
Internet of Things (IoT) systems convert physical activity into digital observations. Sensors measure temperature, vibration, pressure, location, energy use, or human interaction; gateways and networks transport these observations; data platforms store and analyze them; and control systems act on the results. The engineering challenge is not merely collecting data. It is preserving meaning, time, quality, security, and operational usefulness as data move from constrained devices to analytical and artificial-intelligence (AI) systems.
An IoT data pipeline normally includes sensing, ingestion, validation, transformation, storage, stream processing, feature engineering, model inference, visualization, and feedback. This chapter develops these stages and shows how statistical analysis, anomaly detection, forecasting, machine learning, TinyML, and federated learning can be applied responsibly.
Learning Objectives
After completing this chapter, students should be able to:
Design an IoT ingestion and storage pipeline.
Distinguish event time, processing time, watermarks, and allowed lateness.
Clean and aggregate time-series data using reproducible rules.
Select suitable statistical, machine-learning, and forecasting methods.
Explain anomaly detection, TinyML, and federated learning.
Implement a small end-to-end pipeline using practical tools.
Evaluate accuracy, latency, resource use, privacy, and operational risk.
1. IoT Data Engineering Foundations
1.1 Data characteristics
IoT data differ from conventional business records in several ways:
High velocity: devices may produce observations every millisecond or second.
Temporal dependence: nearby observations are usually related.
Heterogeneity: payloads may use different units, schemas, precisions, and identifiers.
Imperfect quality: packets can be duplicated, delayed, missing, corrupted, or out of order.
Edge constraints: devices may have limited memory, energy, bandwidth, and computational power.
Operational consequences: a false alarm can stop production, while a missed alarm can damage equipment or endanger people.
A useful canonical event schema contains:
Field
Purpose
device_id
Stable device identity
event_time
Time at which sensing occurred, preferably UTC
ingest_time
Time received by the platform
sequence_no
Device sequence for duplicate and ordering checks
metric
Measurement name
value
Numeric or categorical measurement
unit
Physical unit, such as °C or kPa
quality_flag
Calibration, validity, or missingness status
firmware_version
Reproducibility and diagnostics
A measurement is not meaningful without its unit, calibration context, and time semantics. A temperature reported as 72 may mean °C or °F; unit normalization must occur before analysis.
1.2 A reference architecture
IoT data engineering pipeline from sensors to decisions
Figure: IoT data engineering pipeline from sensors to decisions
Figure: A layered IoT pipeline combining durable storage and real-time analytics.
A production design commonly uses MQTT or HTTP at the device boundary, a broker such as Mosquitto or Apache Kafka for transport, a stream processor such as Apache Flink, Spark Structured Streaming, or Kafka Streams, and a time-series database such as TimescaleDB, InfluxDB, or OpenTelemetry-compatible storage. Object storage in Parquet format is useful for historical data and model training. Grafana, Superset, or a custom web application can provide visualization.
The design should separate the raw zone (immutable received records), the validated zone (schema and quality checks applied), and the serving zone (aggregates and features optimized for queries). This separation supports replay, auditing, and model reproducibility.
2. Ingestion and Time-Series Storage
2.1 Reliable ingestion
An ingestion service should authenticate the device, validate the topic and schema, attach receipt metadata, reject malformed records, and acknowledge only after the message has been safely accepted. Important reliability mechanisms include:
idempotent writes using (device_id, sequence_no) or an event identifier;
bounded retries with exponential backoff;
dead-letter queues for messages requiring investigation;
buffering at gateways during network outages;
partitioning by device group or geographic region;
monitoring of throughput, lag, rejected records, and queue depth.
“At least once” delivery may create duplicates, so downstream operations must be idempotent. “Exactly once” usually refers to a carefully defined processing guarantee, not a magical property of the whole physical system.
2.2 Time-series modeling
A time-series record can be represented as (ti, xi), where ti is event time and xi is the observed value. A database should index time and device identity, retain tags with controlled cardinality, and apply retention and downsampling policies. Excessive unique tags, such as a rapidly changing request identifier, can degrade performance.
For a regular five-minute energy series, a query might compute average, minimum, maximum, count, and standard deviation per interval. Aggregation must specify whether empty intervals become null, zero, or an imputed estimate; these choices have different meanings.
3. Event Time, Watermarks, and Cleaning
3.1 Time semantics
Event time is when the device says the event happened. Processing time is when a computation handles it. Ingestion time is when the platform receives it. Network delays mean these times can differ substantially.
A watermark is the system’s estimate that events earlier than a particular event-time threshold are unlikely to arrive. For example, a watermark of 10:05:00 means a processor may close windows ending at or before 10:05, subject to its implementation policy. The watermark is a progress estimate, not a duration for which the system waits.
Allowed lateness is a separate policy: it specifies how long after a window’s end the system continues accepting late events and revising results. A system could advance its watermark conservatively while permiting a ten-minute correction period, or advance quickly while accepting no late updates. Late data may be sent to a side output, update an aggregate, or be discarded according to documented policy.
3.2 Quality operations
A defensible cleaning sequence is:
Validate schema, type, range, unit, and timestamp.
Remove exact duplicates and resolve sequence conflicts.
Mark missing values rather than silently converting them to zero.
Identify impossible values using physical limits and sensor diagnostics.
Resample only when the analytical task requires a regular grid.
Impute with a documented method, retaining an imputation flag.
Preserve the raw record and an audit trail.
For linear interpolation between valid points, if ta ≤ t ≤ tb, then:
x(t) = xa + (xb − xa) (t − ta)/(tb − ta)
This assumes a reasonably smooth change and should not bridge long outages or known abrupt events. Forward filling is often suitable for configuration state, but is usually inappropriate for rapidly changing physical measurements.
A rolling mean over the latest k observations is:
mt = (1/k) ∑i=0k−1 xt−i
It reduces random noise but introduces delay and can hide short faults. For a five-point sequence 10, 12, 11, 13, 14, the three-point mean at the final position is (11 + 13 + 14) ÷ 3 = 12.67 units.
Robust scaling uses the median and median absolute deviation (MAD):
zt = (xt − median(x)) / (1.4826 × MAD(x))
where MAD(x) = median(|xt − median(x)|). The factor 1.4826 makes MAD comparable to standard deviation for normally distributed data; this interpretation is not required for arbitrary distributions.
4. Stream Processing and Visualization
Window types serve different questions:
Tumbling window: non-overlapping fixed intervals.
Sliding window: repeated intervals with a specified size and slide.
Session window: activity separated by an inactivity gap.
A stream job should define its trigger, watermark strategy, late-data policy, state retention, and output mode. State must be bounded or checkpointed; otherwise a long-running job can exhaust memory.
A practical dashboard should display current values, trend, uncertainty or quality status, alarm state, and last successful update. It should not imply precision greater than sensor accuracy. Visualizations should support drill-down from an aggregate alarm to device, time, raw records, and model version.
A small executable example
The following Python example illustrates parsing, unit conversion, validation, and hourly aggregation. It is intentionally local and educational; production systems should add authentication, persistent queues, schema registries, and observability.
The Fahrenheit-to-Celsius conversion is TC = (5/9)(TF − 32), with both temperatures interpreted as degrees on their respective scales. The invalid 200 °F value becomes approximately 93.3 °C and is rejected under the chosen physical range. The duplicate is removed using a declared identity rule.
5. Statistical Analysis and Anomaly Detection
Descriptive analysis begins with count, mean, median, variance, quantiles, missingness, and sampling intervals. The sample mean and sample variance are:
x̄ = (1/n) ∑i=1n xi
s² = [1/(n−1)] ∑i=1n (xi − x̄)²
The n−1 denominator estimates population variance from a sample under standard independent-sampling assumptions. IoT observations often violate independence, so confidence estimates require care.
An anomaly is context-dependent. A value can be normal globally but abnormal for a particular machine, shift, season, or operating mode. Methods include:
fixed engineering limits;
rolling z-scores;
robust MAD scores;
exponentially weighted moving averages (EWMA);
change-point detection;
isolation forests and one-class support vector machines;
autoencoders for multivariate patterns.
For EWMA:
St = λxt + (1−λ)St−1, 0 < λ ≤ 1
A larger λ reacts faster but smooths less. Thresholds must be calibrated on representative normal data and validated against maintenance records. Evaluation should report precision, recall, false alarms per operating hour, detection delay, and performance across devices—not only accuracy.
6. Forecasting and Machine Learning
Forecasting predicts future values, while anomaly detection identifies observations inconsistent with expected behavior. A baseline such as “same value one period ago” or a seasonal median should be tested before complex models.
For a simple autoregressive model of order one:
xt = c + φxt−1 + εt
where c is an intercept, φ is the lag coefficient, and εt is an error term. A stable stationary AR(1) process generally requires |φ| < 1; this condition should not be assumed for every time series.
Supervised learning requires labeled examples (X, y). Common IoT tasks are failure classification, remaining-life regression, demand forecasting, and image or acoustic classification. Features may include lagged values, rolling statistics, frequency-domain measures, operating state, and environmental variables.
Time must be respected during evaluation. Randomly shuffling temporal records can leak future information into training. Use chronological train/validation/test splits or rolling-origin evaluation. Classification metrics include:
precision = TP/(TP+FP), recall = TP/(TP+FN)
where TP, FP, and FN denote true positives, false positives, and false negatives. The appropriate trade-off depends on the cost of missed failures and unnecessary interventions. Regression metrics include mean absolute error (MAE):
MAE = (1/n) ∑i=1n |yi − ŷi|
Always compare a model with a simple baseline and monitor drift after deployment.
7. TinyML and Edge Intelligence
TinyML runs inference on microcontrollers or other resource-limited devices. Benefits include low latency, reduced bandwidth, offline operation, and improved privacy. The constraints include limited RAM and flash, integer arithmetic, energy consumption, sensor variability, and difficult software updates.
A typical workflow is: collect representative data; label and split by time/device; train centrally; quantize or prune the model; convert to TensorFlow Lite for Microcontrollers or a similar runtime; test memory, latency, energy, and accuracy; deploy with a rollback mechanism; and monitor drift through summary statistics rather than raw data where appropriate.
Quantization maps a floating-point value to a limited integer range. Calibration must cover expected operating conditions; otherwise saturation can damage accuracy. A small model is not automatically safe: an incorrect edge decision may trigger a physical actuator. Safety interlocks and conservative fallback behavior remain necessary.
8. Federated Learning
Federated learning trains models at multiple devices or sites while keeping raw training data local. Each client receives a global model, performs local optimization, and sends parameters or updates to an aggregator. It can reduce data movement but does not automatically guarantee privacy; updates can leak information and require secure aggregation, access control, and possibly differential privacy.
Let wt be the global parameter vector at round t. Client k has nk training examples and returns locally trained parameters wt+1(k). With K participating clients and N = ∑k=1K nk, FedAvg is:
wt+1 = ∑k=1K (nk/N) wt+1(k)
This formula is correct because the client quantity is the locally trained parameter vector. If clients instead send an update ∆wt(k) = wt+1(k) − wt, the equivalent aggregation is:
wt+1 = wt + ∑k=1K (nk/N) ∆wt(k)
Confusing parameters with updates produces an incorrect algorithm. FedAvg assumes a compatible parameterization and uses data-size weighting; non-identically distributed client data, unreliable participation, communication cost, and client drift can reduce convergence. Evaluation should include per-client performance, not only the global average.
9. Illustrative Case: Predictive Maintenance in a Pumping Station
Background
A municipal pumping station operates 40 pumps. Vibration, motor current, temperature, and pressure are sampled at the edge. Unplanned bearing failures cause service interruptions.
Problem
The existing threshold system generates many alarms during legitimate high-load operation and misses gradual bearing degradation. Raw high-frequency data cannot all be transmitted over the available link.
Analysis
Engineers first synchronize clocks, verify sensor calibration, and join records with maintenance work orders. They discover missing vibration intervals during wireless outages and a change in sensor firmware that shifts the baseline. Random train/test splitting would leak repeated pump behavior, so evaluation is performed by time and by held-out pumps.
Approach and solution
A gateway computes one-minute root-mean-square vibration, temperature slope, current mean, and pressure variability. The raw data are buffered locally and uploaded when connectivity returns. A stream processor applies event-time windows and permits late corrections for ten minutes after a window closes. A robust baseline model produces anomaly scores; a gradient-boosted classifier is trained only when confirmed maintenance labels exist. An edge model classifies short vibration spectrograms for immediate screening, while a central model performs slower fleet-level analysis. The system shows alarm explanation, data quality, model version, and recommended inspection rather than automatically stopping a pump.
Outcome and learning points
In a pilot, success is measured by reduced false alarms, detection lead time, missed failures, bandwidth, and maintenance cost—not by model accuracy alone. The key lesson is that data contracts, event-time policy, labels, evaluation design, and operational safeguards are as important as the algorithm.
10. Governance, Security, and Professional Practice
IoT analytics can reveal occupancy, health, industrial processes, or worker behavior. Data minimization, purpose limitation, retention schedules, consent where applicable, access controls, encryption in transit and at rest, and audit logs should be designed from the beginning. Device identities and keys require secure provisioning and rotation. Firmware should be signed, updates authenticated, and vulnerable components tracked.
Models should be documented with a data sheet or model card describing scope, training population, limitations, known failure modes, and monitoring. Human oversight is essential for safety-critical decisions. Governance must also address cross-border data transfer, sector-specific regulation, intellectual property, and the rights of people affected by automated decisions.
11. Best-Practice Checklist
Define event-time and unit semantics before collecting data.
Preserve immutable raw data and reproducible transformations.
Make writes idempotent and design for duplicates and outages.
Separate watermark progress from allowed-lateness policy.
Validate against physical constraints and retain quality flags.
Use time-aware, leakage-resistant evaluation.
Establish simple baselines before complex AI models.
Measure latency, resource use, calibration, fairness, and operational cost.
Monitor drift, missingness, firmware changes, and alarm burden.
Provide safe fallback behavior and a human escalation path.
Chapter Summary
IoT data engineering creates a reliable path from physical measurements to decisions. Robust pipelines use canonical schemas, authenticated ingestion, durable raw storage, time-series indexing, idempotency, quality flags, and explicit event-time semantics. Watermarks estimate event-time progress, whereas allowed lateness defines how late results may revise a computation. Cleaning, resampling, interpolation, and rolling statistics must be tied to the physical meaning of the data.
Statistical summaries and baselines provide essential reference points for anomaly detection and forecasting. Machine-learning systems require time-aware evaluation, meaningful labels, drift monitoring, and metrics aligned with operational costs. TinyML moves suitable inference to constrained devices, while federated learning keeps raw data local; in FedAvg, parameter aggregation and update aggregation must be defined distinctly. Across all approaches, security, privacy, safety, governance, and explainability are engineering requirements rather than optional additions.
Key Terms
Event time: Time at which an event occurred at the source.
Processing time: Time at which a system processes an event.
Watermark: An estimate of event-time progress used to manage incomplete streams.
Allowed lateness: Policy specifying how long late events may revise results.
Idempotency: Property that repeating an operation does not change its intended result.
Time-series database: Database optimized for timestamped observations.
Sliding window: Window that advances by a slide interval and may overlap.
Session window: Window separated from the next by an inactivity gap.
Data drift: Change in input distribution over time.
Concept drift: Change in the relationship between inputs and target.
Anomaly detection: Identification of observations inconsistent with expected behavior.
Forecasting: Estimation of future values using historical and contextual data.
TinyML: Machine learning executed on resource-constrained edge devices.
Quantization: Representing model values with lower-precision numerical formats.
Federated learning: Collaborative model training that keeps raw data at clients.
FedAvg: Federated averaging of locally trained parameters, usually weighted by client sample counts.
Differential privacy: Privacy framework that limits the effect of any one record on an output.
Model drift: Degradation caused by changing data, behavior, or operating conditions.
Data lineage: Record of data origin, transformations, and destinations.
Review Questions
Short Answer Questions
Why should an IoT pipeline preserve raw data separately from validated data?
Distinguish event time from processing time.
What is the purpose of an idempotent ingestion operation?
State two constraints that influence TinyML design.
Why can random train/test splitting be unsafe for time-series data?
Long Answer Questions
Explain a complete IoT pipeline from sensor measurement to dashboard decision, including reliability controls.
Compare tumbling, sliding, and session windows and give an appropriate use for each.
Explain cleaning, interpolation, missingness flags, and physical-range validation in a time-series system.
Discuss anomaly-detection evaluation when false alarms and missed failures have different costs.
Explain FedAvg using both locally trained parameters and locally computed updates, and discuss non-identically distributed client data.
Analytical / Application Questions
A sensor reports 68 °F, 69.8 °F, and 200 °F. Convert the values to Celsius using TC = (5/9)(TF − 32), identify the questionable value under a −40 °C to 85 °C validity range, and propose an audit-preserving treatment.
Design an event-time policy for a factory stream in which network delays are usually below two minutes but occasional outages last eight minutes. Specify a watermark approach, allowed-lateness policy, and treatment of records arriving later than the policy.
Three federated clients have 100, 300, and 600 training examples and return parameter vectors w(1), w(2), and w(3). Write the FedAvg weights and explain how the result differs from averaging client updates added to the current global model.
Multiple Choice Questions with Answers
Question 1
Which timestamp represents when a sensor measurement physically occurred?
A. Event time
B. Processing time
C. Dashboard refresh time
D. Database backup time
Correct Answer: A. Event time
Question 2
What is the primary purpose of a watermark in stream processing?
A. To encrypt messages
B. To estimate event-time progress
C. To specify sensor calibration
D. To compress historical data
Correct Answer: B. To estimate event-time progress
Question 3
Which statement correctly distinguishes allowed lateness from a watermark?
A. They are exactly the same setting
B. Allowed lateness controls model size, while a watermark controls encryption
C. A watermark estimates progress; allowed lateness states how long late results may revise output
D. A watermark applies only to batch data
Correct Answer: C. A watermark estimates progress; allowed lateness states how long late results may revise output
Question 4
For three consecutive values 11, 13, and 14, what is their arithmetic mean?
A. 11.67
B. 12.00
C. 12.67
D. 13.67
Correct Answer: C. 12.67
Question 5
Which practice best prevents future information from entering time-series model training?
A. Randomly shuffle all observations
B. Use chronological or rolling-origin evaluation
C. Duplicate the latest observations
D. Remove all timestamps
Correct Answer: B. Use chronological or rolling-origin evaluation
Question 6
In the sample variance formula, why is the denominator commonly n−1 rather than n?
A. To convert Celsius to Fahrenheit
B. To estimate population variance from a sample under standard assumptions
C. To remove all outliers
D. To guarantee independent observations
Correct Answer: B. To estimate population variance from a sample under standard assumptions
Question 7
Which is a direct benefit of TinyML?
A. It requires unlimited device memory
B. It guarantees perfect predictions
C. It can reduce latency and bandwidth by performing inference at the edge
D. It eliminates the need for model updates
Correct Answer: C. It can reduce latency and bandwidth by performing inference at the edge
Question 8
In FedAvg, if clients send locally trained parameter vectors, how are they combined?
A. By selecting the largest vector component
B. By a sample-count-weighted average of the client parameter vectors
C. By always subtracting every client vector from the global model
D. By averaging only the client with the most data
Correct Answer: B. By a sample-count-weighted average of the client parameter vectors
Question 9
What does idempotency mean in an ingestion pipeline?
A. Every message is intentionally delayed
B. Repeated processing has the same intended result as one processing
C. All data are converted to images
D. Only one device may publish
Correct Answer: B. Repeated processing has the same intended result as one processing
Question 10
Which metric is especially useful when false alarms impose substantial operational cost?
A. False-positive rate or precision
B. Number of model parameters only
C. File compression ratio only
D. Screen brightness
Correct Answer: A. False-positive rate or precision