Multiple DS18B20 as inputs for 2 state trigger

Hi again, next step is to modify my last DS18B20 project to use multiple probes. Appears to work until triggered then all kinds of wrong triggering stuff happens. As you can probably tell I have no idea of what I’m doing. Can anyone point me in the right direction.
Thanks,
John

  
//#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h> 
#include <OneWire.h>
#include <DallasTemperature.h>

char ssid[] = "J";
char wifiPassword[] = "  ";
char username[] = "  ";
char password[] = "  ";
char clientID[] = "  ";

#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DeviceAddress tempSensorA = { 0x28, 0xFF, 0xC7, 0xA8, 0x60, 0x17, 0x05, 0x96 };  
DeviceAddress tempSensorB = { 0x28, 0xFF, 0x5D, 0x62, 0x51, 0x17, 0x04, 0x95 }; 

#define DATA_CHANNEL1 6 //Virtual channel for publishing sensor data.
#define TRIGGER_CHANNEL1 16 //Virtual channel for publishing the trigger value.

#define DATA_CHANNEL2 7 //Virtual channel for publishing sensor data.
#define TRIGGER_CHANNEL2 17 //Virtual channel for publishing the trigger value.

#define THRESHOLD1 55 //Threshold for the trigger.
#define THRESHOLD2 60 //Threshold for the trigger.

bool sendBelowThreshold = true; //Set to true if the trigger should happen when the data value is below the threshold, 
                                //false if it should happen when the data value is above or equal to the threshold.
bool crossedThreshold = false;
unsigned long lastMillis = 0;

void setup() {
  Serial.begin(9600);
  Cayenne.begin(username, password, clientID, ssid, wifiPassword);
//  sensor_t sensor;
  sensors.begin();
   //set the resolution to 10 bit
  sensors.setResolution(tempSensorA, 10);
}


void sendTriggerValue(int channel, int value, int threshold, bool sendBelowThreshold) {
  if (((value >= threshold) && !sendBelowThreshold) || ((value < threshold) && sendBelowThreshold)) {
    if (!crossedThreshold) {
      Cayenne.virtualWrite(channel, 1, "digital_sensor", "d"); //set trigger two-state widget to 1 
      crossedThreshold = true;
    }
  }
  else
  {
    Cayenne.virtualWrite(channel, 0, "digital_sensor", "d"); //set trigger two-state widget to 0
    crossedThreshold = false;
  }
}


void loop() {
  Cayenne.loop();
  
  //Publish data every 60 seconds (60000 milliseconds).
  if (millis() - lastMillis > 30000) 
   {
    lastMillis = millis();
 
 sensors.requestTemperatures();
 
   int sensor_value1 = sensors.getTempF(tempSensorA);
   sendTriggerValue(TRIGGER_CHANNEL1, sensor_value1, THRESHOLD1, sendBelowThreshold);   
   Cayenne.virtualWrite(DATA_CHANNEL1, sensor_value1 , "temp", "f");

   int sensor_value2 = sensors.getTempF(tempSensorB);
   sendTriggerValue(TRIGGER_CHANNEL2, sensor_value2, THRESHOLD2, sendBelowThreshold);   
   Cayenne.virtualWrite(DATA_CHANNEL2, sensor_value2 , "temp", "f");
    
    }
  }

With a quick scan of your code and not really knowing what you want it to do it seems ok. What exactly isn’t working the way it should?

I’m trying to use the 2 state trigger that Shramik posted to avoid the flood of notifications when triggered. The goal is to have 2 independent triggered DS18B20s with different thresholds. I had 1 DS18B20 working, (posted here on the 9th), but when added another it then reverts to a flood of emails. All widgets appear to work correctly. Can you clarify is the notification limit 300 per day or month. I’ve seen it both ways.
Thanks
John

from your above code, it should send only one trigger only for one widget and not send flood trigger as you say. the issue is wont work for two separate widgets as the crossedThreshold variable is set to true for both the widget. so what you can do is create two separate functions for each widget having unique variables.

