Need help in adding Turbidity and TDS Sensor

hi all,

I’m a complete noob with cayenne but managed to follow some of the listed code that has been shared in this community. first of all, I’m sorry for my broken English, since I’m not fluent in English.

I’m doing a water monitoring system for rearing tilapia fish. in this project I will be using:

  1. ph sensor
  2. ds18b20 temperature sensor
  3. DHT 22 sensor
  4. turbidity sensor
  5. TDS sensor

all of this sensor is working fine when in Arduino but I would like to make use of cayenne to monitor all this sensor.

I managed to connect the ds18b20 sensor and dht22 sensor to raspberry pi and deploy it to the cayenne dashboard. thanks to all the code that has been shared.

I use Arduino UNO for my turbidity sensor and TDS sensor and connect Arduino thru serial USB connection since I can’t connect thru the wifi shield attached to the UNO.

I managed to get connected to my UNO but no widget appears. below is my code:

#include <CayenneMQTTSerial.h>

#define senseInput A1 //Set to A0 as Analog Read
int senseRawValue; //Some variable
float senseTurbidity; //Some floating variable

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username = “xxxxx”;
char password = “xxxxx”;
char clientID = “xxxxxx”;

void setup()
{
//Baud rate can be specified by calling Cayenne.begin(username, password, clientID, 9600);
Serial.begin(9600); //Set as serial communication baud rate 9600
Serial.println(" 14CORE | TURBIDITY SENSING ANALOG TEST CODE ");
Serial.println(“Initializing…”);
delay(4000);
Cayenne.begin(username, password, clientID);
}

void loop() {
Cayenne.loop();
senseRawValue = analogRead(senseInput); //Read input raw value fromt the sensor
senseTurbidity = senseRawValue * (5.0 / 1024.0); //Convert analog data from 0 -1024 to voltage 0 - 5v;
Serial.println("TURBIDITY VALUE > "); //Print the output data to the serial
Serial.println(senseTurbidity);
delay(500);
}

// Default function for sending sensor data at intervals to Cayenne.
// You can also use functions for specific channels, e.g CAYENNE_OUT(1) for sending channel 1 data.
CAYENNE_OUT_DEFAULT()
{
// Write data to Cayenne here. This example just sends the current uptime in milliseconds on virtual channel 0.
Cayenne.virtualWrite(1, senseTurbidity);
// Some examples of other functions you can use to send data.
//Cayenne.celsiusWrite(1, 22.0);
//Cayenne.luxWrite(2, 700);
//Cayenne.virtualWrite(3, 50, TYPE_PROXIMITY, UNIT_CENTIMETER);
}

the original code for my turbidity sensor in Arduino is below:

#define senseInput A1 //Set to A0 as Analog Read
int senseRawValue; //Some variable
float senseTurbidity; //Some floating variable

void setup(){
Serial.begin(9600); //Set as serial communication baud rate 9600
Serial.println(" 14CORE | TURBIDITY SENSING ANALOG TEST CODE ");
Serial.println(“Initializing…”);
delay(4000);
}
void loop(){
senseRawValue = analogRead(senseInput); //Read input raw value fromt the sensor
senseTurbidity = senseRawValue * (5.0 / 1024.0); //Convert analog data from 0 -1024 to voltage 0 - 5v;
Serial.println("TURBIDITY VALUE > "); //Print the output data to the serial
Serial.println(senseTurbidity);
delay(500);

}

and the code for my TDS is below:

#define TdsSensorPin A3
#define VREF 5.0 // analog reference voltage(Volt) of the ADC
#define SCOUNT 30 // sum of sample point
int analogBuffer[SCOUNT]; // store the analog value in the array, read from ADC
int analogBufferTemp[SCOUNT];
int analogBufferIndex = 0,copyIndex = 0;
float averageVoltage = 0,tdsValue = 0,temperature = 25;

void setup()
{
Serial.begin(115200);
pinMode(TdsSensorPin,INPUT);
}

void loop()
{
static unsigned long analogSampleTimepoint = millis();
if(millis()-analogSampleTimepoint > 40U) //every 40 milliseconds,read the analog value from the ADC
{
analogSampleTimepoint = millis();
analogBuffer[analogBufferIndex] = analogRead(TdsSensorPin); //read the analog value and store into the buffer
analogBufferIndex++;
if(analogBufferIndex == SCOUNT)
analogBufferIndex = 0;
}
static unsigned long printTimepoint = millis();
if(millis()-printTimepoint > 800U)
{
printTimepoint = millis();
for(copyIndex=0;copyIndex<SCOUNT;copyIndex++)
analogBufferTemp[copyIndex]= analogBuffer[copyIndex];
averageVoltage = getMedianNum(analogBufferTemp,SCOUNT) * (float)VREF / 1024.0; // read the analog value more stable by the median filtering algorithm, and convert to voltage value
float compensationCoefficient=1.0+0.02*(temperature-25.0); //temperature compensation formula: fFinalResult(25^C) = fFinalResult(current)/(1.0+0.02*(fTP-25.0));
float compensationVolatge=averageVoltage/compensationCoefficient; //temperature compensation
tdsValue=(133.42compensationVolatgecompensationVolatgecompensationVolatge - 255.86compensationVolatgecompensationVolatge + 857.39compensationVolatge)*0.5; //convert voltage value to tds value
//Serial.print(“voltage:”);
//Serial.print(averageVoltage,2);
//Serial.print("V ");
Serial.print(“TDS Value:”);
Serial.print(tdsValue,0);
Serial.println(“ppm”);
}
}
int getMedianNum(int bArray, int iFilterLen)
{
int bTab[iFilterLen];
for (byte i = 0; i<iFilterLen; i++)
bTab[i] = bArray[i];
int i, j, bTemp;
for (j = 0; j < iFilterLen - 1; j++)
{
for (i = 0; i < iFilterLen - j - 1; i++)
{
if (bTab[i] > bTab[i + 1])
{
bTemp = bTab[i];
bTab[i] = bTab[i + 1];
bTab[i + 1] = bTemp;
}
}
}
if ((iFilterLen & 1) > 0)
bTemp = bTab[(iFilterLen - 1) / 2];
else
bTemp = (bTab[iFilterLen / 2] + bTab[iFilterLen / 2 - 1]) / 2;
return bTemp;
}

my question is how I want to add this code correctly in the cayenne code. I know there must be something wrong with the code that I upload in cayenne since no widget is detected. thank you in advance for your help.

this is not best way for beginner as it is very difficult to debug and point out the issue. Also the code intro clearly mention not to any serial print lines in the code. As it will conflict with the serial communication of arduino and raspberry. So you can either but an ethernet shield or best option is get an esp8266 development board like nodemcu.

Thank you so much for your suggestion. Manage to add in the sensor using your suggestion.