Skip to content

smart meter simulator for real-time energy monitoring with AWS IoT Core.

License

Notifications You must be signed in to change notification settings

afondiel/SmartMeterSim

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

⚡ SmartMeterSim

Demo on HF Spaces

Overview

SmartMeterSim is an IoT smart meter simulator for real-time energy monitoring, enabling cloud integration with AWS IoT Core and Interactive dashboard for real-time data visualization. It serves as a foundation for real-world applications such as smart grid and home automation.

Features

  • Simulated Smart Meter: Publishes real-time energy data
  • AWS IoT Core Integration: Secure MQTT.v5 communication
  • Dashboard: Live visualization of energy consumption
  • Scalable Architecture: Can integrate with real smart meters device

Real-world Applications

  • Smart Grid: Grid monitoring, energy analytics
  • Industrial IoT(IIoT): IoT energy metering in factories
  • Home Automation: Integration with smart home systems
  • EV Charging: Energy usage tracking for EV charging stations

Project Structure

📂 SmartMeterSim/
├── 📂 auth/                  # Device cert, key, ca, policy files (gitignored)
├── 📂 data
│   ├── smart_meter_data.csv   # Energy meter data
├── 📂 src
│   ├── mqtt_publisher.py      # Simulates a smart meter & sends data to AWS IoT
│   ├── mqtt_subscriber.py     # Receives & processes real-time meter data 
├── 📂 dashboard
│   ├── gradio_app.py           # Live visualization
│   ├── streamlit_app.py        # Real-time visualization
└── 📂 config
    └── aws_iot_config.json    # AWS IoT credentials (endpoint, topic, devide Id ...)

🔧 Setup & Installation

Prerequisites

  • Python 3.7+
  • AWS Account with AWS IoT Core access

Steps

  1. Clone the repository
git clone https://github.com/afondiel/SmartMeterSim.git
cd SmartMeterSim
  1. Create a virtual environment (Recommended)
python3 -m venv .smsenv
source .smsenv/bin/activate
  1. Install dependencies
pip install -r requirements.txt

Set up AWS IoT Core & Credentials

1️⃣ Using AWS Management Console

  1. Go to AWS IoT Core -> Connect (Make sure you're connected to AWS IoT)
ping <your-aws-iot-account>-ats.iot.<region>.amazonaws.com
  1. Create a new Thing named smartMeter01 -> Next
  2. Choose a platform and device SDK (e.g.: Linux and Python)
  3. Download connection kit -> Next
  4. Instead of 'Run connection kit' -> unzip and copy into auth/ folder
  • Upate your smartMeter01-Policy.jsonand replace all the default config with the following:
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "iot:Connect",
            ],
            "Resource": ["arn:aws:iot:YOUR_REGION:YOUR_AWS_ACCOUNT_ID:connect/${client_id}"]
        },
        {
            "Effect": "Allow",
            "Action": [
                "iot:Publish",
                "iot:Receive",
                "iot:PublishRetain"
            ],
            "Resource": ["arn:aws:iot:YOUR_REGION:YOUR_AWS_ACCOUNT_ID:topic/smartmeter/data"]
        },
        {
            "Effect": "Allow",
            "Action": [
                "iot:Subscribe"
            ],
            "Resource": ["arn:aws:iot:YOUR_REGION:YOUR_AWS_ACCOUNT_ID:topicfilter/smartmeter/data"]
        }
    ]
}

Go back to AWS IoT Core and update the thing policy json file and set the new version as "ACTIVE"

2️⃣ Install all at once using AWS IoT CLI

Run the script:

cd resources/tools/scripts
chmod +x setup_aws_iot.sh
./setup_aws_iot.sh

For Windows user you can run each instruction starting by aws <command>... inside your cmd/PS (If you encounter any problem, please raise an issue)

  • If you haven't AWS IoT CLI installed yet, check out this guide.

Finally:

  • Update config/aws_iot_config.toml with your AWS settings.
cp aws_iot_config_temp aws_iot_config.json  # Create a copy
nano aws_iot_config  # Edit and add your credentials

Usage

1️⃣ Run the Smart Meter Simulator

Run in this order: subscriber > publisher

Subscriber: connects and receives data from AWS IoT MQTT Broker

  python3 src/mqtt_subscriber.py \
    --endpoint <YOUR_AWS_ENDPOINT> \
    --rootCA <ROOT_CA_PATH> \
    --cert <CERT_PATH> \
    --key <KEY_PATH> \
    --logFile <YOUR_LOG_FILE_PATH> \
    --clientId <CLIENT_ID> \
    --topic <TOPIC>
  • Publisher: connects and sends device data to the AWS IoT MQTT Broker
  python3 src/mqtt_publisher.py \
    --path <CSV_PATH> \
    --endpoint <YOUR_AWS_ENDPOINT> \
    --rootCA <ROOT_CA_PATH> \
    --cert <CERT_PATH> \
    --key <KEY_PATH> \
    --clientId <CLIENT_ID> \
    --delay <DELAY> \
    --topic <TOPIC> 

2️⃣ Start the Dashboard

Demo on HF Spaces

  • Gradio:
  python3 dashboard/gradio_app.py \
    --logFile <YOUR_LOG_FILE_PATH>
  • Streamlit:
streamlit run dashboard/streamlit_app.py (TBD)

Security

  • TLS Encryption for MQTT Communication
  • AWS IoT Policies for Controlled Access
  • Environment Secrets for Credential Protection

Contributing

Contributions to SmartMeterSim are welcome! Please refer to the project's GitHub repository for contribution guidelines.

Future Roadmap

  1. IoT Smart Meter Simulator (Current Stage)
  2. Scaling & Cloud-to-Edge Transition
  3. Real Smart Meter Integration (in your software stack)

Contact

Feel free to reach out to me on LinkedIn, if you have any questions. I'll be glad to help!