Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*********
- updated 8/12/9 kentm
- Complete project details at https://randomnerdtutorials.com
- adapted from multisensor NodeRed project. Each sketch addresses
- one modified sonoff basic device that must match "sonoffxxX"
- Search sonoffxxX and replace with current number (four)
- The "S" in the name indicates this a sensor
- modified for RCWL-0516 sensor and ESP826601
- Adjust total "ON" time in final loop statement in millis
- *********/
- // Load libraries
- #include <ESP8266WiFi.h>
- #include <PubSubClient.h>
- // Replace with your network credentials
- const char* ssid = "xxxxxxx";
- const char* password = "xxxxxxxx";
- // Change the variable to your Raspberry Pi IP address,
- // so it connects to your MQTT broker
- const char* mqtt_server = "xxx.xxx.x.x";
- // Initializes the espClient. You should change the
- // espClient name if you have multiple ESPs running
- // in your home automation system
- WiFiClient espClient;
- PubSubClient client(espClient);
- const int motionSensor = 2;
- // Timers - Auxiliary variables
- unsigned long now = millis();
- unsigned long lastMeasure = 0;
- boolean startTimer = false;
- unsigned long currentTime = millis();
- unsigned long previousTime = 0;
- // Don't change the function below.
- // This function connects your ESP8266 to your router
- void setup_wifi() {
- delay(10);
- // We start by connecting to a WiFi network
- 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 is executed when some device publishes
- // a message to a topic that your ESP8266 is subscribed to
- // Change the function below to add logic to your program,
- // so when a device publishes a message to a topic that
- // your ESP8266 is subscribed you can actually do something
- void callback(String topic, byte* message, unsigned int length) {
- Serial.print("Message arrived on topic: ");
- Serial.print(topic);
- Serial.print(". Message: ");
- String messageTemp;
- for (int i = 0; i < length; i++) {
- Serial.print((char)message[i]);
- messageTemp += (char)message[i];
- }
- Serial.println();
- Serial.println();
- }
- // 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...");
- // Create a random client ID
- String clientId = "ESP8266Client-";
- clientId += String(random(0xffff), HEX);
- // Attempt to connect
- if (client.connect(clientId.c_str())) {
- Serial.println("connected");
- // Subscribe or resubscribe to a topic
- // You can subscribe to more topics (to control more outputs)
- client.subscribe("esp8266/output");
- } else {
- Serial.print("failed, rc=");
- Serial.print(client.state());
- Serial.println(" try again in 5 seconds");
- // Wait 5 seconds before retrying
- delay(5000);
- }
- }
- }
- // Checks if motion was detected and the sensors are armed.
- // Then, starts a timer.
- void detectsMovement() {
- Serial.println("MOTION DETECTED!");
- client.publish("esp8266/motion", "MOTION DETECTED-xxX!");
- client.publish("home/office/sonoff-xxX","on");
- previousTime = millis();
- startTimer = true;
- }
- void setup() {
- // Serial port for debugging purposes
- Serial.begin(115200);
- // RDR Motion Sensor mode, then set interrupt function and RISING mode
- pinMode(motionSensor, INPUT);
- attachInterrupt(digitalPinToInterrupt(motionSensor), detectsMovement, RISING);
- setup_wifi();
- client.setServer(mqtt_server, 1883);
- client.setCallback(callback);
- }
- void loop() {
- if (!client.connected()) {
- reconnect();
- }
- client.loop();
- // Timer variable with current time
- now = millis();
- // After 150 seconds have passed since motion was detected,
- // publishes a "No motion" message
- if ((now - previousTime > 150000)&& startTimer) {
- client.publish("esp8266/motion", "NO MOTION-xxX");
- client.publish("home/office/sonoffxxX","off");
- Serial.println("Motion stopped");
- startTimer = false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement