BLYNK
BLYNK.IO       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Ds18b20 temp sensor and new Blynk

Hi all!

I am totally new to home automation and everything related to it.
I want to link the esp8266 with the ds18b20 and have alerts, which has been done in the past. Could I still use the code of the ‘older’ version of blynk?

If not, does anyone have some sample code for this?

If you mean Notifications or emails then these work differently in Blynk 2.0, so some re-working would be needed.
Also, if the examples use the Tome Input widget for scheduling then this is done differently in Blynk 2.0

In addition, there is extra setup required in the web portal of Blynk 2.0 to create the template and data streams.

Pete.

So in general, I should start from scratch?

Will wait if someone hopefully does this for the ds18b20 :wink:

No, that’s not what I said.

You could be waiting some time!
Also, it will be much more difficult to share the entire setup from Blynk 2.0, with no app cloning and the need to document all the template and datastream setup as well as the app setup (and web dashboard setup if used).

Pete.

hmm, yeah, problem is I got no coding experience at all…
Will give it a try. I’m now going through the getting started and did the provisioning.

Hmmm, not sure that I’d use the Edgent provisioning then try to add a Blynk 0.1 sketch into the Edgent sketch.

Pete.

I won’t use an 0.1 sketch :slight_smile: will try to go from the start and import some pieces of the old one :slight_smile:

I think I can use this as a start. Then, need to implement Blynk and find a way to push it using blink like Blynk.writevirtualpin but that will be in the docs of Blynk.

#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 2 // DS18B20 on NodeMCU pin D4 
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);

void setup() 
{
  Serial.begin(115200);
  DS18B20.begin();
  Serial.println("Testing Dual Sensor data");
}

void loop() {
  float temp_0;
  float temp_1;
  DS18B20.requestTemperatures(); 
  temp_0 = DS18B20.getTempCByIndex(0); // Sensor 0 will capture Temp in Celcius
  temp_1 = DS18B20.getTempFByIndex(1); // Sensor 0 will capture Temp in Fahrenheit

  Serial.print("Temp_0: ");
  Serial.print(temp_0);
  Serial.print(" oC . Temp_1: ");
  Serial.print(temp_1);
  Serial.println(" oF");
  delay(1000);
}

When you postcode to the forum you need to put triple backticks at the very beginning and end of the sketch, on a line of their own, rather than using block quotes as you’ve done here.
Triple backticks look like this:
```

Please edit your post (using the pencil icon at the bottom) and correct the code formatting.

Pete.

Yeah, like other forums :man_facepalming:

This code isn’t suitable for use with Blynk as it stands, because of the cluttered void loop and delays.

It would need to be converted to use a timer to call a function that takes the temperature readings then writes them to Blynk.

Pete.

Will search some more in the code but I don’t think I’m up to this, yet…
Thanks Pete!

would this be a good starting point? Sorry, I have zero to none experience with c++

#include <SimpleTimer.h>
#define BLYNK_PRINT Serial 
#include <OneWire.h>
#include <DallasTemperature.h>
#include "BlynkEdgent.h"
#define BLYNK_TEMPLATE_ID "XXXXXXX"
#define BLYNK_DEVICE_NAME "Temperature"
#define BLYNK_FIRMWARE_VERSION        "0.1.0"

#define ONE_WIRE_BUS 2        // This is the ESP8266 pin
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

DeviceAddress tempSensor001 = { 0x28, 0xFF, 0x3A, 0xD4, 0x89, 0x16, 0x03, 0x36 };

SimpleTimer timer;

int temperature001;   // Variables for storing temperatures

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

BlynkEdgent.begin();

  //Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8442);
  Blynk.begin(auth, ssid, pass,"Blynk-cloud.com",8442);
}

sensors.begin();                        // Starts the DS18B20 sensor(s).
  sensors.setResolution(tempSensor001, 10);              // More on resolution: http://www.homautomation.org/2015/11/17/ds18b20-how-to-change-resolution-9101112-bits/
  timer.setInterval(500L, sendSensor001);  // Temperature sensor read interval. 1000 (ms) = 1 seconds.
}

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


void sendSensor001()
{
  sensors.requestTemperatures();                  // Polls the sensors.
  temperature001 = sensors.getTempC(tempSensor001);   // Stores temperature. Change to getTempCByIndex(0) for celcius.
  Blynk.virtualWrite(5, temperature001);         // Send temperature to Blynk app virtual pin 1.
}

Much better.

Pete.

would you consider this complete? Any things you would say are missing to work with the new platform?

It wont work with Blynk 2.0, but is a better starting point than your other code.

Pete.

allright, so need to apply the things for Blynk 2.0, where can I find what should be changed?
Since the docs say : If you’re using Blynk library, your code should be compatible with the new cloud. Some minor changes will be needed:

You have to sign before :stuck_out_tongue_winking_eye:
But I see you are using your local server

I won’t lie, grabbed some older code from the forum :wink: Not supposed to use a local server :wink:

1 Like

The code he found was for local server, but the connection needs to be changed anyway, and the template ID and device name added.

Pete.

1 Like

changed those ones :slight_smile:

Would I flash this over the edgent in the Arduino IDE or next to it, in a new tab?