Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
capteurs:wemos_d1_dht12 [2021/03/17 16:12] – jeff | capteurs:wemos_d1_dht12 [Date inconnue] (Version actuelle) – supprimée - modification externe (Date inconnue) 127.0.0.1 | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | ====== | ||
- | * [[https:// | ||
- | * [[https:// | ||
- | * relai de mesure de la tension , résistance 180k ( pas sur la V1 ) | ||
- | * batterie 18650 | ||
- | ===== Codes ===== | ||
- | V1 | ||
- | <code c> | ||
- | |||
- | #include < | ||
- | #include < | ||
- | #include < | ||
- | #include < | ||
- | #include " | ||
- | |||
- | const char* ssid = " | ||
- | const char* password = " | ||
- | const char* mqtt_server = " | ||
- | const char* host = " | ||
- | const char* mqtt_user = " | ||
- | const char* mqtt_pass = " | ||
- | int mqtt_port = 1883; | ||
- | int prog_version = 1; | ||
- | int version_maj | ||
- | |||
- | EspMQTTClient client(NULL, | ||
- | double batterie = 0; // resistance 180 k A0< | ||
- | float minute_dodo = 10; | ||
- | int boucle1 = 0; | ||
- | unsigned long lastMsg = 0; | ||
- | #define MSG_BUFFER_SIZE | ||
- | char msg[MSG_BUFFER_SIZE]; | ||
- | int value = 0; | ||
- | bool donnee_prete = false; | ||
- | bool connexion_ok = false; | ||
- | bool attend_flash = false; | ||
- | |||
- | #define capteur_dht12 | ||
- | float temperature = 0; | ||
- | float humidite = 0; | ||
- | float pression = 0; | ||
- | |||
- | #ifdef capteur_dht12 | ||
- | #include < | ||
- | DHT12 dht12; | ||
- | #endif | ||
- | void setup_wifi() { | ||
- | |||
- | delay(10); | ||
- | // We start by connecting to a WiFi network | ||
- | Serial.println(); | ||
- | Serial.print(" | ||
- | Serial.println(ssid); | ||
- | |||
- | WiFi.mode(WIFI_STA); | ||
- | WiFi.begin(ssid, | ||
- | |||
- | while (WiFi.status() != WL_CONNECTED) { | ||
- | delay(500); | ||
- | Serial.print(" | ||
- | } | ||
- | Serial.println("" | ||
- | Serial.println(" | ||
- | Serial.println(" | ||
- | Serial.println(WiFi.localIP()); | ||
- | } | ||
- | void callback(char* topic, byte* payload, unsigned int length) { | ||
- | Serial.print(" | ||
- | Serial.print(topic); | ||
- | Serial.print(" | ||
- | for (int i = 0; i < length; i++) { | ||
- | Serial.print((char)payload[i]); | ||
- | } | ||
- | Serial.println(); | ||
- | |||
- | if ((char)payload[0] == ' | ||
- | digitalWrite(BUILTIN_LED, | ||
- | // A FAIRE | ||
- | } else { | ||
- | digitalWrite(BUILTIN_LED, | ||
- | } | ||
- | |||
- | } | ||
- | void dodo() { | ||
- | Serial.print(" | ||
- | Serial.println((float)minute_dodo); | ||
- | // Connect D0 to RST to wake up ! | ||
- | ESP.deepSleep(minute_dodo * 60 * 1000000); | ||
- | } | ||
- | |||
- | bool recup_donnees() { | ||
- | int boucle = 0; | ||
- | while (donnee_prete != true && boucle < 5) { // lescture des donnees | ||
- | if (dht12.get() == 0) { | ||
- | donnee_prete = true; | ||
- | temperature = dht12.cTemp; | ||
- | humidite = dht12.humidity; | ||
- | Serial.print(" | ||
- | Serial.println(temperature); | ||
- | Serial.print(" | ||
- | Serial.println(humidite); | ||
- | Serial.println(); | ||
- | return true; | ||
- | } | ||
- | Serial.println(" | ||
- | delay(1000); | ||
- | boucle++; | ||
- | } | ||
- | Serial.println(" | ||
- | dodo(); | ||
- | return false; | ||
- | } | ||
- | void onConnectionEstablished() { | ||
- | connexion_ok = true; | ||
- | Serial.println(" | ||
- | } | ||
- | void setup() { | ||
- | Serial.begin(115200); | ||
- | pinMode(BUILTIN_LED, | ||
- | Serial.begin(115200); | ||
- | Serial.println("" | ||
- | Serial.print(" | ||
- | Serial.println(ESP.getChipId(), | ||
- | batterie = analogRead(A0) ; | ||
- | batterie = batterie / 214.5; // 209.66 assumes external 180K resistor / 1024) * 6.13; | ||
- | Serial.println(batterie); | ||
- | recup_donnees(); | ||
- | setup_wifi(); | ||
- | client.enableDebuggingMessages(); | ||
- | } | ||
- | |||
- | void loop() { | ||
- | // premiere lecture | ||
- | if (dht12.get() == 0) { | ||
- | donnee_prete = true; | ||
- | temperature = dht12.cTemp; | ||
- | humidite = dht12.humidity; | ||
- | Serial.println("" | ||
- | Serial.print(" | ||
- | Serial.println(temperature); | ||
- | Serial.print(" | ||
- | Serial.println(humidite); | ||
- | Serial.println(); | ||
- | } | ||
- | |||
- | client.loop(); | ||
- | unsigned long now = millis(); | ||
- | if (now - lastMsg > 15000) { | ||
- | Serial.println(" | ||
- | dodo(); | ||
- | } | ||
- | if (now - lastMsg > 5000) { // attend 5s avant de faire | ||
- | lastMsg = now; | ||
- | ++value; | ||
- | if (donnee_prete == true && connexion_ok == true) { // tout est pret | ||
- | Serial.println(" | ||
- | // Publish a message to " | ||
- | String topic1 = " | ||
- | String topic2 = " | ||
- | String topic3 = " | ||
- | String topic4 = " | ||
- | Serial.println(topic1); | ||
- | Serial.println(topic2); | ||
- | Serial.println(topic3); | ||
- | Serial.println(topic4); | ||
- | if ( | ||
- | client.publish(String(topic1).c_str(), | ||
- | client.publish(String(topic2).c_str(), | ||
- | client.publish(String(topic3).c_str(), | ||
- | client.publish(String(topic4).c_str(), | ||
- | ) { | ||
- | Serial.println(" | ||
- | delay(5000); | ||
- | /* | ||
- | // Subscribe to " | ||
- | String topic = ""; | ||
- | topic = String(host) + "/" | ||
- | |||
- | if ( client.subscribe(topic, | ||
- | version_maj = atoi(payload.c_str()) ; | ||
- | }) ) { | ||
- | if (version_maj != prog_version) { | ||
- | client.enableHTTPWebUpdater(); | ||
- | Serial.println(" | ||
- | attend_flash = true; | ||
- | } | ||
- | } | ||
- | */ | ||
- | dodo(); | ||
- | } | ||
- | } | ||
- | } | ||
- | delay(1000); | ||
- | } | ||
- | |||
- | </ |