wemos d1 mini e interrupt

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

#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();
}

Hi,

I think you need to change so the interrupts just samples the new values. They need to be very short to not crash into each other and into the main code.
As it is right now, you might publish dht at the same time as the gas-meter wants to publish.

Good luck!

I modified the program, now it seems to work, loses some impulse from time to time, maybe two of them do not know how to handle them, but interruptions have priority? How can you change it?
thank you

#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 boolean pubblicaGas=false;
volatile boolean pubblicaAcqua=false;
volatile boolean pubblicaLuce=false;
unsigned long t; //timer
unsigned long p; //previous
unsigned long a;

void setup_wifi() {

  delay(10);
  // We start by connecting to a WiFi network
  WiFi.begin(ssid);

  while (WiFi.status() != WL_CONNECTED) {
    for(int i = 0; i<500; i++){
      delay(1);
    }
    Serial.print(".");
  }
}

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

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    if (client.connect("ESP8266Cantina_test")) {
      client.subscribe(inTopic);
      Serial.print("re");
    } else {
      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);
  dtostrf(h,2,2, buffer);
  client.publish(outTopic_dht_umid,buffer);
  }
}

void leggi_acqua()
 
{
  pubblicaAcqua=true;
}

void leggi_gas()
{
  pubblicaGas=true;
  }

void leggi_luce()
{
  pubblicaLuce=true;
   
}


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_acqua, FALLING);
  attachInterrupt(digitalPinToInterrupt(gas), leggi_gas, FALLING);
}

void loop() {

  if (!client.connected()) {
    reconnect();
  }
  client.loop();
  leggi_dht();
  
  if (pubblicaAcqua==true){
    //Serial.print("acqua");
    client.publish(outTopic_acqua, "1");
    pubblicaAcqua=false; }
  
  if (pubblicaGas==true){
    //Serial.print("gas");
    client.publish(outTopic_gas, "1");
    pubblicaGas=false; }
  
  if (pubblicaLuce==true){
    //Serial.print("enel");
    t = millis()/1000;//trasformo in secondi
    char buffer[10];
    a=3600/(t - p);
    dtostrf(a,2,2, buffer);
    client.publish(outTopic_luce, buffer);
    client.publish(outTopic_luce_imp, "1");
    p=t;
    pubblicaLuce=false; }
}

Sorry, I have no info about interrupt priority.

But you can minimize the problem by only publish values for gas and electricity once every minute.
This will reduce the number of client.publish. Another good thing doing this is that IF your server is busy you can detect that and accumulate pulses until the server is ready to receive them. Hopefully nothing is lost then. :slight_smile: