Particle Photon + DS18B20 with Blynk

Hey guys,

I’m trying to get my Photon + DS18B20 running with Blynk, but I only get -127°C or 0°C on my Blynk dashboard.

This is my Code:

// This #include statement was automatically added by the Particle IDE.
#include <SparkCorePolledTimer.h>

// This #include statement was automatically added by the Particle IDE.
#include <blynk.h>

// This #include statement was automatically added by the Particle IDE.
#include <OneWire.h>

// This #include statement was automatically added by the Particle IDE.
#include "spark-dallas-temperature.h"


char auth[] = "xxxxxxxxxxxxxxxxxxx"; 

double tempC = 0.0;
double tempF = 0.0;
/*#define SensorPin D2*/
int SensorPin = D2;

OneWire oneWire(SensorPin);
DallasTemperature sensors(&oneWire);

void setup() {
    
    Serial.begin(9600);
    delay(5000); // Allow board to settle

    pinMode (SensorPin, INPUT);
    sensors.begin();
    Particle.variable("tempc", tempC);
    Particle.variable("tempf", tempF);
        Blynk.begin(auth);
}

void loop() {
  sensors.requestTemperatures();
  double tempCheck = sensors.getTempCByIndex(0);
  Blynk.run();
  Blynk.virtualWrite(2, tempC);
  
  if ( tempCheck > -50 && tempCheck < 50) // might want to guard against a spurious high value as well
  {
    tempC = tempCheck;
    /*tempF = tempC * 9.0 / 5.0 + 32.0;*/
  }
  
      Blynk.virtualWrite(1, tempCheck);

  delay(1000);

}

Do you guys have a solution for my problem?

I wouldn’t say “it’s running with Blynk” with those numbers… :wink:
Did you add the resistor between +V and Data?

And please, avoid delays and use timers to check the temperature

Read through this. There is a working version of code in there that can probably be adapted to your Photon.

https://community.blynk.cc/t/thermal-probes-ds18b20/14164/

I’m now using this Code with a timer. But still -127°C in Blynk.

#include <SparkCorePolledTimer.h>

// This #include statement was automatically added by the Particle IDE.
#include <blynk.h>

// This #include statement was automatically added by the Particle IDE.
#include <OneWire.h>

// This #include statement was automatically added by the Particle IDE.
#include "spark-dallas-temperature.h"


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

//#define SensorPin 2

OneWire oneWire(D2);
DallasTemperature sensors(&oneWire);

double tempc = 0.0;

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 sendSensor()
{
  sensors.requestTemperatures();
  double tempCheck = sensors.getTempCByIndex(0);
  tempc = (double) tempCheck;


  Blynk.virtualWrite(V5, tempCheck);
}

void setup()
{
  // Debug console
  Serial.begin(9600);

  delay(5000); // Allow board to settle
  Blynk.begin(auth);
  
  Particle.variable("tempc", &tempc, DOUBLE);

sensors.begin();
    

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

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

Have you confirmed it’s proper operation “outside” of a Blynk sketch?

Google searching indicates that -127 generally means it is not hooked up properly.

http://henrysbench.capnfatz.com/henrys-bench/arduino-temperature-measurements/ds18b20-arduino-user-manual-introduction-and-contents/