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.