Script BOX temperature


#61

Merci egalement @MathieuA pour ton aide !!!
c’est top d’avoir des pro comme vous !

petite question est-il possible d’ajouter un ecran lcd type i2c 16X2 ?

j’ai trouvé ce code , je dois juste placer ce qu’il y a dans les differentes fonctions setup, loop etc dans mon code actuel au meme endroit?

#include <DHT.h>
#include <Wire.h> // Include I2C bus 

#include <LiquidCrystal_I2C.h>  // Include LCD-I2C bus
#define DHTPIN D4         //pin connect DHT
#define LED D0            //LED BUILTIN for NodeMCU
#define DHTTYPE DHT22     //if use DHT11 change to "DHT11"
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(0x27, 16, 2); 
void setup()
{
  pinMode(LED,OUTPUT);
   
  lcd.init();                  // Start LCD operation
  lcd.backlight();              // Turn on backlight of LCD
  lcd.setCursor(0, 0);          // Set LCD home position
  lcd.print("Humidity=");       // Print Humidity message
  lcd.setCursor(0, 1);          // Set LCD home position of 2nd line
  lcd.print("Temp=");           // Print Temperature message  
  Serial.begin(115200);
  dht.begin();
}

void loop()
{
   digitalWrite(D0,HIGH);
  delay(2500);
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  digitalWrite(D0,LOW);    
  lcd.setCursor(10, 0);          
  lcd.print(h + String("%"));       // Show humidity value in %RH
  lcd.setCursor(6, 1);            
  lcd.print(t + String("C"));        // Show temperature value in Celcuis   
  delay(2500);   
    delay(2500);
}

#62

ok par contre ce n’est pas moi qui ai ouvert le post au départ ,
je fais comment ?


#63

Tu peux pas !!! :slight_smile: J’avais pas vu, désolé.


#64

pour ceux que cela interesse je laisse le code qui fonctionne enfin avec affichage sur un ecran lcd :

@MathieuA tu me dira ce que tu en pense si c’est bien optimisé? J’ai fais comme j’ai pu pour fusionner les 2 codes et ça fonctionne :grinning:

#include <ESP8266WiFi.h>
#include <DHT.h>
#include <Wire.h> // Include I2C bus
#include <LiquidCrystal_I2C.h>  // Include LCD-I2C bus
LiquidCrystal_I2C lcd(0x3f, 16, 2);

#define DHTPIN D5 // broche du capteur
#define DHTTYPE DHT22   // Type du capteur 
DHT dht(DHTPIN, DHTTYPE); //?

const char* ssid = "alex emma chambre"; // nom de la wifi
const char* password = "mdp"; // mot de passe wifi

String requestTemperature = "GET /devicestate/create?token=9880880e2b611db08c8bb8f1816379d1644f57fd&devicetype=43&value=%VALUE";
String requestHumidity = "GET /devicestate/create?token=9880880e2b611db08c8bb8f1816379d1644f57fd&devicetype=44&value=%VALUE";



void setup() {


  lcd.init();                  // Start LCD operation
  lcd.backlight();              // Turn on backlight of LCD
  lcd.setCursor(0, 0);          // Set LCD home position
  lcd.print("Humidite=");       // Print Humidity message
  lcd.setCursor(0, 1);          // Set LCD home position of 2nd line
  lcd.print("Temp=");           // Print Temperature message
  Serial.begin(115200);
  dht.begin();


  Serial.begin(115200);
  delay(10);

  // Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");



  // Print the IP address
  Serial.println(WiFi.localIP());
}

