Homey Community Forum

Homeyduino relays

Hi all,

Today I have tried to make some sketches with the Homeyduino app, i mannaged to make some sensors and let them communicate with the Homey. Now I wanted to make a simple sketch where I can let the homey trigger a output of the ESP8266. I can not make it work, does anybody know if there is a simple basic sketch to trigger an output?

I have tried to do it with the SonOff example but I can just put on and of a LED with this example. The problem is that the example works with states and I do not understand this…

Does anybody have a more simple example to trigger an output of the ESP8266?

Much of thanks!
Sjoerd

What do you mean by “trigger an output”?

I mean I want to Get a ‘state’ from the Homey to the ESP8266 module so I can use that ‘state’ in the rest of the sketch to make (in this case) a relay switch on and off. Sorry I do not have enough programming skills, I cant make it work by using the SonOff sketch and change it…

The Sonoff sketch basically does the same thing: it creates a device with an onoff capability, which is used to switch the internal relay of the Sonoff (connected to pin 12).

I think the only thing you have to do is change the pin to the pin that your device is using. You can also use the symbolic pin name, like D1, D2, … (for the digital pins).

I think I have to be a little more specific about what I want to do, at this moment I have made a sketch for my arduino to open and close my blinds in two ways:
1 At a defined time
2 By a push button for opening and a push button for closing
I want to make it work with my homey but I cant get it work by the Sonoff sketch because I do not know how to integrate my stepper motor in this circuit, I put the sketch below so you can see how it works. When it works with the homey, I do not need the LCD anymore.

I hope this is more clearly for you to understand what I want. Thanks a lot for thinking allong!

Here is my sketch without homey integration working on a arduino NANO:

#include <RTClib.h>
#include <Wire.h>
#include <LiquidCrystal.h>
const int rs = 12, en = 11, d4 = 10, d5 = 9, d6 = 8, d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

#include <Stepper.h>
#define stepsPerRevolution 2040
Stepper stepper(stepsPerRevolution, 4, 6, 5, 3);
int steps1;

const int Open_Switch = 0; // Pin 0 voor de drukknop openen
const int Close_Switch = 1; // Pin 1 voor de drukknop sluiten
int ButtonStateOpen =0;
int ButtonStateClose =0;

RTC_DS3231 rtc;
char daysOfTheWeek[7][12] = {“Zondag”, “Maandag”, “Dinsdag”, “Woensdag”, “Donderdag”, “Vrijdag”, “Zaterdag”};

void setup() {
pinMode(Open_Switch, INPUT);
pinMode(Close_Switch, INPUT);
pinMode (13, OUTPUT);
pinMode (2, OUTPUT);
digitalWrite (2, HIGH); //Relais niet bediend
Wire.begin();
lcd.begin(16, 2);
digitalWrite (13, HIGH); //LED display verlicht
delay(100);
lcd.setCursor(0, 0);
lcd.print(“Reset teller…”);
lcd.setCursor(0, 1);
lcd.print(“Tijd instellen…”);
delay(3000);
lcd.clear();
digitalWrite (13, LOW); //LED display donker
steps1=0;
}

