Problem combining relay, DHT11 and current sensor's code for Cayenne


#1

Hi
I’ve been trying to put together a code to make the relay, DHT11 sensor and the current sensor work together in the Cayenne dashboard but I could not get the relay to do it’s job. Can someone please help me to figure out what is wrong thanks.

I’ve used the following parts:
Arduino UNO
W5200 ethernet shield
DHT11
100A SCT-013-000 Current sensor
Arduino relay

[code]
/*  
 * Pump control
 */

  #define CAYENNE_PRINT Serial
  #include <CayenneMQTTEthernetW5200.h>

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
  char username[] = "";
  char password[] = "";
  char clientID[] = "";
  
//Current sensor
  #include "EmonLib.h"
  // Sensor Calibrations
  const int volt = 220;
  const float ct_calibration = 30; // 29 default
  const int currentSensorPin = A2;
  float Irms = 0;
 // Create an Emon instance
  EnergyMonitor emon1;

//DHT11
  #include "DHT.h"
  #define DHTPIN 2 
  #define DHTTYPE DHT11   // DHT 11
  // Virtual Pin of the widget.
  #define VIRTUAL_PIN V2
  DHT dht(DHTPIN, DHTTYPE);

//Relay
  //#define SDCARD_CS 4
  #define RELAY_PIN 4
  #define VIRTUAL_PIN V5

  unsigned long lastMillis = 0;

void setup() 
{
  Serial.begin(9600);
  Cayenne.begin(username, password, clientID);
  emon1.current(currentSensorPin, ct_calibration);
  emon1.current(1, 111.1);             // Current: input pin, calibration.

  //pinMode(SDCARD_CS, OUTPUT);
  //digitalWrite(SDCARD_CS, HIGH); // Deselect the SD card

  // set digital pin to output
  pinMode(4, OUTPUT);
  digitalWrite(4, HIGH);
}
CAYENNE_IN(V5)
{
  // get value sent from dashboard
  int currentValue = getValue.asInt(); // 0 to 1
  //float d = getValue.asInt();

  // assuming you wire your relay as normally open
  if (currentValue == 0) 
  {
    digitalWrite(4, HIGH);
    Cayenne.virtualWrite(V5, HIGH); //virtual pin
  } 
  else 
  {
    digitalWrite(4, LOW);
    Cayenne.virtualWrite(V5, LOW); //virtual pin
  }
}
void loop() 
{
  Cayenne.loop();
  
  double Irms = emon1.calcIrms(1480);  // Calculate Irms only
  
  //Publish data every 10 seconds (10000 milliseconds). Change this value to publish at a different interval.
  if (millis() - lastMillis > 5000) 
  {
    lastMillis = millis();
    //Write data to Cayenne here. This example just sends the current uptime in milliseconds.

    //Current sensor
    Cayenne.virtualWrite(0, lastMillis);
    Cayenne.virtualWrite(1, Irms*volt);
    Cayenne.virtualWrite(2, Irms);
    
    //Humidity
    float h = dht.readHumidity();
    Cayenne.virtualWrite(4, h); //virtual pin
    
    //Temperature
    float t = dht.readTemperature();
    Cayenne.virtualWrite(3, t); //virtual pin
  }
}

[/code]


#2

Hello and Welcome to the Cayenne community,
Can you follow this tutorial:


You can copy and paste the code and try to run first the relay board. Also double check the wiring scheme :slight_smile:

Also I see that you use in the loop -> Cayenne.loop , why ?


#3

scusatemi, anche io ho dei problemi ha inserire nella dashboard emontx, un aiuto?


#4

Thanks I will give it a go