当前位置:网站首页>Esp32-cam high cost performance temperature and humidity monitoring system

Esp32-cam high cost performance temperature and humidity monitoring system

2022-06-23 10:59:00 daodanjishui

ESP32-CAM ArduinoIDE Development series article catalog


Chapter one :ESP32-CAM High cost performance WIFI Quick start tutorial of image transmission scheme
Second articles :ESP32-CAM The first wireless lighting program
Third articles :ESP32-CAM Design and implementation of intelligent gateway
Fourth articles :ESP32-CAM Create a hot spot composition and style DNS The server
Chapter five :ESP32-CAM High cost performance temperature and humidity monitoring system



Preface

    daodanjishui The core original technology of the Internet of things ESP32 Arduino IDE Development of embedded web server setup 、http Request sending, receiving and parsing 、 Single chip microcomputer IO Oral reading and writing operation 、AJAX technology 、 Concurrent server technology 、DNS Technology and sensors DHT11 Integrated composition :ESP32-CAM High cost performance temperature and humidity monitoring system .


One 、ESP32-CAM What is a cost-effective temperature and humidity monitoring system ?

    daodanjishui The project in the previous article :ESP32-CAM Create a hot spot composition and style DNS Server introduces embedded server LAN in detail DNS Application , At that time, the main page of the server could only be queried ESP32-CAM On board LED Lamp status and control LED Light on and off , But there is no function to query environment parameters , It does not really reflect the essence of the Internet of things technology , This is also the deficiency of the previous article .
    daodanjishui In the project of this article , Keep the functions of the previous article , Will join DHT11 Temperature and humidity sensor module , And add the sensor driver , This module has only one data interface data, So only ESP32-CAM One of the GPIO14, Therefore, it is easy to transform the previous project into a temperature and humidity monitoring system . The function of the system is to input in the window of the form :data Click again cmd Button to submit the command ,ESP32 The server will query the temperature and humidity data and return to the server home page , You can query the environment parameters , Displayed in the feedback message , Buyers who are not clear about the functions of the previous article can go to the previous article ( Fourth articles ) Introduction to . The hardware effect is shown in the figure below :

 Insert picture description here
The running effect of the program is as follows ( The red font is the data collected by the sensor ):
 Insert picture description here
DHT11 At that time, I found a driver on the Internet to collect data , The effect of the test is not very good , It takes several times to get the data , In fact, it's almost enough , To be accurate, you need to modify the driver carefully , I found a more reliable one later DHT The driver , Share it here , Some common libraries can be found in :http://www.taichi-maker.com/homepage/download/#library-download
Download for free .

Two 、 Software development process

1. Import and stock in dht11.h and dht11.cpp

dht11.h The code is as follows :

#ifndef dht11_h
#define dht11_h

#if defined(ARDUINO) && (ARDUINO >= 100)
#include <Arduino.h>
#else
#include <WProgram.h>
#endif

#define DHT11LIB_VERSION "0.4.1"

#define DHTLIB_OK 0
#define DHTLIB_ERROR_CHECKSUM -1
#define DHTLIB_ERROR_TIMEOUT -2

class dht11
{
    
public:
    int read(int pin);
	int humidity;
	int temperature;
};
#endif
//
// END OF FILE
//

dht11.c The code is as follows :

//
// FILE: dht11.cpp
// VERSION: 0.4.1
// PURPOSE: DHT11 Temperature & Humidity Sensor library for Arduino
// LICENSE: GPL v3 (http://www.gnu.org/licenses/gpl.html)
//
// DATASHEET: http://www.micro4you.com/files/sensor/DHT11.pdf
//
// HISTORY:
// George Hadjikyriacou - Original version (??)
// Mod by SimKard - Version 0.2 (24/11/2010)
// Mod by Rob Tillaart - Version 0.3 (28/03/2011)
// + added comments
// + removed all non DHT11 specific code
// + added references
// Mod by Rob Tillaart - Version 0.4 (17/03/2012)
// + added 1.0 support
// Mod by Rob Tillaart - Version 0.4.1 (19/05/2012)
// + added error codes
//

#include "dht11.h"

// Return values:
// DHTLIB_OK
// DHTLIB_ERROR_CHECKSUM
// DHTLIB_ERROR_TIMEOUT
int dht11::read(int pin)
{
    
	// BUFFER TO RECEIVE
	uint8_t bits[5];
	uint8_t cnt = 7;
	uint8_t idx = 0;

	// EMPTY BUFFER
	for (int i=0; i< 5; i++) bits[i] = 0;

	// REQUEST SAMPLE
	pinMode(pin, OUTPUT);
	digitalWrite(pin, LOW);
	delay(18);
	digitalWrite(pin, HIGH);
	delayMicroseconds(40);
	pinMode(pin, INPUT);

	// ACKNOWLEDGE or TIMEOUT
	unsigned int loopCnt = 10000;
	while(digitalRead(pin) == LOW)
		if (loopCnt-- == 0) return DHTLIB_ERROR_TIMEOUT;

	loopCnt = 10000;
	while(digitalRead(pin) == HIGH)
		if (loopCnt-- == 0) return DHTLIB_ERROR_TIMEOUT;

	// READ OUTPUT - 40 BITS => 5 BYTES or TIMEOUT
	for (int i=0; i<40; i++)
	{
    
		loopCnt = 10000;
		while(digitalRead(pin) == LOW)
			if (loopCnt-- == 0) return DHTLIB_ERROR_TIMEOUT;

		unsigned long t = micros();

		loopCnt = 10000;
		while(digitalRead(pin) == HIGH)
			if (loopCnt-- == 0) return DHTLIB_ERROR_TIMEOUT;

		if ((micros() - t) > 40) bits[idx] |= (1 << cnt);
		if (cnt == 0)   // next byte?
		{
    
			cnt = 7;    // restart at MSB
			idx++;      // next byte!
		}
		else cnt--;
	}

	// WRITE TO RIGHT VARS
        // as bits[1] and bits[3] are allways zero they are omitted in formulas.
	humidity    = bits[0]; 
	temperature = bits[2]; 

	uint8_t sum = bits[0] + bits[2];  

	if (bits[4] != sum) return DHTLIB_ERROR_CHECKSUM;
	return DHTLIB_OK;
}
//
// END OF FILE
//