void loop() {
ButtonStateOpen = digitalRead(Open_Switch);
ButtonStateClose = digitalRead(Close_Switch);
DateTime now = rtc.now();

lcd.setCursor(0, 0);
lcd.print(daysOfTheWeek[now.dayOfTheWeek()]);
lcd.print("  0=D 1=O");
lcd.setCursor(0, 1);
lcd.print(now.hour());
lcd.print(":");
lcd.setCursor(3, 1);
lcd.print(now.minute());
lcd.print(":");
lcd.setCursor(6, 1);
lcd.print(now.second());
lcd.setCursor(11, 1);
lcd.print(steps1);
delay(500);

//Openen met drukknop
if (ButtonStateOpen == LOW && (steps1 == 0)) {
digitalWrite (13, HIGH); //LED display verlicht
delay(2000);
digitalWrite (2, LOW); //Relais bediend
delay(100);
stepper.setSpeed(10);
stepper.step(9180);
//Stappenteller
steps1++;
delay(100);
digitalWrite (2, HIGH); //Relais niet bediend
delay(2000);
digitalWrite (13, LOW); //LED display niet verlicht
}

//Sluiten met drukknop
if (ButtonStateClose == LOW && (steps1 == 1)) {
digitalWrite (13, HIGH); //LED display verlicht
delay(2000);
digitalWrite (2, LOW); //Relais bediend
delay(100);
stepper.setSpeed(10);
stepper.step(-9180);
//Stappenteller
steps1–;
delay(100);
digitalWrite (2, HIGH); //Relais niet bediend
delay(2000);
digitalWrite (13, LOW); //LED display niet verlicht
}

// Deze cyclus start openen van de luxaflex.
if (now.hour() == 9 && now.minute() == 30 && (steps1 == 0)) {
digitalWrite (13, HIGH); //LED display verlicht
delay(2000);
digitalWrite (2, LOW); //Relais bediend
delay(100);
stepper.setSpeed(10);
stepper.step(9180);
delay(100);
digitalWrite (2, HIGH); //Relais niet bediend
//Stappenteller
steps1++;
digitalWrite (13, LOW); //LED display dimmen
}

// Deze cyclus start sluiten van de luxaflex.
if (now.hour() == 18 && now.minute() == 30 && (steps1 == 1)) {
digitalWrite (13, HIGH); //LED display verlicht
delay(2000);
digitalWrite (2, LOW); //Relais bediend
delay(100);
stepper.setSpeed(10);
stepper.step(-9180);
delay(100);
digitalWrite (2, HIGH); //Relais niet bediend
//Stappenteller
steps1–;
digitalWrite (13, LOW); //LED display dimmen
}

lcd.clear();
}

Start simple:

  • remove the RTC code
  • remove the button code

So you are just left with a sketch that should either open or close the blinds from within a flow action.

Your sketch will have one capability, onoff: blinds are open (on) or closed (off). At some point, you can even consider adding additional capabilities, like dim or windowcoverings_state so you can partially open/close the blinds too, but like I said: start simple.

You need to add an action handle inside your sketch (caveat: all untested and my Homeyduino knowledge is rusty, so it may require some tweaking to get this working):

Homey.addAction("set_blinds", setState);

void setState() {
  bool state = Homey.value.toInt();
  if (state) {
    // code to open blinds
   ...
  } else {
    // code to close blinds
    ...
  }
}

So whenever you trigger the set_blinds action (in a flow), the value passed is used to determine of the blinds should open or close.

Comment out the other capabilities and actions from the Sonoff code, so they don’t get in the way.

Thanks a lot! It works a little now :smiley:

Just 3 new problems…
It looks like the Homey loses the connection with the module while it is rotating the stepper motor, when the stepper motor is finished everything works fine again.

Another problem is that I cannot rotate the stepper 4,5 total rounds, it will turn a half round, stops, turn anather half round and then it will stay. I can fix this by making a for loop:
delay(100);
for (int i = 0; i <= 17; i++) {
stepper.setSpeed(10);
stepper.step(-510);
steps1–;
delay(500);
}
But I do not understand why I cannot directly sent the stepper 9180 steps…

The last thing is, it would be great to be able to position it half way (lets say 4590 steps), how can I make this work in homeyduino? I just have a push butto like ‘on and off’. Is it also possible to show a vallue under the push button? So I can see the stepcounter in Homey?

And last question, do you know if there is a list with sensors and buttons and things i can make in homeyduino? Now I just know I can make a push button, hummidity sensor, temperature sensor and a motion sensor but I think Homeyduino has more than that :stuck_out_tongue:

Thanks again!

The onoff capability that you’re using is a boolean, so it’s either on (open) or off (closed). If you want to use values in between, you need to use a different capability, like dim or windowcoverings_state. You can consult the developer documentation for more info on those (and all other) capabilities.

As for sensors: Homeyduino itself isn’t concerned with sensor support, so any sensor that you can program for on your microcontroller can be exposed through Homeyduino. The only requirement is that the sensor data value can be mapped to one of the capabilities that Homey supports (for instance, there are sensors that can measure Volatile Organic Compounds, but there’s no capability on Homey to support that value, so that might be difficult).

Wow I did not find that page myself, thats really usefull!

Now I have got it all working with the on off capability, but ofcourse it would be really great if I can make it work with a dim capability. I tried a few things to make that happen but it will not work. How can I make the homey sent a vallue instead of a 0 or a 1? When I put in the capability ‘dim’ instead of ‘on/off’, I still just get a 0 or a 1 vallue from the homey. I know a Bool cannot have other vallues than 0 or 1 so what kind of statement do I need instead of the Bool? And do I need to make more changes in the code to change the on off capability to a dim capability?

