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);
}
}