Skip to content

Instantly share code, notes, and snippets.

@structure7
Last active September 29, 2023 16:01
Show Gist options
  • Save structure7/13c9cf278843a74e688c0252067ed806 to your computer and use it in GitHub Desktop.
Save structure7/13c9cf278843a74e688c0252067ed806 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.
}
@Ximik1324
Copy link

Hello, you can make a note or correct the code to be able to install two or three sensors

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment