# Arduino / Homeduino met ESP8266 aansturen pinnen op Arduino

**URL:** <https://community.homey.app/t/arduino-homeduino-met-esp8266-aansturen-pinnen-op-arduino/6265>\
**Category:** Devices\
**Created:** [January 4, 2019, 12:05am UTC](https://community.homey.app/t/arduino-homeduino-met-esp8266-aansturen-pinnen-op-arduino/6265 "2019-01-04T00:05:38Z")\
**Posts on this page:** 20\
**Page:** 2

<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 5, 2019, 8:23am UTC](https://community.homey.app/t/arduino-homeduino-met-esp8266-aansturen-pinnen-op-arduino/6265/21 "2019-01-05T08:23:57Z")

</div>

For the “receiving requests” part, check out [ESP8266WebServer](https://github.com/esp8266/Arduino/tree/master/libraries/ESP8266WebServer), I’ve used that library a few times and it works well. It has a decent amount of examples for you to try.

And for sending, there’s a [HTTP client implementation](https://github.com/esp8266/Arduino/tree/master/libraries/ESP8266HTTPClient).

These libraries are both part of the “Arduino compatibility library” for ESP8266: [https://github.com/esp8266/Arduino/](https://github.com/esp8266/Arduino/)  
Instructions on how to install can be found on that page.

Overall, I would suggest taking a look at [PlatformIO](https://platformio.org/), which is a great IDE for developing ESP8266 (and other) projects. It has a built-in library manager that you can use to easily find and install third-party libraries (for example, to implement particular sensor support).

---

<div class="post-metadata">

**Author:** ![Sander\_Vlotman](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/sander_vlotman/32/7855_2.png) [@Sander\_Vlotman](https://community.homey.app/u/Sander_Vlotman)\
**Post date:** [January 5, 2019, 9:28am UTC](https://community.homey.app/t/arduino-homeduino-met-esp8266-aansturen-pinnen-op-arduino/6265/22 "2019-01-05T09:28:46Z")

</div>

Thanks Robert, great getting your help. Today another day of learning and then back to work 😉

---

<div class="post-metadata">

**Author:** ![Sander\_Vlotman](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/sander_vlotman/32/7855_2.png) [@Sander\_Vlotman](https://community.homey.app/u/Sander_Vlotman)\
**Post date:** [January 5, 2019, 3:10pm UTC](https://community.homey.app/t/arduino-homeduino-met-esp8266-aansturen-pinnen-op-arduino/6265/23 "2019-01-05T15:10:02Z")

</div>

OK, nailed it. Here the example code and two screenshots on how to make it work on Homey.  
Now some sensors and devices. Not as good as the homedruino as homey won’t recognise it as sensor but if you make local code that filters the right events I think it is a pretty good replacement…

> **[ESP8266 server and client example for Homey](https://docs.google.com/document/d/1gJRBMbjx5qe6gEhHTlB4IHzFMMa_LboeMsqgJy_o1f4/edit?usp=sharing)**
>
> Hi Here an example of code that can be used to make a ESP8266 webserver and client on one of the common wifi boards. I use this sample in combination with the HTTP request flow card app on the Homey appstore from Erik van Dongen First the code and...

---

<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 5, 2019, 3:18pm UTC](https://community.homey.app/t/arduino-homeduino-met-esp8266-aansturen-pinnen-op-arduino/6265/24 "2019-01-05T15:18:03Z")

</div>

You can combine those flows with a [virtual device](https://apps.athom.com/app/com.arjankranenburg.virtual) that would, to Homey, look like a sensor device.

---

<div class="post-metadata">

**Author:** ![KonradWalsh](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/konradwalsh/32/147_2.png) [@KonradWalsh](https://community.homey.app/u/KonradWalsh)\
**Post date:** [January 5, 2019, 7:11pm UTC](https://community.homey.app/t/arduino-homeduino-met-esp8266-aansturen-pinnen-op-arduino/6265/25 "2019-01-05T19:11:43Z")

</div>

FYI

Homeyduino works fine in Homey v2

---

<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 5, 2019, 7:44pm UTC](https://community.homey.app/t/arduino-homeduino-met-esp8266-aansturen-pinnen-op-arduino/6265/26 "2019-01-05T19:44:01Z")

</div>

Other people had lots of crashes when they tried, so YMMV.

---

<div class="post-metadata">

**Author:** ![Magnus\_P](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/magnus_p/32/4344_2.png) [@Magnus\_P](https://community.homey.app/u/Magnus_P)\
**Post date:** [January 6, 2019, 10:14pm UTC](https://community.homey.app/t/arduino-homeduino-met-esp8266-aansturen-pinnen-op-arduino/6265/27 "2019-01-06T22:14:48Z")

</div>

I have developed a LED dimmer using the Homeyduino based on Wemos Mini D1 which I control either from the app or a physical button. Works really great in version 1.5 rc14, I don’t dare to step over to 2.0😰

I forked the basis from [https://github.com/Adminius/DimmerControl](https://github.com/Adminius/DimmerControl)

---

<div class="post-metadata">

**Author:** ![Marlon](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/marlon/32/3794_2.png) [@Marlon](https://community.homey.app/u/Marlon)\
**Post date:** [January 8, 2019, 6:21pm UTC](https://community.homey.app/t/arduino-homeduino-met-esp8266-aansturen-pinnen-op-arduino/6265/28 "2019-01-08T18:21:01Z")

</div>

I recently converted my Homeyduino project (pir, led and lightsensor BH1750) to use MQTT and it was rather painless.

 ![pir_bh1750](https://us1.discourse-cdn.com/flex025/uploads/athom/original/2X/c/c8b8fb962d3bf94888b9a92ce4b99fe37623f9f8.jpeg)

---

<div class="post-metadata">

**Author:** ![Sander\_Vlotman](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/sander_vlotman/32/7855_2.png) [@Sander\_Vlotman](https://community.homey.app/u/Sander_Vlotman)\
**Post date:** [January 9, 2019, 4:53pm UTC](https://community.homey.app/t/arduino-homeduino-met-esp8266-aansturen-pinnen-op-arduino/6265/29 "2019-01-09T16:53:43Z")

</div>

Hi Marlon,

Looks interesting. Can you share your code somewhere so I can learn ;-)?  
Thanks,  
Sander

---

<div class="post-metadata">

**Author:** ![Marlon](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/marlon/32/3794_2.png) [@Marlon](https://community.homey.app/u/Marlon)\
**Post date:** [January 9, 2019, 6:19pm UTC](https://community.homey.app/t/arduino-homeduino-met-esp8266-aansturen-pinnen-op-arduino/6265/30 "2019-01-09T18:19:19Z")

</div>

I don’t mind sharing the entire ino file.

```
#include <Wire.h>
#include <BH1750.h> // Lightsensor
#include <ESP8266WiFi.h>
#include <PubSubClient.h> //https://github.com/knolleary/pubsubclient
#define ledPin D7 // Green LED
#define pirPin D1 // Input for PIR HC-SR501
BH1750 lightMeter(0x23);

int state = LOW; // by default, no motion detected
int pirPinValue; // variable to store read PIR Value

unsigned long previousLuminosity = 0;
unsigned long previousMillis = 0;
const unsigned long ldrInterval = 2 * 60 * 1000; //default interval for lightsensor in milliseconds

String clientId = "ESP_" + String(ESP.getChipId(), HEX);
char *ssid = "ssid";
char *password = "password";

const char *mqttServer = "192.168.1.61";
const int mqttPort = 8883;
const char *mqttUser = "test";
const char *mqttPassword = "uno";
const char *motion_topic = "home/hallway/pir/motion";
const char *luminance_topic = "home/hallway/pir/luminance";

//WiFiClientSecure is required if you are using mqtts usually port 8883
WiFiClientSecure espClient;
// If you are using port 1883 without a certificatie then use WiFiClient instead
PubSubClient client(espClient);

void setup()
{
  Serial.begin(115200);
  pinMode(ledPin, OUTPUT);
  pinMode(pirPin, INPUT);
  digitalWrite(ledPin, LOW); // led off

  // Initialize the I2C bus (BH1750 library doesn't do this automatically)
  Wire.begin(D3, D4);
  // On esp8266 you can select SCL and SDA pins using Wire.begin(D4, D3);

  // begin returns a boolean that can be used to detect setup problems.
  if (lightMeter.begin(BH1750::CONTINUOUS_HIGH_RES_MODE)) {
    Serial.println(F("BH1750 Advanced begin"));
  }
  else {
    Serial.println(F("Error initialising BH1750"));
  }

  previousMillis = ldrInterval;
  randomSeed(analogRead(A0));
}

void loop()
{
  //always running no delay	
  lightDetection(false);
  motionDetection();
}

void lightDetection(bool immediate)
{
  if (immediate || previousMillis == 0 || readLight())
  {
    float lux = lightMeter.readLightLevel();
    Serial.print("Luminosity "); Serial.println(lux);

    wifiConnect();

    client.publish(luminance_topic, String(lux).c_str(), true);
  }
}

bool readLight()
{
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis > ldrInterval)
  {
    previousMillis = currentMillis;
    return true;
  }
  else
  {
    return false;
  }
}

void motionDetection()
{
  pirPinValue = digitalRead(pirPin);
  if (pirPinValue == HIGH)
  {
    digitalWrite(ledPin, HIGH); // turn LED ON
    delay(100); // delay 100 milliseconds
    if (state == LOW)
    {
      Serial.println("Motion detected!");
      state = HIGH;
      lightDetection(true); // before setting motion alarm, send lightsensor value first
      delay(50);
      setMotionAlarm(true);
    }
  }
  else
  {
    digitalWrite(ledPin, LOW); // turn LED OFF
    delay(200); // delay 200 milliseconds

    if (state == HIGH)
    {
      Serial.println("Motion stopped!");
      state = LOW;
      setMotionAlarm(false);
    }
  }
}

void setMotionAlarm(bool motionDetected) {
  wifiConnect();
  Serial.print("Publish "); sprintf(motion_topic, motionDetected ? "true": "false");
  client.publish(motion_topic, motionDetected ? "true": "false", true); // by sending true and false as text, Homey automagically converts those to actual booleans
}

void wifiConnect()
{
  if (WiFi.status() != WL_CONNECTED)
  {
    Serial.print("Not connected to wifi, connecting now");
    WiFi.mode(WIFI_STA); // Static
    WiFi.hostname("Navnet PIR");
    //Connect to network
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED)
    {
      delay(500);
      Serial.print(".");
    }

    //Print IP address
    Serial.print("Connected to IP address: ");
    Serial.print("Ip: "); Serial.print(WiFi.localIP());
    Serial.println("\tMAC: "); Serial.println(WiFi.macAddress());
  }

  if (!client.connected()) {
    client.setServer(mqttServer, mqttPort);
    Serial.print("Connecting to MQTT server "); Serial.print(mqttServer);

    while (!client.connected()) {
      if (client.connect(clientId.c_str(), mqttUser, mqttPassword ))
      {
        Serial.println("\tconnected");
      } else {
        delay(500);
        Serial.print(".");
      }
    }
  }
}

```

More info on the Homey flows and MQTT Broker settings checkout this [post](https://community.homey.app/t/homeyduino-v2-0/6407/11)  
Good luck! 🍻

---

<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 9, 2019, 8:00pm UTC](https://community.homey.app/t/arduino-homeduino-met-esp8266-aansturen-pinnen-op-arduino/6265/31 "2019-01-09T20:00:15Z")

</div>

Here’s what I wrote this evening (also for an BH1750, connected to an ESP8266):

```auto
#define MY_DEBUG
#define MY_GATEWAY_ESP8266
#define MY_GATEWAY_MQTT_CLIENT
#define MY_BAUD_RATE 115200
#define MY_WIFI_SSID "XXX"
#define MY_WIFI_PASSWORD "YYY"
#define MY_MQTT_CLIENT_ID "esp-lightsensor"
#define MY_MQTT_PUBLISH_TOPIC_PREFIX "my/" MY_MQTT_CLIENT_ID "/pub"
#define MY_MQTT_SUBSCRIBE_TOPIC_PREFIX "my/" MY_MQTT_CLIENT_ID "/sub"
#define MY_CONTROLLER_IP_ADDRESS 192, 168, 23, 8
#define MY_PORT 1883

#include <ESP8266WiFi.h>
#include <MySensors.h>
#include <Wire.h>
#include <BH1750.h>

BH1750 lightMeter;
MyMessage message (0, V_LEVEL);

void setup() {
  Wire.begin(D3, D4);
  if (lightMeter.begin(BH1750::CONTINUOUS_HIGH_RES_MODE)) {
    Serial.println(F("BH1750 Advanced begin"));
  } else {
    Serial.println(F("Error initialising BH1750"));
  }
}

void presentation() {
  sendSketchInfo("LightSensor", "1.0");
  present(0, S_LIGHT_LEVEL);
}

void loop() {
  float lux = lightMeter.readLightLevel();
  Serial.println("Measured level: " + String(lux) + " lux.");
  send(message.set((int32_t) lux));
  delay(2000);
  ESP.deepSleep(30e6);
}

```

It uses the [MySensors](https://platformio.org/lib/show/548/MySensors) library, which does most of the heavy lifting with regards to WiFi and MQTT. I use the MQTT Broker app to provide the MQTT server.

There _is_ a MySensors app for Homey, but I don’t think it’s being maintained anymore. I didn’t feel like using it, and instead opted for a quick-and-dirty solution:

- use the Virtual Devices app to create a sensor with the capability _“Helderheid/Luminance”_
- use the MQTT Client app to watch for updates from the light sensor node (it updates every 30 seconds) and set the `measure_luminance` capability of the virtual sensor device:  
 ![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/2X/8/835e7d467b1bbd77eb73d4cefdc379314f705e86.jpeg)

(the full topic used is `my/esp-lightsensor/pub/0/0/1/0/37`, where `my/esp-lightsensor/pub` is a sort of prefix, and the numbers are the MySensors encoding of the sensor type/data).

---

<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 10, 2019, 9:35am UTC](https://community.homey.app/t/arduino-homeduino-met-esp8266-aansturen-pinnen-op-arduino/6265/32 "2019-01-10T09:35:50Z")

</div>

Here’s another interesting project: [https://esphomelib.com/](https://esphomelib.com/)

It supports a lot of sensors and protocols, but will only work on ESP8266/ESP32 devices.

---

<div class="post-metadata">

**Author:** ![Blarp](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/blarp/32/10278_2.png) [@Blarp](https://community.homey.app/u/Blarp)\
**Post date:** [January 10, 2019, 8:32pm UTC](https://community.homey.app/t/arduino-homeduino-met-esp8266-aansturen-pinnen-op-arduino/6265/33 "2019-01-10T20:32:19Z")

</div>

> [@robertklep](#):
>
> Here’s another interesting project: [https://esphomelib.com/](https://esphomelib.com/)  
> It supports a lot of sensors and protocols, **but will only work on ESP8266/ESP32 devices**.

Only esp8266/32 devices? Abundantly available and dirt cheap, those ones?  
But if I understand you correctly, you mean for instance mysensors is not limited to esp but could also tap in to the world of raspberry pi and the like?

As a sidenote: I could not help but notice: [esphomelib API Reference — esphomelib 1.7.0 documentation](https://esphomelib.com/esphomeyaml/components/sensor/ble_rssi.html)  
ESP32 Bluetooth Low Energy RSSI Sensor. Esp32 has bluetooth and you can get the signalstrength (rssi) over mqtt to homey. With multiple esp32 devices you can locate where a bluetooth device is. (not all that precise, but enough to play with)

But a lot to love about esphomelib. What I like about there approach is: no programming experience required (well, editing of YAML configuration files), just playing with sensors.  
I did not hear from it before, It is created/maintained by Otto Winter, an Austrian. Busy guy it seems.  
Esphomelib/esphomeyaml is knee deep in the Home Assistant world. Now is that not a negative thing in principle.

But maybe Esphomelib is not the most interesting to see in that world.  
Home Assistant has a “MQTT Discovery” feature, witch, from what I can see sort of automates the connection from mqtt enabled devices to usable _virtual devices_ in Home Assistant. The configuration is done on the device itself and the topic used by the device.  
And it works with:

- Tasmota
- Esphomelib
- Zigbee2mqtt

---

<div class="post-metadata">

**Author:** ![Blarp](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/blarp/32/10278_2.png) [@Blarp](https://community.homey.app/u/Blarp)\
**Post date:** [January 10, 2019, 8:45pm UTC](https://community.homey.app/t/arduino-homeduino-met-esp8266-aansturen-pinnen-op-arduino/6265/34 "2019-01-10T20:45:45Z")

</div>

> [@Marlon](#):
>
> I don’t mind sharing the entire ino file.

> [@robertklep](#):
>
> Here’s what I wrote this evening

To also share something: I flashed a wemos d1 mini today with Tasmota (sonoff-sensors.bin with esptool) and connected a

- KY-018 light sensor
- 4 Channel Relay Module Board
- AM312 PIR motion dection  
to it and connected it via MQTT to Homey. Great fun!

Fun to see the slightly different approaches in this topic to open up the esp/arduino world to homey.

---

<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 10, 2019, 8:59pm UTC](https://community.homey.app/t/arduino-homeduino-met-esp8266-aansturen-pinnen-op-arduino/6265/35 "2019-01-10T20:59:05Z")

</div>

I don’t think that MySensors has “node” support for RPi, only gateway support. In other words, you can’t use it as a sensor device, but you _can_ use it as a device that sensor nodes can communicate with. Using an RPi as a sensor node seems a bit over-the-top anyway.

And yes, the ESP32 is a capable BLE device, although you can’t use BLE and WiFi together (they share the same internal radio and antenna). But it’s relatively easy to do some BLE stuff, turn off BLE, turn on WiFi, do WiFi stuff, and to back to BLE, in a loop.

I haven’t been able to find anything on the actual protocol that HA uses to (directly) talk to esphomelib nodes, but it might be interesting to see if a Homey app can be build for it.

---

<div class="post-metadata">

**Author:** ![Blarp](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/blarp/32/10278_2.png) [@Blarp](https://community.homey.app/u/Blarp)\
**Post date:** [January 10, 2019, 10:12pm UTC](https://community.homey.app/t/arduino-homeduino-met-esp8266-aansturen-pinnen-op-arduino/6265/36 "2019-01-10T22:12:02Z")

</div>

> [@robertklep](#):
>
> the actual protocol

That would have been nice. But I’m not knee deep in HA and a simple search doesn’t cut it.  
Something for another day

---

<div class="post-metadata">

**Author:** ![VanGilles](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/vangilles/32/11478_2.png) [@VanGilles](https://community.homey.app/u/VanGilles)\
**Post date:** [January 25, 2019, 10:47am UTC](https://community.homey.app/t/arduino-homeduino-met-esp8266-aansturen-pinnen-op-arduino/6265/37 "2019-01-25T10:47:12Z")

</div>

> [@Marlon](#):
>
> //WiFiClientSecure is required if you are using mqtts usually port 8883 WiFiClientSecure espClient; // If you are using port 1883 without a certificatie then use WiFiClient instead PubSubClient client(espClient);

First off, thanks for the sketch. I try to get it to work with a secure connection, but cannot connect to the MQQT server (MQQT broker app on Homey).

You note to connect with ‘WiFiClientSecure’ if you work with port 8883 but I am a bit confused because I don’t see you include this library on the start of the sketch with '#include \<WifiClientSecure.h\>. Don’t you need to include this library first to get it to work?

---

<div class="post-metadata">

**Author:** ![Marlon](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/marlon/32/3794_2.png) [@Marlon](https://community.homey.app/u/Marlon)\
**Post date:** [January 25, 2019, 11:26am UTC](https://community.homey.app/t/arduino-homeduino-met-esp8266-aansturen-pinnen-op-arduino/6265/38 "2019-01-25T11:26:57Z")

</div>

> [@VanGilles](#):
>
> Don’t you need to include this library first to get it to work?

Indeed, WiFiClientSecure and WiFiClient are part of the same library.

```
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>

```

---

<div class="post-metadata">

**Author:** ![VanGilles](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/vangilles/32/11478_2.png) [@VanGilles](https://community.homey.app/u/VanGilles)\
**Post date:** [January 25, 2019, 2:54pm UTC](https://community.homey.app/t/arduino-homeduino-met-esp8266-aansturen-pinnen-op-arduino/6265/39 "2019-01-25T14:54:38Z")

</div>

Thanks for the answer. I included the library in the beginning of the sketch but still i cannot connect secure 8883 (1883 unsecured all fine). A couple of questions I still have. Maybe you can help me out.

- How come you didn’t included the ‘WifiClientSecure library’ in your sample code you posted and have it working?
- Don’t you need to type in a ‘certificate key’ in the arduino code to verify the MQQT-client?
- Are you using the Homey MQQT Broker by Menno van Grinsven?

---

<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, 2019, 2:58pm UTC](https://community.homey.app/t/arduino-homeduino-met-esp8266-aansturen-pinnen-op-arduino/6265/40 "2019-01-25T14:58:15Z")

</div>

Do you _need_ to use a secured connection? I assume that everything is connected to your local WiFi network.

[Previous page](https://community.homey.app/t/arduino-homeduino-met-esp8266-aansturen-pinnen-op-arduino/6265.md?page=1)

[Next page](https://community.homey.app/t/arduino-homeduino-met-esp8266-aansturen-pinnen-op-arduino/6265.md?page=3)
