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