Advertisement
learnelectronics

DHT11 BLYNK ESP8266

Aug 10th, 2017
9,102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.85 KB | None | 0 0
  1. /*************************************************************
  2.   Download latest Blynk library here:
  3.     https://github.com/blynkkk/blynk-library/releases/latest
  4.  
  5.   Blynk is a platform with iOS and Android apps to control
  6.   Arduino, Raspberry Pi and the likes over the Internet.
  7.   You can easily build graphic interfaces for all your
  8.   projects by simply dragging and dropping widgets.
  9.  
  10.     Downloads, docs, tutorials: http://www.blynk.cc
  11.     Sketch generator:           http://examples.blynk.cc
  12.     Blynk community:            http://community.blynk.cc
  13.     Follow us:                  http://www.fb.com/blynkapp
  14.                                 http://twitter.com/blynk_app
  15.  
  16.   Blynk library is licensed under MIT license
  17.   This example code is in public domain.
  18.  
  19.  *************************************************************
  20.  
  21.   This example shows how value can be pushed from Arduino to
  22.   the Blynk App.
  23.  
  24.   WARNING :
  25.   For this example you'll need Adafruit DHT sensor libraries:
  26.     https://github.com/adafruit/Adafruit_Sensor
  27.     https://github.com/adafruit/DHT-sensor-library
  28.  
  29.   App project setup:
  30.     Value Display widget attached to V5
  31.     Value Display widget attached to V6
  32.  *************************************************************/
  33.  
  34. /* Comment this out to disable prints and save space */
  35. #define BLYNK_PRINT Serial
  36.  
  37.  
  38. #include <SPI.h>
  39. #include <ESP8266WiFi.h>
  40. #include <BlynkSimpleEsp8266.h>
  41. #include <DHT.h>
  42.  
  43. // You should get Auth Token in the Blynk App.
  44. // Go to the Project Settings (nut icon).
  45. char auth[] = "anditsallbecause";
  46.  
  47. // Your WiFi credentials.
  48. // Set password to "" for open networks.
  49. char ssid[] = "yourmamadontdance";
  50. char pass[] = "yourdaddydontrockandroll";
  51.  
  52. #define DHTPIN D6          // What digital pin we're connected to
  53.  
  54. // Uncomment whatever type you're using!
  55. #define DHTTYPE DHT11     // DHT 11
  56. //#define DHTTYPE DHT22   // DHT 22, AM2302, AM2321
  57. //#define DHTTYPE DHT21   // DHT 21, AM2301
  58.  
  59. DHT dht(DHTPIN, DHTTYPE);
  60. BlynkTimer timer;
  61.  
  62. // This function sends Arduino's up time every second to Virtual Pin (5).
  63. // In the app, Widget's reading frequency should be set to PUSH. This means
  64. // that you define how often to send data to Blynk App.
  65. void sendSensor()
  66. {
  67.   float h = dht.readHumidity();
  68.   float t = dht.readTemperature(true); // or dht.readTemperature(true) for Fahrenheit
  69.  
  70.   if (isnan(h) || isnan(t)) {
  71.     Serial.println("Failed to read from DHT sensor!");
  72.     return;
  73.   }
  74.   // You can send any value at any time.
  75.   // Please don't send more that 10 values per second.
  76.   Blynk.virtualWrite(V5, h);
  77.   Blynk.virtualWrite(V6, t);
  78. }
  79.  
  80. void setup()
  81. {
  82.   // Debug console
  83.   Serial.begin(9600);
  84.  
  85.   Blynk.begin(auth, ssid, pass);
  86.  
  87.   dht.begin();
  88.  
  89.   // Setup a function to be called every second
  90.   timer.setInterval(1000L, sendSensor);
  91. }
  92.  
  93. void loop()
  94. {
  95.   Blynk.run();
  96.   timer.run();
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement