Bonjour à tous,
Je viens de finir un module permettant de contrôler une ventilation ainsi qu'un système de chauffage grâce à deux relais, en fonction de la température et du taux humidité. Le code prévoit aussi que la ventilation ou le chauffage ne peuvent pas être coupés moins d'une minute après leur mise en marche (et inversement) afin d'éviter de griller le moteur de la ventilation par des on-off intempestif. En effet, lorsque la température arrivait proche de la température minimum visée, il arrivait que le DHT oscillait entre deux température, faisant partir et s’arrêter la ventilation trop rapidement. La lecture de la température est effectuée une fois par seconde.
J'ai également ajouter un "serveur" pour pouvoir contrôler à distance la température et le taux d’humidité nécessaire pour faire partir la ventilation et/ou le chauffage; ainsi que d'avoir un retour sur la température et l’humidité en temps réel.
Tout marche plutôt bien. Le problème c'est que de manière aléatoire, la carte devient indisponible sur le réseau, jusqu'à ce que je la redémarre.
Auriez vous une idée pour stabiliser le tout?
Merci beaucoup !
Voici la première partie du code:
// remettre les valeurs d'hunidité
// remplacer "temperature" par "t"
#include <SPI.h>
#include <WiFi101.h>
#include <LiquidCrystal.h>
#include <DHT.h>
#include <DHT_U.h>
#define DHTPIN 2 //Capteur temperature = D2
#define DHTTYPE DHT11 //Capteur temperature type DHT11
DHT dht(DHTPIN, DHTTYPE);
int relaiPin = 3; //Défini la pin du relai ventilation
int lcdRSPin = 1; //RS = D1
int lcdEPin = 0; //E = D0
int lcdD4Pin = 4; //4 = D4
int lcdD5Pin = 5; //5 = D5
int lcdD6Pin = 6; //6 = D6
int lcdD7Pin = 7; //7 = D7
int href = 35; //Taux d'humidité de référence
int tref = 5; //Température de référence
int delai = 1000; //Durée entre les mesures (millisecondes)
int marche = 4; //Interupteur logique manuel
int minimumON = 90; // Durée minimale d'activation (secondes)
int tempactif = 240; //temps écoulé depuis la dernière activation
int chaufpin = A6; //Défini la pin du relai chayfage
boolean etat; // statut du ventilateur
boolean chauff; // statut du chauffage (non utilise)
boolean etatprecedent = true; //garde l'état de marche du cycle préceédent
char ssid[] = "*****"; //SSID
char pass[] = "*****"; //Mot de passe réseau
int keyIndex = 0; // your network key Index number (needed only for WEP)
IPAddress ip(10, 0, 1, 207);
LiquidCrystal lcd(lcdRSPin, lcdEPin, lcdD4Pin, lcdD5Pin, lcdD6Pin, lcdD7Pin);
int status = WL_IDLE_STATUS;
WiFiServer server(80);
void setup() {
Serial.begin(9600); // ouverture de la communication en série
dht.begin();
lcd.begin(16, 2); //Défini la taille de l'écran
pinMode(relaiPin, OUTPUT); //Défini la pin D2 comme sortie (relai ventilateur)
pinMode(chaufpin, OUTPUT);
WiFi.config(ip);
if (WiFi.status() == WL_NO_SHIELD) { // Vérifie la présence de catre wifi:
Serial.println("WiFi shield not present");
while (true); //Arrete en cas d'absence
}
while ( status != WL_CONNECTED) { //Tentaive de connexion au Wifi
Serial.print("Attempting to connect to Network named: ");
Serial.println(ssid); //inscrit le SSID ;
status = WiFi.begin(ssid, pass); //Connexion a un réseau WPA/WPA2. A changer si WEP:
delay(10000);
}
server.begin(); // démare le serveur sur le port 80
printWifiStatus(); // connecté. inscrit le statut
}
la seconde partie :
void loop() {
tempactif = tempactif + 1;
WiFiClient client = server.available(); // listen for incoming clients
if (client) { // if you get a client,
Serial.println("new client"); // print a message out the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
// the content of the HTTP response follows the header:
client.print("<center>Commande du chauffage et de la ventilation : Écurie du haut</center>");
client.print("
");
client.print("
");
client.print("La température actuelle est de ");
client.print(dht.readTemperature());
client.print(" degrés");
client.print(" et l'hunidité actuelle est de ");
client.print(dht.readHumidity());
client.print(" %");
client.print("
");
client.print("Température programée : ");
client.print(tref);
client.print(" C");
client.print(" <a href=\"/C\"\"><button style='font-size:100%;background-color:darkgray; color:black; border-radius:50px;'>+</button></a>");
client.print(" <a href=\"/D\"\"><button style='font-size:100%;background-color:darkgray; color:black; border-radius:50px;'>-</button></a>");
client.print("
");
client.print("Huniditée programée : ");
client.print(href);
client.print(" %");
client.print(" <a href=\"/A\"\"><button style='font-size:100%;background-color:darkgray; color:black; border-radius:50px;'>+</button></a>");
client.print(" <a href=\"/B\"\"><button style='font-size:100%;background-color:darkgray; color:black; border-radius:50px;'>-</button></a>");
client.print("
");
client.print("Contrôle manuel :");
client.print("
");
client.print(" <a href=\"/AR\"\"><button style='font-size:100%;background-color:darkgray; color:black; border-radius:50px;'>Arrêt Total</button></a>");
client.print(" <a href=\"/MA\"\"><button style='font-size:100%;background-color:darkgray; color:black; border-radius:50px;'>Marche ventilation</button></a>");
client.print(" <a href=\"/HE\"\"><button style='font-size:100%;background-color:darkgray; color:black; border-radius:50px;'>Marche chauffage</button></a>");
client.print(" <a href=\"/TO\"\"><button style='font-size:100%;background-color:darkgray; color:black; border-radius:50px;'>Marche total</button></a>");
client.print(" <a href=\"/AU\"\"><button style='font-size:100%;background-color:darkgray; color:black; border-radius:50px;'>Auto</button></a>");
// The HTTP response ends with another blank line:
client.println();
// break out of the while loop:
break;
}
else { // if you got a newline, then clear currentLine:
currentLine = "";
}
}
else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
// Regarde de que le client renvoie comme info:
if (currentLine.endsWith("GET /A")) {
href++; // GET /H augmente l'huniditée de 1
}
if (currentLine.endsWith("GET /B")) {
href--; // GET /L baisse l'huniditée de 1
}
if (currentLine.endsWith("GET /C")) {
tref++; // GET /HT augmente la temp de 1
}
if (currentLine.endsWith("GET /D")) {
tref--; // GET /L baisse la temp de 1
}
if (currentLine.endsWith("GET /AR")) {
marche = 0; // GET /L arrete tout
}
if (currentLine.endsWith("GET /MA")) {
marche = 1; // GET /L marche ventilation
}
if (currentLine.endsWith("GET /AU")) {
marche = 4; // GET /L mode automatique
}
if (currentLine.endsWith("GET /HE")) {
marche = 2; // GET /L marche chauffage
}
if (currentLine.endsWith("GET /TO")) {
marche = 3; // GET /L marche chauffage et ventilation
}
}
}
// close the connection:
client.stop();
Serial.println("client disonnected");
}
lcd.setCursor(0, 0);
lcd.print("T:");
lcd.print(dht.readTemperature());
lcd.print("C ");
lcd.setCursor(10,0);
lcd.print("Tr:");
lcd.print(tref);
lcd.print("C ");
lcd.setCursor(0,1);
lcd.print("H:");
lcd.print(dht.readHumidity());
lcd.print("%");
lcd.setCursor(10,1);
lcd.print("Hr:");
lcd.print(href);
lcd.print("%");
Serial.print("Temp: ");
Serial.print(dht.readTemperature());
Serial.print("C");
Serial.println("");
Serial.print("Hum: ");
Serial.print(dht.readHumidity());
Serial.print("%");
Serial.println("");
Serial.print(href);
Serial.println("");
Serial.print(tref);
Serial.println("");
Serial.println(marche);
Serial.println(tempactif);
Serial.println(etatprecedent);
Serial.println(etat);
et la troisieme :
if(marche == 0)
{
digitalWrite(relaiPin, LOW);
analogWrite(chaufpin, 0);
etat = 0;
chauff = 0;
}
else if(marche == 1)
{
digitalWrite(relaiPin, HIGH);
analogWrite(chaufpin, 0);
etat = 1;
chauff = 0;
}
else if(marche == 2)
{
digitalWrite(relaiPin, LOW);
analogWrite(chaufpin, 255);
etat = 0;
chauff = 1;
}
else if(marche == 3)
{
digitalWrite(relaiPin, HIGH);
analogWrite(chaufpin, 255);
etat = 1;
chauff = 1;
}
else if (marche == 4 && dht.readTemperature() > tref && dht.readHumidity() > href && tempactif >=minimumON)
{
digitalWrite(relaiPin, HIGH);
analogWrite(chaufpin, 0);
etat = 1;
chauff = 0;
}
else if(marche == 4 && dht.readTemperature() > tref && dht.readHumidity() < href && tempactif >=minimumON)
{
digitalWrite(relaiPin, LOW);
analogWrite(chaufpin, 0);
etat = 0;
chauff = 0;
}
else if(marche == 4 && dht.readTemperature() < tref-1 && tempactif >=minimumON)
{
digitalWrite(relaiPin, LOW);
analogWrite(chaufpin, 255);
etat = 0;
chauff = 1;
}
if(etat != etatprecedent){tempactif = 0;}
etatprecedent = etat;
delay(delai);
}
void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
// print where to go in a browser:
Serial.print("To see this page in action, open a browser to http://");
Serial.println(ip);
}