Probably something like float state = Homey.value.toFloat(), because dim is a floating point number between 0 and 1.

Be aware that if you change capabilities, you need to re-add the device to Homey.

I just tried to use the float state and I can read the float number in my serial monitor, but how can I map this number (between 0.00 and 1.00) to 0 - 100%? Isnt it possible to use an integer instead of a float?

state * 100?

Wow was it really that easy? YES it was! Feel a little embarrassed…
But…! I finished the project, much thanks to you Robert! You helped me a lot!

What this project does:
I use a 28BYJ-48 stepper motor to open and close my blinds. It gets a setpoint from the Homey app and counts up or down the stepper motor until its stepcounter has got the same counts as the setpoint. The stepper motor enable is connected to a relay so when its not in use, the stepper motor dont use any power.

Here is the code for if anybody can use it :smiley:

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <Homey.h>

unsigned long previousMillis = 0;
const unsigned long interval = 2000;            //Interval in milliseconds for reading setpoint from Homey

#include <Stepper.h>
#define stepsPerRevolution 2040
Stepper stepper(stepsPerRevolution, 5, 14, 12, 4);            //This are PIN D1 D5 D6 D2 on the ESP8266

int Relais = 13;                            //PIN D7 on ESP8266 Relay for put on and off stepper motor

float PositionFloat = 0;                    //Setpoint from Homey

int steps1;                                 //Stepcounter
int stepsHomey;                             //Setpoint from Homey changed to stepper total



void wifi() {                               //This loop is for connecting to WiFi
  if (WiFi.status() != WL_CONNECTED) {
    Serial.print("Connecting with WiFi ");
    WiFi.begin("SSID", "PASSWORD");
    uint8_t timeout = 30;
    while (WiFi.status() != WL_CONNECTED) {
      delay(500); Serial.print(".");
      if (timeout<1) break;
    }
    if (WiFi.status() == WL_CONNECTED) {
      Serial.println(" ");
      Serial.println("Connected to WiFi!");
    }
  }
}


void setup() {
  Serial.begin(9600);
  Homey.begin("Luxaflex");                               //The name of your project, every project needs another name!
  Homey.setClass("blinds");                              //Put here the device class, in this case Blinds
  Homey.addCapability("windowcoverings_set", ValHomey);  //Put here the capability
  Homey.setCapabilityValue("windowcoverings_set", ValHomey);
  steps1 = 0;
}

void loop() {
  Homey.loop();
  wifi();
  steppert();
    unsigned long currentMillis = millis();
    if(currentMillis - previousMillis > interval) {
      previousMillis = currentMillis;
      ValHomey ();
      }
}

void steppert(){
stepsHomey = (PositionFloat * 9180);  //[9180] Is the total ammount of steps wich i need to be taken
 if (stepsHomey != steps1){           //To activate relais before steps will be made!
  digitalWrite (Relais, HIGH);        //Relay activated
  }

 if (stepsHomey == steps1){           //To deactivate relais before when stepper motor is not in use
  digitalWrite (Relais, LOW);         //Relay not activated
  }
  
 if (stepsHomey > steps1){           //Circuit for turning CW
  stepper.setSpeed(14);              //Stepper speed
  stepper.step(1);                   //Steps per time
  steps1++;                          //Stepcounter
 }

 if (stepsHomey < steps1){           //Circuit for turning CCW
  stepper.setSpeed(14);              //Stepper speed
  stepper.step(-1);                  //Steps per time
  steps1--;                          //Stepcounter
 }
}

void ValHomey (){
PositionFloat = Homey.value.toFloat(); //number 0.00 - 1.00
Serial.print ("Setpoint from the Homey:             ");  Serial.println(stepsHomey);
Serial.print ("Actual position of the steppermotor: ");  Serial.println(steps1); 
}
1 Like

Hi @Stekkies12, and thanks from me too @robertklep!

I’ve used your code and the remarks from Robert on the developer docs and sensor support and found a way to read 2 limit switches to sense a garage door sled moving alerting me that the garage door is opening.
I’ve opened a new post for anyone wanting to learn.

Thanks!