ESP32 with DHT11 sensor and PWM-signal to caged fan

FrederikSoelver
Posts: 1
Joined: Fri Sep 11, 2020 12:40 pm

ESP32 with DHT11 sensor and PWM-signal to caged fan

Postby FrederikSoelver » Fri Sep 11, 2020 12:44 pm

Hi friends,

I have tried to write a project where I'm using an ESP32, a DHT11 and a 4 wire PWM caged fan.
I'm trying to log the data from the DHT11 on my blynk-app and send PWM signals to my caged fan at the same time.

the first code I have is the basic Blynk example:
  1. #define BLYNK_PRINT Serial
  2.  
  3.  
  4. #include <WiFi.h>
  5. #include <WiFiClient.h>
  6. #include <BlynkSimpleEsp32.h>
  7. #include <DHT.h>
  8.  
  9. // You should get Auth Token in the Blynk App.
  10. // Go to the Project Settings (nut icon).
  11. char auth[] = "YyxSI-rgdqf_ozwx3BmIZkkMFU3cuppC";
  12.  
  13. // Your WiFi credentials.
  14. // Set password to "" for open networks.
  15. char ssid[] = "nabofarm";
  16. char pass[] = "regnbuer";
  17.  
  18. #define DHTPIN 4        // What digital pin we're connected to
  19.  
  20. // Uncomment whatever type you're using!
  21. #define DHTTYPE DHT11     // DHT 11
  22. //#define DHTTYPE DHT22   // DHT 22, AM2302, AM2321
  23. //#define DHTTYPE DHT21   // DHT 21, AM2301
  24.  
  25. DHT dht(DHTPIN, DHTTYPE);
  26. BlynkTimer timer;
  27.  
  28. // This function sends Arduino's up time every second to Virtual Pin (5).
  29. // In the app, Widget's reading frequency should be set to PUSH. This means
  30. // that you define how often to send data to Blynk App.
  31. void sendSensor()
  32. {
  33.   float h = dht.readHumidity();
  34.   float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
  35.  
  36.   if (isnan(h) || isnan(t)) {
  37.     Serial.println("Failed to read from DHT sensor!");
  38.     return;
  39.   }
  40.   // You can send any value at any time.
  41.   // Please don't send more that 10 values per second.
  42.   Blynk.virtualWrite(V5, h);
  43.   Blynk.virtualWrite(V6, t);
  44. }
  45.  
  46. void setup()
  47. {
  48.   // Debug console
  49.   Serial.begin(9600);
  50.  
  51.   Blynk.begin(auth, ssid, pass);
  52.   // You can also specify server:
  53.   //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
  54.   //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);
  55.  
  56.   dht.begin();
  57.  
  58.   // Setup a function to be called every second
  59.   timer.setInterval(1000L, sendSensor);
  60. }
  61.  
  62. void loop()
  63. {
  64.   Blynk.run();
  65.   timer.run();
  66. }
the new one is one I did with a mix of the example and another one where I should be able to send out PWM signal to pin 16.

The example is running fine but when I upload the new one it's only online on the blink app for a short period. Does anyone know what's wrong?

  1. /*  App project setup:
  2.     Value Display widget attached to V5
  3.     Value Display widget attached to V6
  4.  *************************************************************/
  5.  
  6. /* Comment this out to disable prints and save space */
  7. #define BLYNK_PRINT Serial
  8.  
  9.  
  10. #include <WiFi.h>
  11. #include <WiFiClient.h>
  12. #include <BlynkSimpleEsp32.h>
  13. #include <DHT.h>
  14.  
  15. // You should get Auth Token in the Blynk App.
  16. // Go to the Project Settings (nut icon).
  17. char auth[] = "YyxSI-rgdqf_ozwx3BmIZkkMFU3cuppC";
  18.  
  19. // Your WiFi credentials.
  20. // Set password to "" for open networks.
  21. char ssid[] = "nabofarm";
  22. char pass[] = "regnbuer";
  23.  
  24. #define DHTPIN 4        // What digital pin we're connected to
  25.  
  26. // Uncomment whatever type you're using!
  27. #define DHTTYPE DHT11     // DHT 11
  28. //#define DHTTYPE DHT22   // DHT 22, AM2302, AM2321
  29. //#define DHTTYPE DHT21   // DHT 21, AM2301
  30.  
  31. DHT dht(DHTPIN, DHTTYPE);
  32. BlynkTimer timer;
  33.  
  34. // This function sends Arduino's up time every second to Virtual Pin (5).
  35. // In the app, Widget's reading frequency should be set to PUSH. This means
  36. // that you define how often to send data to Blynk App.
  37. void sendSensor()
  38. {
  39.   float h = dht.readHumidity();
  40.   float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
  41.  
  42.   if (isnan(h) || isnan(t)) {
  43.     Serial.println("Failed to read from DHT sensor!");
  44.     return;
  45.   }
  46.   // You can send any value at any time.
  47.   // Please don't send more that 10 values per second.
  48.   Blynk.virtualWrite(V5, h);
  49.   Blynk.virtualWrite(V6, t);
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59. const int ledpin = 16;
  60.  
  61. const int freq= 20000;
  62.  
  63. const int channel= 0;
  64.  
  65. const int resolution= 8;
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80. }
  81.  
  82. void setup()
  83. {
  84.  
  85.  
  86. ledcSetup (0, 20000, 8);
  87.  
  88. ledcAttachPin (16 , 0);
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.   // Debug console
  99.   Serial.begin(9600);
  100.  
  101.   Blynk.begin(auth, ssid, pass);
  102.   // You can also specify server:
  103.   //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
  104.   //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);
  105.  
  106.   dht.begin();
  107.  
  108.   // Setup a function to be called every second
  109.   timer.setInterval(1000L, sendSensor);
  110. }
  111.  
  112. void loop()
  113. {
  114.  
  115.  
  116.  
  117. if (dht.readHumidity() < 60)
  118.   // increase the LED brightness
  119.   {for(int dutyCycle = 0; dutyCycle = 0; ){  
  120.     // changing the LED brightness with PWM
  121.     ledcWrite(0, dutyCycle);}
  122.   }
  123.     else
  124.    
  125.   {
  126.     for(int dutyCycle = 0; dutyCycle = 255; ){  
  127.  
  128.     // changing the LED brightness with PWM
  129.     ledcWrite(0, dutyCycle);}
  130.   }
  131.  
  132.  
  133.  
  134.   delay (1000);
  135.  
  136.  
  137. {
  138.   Blynk.run();
  139.   timer.run();
  140. }
  141.  
  142. }

Who is online

Users browsing this forum: No registered users and 70 guests