Temperature and pH sensor blynk app

hello, i need help with Blynk app here, I connect my two sensors which is temperature and ph sensor to esp32 dev board. the problem is my blynk app keeps telling me the value is 1 until 999 and so on for both sensor. but when i click on serial monitor at arduino ide the reading is ok. what is the problem? is it because of coding?

here the coding:


#define BLYNK_PRINT Serial


#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "MXTXejFHbSqqRltFZA4kPbV1FT-Tk5Cp";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Huawei Nova 5T";
char pass[] = "breakdance12345";


#include "DFRobot_ESP_PH.h"
#include "EEPROM.h"
#include <OneWire.h>
#include <DallasTemperature.h>

#define ONE_WIRE_BUS 4
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DeviceAddress insideThermometer;

DFRobot_ESP_PH ph;
#define ESPADC 4096.0   //the esp Analog Digital Convertion value
#define ESPVOLTAGE 3300 //the esp voltage supply value
#define PH_PIN 35    //the esp gpio data pin number

float voltage, phValue, temperature = 25;

BlynkTimer timer;

// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void myTimerEvent()
{
  //add your code here to get the temperature from your temperature sensor
  // call sensors.requestTemperatures() to issue a global temperature 
  // request to all devices on the bus
  Serial.print("Requesting temperatures...");
  sensors.requestTemperatures(); // Send the command to get temperatures
  Serial.println("DONE");
  // After we got the temperatures, we can print them here.
  // We use the function ByIndex, and as an example get the temperature from the first sensor only.
  Serial.print("Temperature is: ");
  Serial.println(sensors.getTempCByIndex(0));
  Blynk.virtualWrite(V5, millis() / 1000);
}
void myTimerEvent2()
{
  static unsigned long timepoint = millis();
  if (millis() - timepoint > 1000U) //time interval: 1s
  {
    timepoint = millis();
    //voltage = rawPinValue / esp32ADC * esp32Vin
    voltage = analogRead(PH_PIN) / ESPADC * ESPVOLTAGE; // read the voltage
    
    
    
    //temperature = readTemperature();  // read your temperature sensor to execute temperature compensation
    
    

    phValue = ph.readPH(voltage, temperature); // convert voltage to pH with temperature compensation
    Serial.print("pH:");
    Serial.println(phValue, 4);
  }
  ph.calibration(voltage, temperature); // calibration process by Serail CMD
   Blynk.virtualWrite(V6, millis() / 1000);
}

void setup()
{
  // Debug console
  Serial.begin(115200);
  EEPROM.begin(32);//needed to permit storage of calibration value in eeprom
  ph.begin();
  sensors.begin();

  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);

  // Setup a function to be called every second
  timer.setInterval(1000L, myTimerEvent);
  timer.setInterval(1000L, myTimerEvent2);
}

void loop()
{
  Blynk.run();
  timer.run(); // Initiates BlynkTimer
}

The only data that you are sending to Blynk is the number of milliseconds that have elapsed since the device booted, divided by 1000.

Where you do a serial print of phValue you should follow this with a Blynk.virtualWrite of phValue to your chosen virtual pin.

You’re handling the temperature side of things slightly differently, and would be better to do the same thing that you do with PH. This means storing the reading in the temperature variable that you’ve already declared, then use this when you do your serial print and Blynk.virtualWrite.

Pete.

Thank you for reply, in pH do you mean change it to this?

void myTimerEvent2()
{
  static unsigned long timepoint = millis();
  if (millis() - timepoint > 1000U) //time interval: 1s
  {
    timepoint = millis();
    //voltage = rawPinValue / esp32ADC * esp32Vin
    voltage = analogRead(PH_PIN) / ESPADC * ESPVOLTAGE; // read the voltage
    phValue = ph.readPH(voltage, temperature); // convert voltage to pH with temperature compensation
    Serial.print("pH:");
    Serial.println(phValue, 4);
    Blynk.virtualWrite(V6, millis() / 1000);
  }
  ph.calibration(voltage, temperature); // calibration process by Serail CMD
   
}

i do not really understand the use of virual read or write actually. sorry i am new. hoping that you can help me out.

Why this? This will only display millis if you have initiated it divided by 1000.

Blynk.virtualWrite(V6, phValue);

Thanks you so much. i appreciate it. now my pH is working in the app.

now i change my temp coding and it didnt work. it keep getting -127. any solution?