2. Read in the data

Arduino The sensor drive code is as follows :

double Fahrenheit(double celsius) 
{
    
        return 1.8 * celsius + 32;
}    // The Celsius temperature is converted to the Fahrenheit temperature 

double Kelvin(double celsius)
{
    
        return celsius + 273.15;
}     // Centigrade temperature is converted to Kelvin temperature 

//  the dew point ( At this temperature , The air saturates and produces dew drops )
//  Reference resources : http://wahiduddin.net/calc/density_algorithms.htm 
double dewPoint(double celsius, double humidity)
{
    
        double A0= 373.15/(273.15 + celsius);
        double SUM = -7.90298 * (A0-1);
        SUM += 5.02808 * log10(A0);
        SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ;
        SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ;
        SUM += log10(1013.246);
        double VP = pow(10, SUM-3) * humidity;
        double T = log(VP/0.61078);   // temp var
        return (241.88 * T) / (17.558-T);
}

//  Calculate dew point quickly , The velocity is 5 times dewPoint()
//  Reference resources : http://en.wikipedia.org/wiki/Dew_point
double dewPointFast(double celsius, double humidity)
{
    
        double a = 17.271;
        double b = 237.7;
        double temp = (a * celsius) / (b + celsius) + log(humidity/100);
        double Td = (b * temp) / (a - temp);
        return Td;
}

#include <dht11.h>

dht11 DHT11;

#define DHT11PIN 2

void setup()
{
    
  Serial.begin(9600);
  Serial.println("DHT11 TEST PROGRAM ");
  Serial.print("LIBRARY VERSION: ");
  Serial.println(DHT11LIB_VERSION);
  Serial.println();
}

void loop()
{
    
  Serial.println("\n");

  int chk = DHT11.read(DHT11PIN);

  Serial.print("Read sensor: ");
  switch (chk)
  {
    
    case DHTLIB_OK: 
                Serial.println("OK"); 
                break;
    case DHTLIB_ERROR_CHECKSUM: 
                Serial.println("Checksum error"); 
                break;
    case DHTLIB_ERROR_TIMEOUT: 
                Serial.println("Time out error"); 
                break;
    default: 
                Serial.println("Unknown error"); 
                break;
  }

  Serial.print("Humidity (%): ");
  Serial.println((float)DHT11.humidity, 2);

  Serial.print("Temperature (oC): ");
  Serial.println((float)DHT11.temperature, 2);

  Serial.print("Temperature (oF): ");
  Serial.println(Fahrenheit(DHT11.temperature), 2);

  Serial.print("Temperature (K): ");
  Serial.println(Kelvin(DHT11.temperature), 2);

  Serial.print("Dew Point (oC): ");
  Serial.println(dewPoint(DHT11.temperature, DHT11.humidity));

  Serial.print("Dew PointFast (oC): ");
  Serial.println(dewPointFast(DHT11.temperature, DHT11.humidity));

  delay(2000);
}

3. Porting to Engineering

     By adding this code to the previous project, you can use the web page to trigger the query of environment parameters .

 if(!strcmp(value_buf, "data")){
    // Check the temperature and humidity 
            dht11 DHT11;
            if (DHTLIB_OK == DHT11.read(14))// I use it GPIO14
            {
    
              Serial.print("HUMID = ");
              Serial.print(DHT11.humidity);
              Serial.println(" %RH");
              Serial.print("TMEP = ");
              Serial.print(DHT11.temperature);
              Serial.println("^C");
              String  T=DHT11.temperature+"";
              String  H=DHT11.humidity+"";          
              return request->send(200, "text/plain", "temperature="+T+" humidity="+H);// Return temperature and humidity 
            }else return request->send(200, "text/plain", "no temperature and humidity");// Return temperature and humidity 
         }

summary

 Insert picture description here
    daodanjishui This issue has two more files than the source code of the previous issue , These two documents are arduino Of dht11 library , It is also difficult to find on the Internet , There are also many problems in the debugging process , Finally, the function of temperature and humidity query is realized , I also said in the last issue , This sister article has a high degree of code expansion , Improve the function of the system at will , So now the code of the sister chapter is transformed into a temperature and humidity monitoring system , Readers want to learn how to expand the system functions , Consider the similarities and differences between the two sets of code , Most people will make many mistakes when adding library functions , It is not so easy to succeed , Compared these two sets of codes , New sensor code will be added , There must be gains !!!
Code transfer address :https://www.cirmall.com/circuit/19388/
Click jump to download

原网站

版权声明
本文为[daodanjishui]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206231034532539.html