From 7685d5260e6964ae0fcf198a1bf968a65f20d023 Mon Sep 17 00:00:00 2001 From: NanamiAdmin Date: Thu, 14 Aug 2025 22:30:01 +0800 Subject: [PATCH] modified: platformio.ini new file: src/DHT.h new file: src/DHT_U.h modified: src/main.cpp --- platformio.ini | 2 + src/DHT.h | 109 +++++++++++++++++++++++++++++++++++++++++++++++++ src/DHT_U.h | 101 +++++++++++++++++++++++++++++++++++++++++++++ src/main.cpp | 48 +++++++++++++++++++++- 4 files changed, 258 insertions(+), 2 deletions(-) create mode 100644 src/DHT.h create mode 100644 src/DHT_U.h diff --git a/platformio.ini b/platformio.ini index 7230537..75276ef 100644 --- a/platformio.ini +++ b/platformio.ini @@ -12,3 +12,5 @@ platform = espressif8266 board = nodemcu framework = arduino +lib_deps = + adafruit/DHT sensor library diff --git a/src/DHT.h b/src/DHT.h new file mode 100644 index 0000000..95570e1 --- /dev/null +++ b/src/DHT.h @@ -0,0 +1,109 @@ +/*! + * @file DHT.h + * + * This is a library for DHT series of low cost temperature/humidity sensors. + * + * You must have Adafruit Unified Sensor Library library installed to use this + * class. + * + * Adafruit invests time and resources providing this open source code, + * please support Adafruit andopen-source hardware by purchasing products + * from Adafruit! + * + * Written by Adafruit Industries. + * + * MIT license, all text above must be included in any redistribution + */ + +#ifndef DHT_H +#define DHT_H + +#include "Arduino.h" + +/* Uncomment to enable printing out nice debug messages. */ +//#define DHT_DEBUG + +#define DEBUG_PRINTER \ + Serial /**< Define where debug output will be printed. \ + */ + +/* Setup debug printing macros. */ +#ifdef DHT_DEBUG +#define DEBUG_PRINT(...) \ + { DEBUG_PRINTER.print(__VA_ARGS__); } +#define DEBUG_PRINTLN(...) \ + { DEBUG_PRINTER.println(__VA_ARGS__); } +#else +#define DEBUG_PRINT(...) \ + {} /**< Debug Print Placeholder if Debug is disabled */ +#define DEBUG_PRINTLN(...) \ + {} /**< Debug Print Line Placeholder if Debug is disabled */ +#endif + +/* Define types of sensors. */ +static const uint8_t DHT11{11}; /**< DHT TYPE 11 */ +static const uint8_t DHT12{12}; /**< DHY TYPE 12 */ +static const uint8_t DHT21{21}; /**< DHT TYPE 21 */ +static const uint8_t DHT22{22}; /**< DHT TYPE 22 */ +static const uint8_t AM2301{21}; /**< AM2301 */ + +#if defined(TARGET_NAME) && (TARGET_NAME == ARDUINO_NANO33BLE) +#ifndef microsecondsToClockCycles +/*! + * As of 7 Sep 2020 the Arduino Nano 33 BLE boards do not have + * microsecondsToClockCycles defined. + */ +#define microsecondsToClockCycles(a) ((a) * (SystemCoreClock / 1000000L)) +#endif +#endif + +/*! + * @brief Class that stores state and functions for DHT + */ +class DHT { +public: + DHT(uint8_t pin, uint8_t type, uint8_t count = 6); + void begin(uint8_t usec = 55); + float readTemperature(bool S = false, bool force = false); + float convertCtoF(float); + float convertFtoC(float); + float computeHeatIndex(bool isFahrenheit = true); + float computeHeatIndex(float temperature, float percentHumidity, + bool isFahrenheit = true); + float readHumidity(bool force = false); + bool read(bool force = false); + +private: + uint8_t data[5]; + uint8_t _pin, _type; +#ifdef __AVR + // Use direct GPIO access on an 8-bit AVR so keep track of the port and + // bitmask for the digital pin connected to the DHT. Other platforms will use + // digitalRead. + uint8_t _bit, _port; +#endif + uint32_t _lastreadtime, _maxcycles; + bool _lastresult; + uint8_t pullTime; // Time (in usec) to pull up data line before reading + + uint32_t expectPulse(bool level); +}; + +/*! + * @brief Class that defines Interrupt Lock Avaiability + */ +class InterruptLock { +public: + InterruptLock() { +#if !defined(ARDUINO_ARCH_NRF52) + noInterrupts(); +#endif + } + ~InterruptLock() { +#if !defined(ARDUINO_ARCH_NRF52) + interrupts(); +#endif + } +}; + +#endif diff --git a/src/DHT_U.h b/src/DHT_U.h new file mode 100644 index 0000000..057e6a8 --- /dev/null +++ b/src/DHT_U.h @@ -0,0 +1,101 @@ +/*! + * @file DHT_U.h + * + * DHT Temperature & Humidity Unified Sensor Library + * + * Adafruit invests time and resources providing this open source code, + * please support Adafruit andopen-source hardware by purchasing products + * from Adafruit! + * + * Written by Tony DiCola (Adafruit Industries) 2014. + * + * MIT license, all text above must be included in any redistribution + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +#ifndef DHT_U_H +#define DHT_U_H + +#include +#include + +#define DHT_SENSOR_VERSION 1 /**< Sensor Version */ + +/*! + * @brief Class that stores state and functions for interacting with + * DHT_Unified. + */ +class DHT_Unified { +public: + DHT_Unified(uint8_t pin, uint8_t type, uint8_t count = 6, + int32_t tempSensorId = -1, int32_t humiditySensorId = -1); + void begin(); + + /*! + * @brief Class that stores state and functions about Temperature + */ + class Temperature : public Adafruit_Sensor { + public: + Temperature(DHT_Unified *parent, int32_t id); + bool getEvent(sensors_event_t *event); + void getSensor(sensor_t *sensor); + + private: + DHT_Unified *_parent; + int32_t _id; + }; + + /*! + * @brief Class that stores state and functions about Humidity + */ + class Humidity : public Adafruit_Sensor { + public: + Humidity(DHT_Unified *parent, int32_t id); + bool getEvent(sensors_event_t *event); + void getSensor(sensor_t *sensor); + + private: + DHT_Unified *_parent; + int32_t _id; + }; + + /*! + * @brief Returns temperature stored in _temp + * @return Temperature value + */ + Temperature temperature() { return _temp; } + + /*! + * @brief Returns humidity stored in _humidity + * @return Humidity value + */ + Humidity humidity() { return _humidity; } + +private: + DHT _dht; + uint8_t _type; + Temperature _temp; + Humidity _humidity; + + void setName(sensor_t *sensor); + void setMinDelay(sensor_t *sensor); +}; + +#endif diff --git a/src/main.cpp b/src/main.cpp index 442a262..39d7935 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,7 +3,11 @@ #include #include #include +#include +#define DHTPIN 0 +#define DHTTYPE DHT22 +DHT dht22(DHTPIN, DHTTYPE); // <-- Global object #define ledPin 2 int wifiConnectStatus = 0; //WiFi connection status @@ -82,6 +86,7 @@ void setup() { Serial.println("Developed by Madobi Nanami"); Serial.println("Personal site: https://nanami.tech"); Serial.println("Tech support & Feedback: yzt114@yeah.net"); + dht22.begin(); EEPROM.begin(64); pinMode(ledPin, OUTPUT); digitalWrite(ledPin, LOW); @@ -118,10 +123,49 @@ void setup() { Serial.println(ssid); Serial.print("Loaded WiFi Password: "); Serial.println(password); + if (connectWiFi(ssid, password) == 1){ + Serial.println("Connected to WiFi successfully."); + Serial.print("IP Address: "); + Serial.println(WiFi.localIP()); + server.begin(); + digitalWrite(ledPin, LOW); // Turn on LED after connection + } //Connect to WiFi with loaded settings } - } void loop() { - // put your main code here, to run repeatedly: + String user_command; + WiFiClient client; // Declare client at the start + if(server.hasClient()) { // Check if there is a client connected + Serial.println("A new client connected to web server."); + server.setNoDelay(true); + client = server.available(); // Assign client here + if(client) { + Serial.println("New client connected."); + String currentLine = ""; + while(client.connected()) { + if(client.available()) { + char c = client.read(); + Serial.write(c); + if(c == '\n') { + currentLine.trim(); // Remove whitespace + user_command = currentLine; + Serial.printf("Received command: %s\n", user_command.c_str()); + } + } + } + } + } + if (user_command == "temp.read") { + float temperature = dht22.readTemperature(); + client.println(temperature); + Serial.printf("Temperature: %.2f °C\n", temperature); + user_command = ""; // Clear command after processing + } + if (user_command == "hum.read") { + float humidity = dht22.readHumidity(); + client.println(humidity); + Serial.printf("Humidity: %.2f %%\n", humidity); + user_command = ""; // Clear command after processing + } } \ No newline at end of file