Chat here is you are connecting ESP-xx type modules to existing AVR based Arduino

Moderator: igrr

User avatar
By sanluca
#66673 hi,

I'm trying to read pulses to measure the consumption of water, gas and electricity through a wemos d1 mini, along with reading the temperature and humidity with a dht22.
The software does not mistake me, but it does not work, as if it always stops in interruptions.

Can you help me?

thank you all
Code: Select all#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 2     // 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_EXT1";
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/cantina/stato/output/1";
const char* inTopic = "home/cantina/luce";
const char* outTopic_dht_temp = "home/cantina/temperatura";
const char* outTopic_dht_umid = "home/cantina/umidita";
const char* outTopic_gas = "home/cantina/gas";
const char* outTopic_acqua = "home/cantina/acqua";
const char* outTopic_luce = "home/cantina/enel";
const char* outTopic_luce_imp = "home/cantina/enel_imp";

int gas = 14;
int acqua = 13;
int luce = 12;
volatile byte state = LOW;

//int state_acqua; //the state of the input
//int oldstate_acqua;
//int state_luce; //the state of the input
//int oldstate_luce;
//int state_gas; //the state of the input
//int oldstate_gas;
unsigned long t; //timer
//unsigned long s; //samples
//unsigned long c; //count
//unsigned long f; //frequency
static long p1;
static long p2;
static long p3; //precedente
static long diff1;
static long diff2;
static long diff3;
unsigned long a;

// 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) {
    for(int i = 0; i<500; i++){
      delay(1);
    }
    Serial.print(".");
  }
 // 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();
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    //Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect("ESP8266Cantina_test")) {
      //Serial.println("connected");
      // Once connected, publish an announcement...
      client.publish(outTopic, "py-home-slave 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++){
        delay(1);
      }
    }
  }
}


void leggi_dht(){
  unsigned long currentMillis = millis();
  if(currentMillis - previousMillis > interval) {
  float h = dht.readHumidity();
  float f = dht.readTemperature();
  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,2,2, buffer);
  client.publish(outTopic_dht_temp,buffer);
  //Serial.print(buffer);
  dtostrf(h,2,2, buffer);
  client.publish(outTopic_dht_umid,buffer);
  //Serial.print("Temperature: ");
  //Serial.print(f);
  //Serial.print("Umidita: ");
  //Serial.print(h);
  }
}

void leggi_acqua()
//diff1
{
  t = millis(); // read time at start of sampling
  diff1= t-p1;
  if (diff1 >0.1){
  client.publish(outTopic_acqua, "1");
    p1=t;
    }
}

void leggi_gas()
  //diff2
{
  t = millis(); // read time at start of sampling
  diff2= t-p2;
  if (diff1 >0.1){
  client.publish(outTopic_gas, "1");
     p1=t;
    }
  }

void leggi_luce()
//diff3
{
    t = millis()/1000;//trasformo in secondi
  diff3=t - p3;
   if ( diff3 > 0.1 ) {  // Se non è il primo blink calcola il consumo
    a=3600/(t - p3);
    char buffer[10];
    dtostrf(a,2,2, buffer);
    client.publish(outTopic_luce, buffer);
    p3=t;
    client.publish(outTopic_luce_imp, "1");
   }}

 

void setup() {
  dht.begin();
  pinMode(gas, INPUT);     // Initialize the gas pin as an input
  pinMode(acqua, INPUT);
  pinMode(luce, INPUT);
  Serial.begin(115200);
  setup_wifi();                   // Connect to wifi
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
  attachInterrupt(digitalPinToInterrupt(luce), leggi_luce, FALLING);
  attachInterrupt(digitalPinToInterrupt(acqua), leggi_luce, FALLING);
  attachInterrupt(digitalPinToInterrupt(gas), leggi_luce, FALLING);
}

void loop() {

  if (!client.connected()) {
    reconnect();
  }
  client.loop();
  leggi_dht();
}
User avatar
By martinayotte
#66808 You should never call client.publish() during an interrupt.
Interrupts should be quick, setting up a flag and exit as fast as possible.
Then, in your loop(), you should look at those flags and execute the client.publish() accordingly.