Upload files to "doorbel-dht"
This commit is contained in:
138
doorbel-dht/doorbel-dht.ino
Normal file
138
doorbel-dht/doorbel-dht.ino
Normal file
@@ -0,0 +1,138 @@
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <PubSubClient.h>
|
||||
#include <DHT11.h>
|
||||
|
||||
// WiFi credentials
|
||||
const char* ssid = "GCHQ-Sniffer";
|
||||
const char* password = "123qweasd1qa2ws3ed";
|
||||
|
||||
// MQTT server details
|
||||
const char* mqtt_server = "192.168.1.10";
|
||||
const int mqtt_port = 1883;
|
||||
const char* mqtt_user = "ashley";
|
||||
const char* mqtt_password = "Finequasar17";
|
||||
const char* mqtt_topic = "doorbell/button";
|
||||
const char* mqtt_connect_topic = "doorbell/status";
|
||||
const char* mqtt_dht_topic = "doorbell/dht"; // Topic for DHT11 data
|
||||
|
||||
// Button pin
|
||||
const int buttonPin = 4;
|
||||
// Output Pin
|
||||
const int outputPin = 14;
|
||||
// DHT11 Pin
|
||||
const int dhtPin = 12;
|
||||
DHT11 dht(dhtPin); // Initialize DHT sensor
|
||||
|
||||
WiFiClient espClient;
|
||||
PubSubClient client(espClient);
|
||||
|
||||
// Variables for millis()
|
||||
unsigned long lastDebounceTime = 0;
|
||||
unsigned long lastDHTReadTime = 0;
|
||||
const unsigned long debounceDelay = 50; // Debounce delay in milliseconds
|
||||
const unsigned long dhtReadInterval = 60000; // DHT11 read interval in milliseconds
|
||||
|
||||
void setup_wifi() {
|
||||
delay(10);
|
||||
Serial.println();
|
||||
Serial.print("Connecting to ");
|
||||
Serial.println(ssid);
|
||||
|
||||
WiFi.begin(ssid, password);
|
||||
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
Serial.print("conecting ... ");
|
||||
Serial.print(".");
|
||||
}
|
||||
|
||||
Serial.println("");
|
||||
Serial.println("WiFi connected");
|
||||
Serial.println("IP address: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
client.publish(mqtt_connect_topic, "doorbell connected");
|
||||
}
|
||||
|
||||
void reconnect() {
|
||||
while (!client.connected()) {
|
||||
Serial.print("Attempting MQTT connection...");
|
||||
if (client.connect("ESP8266Client", mqtt_user, mqtt_password)) {
|
||||
Serial.println("connected");
|
||||
client.publish(mqtt_connect_topic, "doorbell connected");
|
||||
} else {
|
||||
Serial.print("failed, rc=");
|
||||
Serial.print(client.state());
|
||||
Serial.println(" try again in 5 seconds");
|
||||
delay(5000);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
setup_wifi();
|
||||
client.setServer(mqtt_server, mqtt_port);
|
||||
pinMode(buttonPin, INPUT_PULLUP);
|
||||
pinMode(outputPin, OUTPUT);
|
||||
lastDHTReadTime = millis(); // Initialize lastDHTReadTime
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (WiFi.status() != WL_CONNECTED) {
|
||||
setup_wifi();
|
||||
}
|
||||
if (!client.connected()) {
|
||||
reconnect();
|
||||
}
|
||||
client.loop();
|
||||
|
||||
unsigned long currentMillis = millis();
|
||||
|
||||
// Button debouncing using millis()
|
||||
static bool lastButtonState = HIGH;
|
||||
bool currentButtonState = digitalRead(buttonPin);
|
||||
|
||||
if (currentButtonState != lastButtonState) {
|
||||
if (currentMillis - lastDebounceTime >= debounceDelay) {
|
||||
lastDebounceTime = currentMillis; // Update debounce time
|
||||
if (currentButtonState == LOW) {
|
||||
Serial.println("Button Pressed");
|
||||
client.publish(mqtt_topic, "PRESSED");
|
||||
digitalWrite(outputPin, HIGH);
|
||||
} else {
|
||||
Serial.println("Button Released");
|
||||
client.publish(mqtt_topic, "RELEASED");
|
||||
digitalWrite(outputPin, LOW);
|
||||
}
|
||||
lastButtonState = currentButtonState;
|
||||
}
|
||||
}
|
||||
|
||||
// Read DHT11 data using millis()
|
||||
if (currentMillis - lastDHTReadTime >= dhtReadInterval) {
|
||||
lastDHTReadTime = currentMillis; // Update lastDHTReadTime
|
||||
|
||||
float h = dht.readHumidity();
|
||||
float t = dht.readTemperature();
|
||||
|
||||
if (isnan(h) || isnan(t)) {
|
||||
Serial.println("Failed to read from DHT sensor!");
|
||||
} else {
|
||||
Serial.print("Humidity: ");
|
||||
Serial.print(h);
|
||||
Serial.print(" %\t");
|
||||
Serial.print("Temperature: ");
|
||||
Serial.print(t);
|
||||
Serial.println(" *C");
|
||||
|
||||
// Publish DHT11 data to MQTT
|
||||
if (h <= 101 && t >= -40 && t <= 80) { // Only publish if humidity is 101% or less and temperature is within range
|
||||
String dhtString = "{\"temperature\":\"" + String(t) + "\",\"humidity\":\"" + String(h) + "\"}";
|
||||
client.publish(mqtt_dht_topic, dhtString.c_str());
|
||||
} else {
|
||||
Serial.println("Humidity or Temperature out of range, not sending MQTT message.");
|
||||
}
|
||||
}
|
||||
}
|
||||
delay(10);
|
||||
}
|
||||
Reference in New Issue
Block a user