Select your language

Kui ööd on pimedad siis väike valgus abiks on, et leida tee tualetti
Komponendid:
1. Wemos mini D1
2. Radarsensor RCWL-0516
3. Toide, (tel. laadia)
4. Installatsiooni materjalid

Wemos  wifi sigmaal -> Server MQTT - node red
programm node redis (kui kaua sonoff ON / OFF)
Node red - MQTT -> Sonoff - valgusti

RCWL-0516 <-> Wemos D1
VCC              -   5V
G                 -   GND
OUT             -   A0

wemos digipin pin andis stabiilselt HIGH ?
Töötab kui kasutada analoogsisendit: liikumine 1024, vaikus 9-10

Tsüklisse 100 ms viivitus
kui 5 x input > 20 siis tuvast. liikumine -> MQTT

* Node red
kui sisendis radar-1 on signaal ->   siis  saada msg.payload =1           -> sonoff kontakt ON
                                              oota 3 min saada msf.payload = 0     -> sonoff kontakt OFF                                          
 
// Wemos Mini D1 <-> RCWL-0516
// Client = "espClient_6";

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#define Sensor A0
bool ps = false;            // ps=client.publish()
int i = 0;

const char* ssid = "xxxxxxxxxx";
const char* password = "yyyyyyyyy";
const char* mqtt_server = "xxx.xxx.xxx.x";

WiFiClient espClient_6;
PubSubClient client(espClient_6);

// Don't change the function below. This functions connects your ESP8266 to your router
void setup_wifi() {
  delay(10);
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("WiFi connected - ESP IP address: ");
  Serial.println(WiFi.localIP());
}

// This functions reconnects your ESP8266 to your MQTT broker
// Change the function below if you want to subscribe to more topics with your ESP8266
void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect("espClient_6")) {
      Serial.println("connected");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

// The setup function sets your ESP GPIOs to Outputs, starts the serial communication at a baud rate of 115200
// Sets your mqtt broker and sets the callback function
void setup() {
  Serial.begin(115200);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  pinMode(Sensor, INPUT);
}

// don't need to change anything in the loop function.
// Basically it ensures that you ESP is connected to your broker
void loop() {
  if (!client.connected()) {
    reconnect();
  }
  if (!client.loop())
    client.connect("espClient_6");
  delay(100);
  float x = analogRead(Sensor);
  //Serial.println(x);
  if (x > 20) {
    i++;
  } else {
    i = 0;
  }
  if (i > 5) {
    i = 0;
    //Serial.println("Liikumine !");
    do {
      ps = client.publish("radar-1", String(1).c_str());
    } while (!ps);
    delay (10000);
  }
}​