System as Code Experience
The System as Code Experience in Coreflux provides a revolutionary approach to IoT orchestration by combining documentation and implementation in a single, executable document using LOT Notebooks.
What is System as Code?
System as Code is a paradigm that allows you to create complete, self-documenting IoT systems using LOT Notebooks (.lotnb
files). These notebooks combine:
- Documentation: Markdown cells explain your system architecture and logic
- Code: Executable LOT code cells define models, rules, actions, and routes
- Python Integration: Python code cells for complex data processing, machine learning, and external API integrations
- Visualization: Real-time data charts and diagrams
-
Documentation and Code in One
Combines markdown documentation with executable LOT code, creating self-documenting IoT systems.
-
End-to-End Solutions
Define complete IoT orchestrations with models, rules, actions, routes, and Python processing all in one place.
-
Shareable and Reusable
Create templates for common use cases that can be shared across teams and organizations.
Key Features
- Interactive Development: Write, execute, and see results in real-time
- Documentation First: Explain your system before implementing it
- Automatic Deployment: Execute cells to deploy components to your broker
- Python Integration: Native Python support with direct function calls from LOT actions
- Visualization: Built-in charting for time-series data
- Seamless Integration: Direct connection to your Coreflux MQTT broker
- Version Control: Notebook files can be stored in Git repositories
When to Choose System as Code
The System as Code Experience is ideal for:
- System Architects: Designing complete IoT solutions
- Solution Developers: Building end-to-end IoT applications
- Documentation Teams: Creating comprehensive guides with working examples
- Collaborative Teams: Sharing knowledge and implementation in one document
Use Cases
Coreflux LOT Notebooks excel at creating complete, deployable IoT solutions for specific business needs:
Asset Management Notification Center
Create a notebook that: - Defines models to transform asset data - Sets up rules for access control - Creates actions to trigger alerts based on asset conditions - Implements email routes for notifications - Provides complete documentation of the notification system
Manufacturing Process Data Pipeline
Develop a notebook that: - Models sensor data from manufacturing equipment - Creates rules to secure sensitive production data - Defines actions to respond to production anomalies - Sets up routes to connect multiple factories - Documents the entire pipeline from sensors to analytics
Smart Building with Python Analytics
Create a notebook that: - Uses Python for complex data analysis and machine learning - Processes sensor data with Python functions - Implements predictive maintenance algorithms - Integrates with external APIs for weather data - Provides real-time insights and recommendations
Getting Started
To begin using the System as Code Experience:
- Install Coreflux MQTT Broker:
- Follow the MQTT Broker installation guide to set up your Coreflux MQTT Broker
-
Ensure your broker is running and accessible
-
Explore Ready-to-Use Notebook Examples:
- Temperature Monitoring System: A complete example of monitoring and alerting based on temperature readings
- Browse additional LOT Notebook Samples to find examples relevant to your use case
-
Download examples to use as templates for your own projects
-
Set Up Your Environment to Run the Examples:
- Install Visual Studio Code: Download and install VS Code
-
Install LOT Notebooks Extension:
- Open VS Code Extensions view (Ctrl+Shift+X or Cmd+Shift+X)
- Search for and install the LOT Notebooks extension
-
Connect to Your Coreflux Broker:
- Open the Command Palette (Ctrl+Shift+P or Cmd+Shift+P)
- Type "LOT Notebook: Change Credentials" and select it
- Enter your broker details:
- URL (e.g., mqtt://localhost:1883)
- Username (default is "root")
- Password
-
The extension will test the connection and notify you if it's successful
-
Open and Run the Example Notebooks:
- Open the downloaded notebook files (.lotnb) in VS Code
- Execute the code cells to deploy the components to your Coreflux broker
-
Observe the system in action using an MQTT client like MQTT Explorer
-
Create Your Own Notebook:
- Open the Command Palette (Ctrl+Shift+P or Cmd+Shift+P)
- Type "LOT Notebook: Create New Notebook" and select it
- Add markdown cells to document your system
- Add code cells with LOT definitions (models, rules, actions)
- Add Python cells for complex data processing (must start with
# Script Name:
) - Execute cells to deploy your system components to your Coreflux broker
Python Integration Example
Here's a complete example of how Python integrates with LOT in a System as Code notebook:
Python Cell
# Script Name: SmartThermostat
import json
from datetime import datetime
def calculate_comfort_score(temperature, humidity, target_temp=22):
"""Calculate comfort score based on temperature and humidity"""
temp_diff = abs(temperature - target_temp)
temp_score = max(0, 100 - temp_diff * 5) # 5 points per degree difference
# Humidity comfort (40-60% is ideal)
if 40 <= humidity <= 60:
humidity_score = 100
else:
humidity_score = max(0, 100 - abs(humidity - 50) * 2)
overall_score = (temp_score + humidity_score) / 2
return {
"comfort_score": round(overall_score, 1),
"temperature_score": round(temp_score, 1),
"humidity_score": round(humidity_score, 1),
"recommendation": get_recommendation(overall_score, temperature, target_temp)
}
def get_recommendation(score, current_temp, target_temp):
"""Get heating/cooling recommendation"""
if score >= 80:
return "Comfortable - no action needed"
elif current_temp < target_temp:
return "Turn on heating"
else:
return "Turn on cooling"
def analyze_trend(readings):
"""Analyze temperature trend from recent readings"""
if len(readings) < 3:
return {"trend": "insufficient_data"}
recent = readings[-3:]
if all(recent[i] < recent[i+1] for i in range(len(recent)-1)):
return {"trend": "rising", "rate": recent[-1] - recent[0]}
elif all(recent[i] > recent[i+1] for i in range(len(recent)-1)):
return {"trend": "falling", "rate": recent[0] - recent[-1]}
else:
return {"trend": "stable", "rate": 0}
LOT Action Cell
DEFINE ACTION SmartThermostatControl
ON TOPIC "sensors/room" DO
SET sensor_data GET TOPIC "sensors/room" AS JSON
CALL PYTHON "SmartThermostat.calculate_comfort_score"
WITH ({sensor_data.temperature}, {sensor_data.humidity}, 22)
RETURN AS {comfort_analysis}
CALL PYTHON "SmartThermostat.analyze_trend"
WITH ({sensor_data.recent_temperatures})
RETURN AS {trend_analysis}
PUBLISH TOPIC "thermostat/comfort" WITH {comfort_analysis}
PUBLISH TOPIC "thermostat/trend" WITH {trend_analysis}
IF {comfort_analysis.comfort_score} < 60 THEN
PUBLISH TOPIC "thermostat/command" WITH {comfort_analysis.recommendation}
END
This example demonstrates how Python handles complex calculations and analysis while LOT orchestrates the IoT workflow, all within a single, executable notebook.
Next Steps
- LOT Language Documentation: Learn the Language of Things
- Python Integration Guide: Complete Python integration documentation
- Python Examples: Comprehensive Python examples and patterns
- LOT Notebooks Overview: Complete documentation for the VS Code extension
- LOT Notebook Samples: More example notebooks for common use cases