Arduino / Homeduino met ESP8266 aansturen pinnen op Arduino

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
Good luck! :beers: