How include two PH sensors in cayenne?


#1

Hi there.

A few days ago, my friend Tad.dvor, helped me a include a PH sensor in cayenne. And now I would like add two ph sensors in cayenne.

I’m use this code below to add one sensor

//#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 SensorPin 0 //pH meter Analog output to Arduino Analog Input 0
#define Offset 0.00 //deviation compensate
unsigned long int avgValue; //Store the average value of the sensor feedback
float phValue;

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

void setup()
{
  Serial.begin(9600);
  Cayenne.begin(token);
}

void loop()
{
  Cayenne.run();

  int buf[10]; //buffer for read analog
  for(int i=0;i<10;i++) //Get 10 sample value from the sensor for smooth the value
  {
    buf[i]=analogRead(SensorPin);
    delay(10);
  }
  for(int i=0;i<9;i++) //sort the analog from small to large
  {
    for(int j=i+1;j<10;j++)
     {
        if(buf[i]>buf[j])
        {
          int temp=buf[i];
          buf[i]=buf[j];
          buf[j]=temp;
        }
     }
  }
  
  avgValue=0;
  for(int i=2;i<8;i++) //take the average value of 6 center sample
  avgValue+=buf[i];
  phValue=(float)avgValue*5.0/1024/6; //convert the analog into millivolt
  phValue=3.5*phValue+Offset; //convert the millivolt into pH value  
}

CAYENNE_OUT(V0)
{
  Cayenne.virtualWrite(V0, phValue);
} 

And I was trying this code to add two sensor

   //#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 V0
#define VIRTUAL_PINB V1

#define SensorPin 0 //pH meter Analog output to Arduino Analog Input 0
unsigned long int avgValue; 
#define Offset 0.00 //deviation compensate
float phValue;

//Sobre sensor B
#define SensorPinB 2 //pH meter Analog output to Arduino Analog Input 0
unsigned long int avgValueB; 
#define OffsetB 0.00 //deviation compensate
float phValueB;




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

void setup()
{
  Serial.begin(9600);
  Cayenne.begin(token);
}

void loop()
{
{
  Cayenne.run();

  int buf[10]; //buffer for read analog
  for(int i=0;i<10;i++) //Get 10 sample value from the sensor for smooth the value
  {
    buf[i]=analogRead(SensorPin);
    delay(10);
  }
  for(int i=0;i<9;i++) //sort the analog from small to large
  {
    for(int j=i+1;j<10;j++)
     {
        if(buf[i]>buf[j])
        {
          int temp=buf[i];
          buf[i]=buf[j];
          buf[j]=temp;


        
         avgValue=0;
  for(int i=2;i<8;i++) //take the average value of 6 center sample
  avgValue+=buf[i];
  phValue=(float)avgValue*5.0/1024/6; //convert the analog into millivolt
  phValue=3.5*phValue+Offset; //convert the millivolt into pH value  
}
        
        }
        
     }
  }

 
  Cayenne.run();

  int buf[10]; //buffer for read analog
  for(int i=0;i<10;i++) //Get 10 sample value from the sensor for smooth the value
  {
    buf[i]=analogRead(SensorPinB);
    delay(10);
  }
  for(int i=0;i<9;i++) //sort the analog from small to large
  {
    for(int j=i+1;j<10;j++)
     {
        if(buf[i]>buf[j])
        {
          int temp=buf[i];
          buf[i]=buf[j];
          buf[j]=temp;
  
  avgValueB=0;
  for(int i=2;i<8;i++) //take the average value of 6 center sample
  avgValueB+=buf[i];
  phValueB=(float)avgValueB*5.0/1024/6; //convert the analog into millivolt
  phValueB=3.5*phValueB+OffsetB; //convert the millivolt into pH value  
}
  
        }
     }
  }

CAYENNE_OUT(V0)
{
  Cayenne.virtualWrite(V0, phValue);
}

CAYENNE_OUT(V1)
{
  Cayenne.virtualWrite(V1, phValueB);
}

But the second sensor give-me absurd values.

Thank you.


#2

Try this:

//#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 SensorPin0 A0 //pH meter Analog output to Arduino Analog Input 0
#define SensorPin1 A1 //pH meter Analog output to Arduino Analog Input 1
#define Offset 0.00 //deviation compensate
unsigned long int avgValue0; //Store the average value of the sensor feedback
unsigned long int avgValue1; //Store the average value of the sensor feedback
float phValue0;
float phValue1;

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

void setup()
{
  Serial.begin(9600);
  Cayenne.begin(token);
}

void loop()
{
  Cayenne.run();

  int buf[10]; //buffer for read analog
  for(int i=0;i<10;i++) //Get 10 sample value from the sensor for smooth the value
  {
    buf[i]=analogRead(SensorPin0);
    delay(10);
  }
  for(int i=0;i<9;i++) //sort the analog from small to large
  {
    for(int j=i+1;j<10;j++)
     {
        if(buf[i]>buf[j])
        {
          int temp=buf[i];
          buf[i]=buf[j];
          buf[j]=temp;
        }
     }
  }
  avgValue0=0;
  for(int i=2;i<8;i++) //take the average value of 6 center sample
  avgValue0+=buf[i];
  phValue0=(float)avgValue0*5.0/1024/6; //convert the analog into millivolt
  phValue0=3.5*phValue0+Offset; //convert the millivolt into pH value  


  buf[10]; //buffer for read analog
  for(int i=0;i<10;i++) //Get 10 sample value from the sensor for smooth the value
  {
    buf[i]=analogRead(SensorPin1);
    delay(10);
  }
  for(int i=0;i<9;i++) //sort the analog from small to large
  {
    for(int j=i+1;j<10;j++)
     {
        if(buf[i]>buf[j])
        {
          int temp=buf[i];
          buf[i]=buf[j];
          buf[j]=temp;
        }
     }
  }
  avgValue1=0;
  for(int i=2;i<8;i++) //take the average value of 6 center sample
  avgValue1+=buf[i];
  phValue1=(float)avgValue1*5.0/1024/6; //convert the analog into millivolt
  phValue1=3.5*phValue1+Offset; //convert the millivolt into pH value  
}

CAYENNE_OUT(V0)
{
  Cayenne.virtualWrite(V0, phValue0);
}
CAYENNE_OUT(V1)
{
  Cayenne.virtualWrite(V1, phValue1);
}

#3

I’m appreciate. Thank’s again Tad.dvor. :thumbsup: