-->
Page 1 of 1

Blind Control with lctech x2 relay

PostPosted: Tue Jun 01, 2021 7:48 am
by ZigDuDim
Hi everyone!!

I have the project to control my roller blinds using a lctech 2 way 5v relay, but I have some trouble by using it. The relays are acting weirdly. Some time they switch on, some time they don't, some time with a delay, some time they switch off before they have to... It is very random...

I am pretty sure of my code because it is working like a charm if I don't use the lctech x2 relay but by connecting directly other relay to the ESP.

Best regards

ps: sorry if it isn't clear, my english is far from perfect ;)

Code: Select all
#include <ESP8266WiFi.h>

const char* ssid = "###########";
const char* password = "###########";

int max_up = 100;
int max_down = 0;
int time_travel = 20000;
int pos_target = 100;
int pos_actual = 100;
int time_per_percentage_when_go_down = 180;
int time_per_percentage_when_go_up = 193;

byte powerON[]  = {0xA0, 0x01, 0x01, 0xA2};
byte powerOFF[] = {0xA0, 0x01, 0x00, 0xA1};
byte updownON[]  = {0xA0, 0x02, 0x01, 0xA3};
byte updownOFF[] = {0xA0, 0x02, 0x00, 0xA2};

//Function Prototype
void blind_stop();
void blind_up();
void blind_down();
void blind_move();

WiFiServer server(80);

void setup() {
  delay(10);
  Serial.begin (115200);

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }

  // Start the server
  server.begin();
}

void blind_up() {
  if (pos_actual >= 100){
    pos_actual = 100;
    blind_stop();
    return;
  }
  delay(1);
  Serial.write (powerON, sizeof(powerON));
  delay(2);
  Serial.write (updownOFF, sizeof(updownOFF));
  delay(time_per_percentage_when_go_up);
  if (pos_target == 100)
    delay(50);
  pos_actual++;
}

void blind_down() {
  if (pos_actual <= 0 && pos_target <= 0){
    pos_actual = 0;
    blind_stop();
    return;
  }
  delay(1);
  Serial.write (powerON, sizeof(powerON));
  delay(2);
  Serial.write (updownON, sizeof(updownON));
  delay(time_per_percentage_when_go_down);
  pos_actual--;
}

void blind_stop() {
  Serial.write (powerOFF, sizeof(powerOFF));
  delay(2);
  Serial.write (updownOFF, sizeof(updownOFF));
}

void blind_move() {
  if (pos_actual == 0 && pos_target == 0){
    blind_stop();
    return;
  }
  else if (pos_actual == 100 && pos_target == 100){
    blind_stop();
    return;
  }
  else if (pos_target > pos_actual) {
    blind_up();
    return;
  }
  else if (pos_target < pos_actual) {
    blind_down();
    return;
  }
  else if (pos_target = pos_actual) {
    blind_stop();
    return;
  }
  else
    return;
}

void loop() {

  WiFiClient client = server.available();
  if (!client) {
    blind_move();
    return;
  }

  // Wait until the client sends some data
  while (!client.available()) {
    delay(1);
  }

  // Read the first line of the request
  String request = client.readStringUntil('\r');
  client.flush();

  //Moving request
  if (request.indexOf("/blinds/blinds?open=") != -1) {
    request.remove(0, 24);
    pos_target = request.toInt();
  }
 
  //position request
  else if (request.indexOf("/blinds/status") != -1) {
    String s = "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n{\"position\":\"";
    s += pos_actual;
    s += "\"}";
    client.println(s);
  }
  delay(1);
}

Re: Blind Control with lctech x2 relay

PostPosted: Wed Jun 02, 2021 5:27 am
by ZigDuDim
I think I found a solution.

I have introduced delay between two serial.write command (something between 15 and 50ms) and it starts to have a better behaviour.

Code: Select allint delay_between_cmd = 20;

[...]

Serial.write (relON, sizeof(relON));
delay(delay_between_cmd);
Serial.write (rel2ON, sizeof(rel2ON));