void loop() {



  WiFiClient client;


  /**
     Temperature
  */

  float t = dht.readTemperature();
  // Check if returns are valid, if they are NaN (not a number) then something went wrong!
  if (isnan(t)) {
    Serial.println("Failed to read from DHT");
  } else {
    Serial.println("connecting...");
    // If you get a connection, report back via serial:
    if (client.connect("192.168.1.65", 80)) {
      Serial.println("connected");
      // Make a HTTP request:

      String temperature = String(t, 0);

      // Replacing URL with the value
      requestTemperature.replace("%VALUE", temperature);


      lcd.setCursor(6, 1);
      lcd.print(t + String("C"));        // Show temperature value in Celcuis
      delay(2500);

      Serial.println(requestTemperature);

      client.println(requestTemperature);

      client.println("Connection: close");
      client.println();

      Serial.println();
      Serial.println("disconnecting.");
      client.stop();
    } else {
      // If you didn't get a connection to the server:
      Serial.println("connection failed");
    }
    delay(1000);
  }

  /**
    Humidity
  */

  float h = dht.readHumidity();

  if (isnan(h)) {
    Serial.println("Failed to read from DHT");
  } else {
    Serial.println("connecting...");
    // If you get a connection, report back via serial:
    if (client.connect("192.168.1.65", 80)) {
      Serial.println("connected");
      // Make a HTTP request:

      String humidity = String(h, 0);

      // Replacing URL with the value
      requestHumidity.replace("%VALUE", humidity);

      lcd.setCursor(10, 0);
      lcd.print(h + String("%"));       // Show humidity value in %RH

      delay(2500);


      Serial.println(requestHumidity);

      client.println(requestHumidity);

      client.println("Connection: close");
      client.println();
      Serial.println();
      Serial.println("disconnecting.");
      client.stop();
    } else {
      // If you didn't get a connection to the server:
      Serial.println("connection failed");


    }
    delay(1000);
  }

  delay(10000);
}

#65

C’est assez propre @alexis ^^


#66

Hello!
comme vous pouvez vous en douter j’ai besoin d’aide :grin:

j’ai reçu mon capteur d’air DSM501A que j’ai reussi a faire fonctionner avec le code trouvé sur ce site: https://projetsdiy.fr/calculer-indice-qualite-air-iaq-ciqa-iqa-dsm501-arduino-esp8266/

J’ai donc bien le résultat qui arrive dans mon serial actualisé toutes les 5 min:

25

Du coup forcément le but est de l’afficher sur l’interface de Gladys :grin: mais uniquement les valeurs "bon/moyen/mediocre etc…) je ne veux pas afficher les valeurs de AQI et autres… car celles-ci seront sur un lcd que je brancherais plus tard directement sur l’esp.

J’ai donc téléversé le code dans un esp8266 et j’ai commencé a m’inspirer du code utilisé pour le dht 22 précédemment (qui fonctionne a la perfection encore merci pour votre aide!)

du coup forcément avec mes connaissances limitées… je bloque sur la programmation… pour adapter tout cela.
j’ai pensé commencer par ajouter la requête de ce type:

String requestAirQuality = "GET /devicestate/create?token=9880880e2b611db08c8bb8f1816379d1644f57fd&amp;devicetype=43&amp;value=%VALUE";

suis-je sur la bonne piste?

mais apres je bloque complet!!
Si une ame charitable passe dans le coin…

je vous met le code tel qu’il est actuellement téléversé avec les quelques ajouts que j’ai pu faire.

Merci!!

#include <ESP8266WiFi.h>
/* Connect the DSM501 sensor as follow 
 * https://www.elektronik.ropla.eu/pdf/stock/smy/dsm501.pdf
 * 1 green vert - Not used
 * 2 yellow jaune - Vout2 - 1 microns (PM1.0)
 * 3 white blanc - Vcc
 * 4 red rouge - Vout1 - 2.5 microns (PM2.5)
 * 5 black noir - GND
*/

const char* ssid = "wifisalon"; // nom de la wifi
const char* password = "MDP"; // mot de passe wifi
#define DUST_SENSOR_DIGITAL_PIN_PM10  D3        // DSM501 Pin 2 of DSM501 (jaune / Yellow)
#define DUST_SENSOR_DIGITAL_PIN_PM25  D5        // DSM501 Pin 4 (rouge / red) 
#define COUNTRY                       0         // 0. France, 1. Europe, 2. USA/China
#define EXCELLENT                     "Excellent"
#define GOOD                          "Bon"
#define ACCEPTABLE                    "Moyen"
#define MODERATE                      "Mediocre"
#define HEAVY                         "Mauvais"
#define SEVERE                        "Tres mauvais"
#define HAZARDOUS                     "Dangereux"

