NodeMCU : capteur de température LM35DZ
Dans l’article précédent, on a vu comment corriger l’erreur de conversion analogique concernant l’entrée A0. Nous allons mettre en oeuvre l’utilisation du capteur de température LM35DZ avec la platine de test NodeMCU.
Le brochage de ce capteur est le suivant :
Ce modèle de capteur peut mesurer les températures entre 0°C et 100°C. En sortie nous obtenons une variation de 10mV par °C.
Le programme va effectuer les étapes suivantes :
– Initialisation du WiFi en mode station et en mode point d’accès.
– Affichage sur l’écran OLED pendant 10 secondes de l’adressage IP.
– Affichage des températures minimales, maximales et réelles sur l’écran OLED.
– Affichage sur une page Web des informations identiques que sur l’écran OLED et d’un graphique des mesures pendant les 5 dernières minutes avec un rafraichissement de la page toutes les 5 secondes.
Voilà ce que l’on obtient sur la page Web :
Le programme sera le suivant :
/*
* Programme de gestion température
* température avec affichage des informations sur OLED SSD1306
* température avec affichage sur une page WEB
*
* Matériel :
* NODEMCU V1.0 DEV Kit
* OLED SSD1306
* capteur de température LM35
*/
// Ajout librairie gestion OLED, I2C, WIFI et WEB
#include <Wire.h>
#include « SSD1306.h »
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
// Déclaration de l’afficheur OLED via I2C
// D1 -> SDA
// D2 -> SCL
SSD1306 display(0x3c, D1, D2);
// Déclaration WIFI
const char* ssid = « ssid_wifi »;
const char* password = « mot_de_passe »;
const char* ssid_apmode = « Fabrice »;
const char* password_apmode = « »;
// Déclaration capteur de température LM35
int tension= analogRead(A0);
char str[10]; // permet la transformation float vers char
float temp_celsius;
float temp_min = 100; // 100°C
float temp_max = 0; // 0°C
char indexhtml[400]; // page html de 400 caractères max
float tab_temp[300]; // tableau qui contient les températures des dernières 5 minutes environ
int compteur = 0;
// Création d’une instance serveur
ESP8266WebServer server(80); // port d’écoute 80
void handleRoot()
{
char web_temp[10];
char web_temp_max[10];
char web_temp_min[10];
dtostrf(temp_celsius,5 , 2, web_temp);
dtostrf(temp_max,5 , 2, web_temp_max);
dtostrf(temp_min,5 , 2, web_temp_min);
snprintf ( indexhtml, 400,
« <html>\
<head>\
<meta http-equiv=’refresh’ content=’5’/>\
<title>Température</title>\
</head>\
<body>\
<h3>Température : %s °C</h3>\
<h3>Température maximale : %s °C</h3>\
<h3>Température minimale : %s °C</h3>\
<img src=\ »/graph_temp.svg\ » />\
</body>\
</html> », web_temp, web_temp_max, web_temp_min);
server.send(200, « text/html », indexhtml);
}
void drawGraph()
{
String out = « »;
char tempo[400];
out += « <svg xmlns=\ »http://www.w3.org/2000/svg\ » version=\ »1.1\ » width=\ »300\ » height=\ »100\ »>\n »;
out += « <rect width=\ »300\ » height=\ »100\ » fill=\ »rgb(250, 230, 210)\ » stroke-width=\ »1\ » stroke=\ »rgb(0, 0, 0)\ » />\n »;
out += « <g stroke=\ »black\ »>\n »;
for (int x = 1; x < 300; x++)
{
int tempint_1 = (int) tab_temp[x];
int tempint_2 = (int) tab_temp[x+1];
sprintf(tempo, « <line x1=\ »%d\ » y1=\ »%d\ » x2=\ »%d\ » y2=\ »%d\ » stroke-width=\ »1\ » />\n », x, 100 – tempint_1, x + 1, 100 – tempint_2);
out += tempo;
}
out += « </g>\n</svg>\n »;
server.send ( 200, « image/svg+xml », out);
}
void handleNotFound()
{
server.send(404, « text/plain », « Not Found »);
}
void affiche_oled()
{
display.clear();
display.drawString(0, 0, « Température : »);
display.drawString(70, 0, dtostrf(temp_celsius, 5, 1, str));
display.drawString(95, 0, « °C »);
display.drawString(0, 10, « Temp. max : »);
display.drawString(70, 10, dtostrf(temp_max, 5, 1, str));
display.drawString(95, 10, « °C »);
display.drawString(0, 20, « Temp. min : »);
display.drawString(70, 20, dtostrf(temp_min, 5, 1, str));
display.drawString(95 , 20, « °C »);
display.display(); // affichage
}
void traite_tableau()
{
tab_temp[compteur] = temp_celsius;
if (compteur == 300)
{
compteur = 299;
for (int i=1; i<300; i++) { tab_temp[i] = tab_temp[i+1]; }
}
}
void setup()
{
int x = 0;
char adr_ip[20];
char adr_ip_apmode[20];
IPAddress ip;
String ipStr;
Serial.begin(115200);
display.init(); // initialise l’afficheur
display.flipScreenVertically(); // inverse l’affichage
display.setFont(ArialMT_Plain_10);
WiFi.begin(ssid, password);
display.clear(); // efface l’affichage
display.drawString(10, 0, « Connexion WIFI en cours »);
display.display(); // affichage
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
display.drawString(x, 10, « . »);
display.display();
x = x+5;
}
ip = WiFi.localIP();
ipStr = String(ip[0]) + ‘.’ + String(ip[1]) + ‘.’ + String(ip[2]) + ‘.’ + String(ip[3]);
ipStr.toCharArray(adr_ip, 20);
WiFi.softAP(ssid_apmode, password_apmode, 10);
ip = WiFi.softAPIP();
ipStr = String(ip[0]) + ‘.’ + String(ip[1]) + ‘.’ + String(ip[2]) + ‘.’ + String(ip[3]);
ipStr.toCharArray(adr_ip_apmode, 20);
display.clear(); // efface l’affichage
display.drawString(20, 0, « WIFI connecté »);
display.drawString(0, 10, « IP mode Station : »);
display.drawString(40, 20, (adr_ip));
display.drawString(0, 30, « IP mode AP : »);
display.drawString(40, 40, (adr_ip_apmode));
display.display(); // affichage
server.on(« / », handleRoot); // page HTML par défaut
server.on ( « /graph_temp.svg », drawGraph ); // graphique température
server.onNotFound(handleNotFound); // si page HTML inconnu
server.begin(); // démarre le serveur HTTP
for (int i=0; i<300; i++) { tab_temp[i] = 0; } // initialise le tableau de températures
delay(10000);
display.clear();
}
void loop()
{
server.handleClient(); // écoute les requêtes HTTP des clients
tension= analogRead(A0);
float millivolts= (tension/1024.0) * 2810; // calibrage 2810 mV et non 3300 mV
temp_celsius= millivolts/10;
if (temp_celsius > temp_max) { temp_max = temp_celsius; }
if (temp_celsius < temp_min) { temp_min = temp_celsius; }
affiche_oled();
compteur++;
traite_tableau();
delay(1000);
}
|
Commentaires (0)
Trackbacks (1)
Laisser un commentaire
Rétrolien
-
13/02/2019 à 14:46Utilisation des ports de conversion analogique-numérique sur un ESP32 en MicroPython | iTechnoFrance