Socialize

Showing posts with label IoT sensors. Show all posts
Showing posts with label IoT sensors. Show all posts

Sunday, October 19, 2025

How IoT Works Using .NET Core Web API and SQL Server – Step by Step Guide

 Introduction to IoT (Internet of Things)

The Internet of Things (IoT) is a revolutionary technology that connects physical devices to the internet, allowing them to collect, transmit, and receive data. IoT enables automation, monitoring, and smarter decision-making in smart homes, healthcare, industrial automation, and more.

In this article, we will explore how IoT works and demonstrate a practical example using .NET Core Web API and SQL Server, which allows developers to build scalable IoT applications.


How IoT Works – Step by Step

IoT operates through a series of connected components:

1. Devices and Sensors

These are physical devices that monitor or measure conditions like temperature, humidity, motion, or light.

Example: A temperature sensor in a smart home detects 35°C in a room.

2. Connectivity

Devices transmit data to a server or cloud using protocols such as HTTP, MQTT, or CoAP. Wi-Fi, 4G/5G, and LoRaWAN are common connectivity options.

3. Data Storage and Processing

Servers, such as a .NET Core Web API connected to SQL Server, store and process the data. Analytics or automated actions can be performed based on this data.

4. Visualization and Action

The processed data is presented to users through web dashboards, mobile apps, or alerts. Actions can also be automated, such as switching on a fan if temperature exceeds a threshold.


IoT Architecture Example Using .NET Core and SQL Server

Here’s a simple smart home temperature monitoring system:

[Temperature Sensor] --> [IoT Device / Raspberry Pi] --> [Internet] --> [ASP.NET Core Web API] --> [SQL Server Database] --> [Web Dashboard / Mobile App]

Components:

  • Sensor: Collects temperature data.

  • IoT Device: Sends data to API.

  • Web API: Receives data and stores in SQL Server.

  • Database: Saves all readings for analytics.

  • Dashboard: Visualizes real-time data and triggers alerts.


Step 1: Setting Up SQL Server Database

Create a database named IoTDB with a table to store temperature readings:

CREATE TABLE TemperatureReadings ( Id INT IDENTITY(1,1) PRIMARY KEY, SensorId NVARCHAR(50), Temperature FLOAT, ReadingTime DATETIME DEFAULT GETDATE() );

Step 2: Creating .NET Core Web API Project

dotnet new webapi -n IoTWebApi cd IoTWebApi dotnet add package Microsoft.EntityFrameworkCore.SqlServer dotnet add package Microsoft.EntityFrameworkCore.Tools

Step 3: Define Entity and DbContext

public class TemperatureReading { public int Id { get; set; } public string SensorId { get; set; } public double Temperature { get; set; } public DateTime ReadingTime { get; set; } = DateTime.Now; } using Microsoft.EntityFrameworkCore; public class IoTDbContext : DbContext { public IoTDbContext(DbContextOptions<IoTDbContext> options) : base(options) { } public DbSet<TemperatureReading> TemperatureReadings { get; set; } }

Step 4: Configure Database Connection

appsettings.json

{ "ConnectionStrings": { "DefaultConnection": "Server=YOUR_SERVER_NAME;Database=IoTDB;Trusted_Connection=True;" }, "AllowedHosts": "*" }

Program.cs

builder.Services.AddDbContext<IoTDbContext>(options => options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"))); builder.Services.AddControllers();

Step 5: Create Temperature API Controller

[ApiController] [Route("api/[controller]")] public class TemperatureController : ControllerBase { private readonly IoTDbContext _context; public TemperatureController(IoTDbContext context) => _context = context; [HttpPost] public async Task<IActionResult> PostReading([FromBody] TemperatureReading reading) { _context.TemperatureReadings.Add(reading); await _context.SaveChangesAsync(); return Ok(reading); } [HttpGet] public async Task<IActionResult> GetReadings() { var readings = await _context.TemperatureReadings .OrderByDescending(r => r.ReadingTime) .ToListAsync(); return Ok(readings); } }

Step 6: Simulate IoT Sensor Data

POST Example (JSON)

{ "SensorId": "Room1", "Temperature": 35.5 }
  • This request stores the temperature reading in SQL Server.

  • GET api/temperature retrieves all readings for analytics and dashboard display.


Step 7: Visualizing Data

  • Use Angular, React, or Blazor to consume the API.

  • Display real-time graphs and trigger alerts if the temperature exceeds thresholds.


Benefits of This IoT Implementation

  1. Real-Time Monitoring: Track temperature changes instantly.

  2. Automation: Trigger devices or alerts automatically.

  3. Data Analytics: Store historical data for insights.

  4. Scalable: Multiple sensors and rooms can be integrated easily.


Conclusion

IoT bridges the physical world with the digital world. By combining .NET Core Web API and SQL Server, developers can create scalable, efficient, and real-time IoT systems for smart homes, industries, and more. With proper dashboards and analytics, businesses and users can make informed decisions and improve automation.

Blog Archive

Don't Copy

Protected by Copyscape Online Plagiarism Checker

Pages