unsigned long   duration;
unsigned long   starttime;
unsigned long   endtime;
unsigned long   lowpulseoccupancy = 0;
float           ratio = 0;
unsigned long   SLEEP_TIME    = 2 * 1000;       // Sleep time between reads (in milliseconds)
unsigned long   sampletime_ms = 5 * 60 * 1000;  // Durée de mesure - sample time (ms)

struct structAQI{
  // variable enregistreur - recorder variables
  unsigned long   durationPM10;
  unsigned long   lowpulseoccupancyPM10 = 0;
  unsigned long   durationPM25;
  unsigned long   lowpulseoccupancyPM25 = 0;
  unsigned long   starttime;
  unsigned long   endtime;
  // Sensor AQI data
  float         concentrationPM25 = 0;
  float         concentrationPM10  = 0;
  int           AqiPM10            = -1;
  int           AqiPM25            = -1;
  // Indicateurs AQI - AQI display
  int           AQI                = 0;
  String        AqiString          = "";
  int           AqiColor           = 0;
};
struct structAQI AQI;

SimpleTimer timer;

void updateAQILevel(){
  AQI.AQI = AQI.AqiPM10;
}

void updateAQI() {
  // Actualise les mesures - update measurements
  AQI.endtime = millis();
  float ratio = AQI.lowpulseoccupancyPM10 / (sampletime_ms * 10.0);
  float concentration = 1.1 * pow( ratio, 3) - 3.8 *pow(ratio, 2) + 520 * ratio + 0.62; // using spec sheet curve
  if ( sampletime_ms < 3600000 ) { concentration = concentration * ( sampletime_ms / 3600000.0 ); }
  AQI.lowpulseoccupancyPM10 = 0;
  AQI.concentrationPM10 = concentration;
  
  ratio = AQI.lowpulseoccupancyPM25 / (sampletime_ms * 10.0);
  concentration = 1.1 * pow( ratio, 3) - 3.8 *pow(ratio, 2) + 520 * ratio + 0.62;
  if ( sampletime_ms < 3600000 ) { concentration = concentration * ( sampletime_ms / 3600000.0 ); }
  AQI.lowpulseoccupancyPM25 = 0;
  AQI.concentrationPM25 = concentration;

  Serial.print("Concentrations => PM2.5: "); Serial.print(AQI.concentrationPM25); Serial.print(" | PM10: "); Serial.println(AQI.concentrationPM10);
  
  AQI.starttime = millis();
      
  // Actualise l'AQI de chaque capteur - update AQI for each sensor 
  if ( COUNTRY == 0 ) {
    // France
    AQI.AqiPM25 = getATMO( 0, AQI.concentrationPM25 );
    AQI.AqiPM10 = getATMO( 1, AQI.concentrationPM10 );
  } else if ( COUNTRY == 1 ) {
    // Europe
    AQI.AqiPM25 = getACQI( 0, AQI.concentrationPM25 );
    AQI.AqiPM10 = getACQI( 1, AQI.concentrationPM10 );
  } else {
    // USA / China
    AQI.AqiPM25 = getAQI( 0, AQI.concentrationPM25 );
    AQI.AqiPM10 = getAQI( 0, AQI.concentrationPM10 );
  }

  // Actualise l'indice AQI - update AQI index
  updateAQILevel();
  updateAQIDisplay();
  
  Serial.print("AQIs => PM25: "); Serial.print(AQI.AqiPM25); Serial.print(" | PM10: "); Serial.println(AQI.AqiPM10);
  Serial.print(" | AQI: "); Serial.println(AQI.AQI); Serial.print(" | Message: "); Serial.println(AQI.AqiString);
  

}

void setup() {
 Serial.begin(115200);
   // Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");



  // Print the IP address
  Serial.println(WiFi.localIP());
  // put your setup code here, to run once:
 
  pinMode(DUST_SENSOR_DIGITAL_PIN_PM10,INPUT);
  pinMode(DUST_SENSOR_DIGITAL_PIN_PM25,INPUT);

  // wait 60s for DSM501 to warm up
  for (int i = 1; i <= 60; i++)
  {
    delay(1000); // 1s
    Serial.print(i);
    Serial.println(" s (wait 60s for DSM501 to warm up)");
  }
  
  Serial.println("Ready!");
  
  AQI.starttime = millis();
  timer.setInterval(sampletime_ms, updateAQI);
}

