Mesurer la température et l'humidité à l'aide de M5Stack Core 2 avec DHT11 et DHT22 - Robojax
Mokanda oyo emonisi ndenge ya kokɔta na DHT11 to DHT22 ya température mpe ya malili na M5Stack Core2 mpe kotalisa ba lectures na écran na yango ya kosimba. M5Stack Core2 ezali board ya développement ya ESP2 oyo ezali na LCD ya 2 inches, oyo esalaka ete ezala platform ya malamu mpo na kosala station ya kotalela environnement. Projet oyo ezali malamu mpo na ba applications ndenge na ndenge, lokola:
- Kosala station ya météo ya bureau mpo na kotalela environnement ya ndako to ya bureau na yo.
- Kosala enregistreur ya température mpe ya malili mpo na serre, terrarium, to enclos ya reptiles.
- Kosala nœud ya kotalela environnement mpo na projet monene ya IoT.
- Koyekola ndenge ya kosangisa na ba capteurs ya série DHT oyo eyebani na module ya affichage ya sika.
Ba Composants ya Matériel
- M5Stack Core2 (board ya développement ya ESP32 na affichage)
- Capteur DHT11 (so capteur ya libanga na résistance ya 10kΩ to module ya PCB)
- Capteur DHT22 (AM2302) (so module ya pembe to module ya motane/moyindo)
- Breadboard mpe ba fils ya jumper
Guide ya Câblage
Câblage ezali pete, pamba te M5Stack Core2 ezali na port oyo ekokani na Grove. Vidéo emonisi connexion na port ya extension ya M5Stack Core2. (na vidéo na 03:52)
Tala résumé ya connexion mpo na ba types nyonso ya capteurs:
- Pin ya Données: Kɔta pin ya données/sortie ya capteur na GPIO 27 na M5Stack Core2. (na vidéo na 03:58)
- Pin ya Puissance (VCC/+): Kɔta pin ya puissance ya capteur na pin ya 3.3V na M5Stack Core2. (na vidéo na 04:11)
- Pin ya Terre (GND/-): Kɔta pin ya terre ya capteur na pin ya GND na M5Stack Core2. (na vidéo na 04:17)
Soki ozali kosalela capteur DHT11 to DHT22 ya libanga, esengeli okɔta résistance ya pull-up (mbala mingi 5.6kΩ to 10kΩ) kati na pin ya données mpe pin ya 3.3V. (na vidéo na 04:50)
Explication ya Code
Code yango esalemi mpo na kozala configurable mingi, mpo na kopesa yo nzela ya kobongola type ya capteur, mode ya affichage, ba couleurs, mpe taux ya rafraîchissement. Tala ndenge ba sections principales oyo esengeli kobongola:
Ba Réglages ya Capteur mpe ya Affichage
Na likolo ya code, okoki kosala définition ya type ya capteur mpe mode ya affichage. Mpo na kopona capteur na yo, esengeli okanga to ofungola #define oyo ebongi. (na vidéo na 13:43)
#define DHTPIN 27
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
Na ndakisa, mpo na kosalela DHT11, okanga #define DHTTYPE DHT22 mpe ofungola #define DHTTYPE DHT11.
Variable unit ekolakisa nini na écran. (na vidéo na 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
Okoki kotia yango na valeur nyonso kati na 0 mpe 5 mpo na kolakisa Celsius, Fahrenheit, Humidity, to bosangani na yango.
Variable refresh etie mbala ya kotanga ya capteur mpe ya kozongisa écran, na millisecondes. (na vidéo na 09:15)
const int refresh=3000;//read every 3 seconds
Ba Options ya Personnalisation
Code yango mpe epesaka yo nzela ya kobongola texte mpe ba couleurs na écran. Ba tableaux title mpe unitText babombaka ba labels mpe ba unités oyo emonani na écran. (na vidéo na 10:01)
char *title[]={"Temperature","Temperature","Humidity"};
char *unitText[]={"C","F","%"};
Okoki kobongola ba chaînes oyo mpo na kolakisa texte na langue na yo. Ba variables ya couleur lokola backgroundColor, temperatureTitleColor, mpe humidityValueColor ekoki kotiama na valeur nyonso ya ba couleurs ya TFT oyo esila koyebana to na ba valeurs hexadécimales na yo moko. (na vidéo na 10:21)
Démonstration ya Projet na Direct
Vidéo epesaka démonstration ya mobimba ya projet. Emonisi affichage oyo ezali kosala na capteur DHT22 na ba modes ndenge na ndenge, na ndakisa Fahrenheit, malili mpamba, Celsius na malili, mpe na suka Celsius mpe Fahrenheit. (na vidéo na 21:03) Démonstration yango mpe ezali na test na capteur DHT11, na module ya PCB mpe na capteur ya libanga na résistance ya pull-up. (na vidéo na 22:22) Na suka, vidéo emonisi nini esalemaka soki fil ya moko ebungisami, oyo epesaka lecture ya "None" na écran. (na vidéo na 22:38)
Ba Chapitres ya Vidéo
- [00:00] Introduction mpe Aperçu ya Projet
- [00:55] Ba Caractéristiques ya M5Stack Core2
- [02:22] Ba Spécifications ya Capteur DHT11 vs DHT22
- [03:52] Guide ya Câblage
- [07:40] Installation ya Ba Drivers ya Board mpe Ba Bibliothèques
- [09:01] Explication ya Code
- [21:03] Démonstration na Direct
- [22:53] Conclusion mpe Résumé
/*
* 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
Mokili na bisika
-
L'extérieurM5Stack Core2 Documentation Pagedocs.m5stack.com
-
L'extérieurM5Stack Core2 Product Pagem5stack-store.myshopify.com
Fichiers📁
Aucun fichier disponible.