Step-by-step guide to stream sensor data to an ML model via a web UI—learn easy integration techniques for smart IoT solutions!

Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
// Example: A simulated sensor in Python using threading to generate data
import time
import random
import websocket // Assume a websocket client library
def simulate_sensor_data():
// Continuously generate data every second
ws = websocket.create\_connection("ws://localhost:5000/sensor")
try:
while True:
data = {'temperature': random.uniform(20.0, 30.0), 'humidity': random.uniform(30.0, 60.0)}
ws.send(str(data)) // Send as string or JSON format in a real implementation
time.sleep(1)
finally:
ws.close()
if **name** == '**main**':
simulate_sensor_data()
// Example: Flask server integrating a pre-trained ML model for real-time inference
from flask import Flask, render\_template
from flask\_socketio import SocketIO, emit
import json
import pickle // For loading a simple ML model
app = Flask(**name**)
socketio = SocketIO(app)
# Load your pre-trained ML model (for example, a simple regression or classification model)
with open('ml\_model.pkl', 'rb') as file:
ml\_model = pickle.load(file)
def preprocess\_data(data):
// Convert data to proper format for ML inference; example: list of sensor values
features = [data.get('temperature', 0), data.get('humidity', 0)]
return [features]
def run\_inference(features):
// Get prediction from ML model
prediction = ml\_model.predict(features)
return prediction[0]
@socketio.on('sensor\_data')
def handle_sensor_data(message):
try:
// Parse incoming sensor data
data = json.loads(message)
features = preprocess\_data(data)
prediction = run\_inference(features)
// Emit prediction result to connected clients (for UI updates)
emit('ml\_prediction', {'prediction': prediction})
except Exception as e:
// Log error or manage exception
print("Error processing sensor data:", e)
@app.route('/')
def index():
// Render the main web UI which will display live predictions
return render\_template('index.html')
if **name** == '**main**':
socketio.run(app, port=5000)
Real-Time Sensor Data & ML Predictions
Streaming Sensor Data with ML Predictions
Sensor Data: Waiting for data...
ML Model Prediction: Pending...
From startups to enterprises and everything in between, see for yourself our incredible impact.
Need a dedicated strategic tech and growth partner? Discover what RapidDev can do for your business! Book a call with our team to schedule a free, no-obligation consultation. We’ll discuss your project and provide a custom quote at no cost.