The AD8232 sensor code general analog sensor to a virtual


#1

Hello everyone, I need help I can from issuing of general analog sensor to a virtual pin cayenne who I can give an example
pultroppo the sensor that I have to use not in the of cayenne
the AD8232 sensor code and very simple ache or read the documentation but but anything on the code and why or executed

//#define CAYENNE_DEBUG         // Uncomment to show debug messages
#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space
#include <CayenneEthernet.h>
#define VIRTUAL_PIN V1;
#define VIRTUAL_PIN 1
#define VIRTUAL_PIN1 V3
#define RELAY_DIGITAL_PIN 31


 

// Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
char token[] = "//////////";

float sensorValue1;

void setup()
{
  Serial.begin(9600);
  Cayenne.begin(token);
  pinMode(RELAY_DIGITAL_PIN, OUTPUT);

  pinMode(9, INPUT); // Setup for leads off detection LO +
  pinMode(6, INPUT); // Setup for leads off detection LO -
 
}
CAYENNE_IN(VIRTUAL_PIN)
{
  // get value sent from dashboard
  int currentValue = getValue.asInt(); // 0 to 1

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

void loop()
{
  Cayenne.run();

  int analogValue1 = analogRead(A0); // read the input on analog pin 0

  
  if((digitalRead(6) == 1)||(digitalRead(9) == 1)){
    Serial.println(0);
    
    delay(500);
  }
  else
  {
    // send the value of analog input 0:
      Serial.println(analogRead(A0));
     
      delay(500);
  }
  }

CAYENNE_OUT(VIRTUAL_PIN1)
{
  Cayenne.virtualWrite(VIRTUAL_PIN1, sensorValue1);
  delay(500);
}

#2

Hi @itec.impianti.elettr,

Welcome to the Cayenne community! You speak Italian??

@0lab may be able to help you here :slight_smile:

-B


#3

ok Thank you very much


#4

@itec.impianti.elettr,

From what I see, you never set sensorValue1 anywhere, so the dashboard will always see whatever it was set to by the compiler, probably 0.

I would replace this:
int analogValue1 = analogRead(A0); // read the input on analog pin 0

With this:

sensorValue1 =analogRead(A0):

Also, where you do another read in your print statement below, replace with sensorValue1.

Is this the behaviour you were expecting?

Cheers,

Craig


#5

in a nutshell, I have to remove “int”, to view the sensor data on pin Vitual ?

This code and the right to send data on pin Vitual v3 ?

CAYENNE_OUT (VIRTUAL_PIN1)
{
Cayenne.virtualWrite (VIRTUAL_PIN1, sensorValue1);
delay (500);
}

ok thanks, I try now


#6

Now I try to solve this


#7

The sensorValue1 is global because it is defined outside of a function. It doesn’t need to be float either. Make it an int.

analogValue1 is defined within that function, thus is not available externally.

Cheers,

Craig


#8

Solved the problem so it works, thanks for the advice

/*
Cayenne Ethernet Example

This sketch connects to the Cayenne server using an Arduino Ethernet Shield W5100
and runs the main communication loop.

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

Steps:

  1. Set the token variable to match the Arduino token from the Dashboard.
  2. Compile and upload this sketch.

For Cayenne Dashboard widgets using digital or analog pins this sketch will automatically
send data on those pins to the Cayenne server. If the widgets use Virtual Pins, data
should be sent to those pins using virtualWrites. Examples for sending and receiving
Virtual Pin data are under the Basics folder.
*/

//#define CAYENNE_DEBUG // Uncomment to show debug messages
#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
#include <CayenneEthernet.h>
#define VIRTUAL_PIN V1;

int sensore1 = A1;

// Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
char token[] = “xxxxxxx”;

void setup()
{
Serial.begin(9600);
Cayenne.begin(token);
pinMode(9, INPUT); // Setup for leads off detection LO +
pinMode(6, INPUT); // Setup for leads off detection LO -

}

void loop()
{
int SensorValue1 = analogRead (A0);

if((digitalRead(6) == 1)||(digitalRead(9) == 1)){
Serial.println(0);
Cayenne.virtualWrite(V1,0);

}
else{
// send the value of analog input 0:
Serial.println(analogRead(sensore1));
Cayenne.virtualWrite(V1,(SensorValue1));
}

delay(200);
Cayenne.run();

}