Hi!

This is my page on home automation, nothing fancy, just my own creations to my life easier.

My curtains project

Home Automation

Parts that I used to make this happen.

  • EtherTen Ethernet Arduino Compatibele Microcontroller
  • 12 Volt Power Supply - 5.0 Amp Single Output
  • AIWAN LEZHI 10 Meters GT2 Timing Belt Width 6mm
  • GT2 Pulley 20 Teeth 5mm bore 6mm Width
  • Stepper Motor - Stepper Motor 17HS24-2104S
  • Optical Photosensitive LDR light sensor Module
  • Various hooks, screws, bolts and other mounting materials
  • I wrote my own arduino code, I am sure it can be done better and cleaner, but this works for me. I am still thinking about adding more Homekit compatibility, but I am not an programmer/coder. I would like to have some kind of status report back to Homekit/Homebridge so I can tell if the curtains are open or closed but that might never happen as longs as this code does what it needs to do.
    The curtains close when it gets dark outside and they open again when it gets light outside, no manual actions required there. I added some kind of Homekit support by means of a webserver setup with two embedded links, one to open and one to close the curtains by manual override the light sensor.

                                        
      #include Ethernet.h
      #include AccelStepper.h
    
      AccelStepper stepper(1, 5, 6);
      boolean c_state = false;
      boolean c_man = false;
      const int driver_enable = 9;
      int sensorPin = 3;
      int light = 175; // Threshold when to open
      int dark = 125; // Threshold when to close
      unsigned long currentTime = 0;
      unsigned int sensorValue = 0;
      long previousMillis = 0;
      long interval = 3000;
      byte mac[] = {  0x72, 0x69, 0x67, 0x2E, 0x30, 0x33 }; // Your arduino mac-address
      byte myip[] = { 192, 168, 1, 25 };
      byte gwip[] = { 192, 168, 1, 1 };
      byte dnsip[] = { 192, 168, 1, 1 };
    
      EthernetServer server(80);
    
      void setup() {
        Serial.begin(9600);
        Ethernet.begin(mac, myip, gwip, dnsip);
        server.begin();
        stepper.setMaxSpeed(6000);
        stepper.setAcceleration(2000);
      }
    
      void loop() {
        read_ldr();
        if (sensorValue > light && c_state == false) {
          a_open();
        }
        if (sensorValue < dark && c_state == true) {
          a_close();
        }
        EthernetClient client = server.available();
        if (client) {
          Serial.println("new client");
          boolean currentLineIsBlank = true;
          String postText = "";
          while (client.connected()) {
            if (client.available()) {
              char c = client.read();
              if (postText.length() < 10) {
                postText += c;
              }
              if (c == '\n' && currentLineIsBlank) {
                client.println("HTTP/1.1 200 OK");
                client.println("Content-Type: text/html");
                client.println("Connection: close");
                client.println();
                client.println("");
                client.println("");
                client.println("");
                client.println(" ArduinoCurtains ");
                client.println("");
                client.println("");
                break;
              }
              if (c == '\n') {
                currentLineIsBlank = true;
              }
              else if (c != '\r') {
                currentLineIsBlank = false;
              }
            }
          }
          if (postText.indexOf("?on1") > 0) {
            if (sensorValue < dark && c_state == false && c_man == false) {
              h_open();
            }
            if (sensorValue > light && c_state == true && c_man == false) {
              h_close();
            }
          }
          if (postText.indexOf("?off1") > 0 ) {
            if (sensorValue > light && c_state == true && c_man == true) {
              a_open();
            }
            if (sensorValue < dark && c_state == false && c_man == true) {
              a_close();
            }
          }
          delay(1);
          client.stop();
          Serial.println("client disconnected");
        }
      }
    
      //curtains open Homekit
      void h_open() {
        digitalWrite(driver_enable, LOW);
        Serial.println("h_open");
        delay(200);
        stepper.moveTo(000);
        while (stepper.distanceToGo() != 0)
        {
          stepper.run(); // Take another step
        }
        c_man = true;
        c_state = false;
        digitalWrite(driver_enable, HIGH);
      }
    
      //curtains dicht Homekit
      void h_close() {
        digitalWrite(driver_enable, LOW);
        Serial.println("h_close");
        delay(200);
        stepper.moveTo(-23800);
        while (stepper.distanceToGo() != 0)
        {
          stepper.run(); // Take another step
        }
        c_man = true;
        c_state = true;
        digitalWrite(driver_enable, HIGH);
      }
    
      //curtains open LDR
      void a_open() {
        digitalWrite(driver_enable, LOW);
        Serial.println("ldr open");
        delay(200);
        stepper.moveTo(000);
        while (stepper.distanceToGo() != 0)
        {
          stepper.run(); // Take another step
        }
        c_man = false;
        c_state = true;
        digitalWrite(driver_enable, HIGH);
      }
    
      //curtains dicht LDR
      void a_close() {
        digitalWrite(driver_enable, LOW);
        Serial.println("ldr dicht");
        delay(200);
        stepper.moveTo(-23800);
        while (stepper.distanceToGo() != 0)
        {
          stepper.run(); // Take another step
        }
        c_man = false;
        c_state = false;
        digitalWrite(driver_enable, HIGH);
      }
    
      //read LDR value
      void read_ldr() {
        unsigned long currentMillis = millis();
        if (currentMillis - previousMillis > interval) {
          previousMillis = currentMillis;
          sensorValue = analogRead(sensorPin);
          Serial.print("sensorValue: ");
          Serial.print(sensorValue, DEC);
          Serial.println("");
          if (c_man == true) {
            Serial.println("curtains manual");
            Serial.println("");
          }
          else {
            Serial.println("curtains automatic");
            Serial.println("");
          }
          if (c_state == true) {
            Serial.println("curtains open");
            Serial.println("");
          }
          else {
            Serial.println("curtains closed");
            Serial.println("");
          }
        }
      }