void loop() {

 WiFiClient client;
  
  AQI.lowpulseoccupancyPM10 += pulseIn(DUST_SENSOR_DIGITAL_PIN_PM10, LOW);
  AQI.lowpulseoccupancyPM25 += pulseIn(DUST_SENSOR_DIGITAL_PIN_PM25, LOW);
  
  timer.run(); 
}

/*
 * Calcul l'indice de qualité de l'air français ATMO
 * Calculate French ATMO AQI indicator
 */
int getATMO( int sensor, float density ){
  if ( sensor == 0 ) { //PM2,5
    if ( density <= 11 ) {
      return 1; 
    } else if ( density > 11 && density <= 24 ) {
      return 2;
    } else if ( density > 24 && density <= 36 ) {
      return 3;
    } else if ( density > 36 && density <= 41 ) {
      return 4;
    } else if ( density > 41 && density <= 47 ) {
      return 5;
    } else if ( density > 47 && density <= 53 ) {
      return 6;
    } else if ( density > 53 && density <= 58 ) {
      return 7;
    } else if ( density > 58 && density <= 64 ) {
      return 8;
    } else if ( density > 64 && density <= 69 ) {
      return 9;
    } else {
      return 10;
    }
  } else {
    if ( density <= 6 ) {
      return 1; 
    } else if ( density > 6 && density <= 13 ) {
      return 2;
    } else if ( density > 13 && density <= 20 ) {
      return 3;
    } else if ( density > 20 && density <= 27 ) {
      return 4;
    } else if ( density > 27 && density <= 34 ) {
      return 5;
    } else if ( density > 34 && density <= 41 ) {
      return 6;
    } else if ( density > 41 && density <= 49 ) {
      return 7;
    } else if ( density > 49 && density <= 64 ) {
      return 8;
    } else if ( density > 64 && density <= 79 ) {
      return 9;
    } else {
      return 10;
    }  
  }
}

void updateAQIDisplay(){
  /*
   * 1 EXCELLENT                    
   * 2 GOOD                         
   * 3 ACCEPTABLE               
   * 4 MODERATE            
   * 5 HEAVY               
   * 6 SEVERE
   * 7 HAZARDOUS
   */
  if ( COUNTRY == 0 ) {
    // Système ATMO français - French ATMO AQI system 
    switch ( AQI.AQI) {
      case 10: 
        AQI.AqiString = SEVERE;
        break;
      case 9:
        AQI.AqiString = HEAVY;
        break;
      case 8:
        AQI.AqiString = HEAVY;
        break;  
      case 7:
        AQI.AqiString = MODERATE;
        break;
      case 6:
        AQI.AqiString = MODERATE;
        break;   
      case 5:
        AQI.AqiString = ACCEPTABLE;
        break;
      case 4:
        AQI.AqiString = GOOD;
        break;
      case 3:
        AQI.AqiString = GOOD;
        break;
      case 2:
        AQI.AqiString = EXCELLENT;
        break;
      case 1:
        AQI.AqiString = EXCELLENT;
        break;           
      }
  } else if ( COUNTRY == 1 ) {
    // European CAQI
    switch ( AQI.AQI) {
      case 25: 
        AQI.AqiString = GOOD;
        break;
      case 50:
        AQI.AqiString = ACCEPTABLE;
        break;
      case 75:
        AQI.AqiString = MODERATE;
        break;
      case 100:
        AQI.AqiString = HEAVY;
        break;         
      default:
        AQI.AqiString = SEVERE;
      }  
  } else if ( COUNTRY == 2 ) {
    // USA / CN
    if ( AQI.AQI <= 50 ) {
        AQI.AqiString = GOOD;
    } else if ( AQI.AQI > 50 && AQI.AQI <= 100 ) {
        AQI.AqiString = ACCEPTABLE;
    } else if ( AQI.AQI > 100 && AQI.AQI <= 150 ) {
        AQI.AqiString = MODERATE;
    } else if ( AQI.AQI > 150 && AQI.AQI <= 200 ) {
        AQI.AqiString = HEAVY;
    } else if ( AQI.AQI > 200 && AQI.AQI <= 300 ) {  
        AQI.AqiString = SEVERE;
    } else {    
       AQI.AqiString = HAZARDOUS;
    }  
  }
}
/*
 * CAQI Européen - European CAQI level 
 * source : http://www.airqualitynow.eu/about_indices_definition.php
 */
 
