BLYNK
BLYNK.IO       📲 GETTING STARTED       📗 DOCS       👉 SKETCH BUILDER

How to add codes of ph sensor and ec sensor using esp32 and blynk 2.0

I am having trouble with adding EC sensor codes to PH sensor codes. I don’t know have enough knowledge to code these two sensors. I need help on how to include the EC sensor code to PH sensor code. And also what should not be included in these codes. This EC sensor code does not include temperature sensor and it does not have blynk codes. The PH sensor code has the blynk codes.

This is the code of EC sensor:

#define TdsSensorPin 
#define VREF 3.3              // 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;
int copyIndex = 0;

float averageVoltage = 0;
float tdsValue = 0;
float temperature = 25;       // current temperature for compensation

// median filtering algorithm
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;
}

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(35);    //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];
      
      // read the analog value more stable by the median filtering algorithm, and convert to voltage value
      averageVoltage = getMedianNum(analogBufferTemp,SCOUNT) * (float)VREF / 1024.0;
      
      //temperature compensation formula: fFinalResult(25^C) = fFinalResult(current)/(1.0+0.02*(fTP-25.0)); 
      float compensationCoefficient = 1.0+0.02*(temperature-25.0);
      //temperature compensation
      float compensationVoltage=averageVoltage/compensationCoefficient;
      
      //convert voltage value to tds value
      tdsValue=(133.42*compensationVoltage*compensationVoltage*compensationVoltage - 255.86*compensationVoltage*compensationVoltage + 857.39*compensationVoltage)*0.5;
      //Serial.print("voltage:");
      //Serial.print(averageVoltage,2);
      //Serial.print("V   ");
      Serial.print("TDS Value:");
      Serial.print(tdsValue,0);
      Serial.println("ppm");
    }
  }
}

This is the code of PH sensor with blynk:

#define BLYNK_TEMPLATE_ID ""
#define BLYNK_DEVICE_NAME ""

#define BLYNK_FIRMWARE_VERSION "0.1.0"
#define BLYNK_PRINT Serial
#include "BlynkEdgent.h"

// ph sensor
#define pHSensorPin 34          //pH meter Analog output to Arduino Analog Input 
unsigned long int avgValue;    //Store the average value of the sensor feedback
float b;
int buf[10],temp;

int f;  // for float value to string converstion
float val; // also works with double. 
char buff2[10];
String valueString = "";
String Value = ""; 

// ph sensor
void PH_Value()
{
  for(int i=0;i<10;i++)       //Get 10 sample value from the sensor for smooth the value
  { 
    buf[i]=analogRead(34);
    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])
      {
        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];
  float phValue=(float)avgValue*3.3/1024/6; //convert the analog into millivolt
  phValue=3.5*phValue;                      //convert the millivolt into pH value
  
  Value =  dtostrf(phValue, 4, 2, buff2);  //4 is mininum width, 6 is precision
  Serial.print(Value);
  valueString = "";
  delay(1000);
  Blynk.virtualWrite(V0, 7.06);
}

void setup()
{
  Serial.begin(9600);
  BlynkEdgent.begin();
  delay(2000); 
}

void loop() 
{
  BlynkEdgent.run();
  PH_Value();
}

First of all, you need to stop doing this in your void loop…

and this in your PH_Value function…

Read this for more info (In your case your BlynkEdgent.run() replaces Blynk.run() in this article)…

https://docs.blynk.io/en/legacy-platform/legacy-articles/keep-your-void-loop-clean

As far as your new sketch is concerned, you might be unlucky if you want to read your EC sensor every 40ms whilst doing everything else…

and you certainly can’t write the results to the Blynk server every 40ms, as you’ll flood the server.

Maybe a bit of info on what you are trying to achieve would be good.

Pete.

What should I do? What should I add in these codes?

It’s all explained in the “keep your void loop clean” link I provided.

Have you read it?

Pete.

Can you check this code sir? Thank you

#define BLYNK_TEMPLATE_ID ""
#define BLYNK_DEVICE_NAME ""

#define BLYNK_FIRMWARE_VERSION "0.1.0"
#define BLYNK_PRINT Serial
#define USE_NODE_MCU_BOARD
#include "BlynkEdgent.h"

BlynkTimer timer;

void PH_Value()
{
  for(int i=0;i<10;i++)       //Get 10 sample value from the sensor for smooth the value
  { 
    buf[i]=analogRead(GPIO34);
    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])
      {
        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];
  float phValue=(float)avgValue*3.3/1024/6; //convert the analog into millivolt
  phValue=3.5*phValue;                      //convert the millivolt into pH value
  
  Value =  dtostrf(phValue, 4, 2, buff2);  //4 is mininum width, 6 is precision
  valueString = valueString + Value +","; 
  Serial.print(Value);
  Serial.print("    ");
  Serial.println(valueString);
  valueString = "";
  Blynk.virtualWrite(V0, sensorValue1);
}

void setup() {
  Serial.begin(115200);
  BlynkEdgent.begin();
}

void loop() {
  BlynkEdgent.run;
  Blynk.run();
  timer.run();
  PH_Value();
}

As I explained, you shouldn’t have

in your void loop…

Secondly, this needs to come out of your void loop…

and be called with a timer. You need to define that timer in your void setup, and you haven’t done that.

Re-read the article in the link, but this time try to understand it, and how it relates to your sketch.

Pete.

Hello sir, please check again thank you

#define BLYNK_TEMPLATE_ID ""
#define BLYNK_DEVICE_NAME ""

