Wa Koodu

Wọn Iwọn otutu ati ọriniinitutu ni lilo M5Stack Core 2 pẹlu DHT11 ati DHT22 - Robojax

Wọn Iwọn otutu ati ọriniinitutu ni lilo M5Stack Core 2 pẹlu DHT11 ati DHT22 - Robojax

Itọnisọna yii ṣe afihan bi o ṣe le sopọ sensa iwọn otutu ati ọrinrin DHT11 tabi DHT22 si M5Stack Core2 ki o si ṣe afihan awọn kika lori iboju ifọwọkan ti o wa ninu rẹ. M5Stack Core2 jẹ igbimọ idagbasoke ti o da lori ESP32 ti o lagbara pẹlu LCD 2-inch, ti o jẹ ki o jẹ pẹpẹ ti o dara julọ fun ṣiṣẹda ibudo abojuto ayika ti o duro funrararẹ. Ise agbese yii jẹ pipe fun ọpọlọpọ awọn ohun elo, gẹgẹbi:

  • Kikọ ibudo oju-ọjọ tabili lati ṣe abojuto agbegbe ile tabi ọfiisi rẹ.
  • Ṣiṣẹda igbasilẹ iwọn otutu ati ọrinrin fun eefin, terrarium, tabi apade apanirun.
  • Idagbasoke ipade abojuto ayika ti o rọrun fun ise agbese IoT nla kan.
  • Kikọ ẹkọ bi o ṣe le ṣe ibaraẹnisọrọ pẹlu awọn sensa olokiki DHT lilo module ifihan ode oni.

Awọn Ẹya Ohun elo

  • M5Stack Core2 (igbimọ idagbasoke ESP32 pẹlu ifihan)
  • Sensa DHT11 (boya sensa lasan pẹlu resistor 10kΩ tabi module PCB)
  • Sensa DHT22 (AM2302) (boya module funfun tabi module pupa/dudu)
  • Breadboard ati awọn okun waya

Itọnisọna Asopọ

Asopọ naa jẹ taara, nitori M5Stack Core2 pese ibudo ibaramu Grove ti o rọrun. Fidio naa ṣe afihan asopọ lori ibudo imugboroosi M5Stack Core2. (ninu fidio ni 03:52)

Eyi ni akopọ asopọ fun gbogbo awọn iru sensa:

  • Pin Data: Sopọ pin data/jade ti sensa si GPIO 27 lori M5Stack Core2. (ninu fidio ni 03:58)
  • Pin Agbara (VCC/+): Sopọ pin agbara ti sensa si pin 3.3V lori M5Stack Core2. (ninu fidio ni 04:11)
  • Pin Ilẹ (GND/-): Sopọ pin ilẹ ti sensa si pin GND lori M5Stack Core2. (ninu fidio ni 04:17)

Ti o ba nlo sensa DHT11 tabi DHT22 lasan, o gbọdọ ṣafikun resistor fifa-soke (nigbagbogbo 5.6kΩ si 10kΩ) laarin pin data ati pin 3.3V. (ninu fidio ni 04:50)

Alaye Koodu

Koodu naa jẹ apẹrẹ lati jẹ iṣeto pupọ, ti o gba ọ laaye lati yipada iru sensa, ipo ifihan, awọn awọ, ati oṣuwọn isọdọtun ni irọrun. Eyi ni pipin ti awọn apakan akọkọ ti olumulo le ṣeto:

Sensa ati Awọn Eto Ifihan

Ni oke koodu naa, o le ṣalaye iru sensa ati ipo ifihan. Lati yan sensa rẹ, o gbọdọ ṣe asọye tabi yọ asọye kuro ni laini #define ti o tọ. (ninu fidio ni 13:43)

#define DHTPIN 27 
#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
//#define DHTTYPE DHT11   // DHT 11
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

Fun apẹẹrẹ, lati lo DHT11, iwọ yoo yọ asọye kuro ni #define DHTTYPE DHT22 ki o si ṣe asọye #define DHTTYPE DHT11.

Oniyipada unit n ṣakoso kini data ti o han lori iboju. (ninu fidio ni 09:33)

unsigned int unit=3;//
//              0=C celsius
//              1=F fahrenheit
//              2=Humidity
//              3=C and Humidity
//              4=F and Humidity
//              5=C and F

O le ṣeto eyi si eyikeyi iye lati 0 si 5 lati ṣe afihan Celsius, Fahrenheit, Ọrinrin, tabi akojọpọ wọn.

Oniyipada refresh ṣeto igbohunsafẹfẹ ti a ka sensa ati ti a ṣe imudojuiwọn iboju, ni awọn milliseconds. (ninu fidio ni 09:15)

const int refresh=3000;//read every 3 seconds

Awọn Aṣayan Isọdi

Koodu naa tun gba ọ laaye lati ṣe isọdi ọrọ ati awọn awọ lori iboju. Awọn akojọpọ title ati unitText tọju awọn aami ati awọn ẹya ti o han lori iboju. (ninu fidio ni 10:01)