{ 
  // Send the command to get temperatures
  sensors.requestTemperatures(); 

  //print the temperature in Celsius
  Serial.print("Temperature: ");
  Serial.print(sensors.getTempCByIndex(0));
  Serial.print((char)176);//shows degrees character
  Serial.print("C  |  ");
  
  //print the temperature in Fahrenheit
  Serial.print((sensors.getTempCByIndex(0) * 9.0) / 5.0 + 32.0);
  Serial.print((char)176);//shows degrees character
  Serial.println("F");
   Blynk.virtualWrite(V5, sensors.getTempCByIndex(0));
  
}

Okay, this was your original code for taking temperature readings, after removing the comments:

void myTimerEvent()
{
  Serial.print("Requesting temperatures...");
  sensors.requestTemperatures(); // Send the command to get temperatures
  Serial.println("DONE");
  Serial.print("Temperature is: ");
  Serial.println(sensors.getTempCByIndex(0));
  Blynk.virtualWrite(V5, millis() / 1000);
}

The bit of code inside the serial print statement that says sensors.getTempCByIndex(0) is where the temperature value is obtained.

Rather than outputting it directly to the serial monitor as your original code does, we want to store it in the variable called temperature that you declared earlier…

float voltage, phValue, temperature = 25;
void myTimerEvent()
{
  Serial.print("Requesting temperatures...");
  sensors.requestTemperatures(); // Send the command to get temperatures
  Serial.println("DONE");
  temperature = sensors.getTempCByIndex(0); // Save the reading to use later
  Serial.print("Temperature is: ");
  Serial.println(temperature); // print the reading that we just took
  Blynk.virtualWrite(V5, temperature); // and send the same reading to virtual pin 5
}

Does this make sense?

By the way, you are assigning a value of 25 to your temperature variable when you initialise it. If you don’t specify a value - as is the case with the voltage (not sure what you’re using this for) and phValue variables - then they will initialise with a value of zero.
I think a value of zero is more useful in this situation, because you aren’t likely to mistake it for an actual reading (unless the probe is in a freezer), should the process of reading the temperature probe fail to work correctly.

Pete.

I found when I tried this it did not work. I created a variable for the tempRead which was assigned to sensors.getTempCByIndex(0)

float tempRead = sensors.getTempCByIndex(0);
Blynk.virtualWrite (V5, tempRead);

ya you are right. ohh i forgot to delete the voltage code since i am not using voltage. btw, i already tried your code and it’s still -127.

yup same here. change it with no luck

Post your latest code, and details of what widgets you have set up in your app.

Pete.

Also how have you attached you temp sensor?

it’s your code. here temp code that i am using latest

{
  
  Serial.print("Requesting temperatures...");
  sensors.requestTemperatures(); // Send the command to get temperatures
  Serial.println("DONE");
  temperature = sensors.getTempCByIndex(0); // Save the reading to use later
  Serial.print("Temperature is: ");
  Serial.println(temperature); // print the reading that we just took
  Blynk.virtualWrite(V5, temperature); // and send the same reading to virtual pin 5
  
}

and here is the widget i am using.

do you mean my board? if you mean by board, then i am connecting to esp32 dev board. and i attached my 4.7k ohm resistor because dallas temperature need it. i guess.

I meant the FULL code.

Pete.

-127 is the generic “device not working” message provided by the sensor library. Basically, if there is a physical or electrical issue with your sensor or hookup, any code will provide the same “error” message of -127 for that sensor type.

actually after i tried coding with this code, then i get error -127. but if i revert back to basic code which is no blynk code it work perfectly.

{ 
  // Send the command to get temperatures
  sensors.requestTemperatures(); 

  //print the temperature in Celsius
  Serial.print("Temperature: ");
  Serial.print(sensors.getTempCByIndex(0));
  Serial.print((char)176);//shows degrees character
  Serial.print("C  |  ");
  
  //print the temperature in Fahrenheit
  Serial.print((sensors.getTempCByIndex(0) * 9.0) / 5.0 + 32.0);
  Serial.print((char)176);//shows degrees character
  Serial.println("F");
   Blynk.virtualWrite(V5, sensors.getTempCByIndex(0));
  
}

oh sorry. here full code:

#define BLYNK_PRINT Serial


#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "MXTXejFHbSqqRltFZA4kPbV1FT-Tk5Cp";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Huawei Nova 5T";
char pass[] = "breakdance12345";


#include "DFRobot_ESP_PH.h"
#include "EEPROM.h"
#include <OneWire.h>
#include <DallasTemperature.h>

#define ONE_WIRE_BUS 4
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DeviceAddress insideThermometer;

