wemos d1 dht22

ciao a tutti,
spero sia la sezione gista.

ho un wemos d1 mini con sensore dht22, e un relè, tramite mqtt vorrei comandare il relè e inviare lettura temperatura e umidità.

con il relè dovrei esserci riuscito, ma con il dht22 mi continua a dare errore di lettura.

vi allego il codice.

grazie a tutti per l’eventuale aiuto

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <Bounce2.h>
#include <EEPROM.h>
#include <DHT.h>      // DHT library from http://github.com/adafruit/DHT-sensor-library
                     // Written by ladyada, public domain

#define DHTPIN 4     // what pin we're connected to

// Uncomment whatever type you're using!
//#define DHTTYPE DHT11   // DHT 11
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE, 20);

const char* ssid = "linksys";
const char* password = "YourPASS";
const char* mqtt_server = "192.168.1.60";
long previousMillis = 0;
long interval = 60000;

WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;

const char* outTopic = "home/iroomba/stato";
const char* inTopic = "home/iroomba";
const char* outTopic_dht_temp = "home/temp";
const char* outTopic_dht_umid = "home/umid";

int relay_pin = 1;
int button_pin = 0;
bool relayState = LOW;

// Instantiate a Bounce object :
Bounce debouncer = Bounce(); 


void setup_wifi() {

 delay(10);
 // We start by connecting to a WiFi network
 Serial.println();
 Serial.print("Connecting to ");
 Serial.println(ssid);

 //WiFi.begin(ssid, password);
 WiFi.begin(ssid);

 while (WiFi.status() != WL_CONNECTED) {
   extButton();
   for(int i = 0; i<500; i++){
     extButton();
     delay(1);
   }
   Serial.print(".");
 }
 digitalWrite(13, LOW);
 delay(500);
 digitalWrite(13, HIGH);
 delay(500);
 digitalWrite(13, LOW);
 delay(500);
 digitalWrite(13, HIGH);
 Serial.println("");
 Serial.println("WiFi connected");
 Serial.println("IP address: ");
 Serial.println(WiFi.localIP());
}

void callback(char* topic, byte* payload, unsigned int length) {
 Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
 for (int i = 0; i < length; i++) {
   Serial.print((char)payload[i]);
 }
 Serial.println();

 // Switch on the LED if an 1 was received as first character
 if ((char)payload[0] == '0') {
   digitalWrite(relay_pin, LOW);   // Turn the LED on (Note that LOW is the voltage level
   Serial.println("relay_pin -> LOW");
   relayState = LOW;
   client.publish(outTopic, "0");
   EEPROM.write(0, relayState);    // Write state to EEPROM
   EEPROM.commit();
 } else if ((char)payload[0] == '1') {
   digitalWrite(relay_pin, HIGH);  // Turn the LED off by making the voltage HIGH
   Serial.println("relay_pin -> HIGH");
   relayState = HIGH;
   client.publish(outTopic, "1");
   delay(1000);
   digitalWrite(relay_pin, LOW);
   EEPROM.write(0, relayState);    // Write state to EEPROM
   EEPROM.commit();
 } else if ((char)payload[0] == '2') {
   relayState = !relayState;
   digitalWrite(relay_pin, relayState);  // Turn the LED off by making the voltage HIGH
   Serial.print("relay_pin -> switched to ");
   Serial.println(relayState); 
   EEPROM.write(0, relayState);    // Write state to EEPROM
   EEPROM.commit();
 }
}

void reconnect() {
 // Loop until we're reconnected
 while (!client.connected()) {
   Serial.print("Attempting MQTT connection...");
   // Attempt to connect
   if (client.connect("ESP8266Client")) {
     Serial.println("connected");
     // Once connected, publish an announcement...
     client.publish(outTopic, "iroomba booted");
     // ... and resubscribe
     client.subscribe(inTopic);
   } else {
     Serial.print("failed, rc=");
     Serial.print(client.state());
     Serial.println(" try again in 5 seconds");
     // Wait 5 seconds before retrying
     for(int i = 0; i<5000; i++){
       extButton();
       delay(1);
     }
   }
 }
}

void extButton() {
 debouncer.update();
  
  // Call code if Bounce fell (transition from HIGH to LOW) :
  if ( debouncer.fell() ) {
    Serial.println("Debouncer fell");
    // Toggle relay state :
    relayState = !relayState;
    digitalWrite(relay_pin,relayState);
    EEPROM.write(0, relayState);    // Write state to EEPROM
    if (relayState == 1){
     client.publish(outTopic, "1");
    }
    else if (relayState == 0){
     client.publish(outTopic, "0");
    }
  }
}

void leggi_dht(){
 unsigned long currentMillis = millis();
 if(currentMillis - previousMillis > interval) {
 float h = dht.readHumidity();
 float f = dht.readTemperature(true);
 if (isnan(h) || isnan(f)) {
         Serial.println("Failed to read from DHT sensor!");
         return;
       }
 previousMillis = currentMillis;
     
 
 Serial.println("DHT sensor read and transmitted");
 char buffer[10];
 dtostrf(f,0,0, buffer);
 client.publish(outTopic_dht_temp,buffer);
 dtostrf(h,0,0, buffer);
 client.publish(outTopic_dht_umid,buffer);
 Serial.print("Temperature: ");
 Serial.print(f);
 Serial.print("Umidita: ");
 Serial.print(h);
 }
 //delay(2000);
}


void setup() {
 dht.begin();
 EEPROM.begin(512);              // Begin eeprom to store on/off state
 pinMode(relay_pin, OUTPUT);     // Initialize the relay pin as an output
 pinMode(button_pin, INPUT);     // Initialize the relay pin as an output
 pinMode(13, OUTPUT);
 relayState = EEPROM.read(0);
 digitalWrite(relay_pin,relayState);
 
 debouncer.attach(button_pin);   // Use the bounce2 library to debounce the built in button
 debouncer.interval(50);         // Input must be low for 50 ms
 
 digitalWrite(13, LOW);          // Blink to indicate setup
 delay(500);
 digitalWrite(13, HIGH);
 delay(500);
 
 Serial.begin(115200);
 setup_wifi();                   // Connect to wifi 
 client.setServer(mqtt_server, 1883);
 client.setCallback(callback);
}

void loop() {

 if (!client.connected()) {
   reconnect();
 }
 client.loop();
 
 
 extButton();
 leggi_dht();
}

**>sanluca: ** ti ricordo che in conformità al regolamento, punto 7, devi editare il tuo post (in basso a destra del post, bottone More → Modify) e racchiudere il codice all’interno dei tag CODE (… sono quelli che in edit inserisce il bottone fatto così: </>, tutto a sinistra). Grazie.

Guglielmo

gpb01:
**>sanluca: ** ti ricordo che in conformità al regolamento, punto 7, devi editare il tuo post (in basso a destra del post, bottone More → Modify) e racchiudere il codice all’interno dei tag CODE (… sono quelli che in edit inserisce il bottone fatto così: </>, tutto a sinistra). Grazie.

Guglielmo

chiedo venia

fatto

grazie