int getACQI( int sensor, float density ){  
  if ( sensor == 0 ) {  //PM2,5
    if ( density == 0 ) {
      return 0; 
    } else if ( density <= 15 ) {
      return 25 ;
    } else if ( density > 15 && density <= 30 ) {
      return 50;
    } else if ( density > 30 && density <= 55 ) {
      return 75;
    } else if ( density > 55 && density <= 110 ) {
      return 100;
    } else {
      return 150;
    }
  } else {              //PM10
    if ( density == 0 ) {
      return 0; 
    } else if ( density <= 25 ) {
      return 25 ;
    } else if ( density > 25 && density <= 50 ) {
      return 50;
    } else if ( density > 50 && density <= 90 ) {
      return 75;
    } else if ( density > 90 && density <= 180 ) {
      return 100;
    } else {
      return 150;
    }
  }
}

/*
 * AQI formula: https://en.wikipedia.org/wiki/Air_Quality_Index#United_States
 * Arduino code https://gist.github.com/nfjinjing/8d63012c18feea3ed04e
 * On line AQI calculator https://www.airnow.gov/index.cfm?action=resources.conc_aqi_calc
 */
float calcAQI(float I_high, float I_low, float C_high, float C_low, float C) {
  return (I_high - I_low) * (C - C_low) / (C_high - C_low) + I_low;
}

int getAQI(int sensor, float density) {
  int d10 = (int)(density * 10);
  if ( sensor == 0 ) {
    if (d10 <= 0) {
      return 0;
    }
    else if(d10 <= 120) {
      return calcAQI(50, 0, 120, 0, d10);
    }
    else if (d10 <= 354) {
      return calcAQI(100, 51, 354, 121, d10);
    }
    else if (d10 <= 554) {
      return calcAQI(150, 101, 554, 355, d10);
    }
    else if (d10 <= 1504) {
      return calcAQI(200, 151, 1504, 555, d10);
    }
    else if (d10 <= 2504) {
      return calcAQI(300, 201, 2504, 1505, d10);
    }
    else if (d10 <= 3504) {
      return calcAQI(400, 301, 3504, 2505, d10);
    }
    else if (d10 <= 5004) {
      return calcAQI(500, 401, 5004, 3505, d10);
    }
    else if (d10 <= 10000) {
      return calcAQI(1000, 501, 10000, 5005, d10);
    }
    else {
      return 1001;
    }
  } else {
    if (d10 <= 0) {
      return 0;
    }
    else if(d10 <= 540) {
      return calcAQI(50, 0, 540, 0, d10);
    }
    else if (d10 <= 1540) {
      return calcAQI(100, 51, 1540, 541, d10);
    }
    else if (d10 <= 2540) {
      return calcAQI(150, 101, 2540, 1541, d10);
    }
    else if (d10 <= 3550) {
      return calcAQI(200, 151, 3550, 2541, d10);
    }
    else if (d10 <= 4250) {
      return calcAQI(300, 201, 4250, 3551, d10);
    }
    else if (d10 <= 5050) {
      return calcAQI(400, 301, 5050, 4251, d10);
    }
    else if (d10 <= 6050) {
      return calcAQI(500, 401, 6050, 5051, d10);
    }
    else {
      return 1001;
    }
  }   
}


j’ai commencé également a creer un device dabns Gladys de cette façon:

par contre apres je bloque aussi du coup vu que je veux afficher un texte e non pas des degrés ou autre…