Testing digital PIN state not working properly

#1
  • Device: Arduino Uno with W5100 ethernet shield with mixed dashboard Web, Android
    The issue is to do with reading a PIN state and switching a digital icon on the dashboard. I have copied some code from a older post, but I cannot get it to work properly.
    The aim is to verify the actual state of a PIN and showing it on the dashboard, independently from what the state of the digital switch is. The purpose is to keep a check on what happens since the reliability of the system has not yet got to an acceptable level.

There are 3 RELAY_DIGITAL_PINX connected, and I am trying to verify only Pin 7 (RELAY_DIGITAL_PIN1), but the code seems to react on all 3 digital switches, changing state as soon as any of the digital pins are toggled…

// this is not working, as the signal goes high and low when ANY of the 3 actuators are toggled
//addition for control signal to ensure action on actuator pin ----------------------------------------
//================================================================
if(digitalRead(RELAY_DIGITAL_PIN1) == LOW) {
Cayenne.virtualWrite(V6, 1);
}
else
{
Cayenne.virtualWrite(V6, 0);
}
//addition for control signal to ensure action on actuator pin ----------------------------------------
//================================================================

#2

Should this not be other way? As when LOW send 0. And is V6 is the same pin used for receiving the relay state from cayenne?

#3

The relay is wired up so as to be on when signal is LOW, but this is not the issue, as I am testing the actual toggling of the state. If this was wrong, then I should simply get the opposite state indicated.
V6 is not the same pin used, in fact it is an unused pin.

