PIR Sensor to Servo - Newbie

Hello everyone!

I am a complete newbie and know NOTHING about coding/programing. This has been many trials and error. The reason I start this is because my cat uses the toilet and likes to use a fresh clean bowl. I have gotten it setup to my internet so when I am not at home, I can flush it remotely. Just... in the mornings well... let's just say I don't get to it in time.

I have a ESP8266 ESP-12E board.
I currently bought both AM312 Mini Detector Module HC-SR312 and HC-SR501 Pir Motion IR Sensor Body Module Infrared for Arduino as I was not sure which would work. Any advice?

Also, I am not sure how to program the code of the PIR sensor to the one that I currently have. If you have any advice that would be lovely. I appreciate all the help as I am now pulling my hair out due to frustration.

  #define DEBUG_ESP_PORT Serial
  #define NODEBUG_WEBSOCKETS
  #define NDEBUG
#endif

#include <Arduino.h>
#ifdef ESP8266
#include <ESP8266WiFi.h>
#endif
#ifdef ESP32
#include <WiFi.h>
#endif

#include "SinricPro.h"
#include "SinricProSwitch.h"
#include <Servo.h>

Servo toiletServo;

#define WIFI_SSID   "WIFI Info"
#define WIFI_PASS   "PASSWORD"
#define APP_KEY     "APPKEY INFO"
#define APP_SECRET  "APPSECRET"
#define SWITCH_ID_1 "ID"
#define BAUD_RATE   9600

int pos = 0;
int servoPin = D1;

bool onPowerState1(const String &deviceId, bool &state) {
  Serial.printf("Device 1 turned %s\r\n", state ? "on" : "off");

  toiletServo.attach(servoPin);

  for (pos = 0; pos <= 180; pos += 1) {
    toiletServo.write(pos);
    delay(15);
  }
  delay(1000);

  for (pos = 180; pos >= 0; pos -= 1) {
    toiletServo.write(pos);
    delay(15);
  }
  delay(1000);

  return true; // request handled properly
}

void setupWiFi() {
  Serial.printf("\r\n[Wifi]: Connecting");
  WiFi.begin(WIFI_SSID, WIFI_PASS);

  while (WiFi.status() != WL_CONNECTED) {
    Serial.printf(".");
    delay(250);
  }

  Serial.printf("connected!\r\n[WiFi]: IP-Address is %s\r\n", WiFi.localIP().toString().c_str());
}

void setupSinricPro() {
  SinricProSwitch& mySwitch1 = SinricPro[SWITCH_ID_1];
  mySwitch1.onPowerState(onPowerState1);

  SinricPro.onConnected([]() { Serial.printf("Connected to SinricPro\r\n"); });
  SinricPro.onDisconnected([]() { Serial.printf("Disconnected from SinricPro\r\n"); });
  SinricPro.begin(APP_KEY, APP_SECRET);
}

void setup() {
  Serial.begin(BAUD_RATE);
  Serial.printf("\r\n\r\n");

  setupWiFi();
  setupSinricPro();
}

void loop() {
  SinricPro.handle();
}
type or paste code here

Welcome to the forum. All your trouble is over. :expressionless:

Thanks for posting the code properly. That's not a low bar, evidently.

Are we to assume the posted code works fine, and you want to add the ability to trigger the event with the PIR?

All three of those modules are essentially identical, at a glance. If they work they do, and if not they don't. They can be programmed with, I bet, very nearly the same code.

Have you tried a sketch using just the PIR, preferably one you did not write or mess with at all? Many simple examples can be found, many times where you bought the sensor may provide code or links to tutorials or help.

This is the best way to proceed. Get the PIR to just report what it is sensing, printing the raw output perhaps, or running just one LED.

It's as simple,e as digitalRead the sensor digital output, and use that to print something like "motion" or "no motion" in the loop function.

Use a large animal first, like a handy human, to test the sensors and get an idea of how they report what they sense.

Look into any jumpers or adjustments so you can fiddle with them, if indeed any of you sensors afford such tuning.

Next get your cat to help you test whether and how the sensor reacts (or does not…) to the presence of cat motion.

One sensor or the other might be more sensitive to smaller warmish things in motion,

Leave everything else for later. If you can't get a reliable useful signal from any of your PIR modules and the motion you cat will make leaving or entering the detect zone, it is pointless to worry about integrating a new method for trigger the flush event.

You may want to think meanwhile about incorporating some kind of delay, so that the flush comes, say, seven minutes after motion has ceased. I know something about cats, and yours may not like to be flushed while in the middle of things.

HTH

a7

Hi!

Thank you for the reply. I was able to test out HC-SR501 Pir Motion IR Sensor Body Module Infrared for Arduino which actually came with a test script. I have uploaded a screen shot below.

Yes, I totally agree. I was hoping since the HC-SR501 has some type of delay on the device I might be able to set it to the max delay.

I'm old fashioned, so I would be more likely to make handy anything fancy using code, and not the build-in delay or other sensor features.

But whatever works. It might just be perfect that motion makes the sensor signal appear for seven minutes, then noticing its absence could trigger the response.

Please use the same mechanism for posting anything text you could cut and paste as you did for the sketch in your first post. It is always preferred. Here for example, someone (me) might want to use what are essentially the results of your experiments in some way that would be made easier than cutting and pasting your code or output.

Photos for your cat, yes. For code, not so much.

Also you did neither confirm nor deny that the code in #1 functions well nd is indeed how you remote flush now.

I'm in transit and will be tuning out at the end of the journey, so for now I just ask what is your problem with adding the sensor? What have you tired or what is stopping you from just going ahead?

If you wrote the code in #1, adding a PIR seems like no large task. If you did not, maybe say something about the last sketch you did write. If you haven't written any simple sketches, you are asking someone to do this integration for you. You may find forum participants here willing and able to do.

Again here it is preferred that you make an attempt to do this yourself, and come here with such attempts and your observations as to whether and how they do things they are not supposed to, or don't do things they should.

Have you worked through any of the basic sketches on offer in the IDE, or followed any learning material found on the internets that purports to teach yoh how to program?

Look for a few classic patterns there. Two that you might make good use of, and should try to come to a complete understanding of, are "blink without delay" and "state change detection".

Also googling for either whilst adding "arduino" to focus you results should bury you with hits. Spend five or ten minutes looking at five or ten that seem to be talking to you at your current level and see if anything makes sense and how you might use them to implement this new feature.

An hour +/- well spent right there, which expenditure will pay off for the rest of your life. :expressionless:

This is a relatively easy addition to the code you have and as such affords a good opportunity to test and stretch stuff you should know by now or learn sooner rather than later or never.

Programming isn't mastered overnight. There is no instant or even rapid gratification. Although trial and error can enter into learning and even doing it, that should not be how you proceed forever. I sympathize with you hair problem, but accomplished hobbyists and even professional programmers can be hard on hair.

Take a stab at an attempt to add the sensor.

a7

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.