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:
- #define BLYNK_PRINT Serial
- #include <WiFi.h>
- #include <WiFiClient.h>
- #include <BlynkSimpleEsp32.h>
- #include <DHT.h>
- // You should get Auth Token in the Blynk App.
- // Go to the Project Settings (nut icon).
- char auth[] = "YyxSI-rgdqf_ozwx3BmIZkkMFU3cuppC";
- // Your WiFi credentials.
- // Set password to "" for open networks.
- char ssid[] = "nabofarm";
- char pass[] = "regnbuer";
- #define DHTPIN 4 // What digital pin we're connected to
- // Uncomment whatever type you're using!
- #define DHTTYPE DHT11 // DHT 11
- //#define DHTTYPE DHT22 // DHT 22, AM2302, AM2321
- //#define DHTTYPE DHT21 // DHT 21, AM2301
- DHT dht(DHTPIN, DHTTYPE);
- BlynkTimer timer;
- // This function sends Arduino's up time every second to Virtual Pin (5).
- // In the app, Widget's reading frequency should be set to PUSH. This means
- // that you define how often to send data to Blynk App.
- void sendSensor()
- {
- float h = dht.readHumidity();
- float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
- if (isnan(h) || isnan(t)) {
- Serial.println("Failed to read from DHT sensor!");
- return;
- }
- // You can send any value at any time.
- // Please don't send more that 10 values per second.
- Blynk.virtualWrite(V5, h);
- Blynk.virtualWrite(V6, t);
- }
- void setup()
- {
- // Debug console
- Serial.begin(9600);
- Blynk.begin(auth, ssid, pass);
- // You can also specify server:
- //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
- //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);
- dht.begin();
- // Setup a function to be called every second
- timer.setInterval(1000L, sendSensor);
- }
- void loop()
- {
- Blynk.run();
- timer.run();
- }
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?
- /* App project setup:
- Value Display widget attached to V5
- Value Display widget attached to V6
- *************************************************************/
- /* Comment this out to disable prints and save space */
- #define BLYNK_PRINT Serial
- #include <WiFi.h>
- #include <WiFiClient.h>
- #include <BlynkSimpleEsp32.h>
- #include <DHT.h>
- // You should get Auth Token in the Blynk App.
- // Go to the Project Settings (nut icon).
- char auth[] = "YyxSI-rgdqf_ozwx3BmIZkkMFU3cuppC";
- // Your WiFi credentials.
- // Set password to "" for open networks.
- char ssid[] = "nabofarm";
- char pass[] = "regnbuer";
- #define DHTPIN 4 // What digital pin we're connected to
- // Uncomment whatever type you're using!
- #define DHTTYPE DHT11 // DHT 11
- //#define DHTTYPE DHT22 // DHT 22, AM2302, AM2321
- //#define DHTTYPE DHT21 // DHT 21, AM2301
- DHT dht(DHTPIN, DHTTYPE);
- BlynkTimer timer;
- // This function sends Arduino's up time every second to Virtual Pin (5).
- // In the app, Widget's reading frequency should be set to PUSH. This means
- // that you define how often to send data to Blynk App.
- void sendSensor()
- {
- float h = dht.readHumidity();
- float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
- if (isnan(h) || isnan(t)) {
- Serial.println("Failed to read from DHT sensor!");
- return;
- }
- // You can send any value at any time.
- // Please don't send more that 10 values per second.
- Blynk.virtualWrite(V5, h);
- Blynk.virtualWrite(V6, t);
- const int ledpin = 16;
- const int freq= 20000;
- const int channel= 0;
- const int resolution= 8;
- }
- void setup()
- {
- ledcSetup (0, 20000, 8);
- ledcAttachPin (16 , 0);
- // Debug console
- Serial.begin(9600);
- Blynk.begin(auth, ssid, pass);
- // You can also specify server:
- //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
- //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);
- dht.begin();
- // Setup a function to be called every second
- timer.setInterval(1000L, sendSensor);
- }
- void loop()
- {
- if (dht.readHumidity() < 60)
- // increase the LED brightness
- {for(int dutyCycle = 0; dutyCycle = 0; ){
- // changing the LED brightness with PWM
- ledcWrite(0, dutyCycle);}
- }
- else
- {
- for(int dutyCycle = 0; dutyCycle = 255; ){
- // changing the LED brightness with PWM
- ledcWrite(0, dutyCycle);}
- }
- delay (1000);
- {
- Blynk.run();
- timer.run();
- }
- }