Conversion from legacy to mqtt

Hi guys,

I had just started with cayenne recently and started coding a watering system/hrv micro controller for my house, and now i have to convert it to mqtt - What i am having trouble with is, when i put cayenne.begin into my code, my ds18b20 chain stops reading - without the cayenne.begin everything reads and responds fine. If i run one sensor it seems to work ok as well, but as soon as theres a true one wire bus with more than one sensor it loses the plot.

Will I have to do individual pins for my sensors, or is there something else to fix it? Im not experienced in coding, obviously enough. Most of the bottom of the code is commented out for now as problem solving.

Cheers
Brendon

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

// Cayenne authentication token. This should be obtained from the Cayenne Dashboard.

char username[] = "x";
char password[] = "x";
char clientID[] = "x";

unsigned long lastMillis = 0;
const int tmpPin = 3; //dallas one wire sensor on pin3
OneWire oneWire(tmpPin);
DallasTemperature sensors(&oneWire);

const float sensorPin = A0;           // select the input pin for the potentiometer
const int relayPin = 2;               // select the pin for the relay
float moisture = 0;            // variable to store the value coming from the sensor
const int fanPin = 5;                 // fan output pin

void setup() {
  // declare the ledPin as an OUTPUT:
  pinMode(relayPin, OUTPUT);
  pinMode(fanPin, OUTPUT);
  pinMode(sensorPin, INPUT);
  Serial.begin(9600);
  Cayenne.begin(username, password, clientID);
  
  
}


void loop() {



  // read the value from the sensor:
  moisture = analogRead(sensorPin);
  moisture = (moisture / 700) * 100;
  sensors.requestTemperatures();
  Serial.println("");
  Serial.println("Temperatures in house");
  Serial.println("=====================");
  Serial.print("Temperature OUTSIDE is: ");
  Serial.println(sensors.getTempCByIndex(0));
  delay(1000);
  Serial.print("Temperature CEILING is: ");
  Serial.println(sensors.getTempCByIndex(1));
  delay(1000);
  Serial.print("Temperature ROOM is: ");
  Serial.println(sensors.getTempCByIndex(2));
  delay(1000);
  Cayenne.virtualWrite(1, moisture, UNIT_PERCENT);

  ////////////////////////////////// HRV SYSTEM ///////////////////////////////////////

  if (sensors.getTempCByIndex(0) > sensors.getTempCByIndex(1))
  {
    Serial.println("high temp");
    digitalWrite(5, HIGH);
  }

  else
  {
    Serial.println("low temp");

    digitalWrite(5, LOW);
    Serial.println("end of loop, waiting 3 seconds");
  }

  delay(3000);

  ///////////////////////////////////// WATERING SYSTEM /////////////////////////////////



  Serial.println("");
  Serial.println("");
  Serial.println("Garden Moisture Levels");
  Serial.println("=====================");
  Serial.print("Moisture is: ");
  Serial.print(moisture);
  Serial.println("%");
  // turn the WATERING RELAY ON on
  if (moisture < 30)
  {
    Serial.println("Moisture is less than 30%, starting watering system");
    digitalWrite(relayPin, HIGH);
  }
  else
  {
    Serial.println("Moisture is above 30%, watering not required");
    digitalWrite(relayPin, LOW);
  }
}
 /* Cayenne.loop();
  }

CAYENNE_OUT_DEFAULT()
{


  // Write data to Cayenne here. This example just sends the current uptime in milliseconds on virtual channel 0.
                       Cayenne.virtualWrite(0, sensors.getTempCByIndex(0));
                       Cayenne.virtualWrite(1, sensors.getTempCByIndex(1));
                       Cayenne.virtualWrite(2, sensors.getTempCByIndex(2));
                       Cayenne.virtualWrite(3, moisture);
}

                       CAYENNE_IN_DEFAULT()
{

                       CAYENNE_LOG("Channel %u, value %s", request.channel, getValue.asString());

}
*/

have a look at this post and code Converting Cayenne Arduino LIbrary sketches to Cayenne MQTT Arduino - #2 by rsiegel

1 Like