Thanks Shramik, not totally sure what you are suggesting, but I’ll give it another try.
John

Try this:

//#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h> 
#include <OneWire.h>
#include <DallasTemperature.h>

char ssid[] = "J";
char wifiPassword[] = "  ";
char username[] = "  ";
char password[] = "  ";
char clientID[] = "  ";

#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DeviceAddress tempSensorA = { 0x28, 0xFF, 0xC7, 0xA8, 0x60, 0x17, 0x05, 0x96 };  
DeviceAddress tempSensorB = { 0x28, 0xFF, 0x5D, 0x62, 0x51, 0x17, 0x04, 0x95 }; 

#define DATA_CHANNEL1 6 //Virtual channel for publishing sensor data.
#define TRIGGER_CHANNEL1 16 //Virtual channel for publishing the trigger value.

#define DATA_CHANNEL2 7 //Virtual channel for publishing sensor data.
#define TRIGGER_CHANNEL2 17 //Virtual channel for publishing the trigger value.

#define THRESHOLD1 55 //Threshold for the trigger.
#define THRESHOLD2 60 //Threshold for the trigger.

bool sendBelowThreshold = true; //Set to true if the trigger should happen when the data value is below the threshold, 
                                //false if it should happen when the data value is above or equal to the threshold.
bool crossedThreshold = false;
bool crossedThreshold2 = false;
unsigned long lastMillis = 0;

void setup() {
  Serial.begin(9600);
  Cayenne.begin(username, password, clientID, ssid, wifiPassword);
//  sensor_t sensor;
  sensors.begin();
   //set the resolution to 10 bit
  sensors.setResolution(tempSensorA, 10);
}


void sendTriggerValue(int channel, int value, int threshold, bool sendBelowThreshold) {
  if (((value >= threshold) && !sendBelowThreshold) || ((value < threshold) && sendBelowThreshold)) {
    if (!crossedThreshold) {
      Cayenne.virtualWrite(channel, 1, "digital_sensor", "d"); //set trigger two-state widget to 1 
      crossedThreshold = true;
    }
  }
  else
  {
    Cayenne.virtualWrite(channel, 0, "digital_sensor", "d"); //set trigger two-state widget to 0
    crossedThreshold = false;
  }
}

void sendTriggerValue2(int channel, int value, int threshold, bool sendBelowThreshold) {
  if (((value >= threshold) && !sendBelowThreshold) || ((value < threshold) && sendBelowThreshold)) {
    if (!crossedThreshold2) {
      Cayenne.virtualWrite(channel, 1, "digital_sensor", "d"); //set trigger two-state widget to 1 
      crossedThreshold2 = true;
    }
  }
  else
  {
    Cayenne.virtualWrite(channel, 0, "digital_sensor", "d"); //set trigger two-state widget to 0
    crossedThreshold2 = false;
  }
}

void loop() {
  Cayenne.loop();
  
  //Publish data every 60 seconds (60000 milliseconds).
  if (millis() - lastMillis > 30000) 
   {
    lastMillis = millis();
 
 sensors.requestTemperatures();
 
   int sensor_value1 = sensors.getTempF(tempSensorA);
   sendTriggerValue(TRIGGER_CHANNEL1, sensor_value1, THRESHOLD1, sendBelowThreshold);   
   Cayenne.virtualWrite(DATA_CHANNEL1, sensor_value1 , "temp", "f");

   int sensor_value2 = sensors.getTempF(tempSensorB);
   sendTriggerValue2(TRIGGER_CHANNEL2, sensor_value2, THRESHOLD2, sendBelowThreshold);   
   Cayenne.virtualWrite(DATA_CHANNEL2, sensor_value2 , "temp", "f");
    
    }
  }
2 Likes

@adam
It works perfectly as is. Now I can add the rest of the inputs for the project. All can now be a no flood trigger. I got caught a couple of weeks ago with a semi flood of notifications, figured it was time to fix that. Thank You!!
John

2 Likes