Here is the entire code (please note that it is only a copy I am working on and not the final official code:

/*
  Solarduino main
  From "Cayenne DS18B20 Example"and other examples

                                                          "cayenne5MQTT_SolArduino1Main_camping"


  This sketch shows how to send temperature data to a DS18B20 Sensor in the Cayenne Dashboard.

  The CayenneMQTT Library is required to run this sketch. If you have not already done so you can install it from the Arduino IDE Library Manager.

*/

/* wirtten by Giorgio Chiaretta, May 2019, for Camping Verna - Cumiana (TO) Italy
 */

// SETTING FOT THE SOLARDUINO MAIN FOR CAMPING VERNA SOLAR PANELS SYSTEM. V 2.0
// SAME AS SOLARDUINO AMBIENT, BUT WITH ALL 5 SENSORS AND 3 ACTUATORS USED IN THIS CASE, BUT THE SKETCH HAS BEEN KEPT
// THE SAME TO ENSURE IT IS CONSISTENT. THE ONLY CHANGES CONCERN THE "Resolution test", NOT PRESENT HERE.

//#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space
#include <OneWire.h>
#include <DallasTemperature.h>
#include <CayenneMQTTEthernet.h>//NEW MQTT library



// Virtual Pin of the DS18B20 widget.
#define VIRTUAL_CHANNEL1 1// "Anello bungalow" sensor
#define VIRTUAL_CHANNEL2 2// "Temepratura Caldaia" sensor
#define VIRTUAL_CHANNEL3 3// "Temperatura accumulo acqua sanitaria" sensor
#define VIRTUAL_CHANNEL4 4// "Temperatura pompa pannelli solari" sensor
#define VIRTUAL_CHANNEL5 5// "Temperatura accumulo acqua tecnica" sensor
// Channel 0 is used only to send time elapsed since Arudino borad is on


#define VIRTUAL_CHANNEL7 7// external hot water ciruit valve relè
#define VIRTUAL_CHANNEL8 8// water heater relè
#define VIRTUAL_CHANNEL9 9// hot water mixer relè


#define RELAY_DIGITAL_PIN1 7// external hot water ciruit valve relè pin
#define RELAY_DIGITAL_PIN2 8// water heater relè pin
#define RELAY_DIGITAL_PIN3 9// hot water mixer relè pin

// Digital pin the DS18B20 is connected to. DO NOT USE digital pins 0 or 1 since those conflict with the use of Serial.
const byte tmpPin1 = 2;// "Anello bungalow" sensor
const byte tmpPin2 = 3;// "Temepratura Caldaia" sensor
const byte tmpPin3 = 4;// "Temperatura accumulo acqua sanitaria" sensor
const byte tmpPin4 = 5;// "Temperatura pompa pannelli solari" sensor
const byte tmpPin5 = 6;// "Temperatura accumulo acqua tecnica" sensor


unsigned long lastMillis = 0;
int x = 0;// set default for actuators; on (=1) or OFF (=0)
unsigned char TimeDisplay = 0;

//addition for online LED ---------------------------------------- 
//================================================================
boolean onlineStatus = false;
//================================================================
//addition for online LED ----------------------------------------
// this function is not working. Cayenne say they will try to find a solution for ONLINE status monitor, but asd of today they have no solution.


OneWire oneWire1(tmpPin1);
OneWire oneWire2(tmpPin2);
OneWire oneWire3(tmpPin3);
OneWire oneWire4(tmpPin4);
OneWire oneWire5(tmpPin5);


DallasTemperature sensors1(&oneWire1);
DallasTemperature sensors2(&oneWire2);
DallasTemperature sensors3(&oneWire3);
DallasTemperature sensors4(&oneWire4);
DallasTemperature sensors5(&oneWire5);



// NEW Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
// Solarduino Main  - gchiaretta@gmail.com
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "xxx";
char password[] = "xxx";
char clientID[] = "xxx";


// ---- static IP setting START ----
//...........................................

void setup()
{
  // set digital pin to output

 // (7) 1 = external hot water ciruit valve relè
 // (8) 2 =  water heater relè
 // (9) 3 =  hot water mixer relè
   pinMode(RELAY_DIGITAL_PIN1, OUTPUT);// relè pin
   pinMode(RELAY_DIGITAL_PIN2, OUTPUT);// relè pin
   pinMode(RELAY_DIGITAL_PIN3, OUTPUT);// relè pin

//addition for online LED ---------------------------------------- 
//================================================================
  pinMode(LED_BUILTIN, OUTPUT);// this is Pin 13
//================================================================ 
//addition for online LED ----------------------------------------


   digitalWrite(RELAY_DIGITAL_PIN1, HIGH);// set relay to off at startup// set to Off
   digitalWrite(RELAY_DIGITAL_PIN2, LOW);// set relay to off at startup// set to On
   digitalWrite(RELAY_DIGITAL_PIN3, LOW);// set relay to off at startup// set to On
 //  digitalWrite(TestPin, LOW);// set LED to off at startup// set to Off
  

  //Serial.begin(9600);
  
  Cayenne.begin(username, password, clientID);

  sensors1.begin();
  sensors2.begin();
  sensors3.begin();
  sensors4.begin();
  sensors5.begin();

}


void loop()
{
  Cayenne.loop();
  //Publish data every 10 seconds (10000 milliseconds). Change this value to publish at a different interval.

  if (millis() - lastMillis > 10000) {
    lastMillis = millis();
    TimeDisplay = (lastMillis/10000);

    // 1 = "Anello bungalow" sensor
    // 2 = "Temepratura Caldaia" sensor
    // 3 = "Temperatura accumulo acqua sanitaria" sensor
    // 4 = "Temperatura pompa pannelli solari" sensor
    // 4 = "Temperatura accumulo acqua tecnica" sensor
    sensors1.requestTemperatures();
    sensors2.requestTemperatures();
    sensors3.requestTemperatures();
    sensors4.requestTemperatures();
    sensors5.requestTemperatures();
    
if (lastMillis > 1000000) {//if the time displayed value is too great to fit the diplay, then reduce it by dividing it to 1000000
  TimeDisplay = lastMillis/1000000;


//addition for online LED ---------------------------------------- 
//================================================================
if (onlineStatus == true){
 digitalWrite(LED_BUILTIN, HIGH);
 }
if (onlineStatus == false){
 digitalWrite(LED_BUILTIN, LOW);
}
//================================================================
//addition for online LED ---------------------------------------- 

}

    Cayenne.virtualWrite(0, (TimeDisplay), "analog_sensor", "null");// write 10 seconds since, on on channel 0
    Cayenne.virtualWrite(7, x, "digital_actuator", "d");// and this, if the abbove did work, would set the actuator pin to the corret state.
    // 1 = "Anello bungalow" sensor
    // 2 = "Temepratura Caldaia" sensor
    // 3 = "Temperatura accumulo acqua sanitaria" sensor
    // 4 = "Temperatura pompa pannelli solari" sensor
    // 4 = "Temperatura accumulo acqua tecnica" sensor
    Cayenne.celsiusWrite(VIRTUAL_CHANNEL1, sensors1.getTempCByIndex(0));
    Cayenne.celsiusWrite(VIRTUAL_CHANNEL2, sensors2.getTempCByIndex(0));
    Cayenne.celsiusWrite(VIRTUAL_CHANNEL3, sensors3.getTempCByIndex(0));
    Cayenne.celsiusWrite(VIRTUAL_CHANNEL4, sensors4.getTempCByIndex(0));
    Cayenne.celsiusWrite(VIRTUAL_CHANNEL5, sensors5.getTempCByIndex(0));

/*
// this is not working, as the signak goes high and low when ANY of the 3 actuators are toggled
//addition for control signal to ensure action on actuator pin  ---------------------------------------- 
//================================================================
if(digitalRead(RELAY_DIGITAL_PIN1) == LOW) {
    Cayenne.virtualWrite(V6, 1);
  }
else
  {
    Cayenne.virtualWrite(V6, 0);
  }  
//addition for control signal to ensure action on actuator pin  ---------------------------------------- 
//================================================================
*/

 }

}

//
// 7 = external hot water ciruit valve relè
// 8 =  water heater relè
// 9 =  hot water mixer relè

CAYENNE_IN(VIRTUAL_CHANNEL7)
{
  x = 0;// set default for actuators; on (=1) or OFF (=0)
  CAYENNE_LOG("Channel %u, value %s", request.channel, getValue.asString());
  //Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError("Error message");

  {
    x = getValue.asInt();// if all 3 actuators get setup as this, there will be incorrect funcions 
    //and channel 7 will turn ON or OFF acording to last channel tunred ON or OFF
 
    digitalWrite(RELAY_DIGITAL_PIN1, !x);
  }

  /*
   * other way to work (used for the other actuators) 
   * 
   * // get value sent from dashboard
    int currentValue7 = getValue.asInt(); // 7 to 9

    // assuming you wire your relay as normally open
    if (currentValue7 == 0) {
    digitalWrite(RELAY_DIGITAL_PIN1, HIGH);
    } else {
    digitalWrite(RELAY_DIGITAL_PIN1, LOW);
    }
  */
}

CAYENNE_IN(VIRTUAL_CHANNEL8)
{
    x = 0;// set default for actuators; on (=1) or OFF (=0)

  CAYENNE_LOG("Channel %u, value %s", request.channel, getValue.asString());
  //Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError("Error message");

  // get value sent from dashboard
    int currentValue8 = getValue.asInt(); // 7 to 9

    // assuming you wire your relay as normally open
    if (currentValue8 == 0) {
    digitalWrite(RELAY_DIGITAL_PIN1, HIGH);
    } else {
    digitalWrite(RELAY_DIGITAL_PIN1, LOW);
    }
  }

CAYENNE_IN(VIRTUAL_CHANNEL9)
{
    x = 0;// set default for actuators; on (=1) or OFF (=0)

  CAYENNE_LOG("Channel %u, value %s", request.channel, getValue.asString());
  //Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError("Error message");

  // get value sent from dashboard
      int currentValue9 = getValue.asInt(); // 7 to 9

    // assuming you wire your relay as normally open
    if (currentValue9 == 0) {
    digitalWrite(RELAY_DIGITAL_PIN1, HIGH);
    } else {
    digitalWrite(RELAY_DIGITAL_PIN1, LOW);
    }
  
}

//addition for online LED ---------------------------------------- 
//================================================================
CAYENNE_CONNECTED() {
onlineStatus = true;
}

CAYENNE_DISCONNECTED() {
onlineStatus = false;
}
//================================================================
//addition for online LED ----------------------------------------