modified: platformio.ini
new file: src/DHT.h new file: src/DHT_U.h modified: src/main.cpp
This commit is contained in:
parent
7c08fce636
commit
7685d5260e
@ -12,3 +12,5 @@
|
|||||||
platform = espressif8266
|
platform = espressif8266
|
||||||
board = nodemcu
|
board = nodemcu
|
||||||
framework = arduino
|
framework = arduino
|
||||||
|
lib_deps =
|
||||||
|
adafruit/DHT sensor library
|
||||||
|
109
src/DHT.h
Normal file
109
src/DHT.h
Normal file
@ -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
|
101
src/DHT_U.h
Normal file
101
src/DHT_U.h
Normal file
@ -0,0 +1,101 @@
|
|||||||
|
/*!
|
||||||
|
* @file DHT_U.h
|
||||||
|
*
|
||||||
|
* DHT Temperature & Humidity Unified Sensor Library<Paste>
|
||||||
|
*
|
||||||
|
* 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 <Adafruit_Sensor.h>
|
||||||
|
#include <DHT.h>
|
||||||
|
|
||||||
|
#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
|
48
src/main.cpp
48
src/main.cpp
@ -3,7 +3,11 @@
|
|||||||
#include <ESP8266WiFi.h>
|
#include <ESP8266WiFi.h>
|
||||||
#include <ESP8266WebServer.h>
|
#include <ESP8266WebServer.h>
|
||||||
#include <EEPROM.h>
|
#include <EEPROM.h>
|
||||||
|
#include <DHT.h>
|
||||||
|
|
||||||
|
#define DHTPIN 0
|
||||||
|
#define DHTTYPE DHT22
|
||||||
|
DHT dht22(DHTPIN, DHTTYPE); // <-- Global object
|
||||||
#define ledPin 2
|
#define ledPin 2
|
||||||
int wifiConnectStatus = 0; //WiFi connection status
|
int wifiConnectStatus = 0; //WiFi connection status
|
||||||
|
|
||||||
@ -82,6 +86,7 @@ void setup() {
|
|||||||
Serial.println("Developed by Madobi Nanami");
|
Serial.println("Developed by Madobi Nanami");
|
||||||
Serial.println("Personal site: https://nanami.tech");
|
Serial.println("Personal site: https://nanami.tech");
|
||||||
Serial.println("Tech support & Feedback: yzt114@yeah.net");
|
Serial.println("Tech support & Feedback: yzt114@yeah.net");
|
||||||
|
dht22.begin();
|
||||||
EEPROM.begin(64);
|
EEPROM.begin(64);
|
||||||
pinMode(ledPin, OUTPUT);
|
pinMode(ledPin, OUTPUT);
|
||||||
digitalWrite(ledPin, LOW);
|
digitalWrite(ledPin, LOW);
|
||||||
@ -118,10 +123,49 @@ void setup() {
|
|||||||
Serial.println(ssid);
|
Serial.println(ssid);
|
||||||
Serial.print("Loaded WiFi Password: ");
|
Serial.print("Loaded WiFi Password: ");
|
||||||
Serial.println(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() {
|
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
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user