Want code to start another thing when value drops below certain value

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
}

It probably does but what is another thing? Partial code us time wasting.

i don't see any code the compares the humidity, 'h' to any value?

Maybe this is what does that:

    // Check if humidity is below the threshold
    if (h < HUMIDITY_THRESHOLD) {
      // If humidity is below threshold, start another thing
      startAnotherThing();
    }

a7

right

shouldn't it be in loop()

It's in setup().

a7

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();
    }
  });


a7

i think it's just in the wrong place

1 Like

OK I am curious about this syntax and interpretation of

  server.on("/", HTTP_GET, []() {

// guts removed

  });

is this an anonymous callback? Does it get stashed somewhere to live beyond setup()?

a7

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.

a7

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 :slight_smile:

answer #8

why don't you do

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

here is what i have now but it wont work :S

#include <WiFi.h>
#include <WebServer.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.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 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 (Nicolas N. Jensen)</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
  float t = dht.readTemperature();
  float h = dht.readHumidity();

  // 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
}

I GOT IT TO WORK!

#include <WiFi.h>
#include <WebServer.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DHT.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 30 // Define the humidity threshold
#define LED_PIN A1       // Digital pin connected to the LED

const char *ssid = "Homeassistant";
const char *password = "elev123456";

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
WebServer server(80);
DHT 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, []() {
    float t = dht.readTemperature();
    float h = dht.readHumidity();
    String htmlContent = "<h1>TEMP AND HUMIDITY (Nicolas N. Jensen)</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);
  });

  // Start server
  server.begin();

  // Initialize LED pin
  pinMode(LED_PIN, OUTPUT);
}

void loop() {
  server.handleClient();

  // Read data from DHT sensor
  float t = dht.readTemperature();
  float h = dht.readHumidity();

  // 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();

  // Check if humidity is below the threshold
  if (h < HUMIDITY_THRESHOLD) {
    // If humidity is below threshold, start another thing
    startAnotherThing();
  }

  // Check if humidity is below the threshold
  if (h < HUMIDITY_THRESHOLD) {
    // If humidity is below threshold, start LED on pin 3
    digitalWrite(LED_PIN, HIGH);
    Serial.println("Humidity is below threshold! LED turned on.");
  } else {
    // Otherwise, turn off the LED
    digitalWrite(LED_PIN, LOW);
    Serial.println("Humidity is above threshold. LED turned off.");
  }

  delay(2000); // Wait for 2 seconds before reading again
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.