As the title says... Chat on...

User avatar
By Ceramiclover
#82115 Low experience level, so please bear with me.
We do not have a backdoor at the house I rent, so my dog goes out a window to a ramp to get to the yard.
I tried connect a nodemcu to a PIR sensor to notify me when she is waiting.

The problem is that it notifies me every 5 minutes and on startup that she is there, even if the PIR sensor is unplugged.

I repurposed the code from someone else's project and admit, I do not understand hardly any of the wireless programming.

Any suggestions would be appreciated.

I set up the timing with the goal of checking every 15 seconds for motion. If the sensor pulls the input high, send notification, then wait 5 minutes.

My code is as follows:

/************
GeeksTips.com
ESP8266 Arduino Tutorial - Push notification messages example
Arduino code example
www.geekstips.com

- replace the dots with your Wi-fi credentials and
- your deviceId from pushingBox account
*/
#include <ESP8266WiFi.h>

// constants won't change. They're used here to set pin numbers:
const int SIGNALPIN = D0; // the number of the PIR Signal Wire

// variables will change:
int PIRSTATE = 0; // variable for reading the PIR status

// PushingBox scenario DeviceId code and API
String deviceId = "...";
const char* logServer = "api.pushingbox.com";

const char* ssid = "...";
const char* password = "...";

void setup() {
// initialize the PIRsignal pin as an input:
pinMode(SIGNALPIN, INPUT);
digitalWrite(D0, LOW); //enable pullups to make pin LOW
Serial.begin(74880);
// Sending a notification to your mobile phone
// function takes the message as a parameter

}

void sendNotification(String message){

Serial.println("- connecting to Home Router SID: " + String(ssid));

WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}

Serial.println();
Serial.println("- succesfully connected");
Serial.println("- starting client");

WiFiClient client;

Serial.println("- connecting to pushing server: " + String(logServer));
if (client.connect(logServer, 80)) {
Serial.println("- succesfully connected");

String postStr = "devid=";
postStr += String(deviceId);
postStr += "&message_parameter=";
postStr += String(message);
postStr += "\r\n\r\n";

Serial.println("- sending data...");

client.print("POST /pushingbox HTTP/1.1\n");
client.print("Host: api.pushingbox.com\n");
client.print("Connection: close\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(postStr.length());
client.print("\n\n");
client.print(postStr);
}
client.stop();
Serial.println("- stopping the client");
}

void loop(){
delay (15000);
// read the state of the PIRsignal value:
PIRSTATE = digitalRead(SIGNALPIN);


// check if the SIGNALPIN is ACTIVE. If it is, the SIGNALPIN is HIGH:
if (PIRSTATE == HIGH ) {
sendNotification("Willow is at the window");
}
delay (300000);
if (PIRSTATE == LOW ) {
delay (15000);
}


}
User avatar
By schufti
#82131 I think this belongs to Arduino not Lua, but .....

not looked closely at the rest of code but maybe modify

in setup()
Code: Select all// initialize the PIRsignal pin as an input:
//pinMode(SIGNALPIN, INPUT);
digitalWrite(D0, LOW); //enable pullups to make pin LOW
pinMode(D0,INPUT_PULLDOWN_16);


Code: Select allvoid loop(){
// check if the SIGNALPIN is ACTIVE. If it is, send message and wait 5min else wait 15s
if (digitalRead(D0) ) {
  sendNotification("Willow is at the window");
  delay (300000); }
else
  delay(15000);

}


of course that can't solve simple erratic behaviour of the pir sensor.
You would need to implement some clever logic to avoid false alarms due to other "trigger events" like suppress to short events, to many in short period etc
User avatar
By Ceramiclover
#82134 As far as I have observed with a meter, the PIR is operating as intended.

I am testing the updated code now.

Can you explain what the difference is between these two lines?

digitalWrite(D0, LOW); //enable pullups to make pin LOW
pinMode(D0,INPUT_PULLDOWN_16);

and is there some clarity as to when to refer to the esp8266 pin id (16) and when to use the label on the nodemcu(DO)?


and I am wondering how:
"if (digitalRead(D0) ) {"
means read whether the pin is reading HIGH

Thank you for your help, and again, sorry for my ignorance.
User avatar
By torntrousers
#82145 Hello Ceramiclover.

NodeMCU's use the D0, D1, D2 etc naming convention for the ESP8266 GPIO pins, and D0 is the same as what is GPIO 16.

Most ESP8266 GPIO pins have built in pull up resistors but GPIO-16 is the exception and it instead has a pull down resistor. Doing a digitalWrite to an input pin was an old way of enabling the pull up/down resistor, now you can do that with the pinMode function, so pinMode(D0,INPUT_PULLDOWN_16) enables the pull down resistor on GPIO16. The '_16' at the end of INPUT_PULLDOWN_16 is just a hint that it only works for GPIO 16.

digitalRead(D0) returns HIGH or LOW, which means either a 1 or a 0. An if statement tests for TRUE or FALSE which are also 1 or 0. So 'if (digitalRead(D0))' is just a more concise way of doing 'if (digitalRead(D0) == HIGH)'

From what I understand of how most PIR sensors work is that you don't need to use any pull up/down so in setup() just do pinMode(SIGNALPIN, INPUT); and then after that add a delay(5000) because most PIR's need a few seconds to settle down after power up. Then in the loop() you should be able to have it simply do:
Code: Select allvoid loop() {
    if (digitalRead(SIGNALPIN)) {
        sendNotification("Willow is at the window");
        delay (300000);
    }
}

Note also that a lot of PIR sensor modules need more than 3.3v to be stable so if you've one of those then power it from the NodeMCU VIN pin (which is 5v) not the 3.3v pin.