Making a smart home with ASP.NET Core and Arduino

During quarantine, there was a lot of time that I wanted to spend profitably and consolidate my knowledge gained from different areas, this turned out to be a small project that any developer can easily repeat.


image


I am a student, I live in a dormitory. There are always problems with the micro-climate in the room: it’s hot, then cold, then there isn’t enough air, but I wanted to live in normal conditions for comfortable work and study.


So let's get started.


Minimum required


In the corner was an old but powerful fan.


image


There is also an Arduino Uno left over from school.


image


, -. VPS Centos 7.



Arduino. .


  #include <Adafruit_GFX.h>
  #include <Adafruit_PCD8544.h>
  #include "DHT.h"
  #define DHTPIN 2 //  ,    
  int fanPin = 3;
  byte incomingByte;
  DHT dht(DHTPIN, DHT22);

  void setup() {
    pinMode(fanPin, OUTPUT);
    Serial.begin(9600);
    dht.begin();
  }

  void loop() {
    //  2   
    delay(2000);
    // 
    float h = dht.readHumidity();
    //  
    float t = dht.readTemperature();
    //     .
    if (isnan(h) || isnan(t)) {
      Serial.println("   ");
      return;
    } 

    Serial.print(" ");
    Serial.print( h );
    Serial.print(" ");
    Serial.print(t);
    Serial.print(" \n");

    if (Serial.available() > 0) {
      incomingByte = Serial.read();

      if(incomingByte == '0'){
        digitalWrite(fanPin, HIGH);
      }
      else if (incomingByte == '1'){
        digitalWrite(fanPin, LOW); 
      }
        Serial.print("I received: ");
        Serial.println(incomingByte, DEC);
    }
  }

ASP.Net ore Arduino . , / - Arduino.


        public void Program()
        {
weatherContext db = new weatherContext();
            var serialPort = new SerialPort("COM3")
            {
                BaudRate = 9600,
                Parity = Parity.None,
                StopBits = StopBits.One,
                DataBits = 8,
                Handshake = Handshake.None
            };
            serialPort.Open();
            var reg = new Regex(@"[0-9]{2}\.[0-9]{2}\s[0-9]{2}\.[0-9]{2}");

            List<DeviceDTO> deviceDTOs = new List<DeviceDTO>();
            var userDevices = db.UserDevices.Where(d => d.UserId == 0);
            foreach (var userDevice in userDevices)
            {
                var device = db.Devices.Where(d => d.Id == userDevice.DeviceId).FirstOrDefault();
                if (device != null)
                {
                    DeviceDTO deviceDTO = new DeviceDTO()
                    {
                        Id = device.Id,
                        Status = device.Status,
                        Name = device.Name
                    };
                    deviceDTOs.Add(deviceDTO);
                }
            }
            while (true)
            {
                DeviceActivator(serialPort, deviceDTOs);
                string message = serialPort.ReadExisting();
                Console.WriteLine("message is: " + message);
                var match = reg.Match(message);
                IFormatProvider formatter = new NumberFormatInfo { NumberDecimalSeparator = "." };
                if (match.Success)
                {
                    string a = message.Substring(0, 5);
                    double humidity = double.Parse(a, formatter);
                    double temperature = double.Parse(message.Substring(6, 5), formatter);

                    List<MeteringWriteDTO> meterings = new List<MeteringWriteDTO>();
                    var meteringTemperature = new MeteringWriteDTO()
                    {
                        Id = meteringCount + 1,
                        Date = DateTime.UtcNow,
                        Value = temperature,
                        SensorId = 0,
                    };
                    meterings.Add(meteringTemperature);
                    var meteringHumidity = new MeteringWriteDTO()
                    {
                        Id = meteringCount + 1,
                        Date = DateTime.UtcNow,
                        Value = humidity,
                        SensorId = 1,
                    };
                    meterings.Add(meteringHumidity);
                    SetSecondMetering(meterings);
                    Console.WriteLine(message + DateTime.UtcNow + '\n');
                    System.Threading.Thread.Sleep(1000);
                }
                if (DateTime.UtcNow.Second == 0)
                {
                    SetMinuteMetering();
                }
                if (DateTime.UtcNow.Minute == 0 && DateTime.UtcNow.Second == 0)
                {
                    SetHourMetering();
                }
                if (DateTime.UtcNow.Hour == 0 && DateTime.UtcNow.Minute == 0 && DateTime.UtcNow.Second == 0)
                {
                    AddDay();
                }
            }
        }

PostgreSQL.


, :


image


, :


image


- ASP.NET Core, . :


image


As you can see, everything is brief and capacious. You can change the status of the device and see the readings of sensors for a certain period of time.


Summary


Testing what happened:


image


Everything is working! The fan starts and stops almost without delay.


It is planned to add graphs with sensor readings, an authorization system so that other users can deploy this system at home. You can easily adjust the values ​​at which devices will turn on automatically. Also, the number of sensors and devices is supposed to increase.


If you are interested in the project, then I can make separate articles for each development step.


All Articles