DFRobot_ESP_PH ph;
#define ESPADC 4096.0   //the esp Analog Digital Convertion value
#define ESPVOLTAGE 3300 //the esp voltage supply value
#define PH_PIN 35    //the esp gpio data pin number

float voltage, phValue, temperature = 25;

BlynkTimer timer;

// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void myTimerEvent()
{
  
  Serial.print("Requesting temperatures...");
  sensors.requestTemperatures(); // Send the command to get temperatures
  Serial.println("DONE");
  temperature = sensors.getTempCByIndex(0); // Save the reading to use later
  Serial.print("Temperature is: ");
  Serial.println(temperature); // print the reading that we just took
  Blynk.virtualWrite(V5, temperature); // and send the same reading to virtual pin 5
  
}
void myTimerEvent2()
{
  static unsigned long timepoint = millis();
  if (millis() - timepoint > 1000U) //time interval: 1s
  {
    timepoint = millis();
    //voltage = rawPinValue / esp32ADC * esp32Vin
    voltage = analogRead(PH_PIN) / ESPADC * ESPVOLTAGE; // read the voltage
    phValue = ph.readPH(voltage, temperature); // convert voltage to pH with temperature compensation
    Serial.print("pH:");
    Serial.println(phValue, 4);
    Blynk.virtualWrite(V6, phValue);
  }
  ph.calibration(voltage, temperature); // calibration process by Serail CMD
   
}

void setup()
{
  // Debug console
  Serial.begin(115200);
  EEPROM.begin(32);//needed to permit storage of calibration value in eeprom
  ph.begin();
  sensors.begin();

  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);

  // Setup a function to be called every second
  timer.setInterval(1000L, myTimerEvent);
  timer.setInterval(1000L, myTimerEvent2);
}

void loop()
{
  Blynk.run();
  timer.run(); // Initiates BlynkTimer
}

Okay, so previously you said that the correct temperature and humidity readings were shown in the serial monitor.
With this revised code, does the serial monitor show -127, or does it show the correct value?

If you revert to your original code then does it show the correct temperature reading in the serial monitor?

Have you double-checked the connections to your temperature probe?

Pete.

ok will updated to you soon

i tried rerun with this code and it actually working. the temp and ph sensor now is reading in serial and blynk app.

#define BLYNK_PRINT Serial


#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "MXTXejFHbSqqRltFZA4kPbV1FT-Tk5Cp";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Huawei Nova 5T";
char pass[] = "breakdance12345";


#include "DFRobot_ESP_PH.h"
#include "EEPROM.h"
#include <OneWire.h>
#include <DallasTemperature.h>

#define ONE_WIRE_BUS 4
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DeviceAddress insideThermometer;

DFRobot_ESP_PH ph;
#define ESPADC 4096.0   //the esp Analog Digital Convertion value
#define ESPVOLTAGE 3300 //the esp voltage supply value
#define PH_PIN 35    //the esp gpio data pin number

float voltage, phValue, temperature = 25;

BlynkTimer timer;

// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void myTimerEvent()
{
  
  Serial.print("Requesting temperatures...");
  sensors.requestTemperatures(); // Send the command to get temperatures
  Serial.println("DONE");
  temperature = sensors.getTempCByIndex(0); // Save the reading to use later
  Serial.print("Temperature is: ");
  Serial.println(temperature); // print the reading that we just took
  Blynk.virtualWrite(V5, temperature); // and send the same reading to virtual pin 5
  
}
void myTimerEvent2()
{
  static unsigned long timepoint = millis();
  if (millis() - timepoint > 1000U) //time interval: 1s
  {
    timepoint = millis();
    //voltage = rawPinValue / esp32ADC * esp32Vin
    voltage = analogRead(PH_PIN) / ESPADC * ESPVOLTAGE; // read the voltage
    phValue = ph.readPH(voltage, temperature); // convert voltage to pH with temperature compensation
    Serial.print("pH:");
    Serial.println(phValue, 4);
    Blynk.virtualWrite(V6, phValue);
  }
  ph.calibration(voltage, temperature); // calibration process by Serail CMD
   
}

void setup()
{
  // Debug console
  Serial.begin(115200);
  EEPROM.begin(32);//needed to permit storage of calibration value in eeprom
  ph.begin();
  sensors.begin();

  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);

  // Setup a function to be called every second
  timer.setInterval(1000L, myTimerEvent);
  timer.setInterval(1000L, myTimerEvent2);
}

void loop()
{
  Blynk.run();
  timer.run(); // Initiates BlynkTimer
}

now my new problem is ph sensor keep giving me negative value.