char *title[]={"Temperature","Temperature","Humidity"};
char *unitText[]={"C","F","%"};

O le yi awọn okun wọnyi pada lati ṣe afihan ọrọ ni ede tirẹ. Awọn oniyipada awọ gẹgẹbi backgroundColor, temperatureTitleColor, ati humidityValueColor le ṣee ṣeto si eyikeyi awọn iye awọ TFT ti a ti ṣalaye tẹlẹ tabi awọn iye hex aṣa tirẹ. (ninu fidio ni 10:21)

Ifihan Ise Agbese Laaye

Fidio naa pese ifihan pipe ti ise agbese naa. O ṣe afihan ifihan ti n ṣiṣẹ pẹlu sensa DHT22 ni ọpọlọpọ awọn ipo, pẹlu Fahrenheit, ọrinrin nikan, Celsius pẹlu ọrinrin, ati nikẹhin Celsius ati Fahrenheit. (ninu fidio ni 21:03) Ifihan naa tun pẹlu idanwo pẹlu sensa DHT11, mejeeji pẹlu module PCB ati bi sensa lasan pẹlu resistor fifa-soke. (ninu fidio ni 22:22) Nikẹhin, fidio naa ṣe afihan ohun ti o ṣẹlẹ nigbati okun waya ba ge asopọ, eyiti o yorisi kika "None" lori iboju. (ninu fidio ni 22:38)

Awọn Abala Fidio

  • [00:00] Ifihan ati Akopọ Ise Agbese
  • [00:55] Awọn Ẹya M5Stack Core2
  • [02:22] Awọn Alaye Sensa DHT11 vs DHT22
  • [03:52] Itọnisọna Asopọ
  • [07:40] Fifi sori ẹrọ Awọn Awakọ ati Awọn ile-ikawe Igbimọ
  • [09:01] Alaye Koodu
  • [21:03] Ifihan Laaye
  • [22:53] Ipari ati Akopọ

Aworan

M5Stack Core2: Main
M5Stack Core2: Main
M5Stack Core2: Included
M5Stack Core2: Included
M5Stack Core2: side-right
M5Stack Core2: side-right
M5Stack Core2: DHT11, DHT22
M5Stack Core2: DHT11 DHT22
366-M5Stack Core 2 ESP32 with DHT11 and DHT22
Ede: C++
/*
 * file: M5Stack_DHT11_DHT22 
  Measure Temperature and Humidity using DHT11, DHT22, DHT21 Sensor and display on M5Stack Core 2 ESP32 IoT board
 * Written by Ahmad Shamshiri on April 22, 2021 in Ontario, Canada
 * Watch video instruction https://youtu.be/ZDZm_QOlCJo
 www.Robojax.com YouTube http://www.youtube.com/robojaxTV
 
 * 
  * 
 * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact. * 
 * This code has been downloaded from Robojax.com
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.

  
*/
#include <M5Core2.h>


const int refresh=3000;//read every 3 seconds

unsigned int unit=3;//
//              0=C celsius
//              1=F fahrenheit
//              2=Humidity
//              3=C and Humidity
//              4=F and Humidity
//              5=C and F

char *title[]={"Temperature","Temperature","Humidity"};
char *unitText[]={"C","F","%"};

//set more color. see https://docs.m5stack.com/#/en/api/lcd

unsigned int backgroundColor= YELLOW;
unsigned int temperatureTitleColor= BLACK;
unsigned int temperatureValueColor= RED;

unsigned int humidityTitleColor= BLACK;
unsigned int humidityValueColor= RED;

unsigned int background2Color= TFT_CYAN;

uint8_t titleV,unitV,degree;

uint8_t line1TitleY=3;
uint8_t line1ValueY=40;

uint8_t line2TitleY=110;
uint8_t line2ValueY=150;

#include "DHT.h"
#define DHTPIN 27 
//#define DHTTYPE DHT11   // DHT 11
#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
//#define DHTTYPE DHT21   // DHT 21 (AM2301)
DHT dht(DHTPIN, DHTTYPE);
float temperatureCValue,temperatureFValue, humidityValue,myValue1,myValue2;// 
// ****** DHT settings end (Robojax.com )

void readTemperature();
void displayTemperature(uint8_t line );
void setup() {

  M5.begin();
  M5.Lcd.setTextSize(4); 

  Serial.begin(9600);//default is 115200
  Serial.println("M5Stack DHT Test ");

 // Robojax.com code for ESP32 DHT11 DHT22
   dht.begin();  
delay(1000);
}

void loop() {
  readTemperature();//reads the temperature and humidity
  displayMyValue();//displays temperature or humidity

  //printOnSerial();
   delay(refresh);
}


/*
 * www.robojax.com
 * see video https://youtu.be/ZDZm_QOlCJo 
 * for details
 */
