hi, i'm trying to make a code that can start another thing when the humidity is below a certain value (60 %) but when it drops below it dosent do anything? help
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#include <WiFi.h>
#include <WebServer.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define DHTPIN 2 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT22 // Define the sensor type
#define HUMIDITY_THRESHOLD 60 // Define the humidity threshold
const char *ssid = "Homeassistant";
const char *password = "elev123456";
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
WebServer server(80);
DHT_Unified dht(DHTPIN, DHTTYPE);
void startAnotherThing() {
// Function to start another thing when humidity is low
Serial.println("Humidity is below threshold! Starting another thing...");
// Add your code here to start another thing
}
void setup() {
Serial.begin(9600);
delay(1000);
// Initialize OLED display
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;)
;
}
display.clearDisplay();
// Connect to WiFi
Serial.println();
Serial.println("Connecting to WiFi...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
// Start DHT sensor
dht.begin();
// Route for root / web page
server.on("/", HTTP_GET, []() {
sensors_event_t event;
dht.temperature().getEvent(&event);
float t = event.temperature;
dht.humidity().getEvent(&event);
float h = event.relative_humidity;
String htmlContent = "<h1>TEMP AND HUMIDITY</h1>";
htmlContent += "<p>Temperature: ";
htmlContent += String(t, 1); // Display temperature with one decimal
htmlContent += " *C</p>";
htmlContent += "<p>Humidity: ";
htmlContent += String(h, 1); // Display humidity with one decimal
htmlContent += " %</p>";
server.send(200, "text/html", htmlContent);
// Check if humidity is below the threshold
if (h < HUMIDITY_THRESHOLD) {
// If humidity is below threshold, start another thing
startAnotherThing();
}
});
// Start server
server.begin();
}
void loop() {
server.handleClient();
// Read data from DHT sensor
sensors_event_t event;
dht.temperature().getEvent(&event);
float t = event.temperature;
dht.humidity().getEvent(&event);
float h = event.relative_humidity;
// Printing the results on the serial monitor
Serial.print("Temperature = ");
Serial.print(t);
Serial.print(" *C ");
Serial.print(" Humidity = ");
Serial.print(h);
Serial.println(" % ");
// Display temperature and humidity on OLED
display.clearDisplay();
display.setTextSize(2); // Increase text size to 2
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.print("Temp:");
display.println(t);
display.print("Humidity: ");
display.println(h);
display.display();
delay(2200); // Wait for 2 seconds before reading again
}
// Check if humidity is below the threshold
if (h < HUMIDITY_THRESHOLD) {
// If humidity is below threshold, start another thing
startAnotherThing();
}
yes, i see that. in the code invoked when there is an request sent to the server.
shouldn't it be in loop() where the humidity can be constantly monitored and an action taken when the humidity drops below the threshold, no just when there happens to be a server request?
or is that the intent. that there needs to be a server request?
@ticsy yeah, post an entire working sketch. one that compiles and runs and does the wrong thing or doesn't do the right thing.
Reading more carefully it looks like a total big mess.
The humidity check seems to be in a callback function in the middle of setup(), time spent wasted at the moment.
// Route for root / web page
server.on("/", HTTP_GET, []() {
///
// Check if humidity is below the threshold
if (h < HUMIDITY_THRESHOLD) {
// If humidity is below threshold, start another thing
startAnotherThing();
}
});
it's a lamda function, like a callback. in this case it gets invoked when the board receives a server request from a client. it generates text output that is sent back to the requester
THX. I've seen that, but only as a function passed as code, not pointer-to a function, which would only be needed during the time the function it was passed to was running.
Like in the old days handing quicksort the tool for comparing. Only you just write it out in the argument list.
I will say I don't much care for that beyond the trivial. Twenty lines of code deserves a name!
Therefor I remain curious about where that lambda code is, as it seems like it is now available as a call back.
All of which makes sense for it to be in setup().
Except of course your observation and question about the intent of the test.
hi im sorry slow answering.. I don''t understand you, whats wrong with my code? i just want the posted code to start an action if humidity is under 60 :S nothing moore, nothing less
void loop() {
server.handleClient();
// Read data from DHT sensor
sensors_event_t event;
dht.temperature().getEvent(&event);
float t = event.temperature;
dht.humidity().getEvent(&event);
float h = event.relative_humidity;
// Check if humidity is below the threshold
if (h < HUMIDITY_THRESHOLD) {
// If humidity is below threshold, start another thing
startAnotherThing();
}
// Printing the results on the serial monitor