#define BLYNK_FIRMWARE_VERSION "0.1.0"
#define BLYNK_PRINT Serial
#include "BlynkEdgent.h"

BlynkTimer timer;

unsigned long int avgValue;  //Store the average value of the sensor feedback
float b;
int buf[10],temp;


int f;  // for float value to string converstion
float val; // also works with double. 
char buff2[10];
String valueString = "";
String Value = ""; 

void sensorDataSend()
{
  for(int i=0;i<10;i++)       //Get 10 sample value from the sensor for smooth the value
  { 
    buf[i]=analogRead(GPIO34);
    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])
      {
        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];
  float phValue=(float)avgValue*3.3/1024/6; //convert the analog into millivolt
  phValue=3.5*phValue;                      //convert the millivolt into pH value
  
  Value =  dtostrf(phValue, 4, 2, buff2);  //4 is mininum width, 6 is precision
  valueString = valueString + Value +","; 
  Serial.print(Value);
  Serial.print("    ");
  Serial.println(valueString);
  valueString = "";
  Blynk.virtualWrite(V0, );
}

void setup() {
  Serial.begin(115200);
  BlynkEdgent.begin();
  timer.setInterval(1000L, sensorDataSend); //timer will run every sec 
}

void loop() {
  BlynkEdgent.run;
  timer.run();
}

Looks better, does it work?

Pete.

This is now the error

BlynkEdgent.h: No such file or directory

I guess you’ve just copied the .ino file from somewhere, and not taken the full Edgent example sketch with all of its associated.h files?

Pete.

Yes you are right sir. How do I get that?

If you’re using the Arduino IDE with the latest Blynk library installed then go to:
File > Examples > Blynk > Blynk.Edgent > Edgent_ESP32

Replace the initial .ino tab with your code and save the project then try compiling it.

Pete.

Hello Pete, after I follow what you said this is what the codes now. I’ll edit these codes under Edgent_ESP32?

// Fill-in information from your Blynk Template here
//#define BLYNK_TEMPLATE_ID           "TMPLxxxxxx"
//#define BLYNK_DEVICE_NAME           "Device"

#define BLYNK_FIRMWARE_VERSION        "0.1.0"

#define BLYNK_PRINT Serial
//#define BLYNK_DEBUG

#define APP_DEBUG

// Uncomment your board, or configure a custom board in Settings.h
//#define USE_WROVER_BOARD
//#define USE_TTGO_T7
//#define USE_ESP32C3_DEV_MODULE
//#define USE_ESP32S2_DEV_KIT

#include "BlynkEdgent.h"

void setup()
{
  Serial.begin(115200);
  delay(100);

  BlynkEdgent.begin();
}

void loop() {
  BlynkEdgent.run();
}

Pete.

‘GPIO34’ was not declared in this scope
This is the error sir

What board type did you choose in the IDE?

Pete.

This syntax is incorrect. it should be buf[i]=analogRead(34); if you wnat to read pin 34.

Pete.

What should I put after V0, ?

I’d suggest you read the Blynk documentation.

Pete.

This is now the error sir

C:\Users\ADDESSA\Documents\Arduino\Edgent_ESP32\Edgent_ESP32.ino: In function 'void loop()':
Edgent_ESP32:63:15: error: invalid use of non-static member function 'void Edgent::run()'
   BlynkEdgent.run;
   ~~~~~~~~~~~~^~~
In file included from C:\Users\ADDESSA\Documents\Arduino\Edgent_ESP32\Edgent_ESP32.ino:7:
C:\Users\ADDESSA\Documents\Arduino\Edgent_ESP32\BlynkEdgent.h:97:8: note: declared here
   void run() {
        ^~~
Multiple libraries were found for "WiFi.h"
 Used: C:\Users\ADDESSA\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.5\libraries\WiFi
 Not used: C:\Program Files (x86)\Arduino\libraries\WiFi
exit status 1
invalid use of non-static member function 'void Edgent::run()'

This is the code that I uploaded to my board ESP32-WROOM-DA-Module


#define BLYNK_TEMPLATE_ID ""
#define BLYNK_DEVICE_NAME ""

#define BLYNK_FIRMWARE_VERSION "0.1.0"
#define BLYNK_PRINT Serial
#include "BlynkEdgent.h"

BlynkTimer timer;

unsigned long int avgValue;  //Store the average value of the sensor feedback
float b;
int buf[10],temp;


int f;  // for float value to string converstion
float val; // also works with double. 
char buff2[10];
String valueString = "";
String Value = ""; 

void sensorDataSend()
{
  for(int i=0;i<10;i++)       //Get 10 sample value from the sensor for smooth the value
  { 
    buf[i]=analogRead(34);
    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])
      {
        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];
  float phValue=(float)avgValue*3.3/1024/6; //convert the analog into millivolt
  phValue=3.5*phValue;                      //convert the millivolt into pH value
  
  Value =  dtostrf(phValue, 4, 2, buff2);  //4 is mininum width, 6 is precision
  valueString = valueString + Value +","; 
  Serial.print(Value);
  Serial.print("    ");
  Serial.println(valueString);
  valueString = "";
  Blynk.virtualWrite(V0, 7.06);
}

void setup() {
  Serial.begin(115200);
  BlynkEdgent.begin();
  timer.setInterval(1000L, sensorDataSend); //timer will run every sec 
}

void loop() {
  BlynkEdgent.run;
  timer.run();
}