void readTemperature()
{
  temperatureCValue = dht.readTemperature();// Read temperature as Celsius (the default)
  humidityValue = dht.readHumidity();// Reading humidity 
  temperatureFValue = dht.readTemperature(true);// Read temperature as Fahrenheit (isFahrenheit = true)

}//readTemperature() end

void displayMyValue()
{
  
  switch (unit) {
    case 1:
      titleV = 1;
      unitV =1; 
      myValue1= temperatureFValue;    
      displayTemperature( 1 , true);    
    break;

    case 2:
      titleV = 2;
      unitV =2;   
      myValue1= humidityValue;  
     displayTemperature( 1, false );    
    break;

    case 3:
      titleV = 0;
      unitV =0;     
      myValue1= temperatureCValue;      
      displayTemperature( 1 , true);
 
      titleV = 2;
      unitV =2;  
      myValue2= humidityValue; 
      displayTemperature( 2, false );               
    break;

    case 4:
      titleV = 1;
      unitV =1;     
      myValue1= temperatureFValue;      
      displayTemperature( 1 , true);
 
      titleV = 2;
      unitV =2;  
      myValue2= humidityValue; 
      displayTemperature( 2 , false);      
    break;

    case 5:
      titleV = 0;
      unitV =0;     
      myValue1= temperatureCValue;      
      displayTemperature( 1, true);
 
      titleV = 1;
      unitV =1;  
      myValue2= temperatureFValue; 
      displayTemperature( 2 , true);         
    break;

    default:
      titleV = 0;
      unitV =0;    
      myValue1= temperatureCValue;
      displayTemperature(1, true);    
    break;
    
  }

 
}//displayMyValue() end

/*
 * displayTemperature(uint8_t line, bool degree)
 * Displays temperature on Core2 screen
 * See video for details
 * https://youtu.be/ZDZm_QOlCJo
 */
void displayTemperature(uint8_t line, bool degree)
{
  uint8_t Y_lineTitle,Y_lineValue;
  float valueToDisplay;
  //M5.Lcd.fillScreen(backgroundColor);
  M5.Lcd.setTextColor(temperatureTitleColor);  
 
  M5.Lcd.setTextSize(4);
  if(line ==1){
    Y_lineTitle =line1TitleY;
    Y_lineValue =line1ValueY;
    valueToDisplay=myValue1;
    //M5.Lcd.fillRect(0, 0, 340, line2TitleY, backgroundColor);            
   M5.Lcd.fillScreen(backgroundColor);     
  }else{
    //M5.Lcd.drawRect(0, line2TitleY, 340, 240-line2TitleY, background2Color);
    M5.Lcd.fillRect(0, line2TitleY, 340, 240-line2TitleY, background2Color);      
    Y_lineTitle =line2TitleY;
    Y_lineValue =line2ValueY; 
    valueToDisplay=myValue2;
  }

  M5.Lcd.setCursor(3,Y_lineTitle); 
 
  M5.Lcd.print(title[titleV]);
  M5.Lcd.setTextColor(temperatureValueColor);    
  M5.Lcd.setTextSize(7);  
  M5.Lcd.setCursor(3,Y_lineValue);  
  M5.Lcd.printf("%.1f", valueToDisplay);  
  if(degree){
   printDegree();//print degree symbol if needed
  }
  M5.Lcd.print(unitText[unitV]);    
}
/*
 * displayHumidity()
 * displays humidity from DHT11 or DHT22 on Core 2
 * written by Ahmad Shamshiri
 * on April 21, 2021 in Ajax, Ontario, Canada
 * www.robojax.com
 */
void displayHumidity()
{

  M5.Lcd.setTextColor(humidityTitleColor);      
  M5.Lcd.setTextSize(4);    
  M5.Lcd.setCursor(3,110); 
  M5.Lcd.print(title[2]); //humidity title   
  M5.Lcd.setTextColor(humidityValueColor);    
  M5.Lcd.setTextSize(7);  
  M5.Lcd.setCursor(3,150);   
  M5.Lcd.printf("%.1f", humidityValue);       
  M5.Lcd.print(unitText[2]);    

}//displayHumidity() end


/*
 * printDegree()
 * prints degree symbol
 * Written by Ahmad Shamshiri 
 * on April 2, 2021 at 23:53
 * www.robojax.com
 */
void printDegree()
{

  M5.Lcd.setTextSize(3);  
  
  M5.Lcd.print("o");  
  M5.Lcd.setTextSize(7);    
  M5.Lcd.setCursor(M5.Lcd.getCursorX()+15,M5.Lcd.getCursorY());   
}//printDegree() end

/*
written by Ahmad Shamshiri for Robojax, Robojax.com
on April 21, 2021 in Ajax, Ontario, Canada
*/
void printOnSerial()
{
      Serial.print(title[unit]); 
      Serial.print(" "); 
      Serial.print(myValue1);  
             
      Serial.print(unitText[unit]);      
      Serial.println();  
}//printOnSerial() end

Orisun & itọkasi

Fáìlìs📁

Ko si awọn faili ti o wa.