-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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