Skip to content

mishcodes09/EnergySmartMeter

Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

5 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

image

⚑ Smart Energy Meter API

A RESTful API built with ASP.NET Core 8 to collect, store, and serve energy meter readings from IoT devices such as ESP8266. The API supports CRUD operations, CSV export, pagination, and filtering by device or date range.


🌟 Features

  • πŸ“₯ POST energy readings from devices
  • πŸ“„ GET single or multiple readings with pagination
  • πŸ•’ Retrieve the latest reading per device
  • πŸ’Ύ Export readings as CSV
  • 🌐 Supports CORS for IoT devices like ESP8266
  • πŸ“Š In-memory or SQL Server database support
  • πŸ› οΈ Auto-generated Swagger UI for API testing

πŸ› οΈ Project Structure

SmartEnergyMeterAPI/
β”‚
β”œβ”€β”€ Controllers/
β”‚   └── EnergyMeterController.cs       # Main API controller
β”‚
β”œβ”€β”€ Models/
β”‚   β”œβ”€β”€ EnergyReading.cs               # Database model
β”‚   └── EnergyReadingDto.cs            # DTO for incoming POST requests
β”‚
β”œβ”€β”€ Data/
β”‚   └── ApplicationDbContext.cs        # EF Core DbContext
β”‚
β”œβ”€β”€ Program.cs                         # Application entry point
β”œβ”€β”€ SmartEnergyMeterAPI.csproj         # Project file with dependencies
└── README.md

yaml Copy code


βš™οΈ Setup Instructions

1. Prerequisites

  • .NET 8 SDK
  • Visual Studio 2022 or VS Code
  • Optional: SQL Server for persistent storage

2. Clone Repository

git clone https://github.com/yourusername/SmartEnergyMeterAPI.git
cd SmartEnergyMeterAPI

3. Configure Database

By default, In-Memory Database is used for development.

To use SQL Server, add a connection string in appsettings.json:

json Copy code "ConnectionStrings": { "DefaultConnection": "Server=YOUR_SERVER;Database=EnergyMeterDb;Trusted_Connection=True;" } The API will automatically switch to SQL Server if the connection string is provided.

4. Run the API

bash Copy code dotnet run Swagger UI will be available at https://localhost:{PORT}/swagger

πŸ“ API Endpoints

POST /api/EnergyMeter/reading Adds a new energy reading

Body (JSON)

json Copy code { "voltage": 230.5, "current": 5.2, "power": 1200.4, "frequency": 50.0, "powerFactor": 0.98, "deviceId": "ESP8266_01" } Response

201 Created with the saved reading

GET /api/EnergyMeter/reading/{id} Retrieve a specific reading by its ID

GET /api/EnergyMeter/readings Retrieve multiple readings with optional query parameters:

Parameter Type Description page int Page number (default 1) pageSize int Records per page (default 50) deviceId string Filter by device ID fromDate DateTime Start date filter toDate DateTime End date filter

GET /api/EnergyMeter/readings/latest Retrieve the latest reading for a specific device (optional deviceId query)

GET /api/EnergyMeter/readings/csv Export readings as CSV

Optional filters: deviceId, fromDate, toDate

Response

CSV file with columns: DateTime,Voltage,Current,Power,Frequency,PowerFactor,DeviceId

🧩 Dependencies

Microsoft.EntityFrameworkCore.SqlServer

Microsoft.EntityFrameworkCore.InMemory

Microsoft.EntityFrameworkCore.Tools

Swashbuckle.AspNetCore (Swagger)

⚠️ Notes

##πŸ›‘οΈ CORS is enabled for ESP8266 devices

Time is stored in UTC

Suitable for IoT integrations and energy monitoring dashboards

πŸ“„ License

This project is licensed under the MIT License.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 100.0%