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");
}
}