# Arduino or ESP8266?

**URL:** <https://community.homey.app/t/arduino-or-esp8266/41735>\
**Category:** Questions & Help\
**Created:** [January 25, 2021, 7:40am UTC](https://community.homey.app/t/arduino-or-esp8266/41735 "2021-01-25T07:40:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![wesley\_w](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/wesley_w/32/10127_2.png) [@wesley\_w](https://community.homey.app/u/wesley_w)\
**Post date:** [January 25, 2021, 7:40am UTC](https://community.homey.app/t/arduino-or-esp8266/41735/1 "2021-01-25T07:40:30Z")

</div>

I would like to controll 4-5 relais from homey. I thought do this with shelly prodcuts but then all these Shelly 1 are also added in my Googlehome and it will be a very bullky system. These relais are used for controlling a 24V magnetic lock and some led signal lights. So i know arduino can be used for this or ESP8266? Who could help me what unit i would need and a tutorial how to connect this to homey? I was thinking of this module:

> **[US $11.64 31% OFF|ESP8266 ESP 12F Wifi Relais Module 4 Kanaals AC90 250V/DC7...](https://nl.aliexpress.com/item/1005001652433251.html?spm=a2g0o.detail.0.0.46282b5dFMmHhY&gps-id=pcDetailBottomMoreThisSeller&scm=1007.13339.169870.0&scm_id=1007.13339.169870.0&scm-url=1007.13339.169870.0&pvid=910773d1-cedf-4e0c-a744-226392cdfe62&_t=gps-id:pcDetailBottomMoreThisSeller,scm-url:1007.13339.169870.0,pvid:910773d1-cedf-4e0c-a744-226392cdfe62,tpp_buckets:668%230%23131923%2379_668%23888%233325%2314_668%232846%238111%231996_668%232717%237567%23930_668%231000022185%231000066058%230_668%233468%2315615%23645)**
>
> Smarter Shopping, Better Living! Aliexpress.com

Any tips or step by step tutorial is appriciated.

Found [Homeyduino Relay met Google Assistent ondersteuning - Huisvanvandaag.nl](https://huisvanvandaag.nl/homeyduino-relay/) gevonden… What is the code for more relays?

---

<div class="post-metadata">

**Author:** ![JPe4619](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/jpe4619/32/9017_2.png) [@JPe4619](https://community.homey.app/u/JPe4619)\
**Post date:** [January 25, 2021, 12:41pm UTC](https://community.homey.app/t/arduino-or-esp8266/41735/2 "2021-01-25T12:41:56Z")

</div>

Found my idea at: [[HOWTO] BUVA Boxstream Mechanical ventilation hack](https://community.homey.app/t/howto-buva-boxstream-mechanical-ventilation-hack/7596) Maybe that is already working for you. (Thanks to Niels De Klerk)

Made some adaptions for this module 🙂

> **[US $6.17 5% OFF|1 Pcs 5 V/12V ESP8266 ESP 01 4 Kanaals Wifi Relais Module...](https://nl.aliexpress.com/item/4000152759077.html?spm=a2g0o.productlist.0.0.3d6646d3mLMcwE&algo_pvid=8874f444-4472-4859-86a3-8c485ed2d9fb&algo_expid=8874f444-4472-4859-86a3-8c485ed2d9fb-0&btsid=0b0a182b16115771689071828eeb6e&ws_ab_test=searchweb0_0,searchweb201602_,searchweb201603_)**
>
> Smarter Shopping, Better Living! Aliexpress.com

HomeyDuino can only control 1 switch per device, therefore I used an alternative control methode.  
Can be easy controlled in a flowcard by:

![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/0/0/00189ec5105c10fe59da57995a9daa6062e56cbf.png)

You have to flash new firmware into the ESP8266

---

<div class="post-metadata">

**Author:** ![wesley\_w](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/wesley_w/32/10127_2.png) [@wesley\_w](https://community.homey.app/u/wesley_w)\
**Post date:** [January 25, 2021, 1:35pm UTC](https://community.homey.app/t/arduino-or-esp8266/41735/3 "2021-01-25T13:35:59Z")

</div>

Thanks for the info. How do you program this module? You connect it trough the GPIO pins to an anduino? and what firmware do you need to flash? With these webhooks there is no return signal? So Homey does not know of it succeded or what state the relay is in? If the relay state in also visable in homey i can create new flows depending on the on or off state of the relay

---

<div class="post-metadata">

**Author:** ![JPe4619](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/jpe4619/32/9017_2.png) [@JPe4619](https://community.homey.app/u/JPe4619)\
**Post date:** [January 25, 2021, 2:20pm UTC](https://community.homey.app/t/arduino-or-esp8266/41735/4 "2021-01-25T14:20:31Z")

</div>

For programming the ESP-01, I use a device like this:

> **[US $0.79 10% OFF|ESP01 Programmeur Adapter Uart GPIO0 ESP 01 Adaptater...](https://nl.aliexpress.com/item/32831857167.html?spm=a2g0o.productlist.0.0.5bf75edaFOjyLH&algo_pvid=2e343cad-2133-4fc8-9436-a1453a0eb653&algo_expid=2e343cad-2133-4fc8-9436-a1453a0eb653-3&btsid=0b0a0ac216115825454558145e4e69&ws_ab_test=searchweb0_0,searchweb201602_,searchweb201603_)**
>
> Smarter Shopping, Better Living! Aliexpress.com

Be aware that GPIO-0 has to be grounded while power-up for programming.  
The Arduino-IDE can be used for programming this device.

There is no relay-state feed-back, but you can set or reset a boolean-logic-variable at the same time as you set or reset the relay and use that as an index for the relay-state. Or make a Virtual device (switch) for this, to make it visible.

You have to insert your SSID and Password(router) in the code and the preferred IP-address of the relay-board.

File for credentials, named: MyCredentials.h (placed in the same directory as source-file)

```
/ ************** 
     My Credentials // 
#include "MyCredentials.h" 
************ /

//my network credentials
const char* ssid = "your-modem-router-SSID";
const char* password = "your-modem-route-password";

// Static IP address
IPAddress local_IP(192, 168, 2, 80); // change to your wanted device-IP

// Gateway IP address
IPAddress gateway(192, 168, 2, 254); // change to your modem-routerIP
IPAddress subnet(255, 255, 0, 0);
IPAddress primaryDNS(8, 8, 8, 8); //optional
IPAddress secondaryDNS(8, 8, 4, 4); //optional

```

source-file for four relay-board X4, named: FourRelay.ino

```
/* Remote for ESP8266 Relay-X4 module */
/* From origin Niels De Klerk https://community.homey.app/t/howto-buva-boxstream-mechanical-ventilation-hack/7596 */

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include "MyCredentials.h"

//#ifndef STASSID
//#define STASSID "MY SSID"
//#define STAPSK "MY PASSWORD"
//#endif

//const char* ssid = STASSID; //done by #include "MyCredentials.h"
//const char* password = STAPSK; //done by #include "MyCredentials.h"

byte rel1ON[] = {0xA0, 0x01, 0x01, 0xA2}; //Hex command to send to serial for open relay-1
byte rel1OFF[] = {0xA0, 0x01, 0x00, 0xA1}; //Hex command to send to serial for close relay-1

byte rel2ON[] = {0xA0, 0x02, 0x01, 0xA3}; //Hex command to send to serial for open relay-2
byte rel2OFF[] = {0xA0, 0x02, 0x00, 0xA2}; //Hex command to send to serial for close relay-2

byte rel3ON[] = {0xA0, 0x03, 0x01, 0xA4}; //Hex command to send to serial for open relay-3
byte rel3OFF[] = {0xA0, 0x03, 0x00, 0xA3}; //Hex command to send to serial for close relay-3

byte rel4ON[] = {0xA0, 0x04, 0x01, 0xA5}; //Hex command to send to serial for open relay-4
byte rel4OFF[] = {0xA0, 0x04, 0x00, 0xA4}; //Hex command to send to serial for close relay-4

// Flash : GPIO0
// tx_pin: GPIO1
// led : GPIO2
// rx_pin: GPIO3

ESP8266WebServer server(80);

void httpDefaultResponse() {
  digitalWrite(2, 0);
  server.send(200, "text/html", "<html><head></head><body><b>Relay Control</b><br><br><a href=\"/relay1:on\">Relay-1: ON</a><br><br><a href=\"/relay1:off\">Relay-1: OFF</a><br><br><a href=\"/relay2:on\">Relay-2: ON</a><br><br><a href=\"/relay2:off\">Relay-2: OFF</a><br><br><a href=\"/relay3:on\">Relay-3: ON</a><br><br><a href=\"/relay3:off\">Relay-3: OFF</a><br><br><a href=\"/relay4:on\">Relay-4: ON</a><br><br><a href=\"/relay4:off\">Relay-4: OFF</a></body></html>");

  /*original code Niels De Klerk: */
  /*server.send(200, "text/html", "<html><head></head><body><b>Control</b><br><br><a href=\"/mode1\">Set Mode: 1</a><br><br><a href=\"/mode2\">Set Mode: 2</a><br><br><a href=\"/mode3\">Set Mode: 3</a><br><br><a href=\"/mode4\">Set Mode: 4</a><br><br><a href=\"/mode5\">Set Mode: 5</a><br><br><a href=\"/mode6\">Set Mode: 6</a></body></html>");
  /*server.send(200, "text/html", "<html><head></head><body><b>Control</b><br><a href=\"/mode1\">Set Mode: 1</a><br><a href=\"/mode2\">Set Mode: 2</a><br><a href=\"/mode3\">Set Mode: 3</a><br><a href=\"/modeT1\">Set Mode: 4 - 1</a><br><a href=\"/modeT2\">Set Mode: 4 -2</a><br><br><br><b>Pairing<b><br>The ventilation motor allows pairing only in the first 10 minutes after getting power.<br>To pair a remote, disconnect the power cable and then reconnect the power cable of the ventilation motor.<br><a href=\"/pairing\">Start pairing.</a></body></html>");
  */
  
  digitalWrite(2, 1);
}

void setup(void) {
  // pinMode(0, OUTPUT);
  // digitalWrite(0, 0);
  // pinMode(1, OUTPUT);
  // digitalWrite(1, 0);
  pinMode(2, OUTPUT);
  digitalWrite(2, 0);
  // pinMode(3, OUTPUT);
  // digitalWrite(3, 0);
  // pinMode(4, OUTPUT);
  // digitalWrite(4, 1);
  // pinMode(5, OUTPUT);
  // digitalWrite(5, 0);
  // pinMode(6, OUTPUT);
  // digitalWrite(6, 0);

  Serial.begin(115200);
  // Configures static IP address, defined in [#include "MyCredentials.h"]
  if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) {
    //Serial.println("STA Failed to configure");
  }
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  //Serial.println("");

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    //Serial.print(".");
  }
  //Serial.println("");
  //Serial.print("Connected to ");
  //Serial.println(ssid);
  //Serial.print("IP address: ");
  //Serial.println(WiFi.localIP());
  //digitalWrite(1, 1);
  if (MDNS.begin("esp8266")) {
  //Serial.println("MDNS responder started");
  }

  server.on("/", httpDefaultResponse);

  server.on("/relay1:on", []() {
    httpDefaultResponse();
    Serial.write(rel1ON, sizeof(rel1ON)); // turns the relay ON
  });

  server.on("/relay1:off", []() {
    httpDefaultResponse();
    Serial.write(rel1OFF, sizeof(rel1OFF)); // turns the relay OFF
  });

  server.on("/relay2:on", []() {
    httpDefaultResponse();
    Serial.write(rel2ON, sizeof(rel2ON)); // turns the relay ON
  });

  server.on("/relay2:off", []() {
    httpDefaultResponse();
    Serial.write(rel2OFF, sizeof(rel2OFF)); // turns the relay OFF
  });

  server.on("/relay3:on", []() {
    httpDefaultResponse();
    Serial.write(rel3ON, sizeof(rel3ON)); // turns the relay ON
  });

  server.on("/relay3:off", []() {
    httpDefaultResponse();
    Serial.write(rel3OFF, sizeof(rel3OFF)); // turns the relay OFF
  });

  server.on("/relay4:on", []() {
    httpDefaultResponse();
    Serial.write(rel4ON, sizeof(rel4ON)); // turns the relay ON
  });

  server.on("/relay4:off", []() {
    httpDefaultResponse();
    Serial.write(rel4OFF, sizeof(rel4OFF)); // turns the relay OFF
  });

  server.onNotFound(httpDefaultResponse);

  server.begin();
  //Serial.println("HTTP server started");
}

void loop(void) {
  server.handleClient();
  MDNS.update();
} // end loop.

```

It is also possible to control the relays from a WebBrouwser pointing to the Device-IP  
 ![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/b/b/bb4550a512d881db236ce77c4a2964bf03a0f7fc.png)

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [January 25, 2021, 3:51pm UTC](https://community.homey.app/t/arduino-or-esp8266/41735/5 "2021-01-25T15:51:18Z")

</div>

Someone should write an ESPHome app for Homey 😃

---

<div class="post-metadata">

**Author:** ![FELIPEPARENTE](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/felipeparente/32/34467_2.png) [@FELIPEPARENTE](https://community.homey.app/u/FELIPEPARENTE)\
**Post date:** [October 4, 2021, 5:33pm UTC](https://community.homey.app/t/arduino-or-esp8266/41735/6 "2021-10-04T17:33:42Z")

</div>

How can I control the flashing of the LEDs (D5, D6 and D7) and the pressing of the S1 and S2 buttons?
