NodeMCU ESP8266_PIR stays HIGH

Moderators: adafruit_support_bill, adafruit

Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.
Locked
User avatar
QnBarb
 
Posts: 18
Joined: Mon Jan 23, 2023 9:44 am

NodeMCU ESP8266_PIR stays HIGH

Post by QnBarb »

Hello,
Does anyone know why the NodeMCU ESP8266 board is sending PIR sensor data as 'HIGH' even though the sensor is not connected. Therefore, it's sending false data to Adafruit IO.
Thanks in advance.
Just in case, here is the code I used:

Code: Select all

#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
const char *ssid = "****"; // Enter your WiFi Name
const char *pass = "****"; // Enter your WiFi Password

WiFiClient client;

#define MQTT_SERV "io.adafruit.com"
#define MQTT_PORT 1883
#define MQTT_NAME "****" // Your Adafruit IO Username
#define MQTT_PASS "****" // Adafruit IO AIO key

const int ledPin = D0;
const int sensor = D2;
const int buzzer = D6; // Here we are defining the pins which we will use to connect our sensors, LED and Buzzer

//Set up the feed you're publishing to
Adafruit_MQTT_Client mqtt(&client, MQTT_SERV, MQTT_PORT, MQTT_NAME, MQTT_PASS);
Adafruit_MQTT_Publish pirsensor = Adafruit_MQTT_Publish(&mqtt,MQTT_NAME "/f/pirsensor"); // pirsensor is the Adafruit IO feed name where you will publish your data

Adafruit_MQTT_Subscribe LED = Adafruit_MQTT_Subscribe(&mqtt, MQTT_NAME "/f/LED"); //Set up the feed you're subscribing to from Adafruit IO

void setup()
{
Serial.begin(115200);
delay(10);
mqtt.subscribe(&LED); //To start the communication between the NodeMCU and Adafruit IO
pinMode(buzzer, OUTPUT);
pinMode(ledPin, OUTPUT); // The declaration of PinMode of the NodeMCu


Serial.println("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, pass); // Here the NodeMCU ESP8266 will try to connect to the wifi with the ID and Password you have provided
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print("."); // this piece will print "....." till not connected
}
Serial.println("");
Serial.println("WiFi connected"); // this will be printed when wifi connection is made
}

void loop()

{
MQTT_connect(); // This code utilizes MQQT transfer Protocol. MQTT stands for MQ Telemetry Transport. It is a publish/subscribe, extremely simple and lightweight messaging protocol, designed for constrained devices and low-bandwidth, high-latency or unreliable networks.

int state = digitalRead(sensor);
if(state == HIGH) {
pirsensor.publish(state);
delay(1000);
digitalWrite(ledPin, LOW); // Turn the LED on (Note that LOW is the voltage level
digitalWrite(buzzer,HIGH);
Serial.println("Motion detected!");
delay(100);
}
else{
digitalWrite(ledPin, HIGH); // Turn the LED off by making the voltage HIGH
digitalWrite(buzzer,LOW);
Serial.println("Motion Absent");
delay(100); // Wait for two seconds (to demonstrate the active low LED)
}

Adafruit_MQTT_Subscribe * subscription;
while ((subscription = mqtt.readSubscription(5000))) //Dont use this one until you are conrolling something or getting data from Adafruit IO. Here we are only controlling the LED from the toggle switch of Adfruit IO Dashboard
{

if (subscription == &LED)
{
//Print the new value to the serial monitor
Serial.println((char*) LED.lastread);

if (!strcmp((char*) LED.lastread, "OFF"))
{
digitalWrite(ledPin, HIGH);
}
if (!strcmp((char*) LED.lastread, "ON"))
{
digitalWrite(ledPin, LOW);
}
}

}
}
void MQTT_connect() // This little section of code here is how MQQT Protocol sends the data to the server.
{
int8_t ret;

// Stop if already connected.
if (mqtt.connected())
{
return;
}

uint8_t retries = 3;
while ((ret = mqtt.connect()) != 0) // connect will return 0 for connected
{

mqtt.disconnect();
delay(5000); // wait 5 seconds
retries--;
if (retries == 0)
{
// basically die and wait for WDT to reset me
while (1);
}
}
}
Last edited by adafruit_support_bill on Wed Jan 25, 2023 8:59 am, edited 1 time in total.
Reason: Please use [code] tags when posting code to the forums

User avatar
bidrohini
 
Posts: 202
Joined: Thu Oct 20, 2022 10:03 am

Re: NodeMCU ESP8266_PIR stays HIGH

Post by bidrohini »

I suggest you place the PIR and the ESP8266 not close to each other. Earlier I have read some other users facing the same problem. It happens because the PIR signal gets interrupted by the internal circuit of ESP modules.

User avatar
QnBarb
 
Posts: 18
Joined: Mon Jan 23, 2023 9:44 am

Re: NodeMCU ESP8266_PIR stays HIGH

Post by QnBarb »

Thank you for the advice. I've also disconnected the sensor, but the board still sending out the pin was HIGH. Do you you know why?

User avatar
bidrohini
 
Posts: 202
Joined: Thu Oct 20, 2022 10:03 am

Re: NodeMCU ESP8266_PIR stays HIGH

Post by bidrohini »

I can see that your PIR is connected to D2. Maybe this pin is always HIGH by default. I think you can run a couple of tests. You can disconnect everything and upload any other simple code to the board. And then measure the D2 pin to see if it is always high no matter what you upload. You can also change your PIR pin to any pin other than D2. And see if this problem still persists. It will be better if you just try a simple code first without the MQTT library. Here is a simple example featuring PIR and ESP32. I know, you are not using the same board. But you can follow the example to test.
https://www.pcbway.com/project/sharepro ... 9ccbd.html

User avatar
adafruit_support_bill
 
Posts: 88096
Joined: Sat Feb 07, 2009 10:11 am

Re: NodeMCU ESP8266_PIR stays HIGH

Post by adafruit_support_bill »

board is sending PIR sensor data as 'HIGH' even though the sensor is not connected.
With nothing connected to the pin, the value will 'float' and result are unpredictable.

User avatar
QnBarb
 
Posts: 18
Joined: Mon Jan 23, 2023 9:44 am

Re: NodeMCU ESP8266_PIR stays HIGH

Post by QnBarb »

I switched to pin D1, and it works. Thank you all.

Locked
Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.

Return to “Internet of Things: Adafruit IO and Wippersnapper”