Three ds18b20 with blynk

hello everyone, I have three ds18b20 connected to onewire on my esp 32.
I would like to post these values on blynk on three virtual pin.
How to integrate “Blynk.virtualWrite” into my code ?

// Send command to all the sensors for temperature conversion
  sensors.requestTemperatures(); 
  
  // Display temperature from each sensor
  for (int i = 0;  i < deviceCount;  i++)
  {
    Serial.print("Sensor ");
    Serial.print(i+1);
    Serial.print(" : ");
    tempC = sensors.getTempCByIndex(i);
    Serial.print(tempC);
    Serial.print((char)176);//shows degrees character
    Serial.print("C  |  ");

If the virtual pins are 0, 1 and 2 then …

Blynk.virtualWrite(i,tempC)

If you want to use different virtual pins, say 20, 21 & 22 then …
Blynk.virtualWrite(i+20,tempC)

Pete.

why doesn’t work ?

#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <OneWire.h>
#include <DallasTemperature.h>


char auth[] = "xxxxxxx";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "xxxxx";
char pass[] = "xxxxx";
BlynkTimer timer;

// Data wire is plugged into digital pin 2 on the Arduino
#define ONE_WIRE_BUS 23

// Setup a oneWire instance to communicate with any OneWire device
OneWire oneWire(ONE_WIRE_BUS);  

// Pass oneWire reference to DallasTemperature library
DallasTemperature sensors(&oneWire);

int deviceCount = 0;
float tempC;

void setup(void)
{
  Blynk.begin(auth, ssid, pass);
  sensors.begin();  // Start up the library
  Serial.begin(9600);
  
  // locate devices on the bus
  Serial.print("Locating devices...");
  Serial.print("Found ");
  deviceCount = sensors.getDeviceCount();
  Serial.print(deviceCount, DEC);
  Serial.println(" devices.");
  Serial.println("");

  timer.setInterval(9000L, temperature);
}

void loop()
{ 
  Blynk.run();
  timer.run();
}
void temperature()
{
  // Send command to all the sensors for temperature conversion
  sensors.requestTemperatures(); 
  
  // Display temperature from each sensor
  for (int i = 0;  i < deviceCount;  i++)
  {
    
    tempC = sensors.getTempCByIndex(i);
    Blynk.virtualWrite(i+21,tempC);
    Blynk.virtualWrite(i+22,tempC);
    Blynk.virtualWrite(i+20,tempC);
  
  }
  
  
  
}

Because you only need one virtualWrite in your void loop, not three.

The loop reads sensor 0 the first time around, and will write to virtual pin 0 if you use Blynk.virtualWrite(i,tempC);

Next time around i will be 1 and it will read sensor 1 and write to virtual pin 1
Third time around i will be 2 and it will read sensor 2 and write to virtual pin 2

Pete.

thank you @PeteKnight

1 Like