Skip to content

Instantly share code, notes, and snippets.

@Ximik1324
Forked from structure7/blynkDS18B20.ico
Created May 17, 2021 16:33
Show Gist options
  • Save Ximik1324/c43e3e984898a6e8e1b2b163d5995971 to your computer and use it in GitHub Desktop.
Save Ximik1324/c43e3e984898a6e8e1b2b163d5995971 to your computer and use it in GitHub Desktop.
Simple sketch for using the DS18B20 temp sensor on an ESP8266 with Blynk
#include <SimpleTimer.h> // Allows us to call functions without putting them in loop()
#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <BlynkSimpleEsp8266.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 2 // Your ESP8266 pin (ESP8266 GPIO 2 = WeMos D1 Mini pin D4)
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
char auth[] = "fromBlynkApp";
char ssid[] = "ssid";
char pass[] = "pw";
SimpleTimer timer;
int roomTemperature; // Room temperature in F
void setup()
{
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
while (Blynk.connect() == false) {
// Wait until connected
}
sensors.begin(); // Starts the DS18B20 sensor(s).
sensors.setResolution(10); // More on resolution: http://www.homautomation.org/2015/11/17/ds18b20-how-to-change-resolution-9101112-bits/
timer.setInterval(2000L, sendTemps); // Temperature sensor read interval. 2000 (ms) = 2 seconds.
}
// Notice how there is very little in the loop()? Blynk works best that way.
void loop()
{
Blynk.run();
timer.run();
}
// Notice how there are no delays in the function below? Blynk works best that way.
void sendTemps()
{
sensors.requestTemperatures(); // Polls the sensors.
roomTemperature = sensors.getTempFByIndex(0); // Stores temperature. Change to getTempCByIndex(0) for celcius.
Blynk.virtualWrite(1, roomTemperature); // Send temperature to Blynk app virtual pin 1.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment