#include <SPI.h> #include <RF24.h> #include <Wire.h> #include <LoRa.h> #include <DHT.h>…
- User
#include <SPI.h> #include <RF24.h> #include <Wire.h> #include <LoRa.h> #include <DHT.h>
// NRF24L01 module RF24 radio(9, 10); // CE, CSN pins for NRF24L01 module const byte address[6] = "2Node"; // Pipe address, should match the transmitting Arduino
// DHT22 sensor #define DHTPIN 4 // Pin to which DHT22 is connected DHT dht(DHTPIN, DHT22); // Initialize DHT sensor
// PIR sensor const int pirPin = 7; // Replace with the actual pin connected to the PIR sensor
// LED pin for indicating motion const int ledPin = 13; // Replace with the pin where you have an LED connected
// Timer unsigned long previousMillis = 0; unsigned long interval = 10000; // 10 seconds interval
void setup() { // Initialize serial communication Serial.begin(9600);
// NRF24L01 module initialization radio.begin(); radio.setPALevel(RF24_PA_LOW); radio.setDataRate(RF24_250KBPS); radio.setChannel(0x76); radio.openReadingPipe(1, address); radio.startListening();
// DHT22 sensor initialization dht.begin();
// PIR sensor initialization pinMode(pirPin, INPUT);
// LED setup pinMode(ledPin, OUTPUT); }
void loop() { unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) { previousMillis = currentMillis;
// Attempt to read the temperature and humidity data float temperature = dht.readTemperature(); float humidity = dht.readHumidity(); if (!isnan(temperature) && !isnan(humidity)) { Serial.print("Temperature: "); Serial.print(temperature); Serial.println("C"); Serial.print("Humidity: "); Serial.print(humidity); Serial.println("%"); } else { Serial.println("Failed to read DHT22 sensor data."); } // Read PIR sensor digital output pin int pirState = digitalRead(pirPin); if (pirState == HIGH) { // Additional actions when motion is detected Serial.println("Motion detected!"); // Example: Turn on the LED to indicate motion digitalWrite(ledPin, HIGH); // Example: Record a timestamp for the event unsigned long currentTimestamp = millis(); Serial.print("Motion detected at timestamp: "); Serial.println(currentTimestamp); // Example: Send a message to another Arduino or device using NRF24L01 module radio.write(&pirState, sizeof(pirState)); } else { // Reset the LED and any other devices to their initial state when no motion is detected digitalWrite(ledPin, LOW); } delay(10000);} } .... 34:50.444 -> Failed to read DHT22 sensor data. 13:34:50.444 -> Motion detected! 13:34:50.444 -> Motion detected at timestamp: 60306 13:35:00.591 -> Failed to read DHT22 sensor data. 13:35:00.591 -> Motion detected! 13:35:00.591 -> Motion detected at timestamp: 70405 13:35:10.739 -> Failed to read DHT22 sensor data. 13:35:10.739 -> Motion detected! 13:35:10.739 -> Motion detected at timestamp: 80504 13:35:20.897 -> Failed to read DHT22 sensor data. 13:35:20.897 -> Motion detected! 13:35:20.897 -> Motion detected at timestamp: 90603 ??????
- Assistant
It seems that there is an issue with reading the DHT22 sensor data. The error message "Failed to read DHT22 sensor data" indicates that the sensor is not providing valid temperature and humidity measurements.
Here are a few possible reasons for this issue:
-
Wiring: Double-check the wiring connections between the DHT22 sensor and the Arduino. Make sure the data pin of the sensor is connected to pin 4 on the Arduino.
-
Power supply: Ensure that the DHT22 sensor is receiving sufficient power. It requires a 3.3V power supply, so verify that it is connected to a suitable power source.
-
Sensor failure: It is possible that the DHT22 sensor itself is faulty. Try using a different DHT22 sensor to see if the issue persists.
-
Timing: The DHT22 sensor requires a specific timing sequence to read data. Check that the timing in your code is correct and that the sensor has enough time to stabilize before reading data.
Make sure to address these potential causes and troubleshoot accordingly.
-