Need help with code for greenhouse automation

Okay, you obviously don’t understand how a C++ sketch executes.
The Boid Setup runs once, then when it’s competed the void loop runs constantly, hundreds, if not thousands of times per second.
The timer.run in your void loop will execute multiple times until it’s time to call the sendSensor function.

Your code tells the ESP to go to sleep before the void setup completes. Void loop never executes, neither does your sendSensor function because it’s never called.

Timers have no place in a deep sleep sketch, because they just waste time, forcing your code to wait unnecessarily, which just lengthens the awake time.

I’d suggest that you read through this topic if you want to learn more about how to structure deep sleep code:

Pete.

2 Likes

I guess you have tried your best and now understand a lot more about programming, Blynk, sensor, board and deep-sleep through this experience. That’s all we here want, so that the gained experience will help you in your next project. Just throw the code out will not be helpful at all.

This is the modified code, based on yours, combined with code from the link @PeteKnight posted. Pls give it a try, then add more features as necessary.

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "";
char pass[] = "";

#define DHTPIN 2          // 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);


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

// Use this to avoid being blocked here if no WiFi
void connectWiFi(const char* ssid, const char* pass)
{
    #if (DEBUG_LOOP > 1)
    Serial.printf("Connecting to %s\n", ssid);
    #endif
    
    WiFi.mode(WIFI_STA);
    if (pass && strlen(pass)) 
    {
        WiFi.begin(ssid, pass);
    } else 
    {
        WiFi.begin(ssid);
    }
    int i = 0;
    while ((i++ < 30) && (WiFi.status() != WL_CONNECTED)) 
    {
        BlynkDelay(500);
    }

    #if (DEBUG_LOOP > 1)
    if (WiFi.status() == WL_CONNECTED)
      Serial.println("Connected to WiFi");
    else
      Serial.println("Can't connect to WiFi");
    #endif
}

void setup()
{
  // Debug console, change baud rate to match
  Serial.begin(115200);

  connectWiFi(ssid, pass);
  Blynk.config(auth);
  Blynk.connect();

  //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,60), 9443);

  dht.begin();
}

void loop()
{
  sendSensor();
  Blynk.run();
  //sendSensor();

  delay(100);
  Serial.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>Going to sleep");
  Serial.println();

  ESP.deepSleep(600 * 1000000, WAKE_RF_DEFAULT);   //Deep Sleep for 10mins
  delay(2000);
}

Good luck

I really really appreciate it. Thank you all for all the help !

Just a question here khoih. How hard would it be to convert this code over to use a bme280. Getting some fluctuating temps and humidity while using the DHT11 so I bought a BME280 for the accuracy. I would also like to be able to monitor voltage of the battery I have connected. Would this be hard to do ?

Both of these are fairly simple.

If you search this forum, or the internet in general for BME282 code example you will find lots. Use one of the se, without any Blynk code, to test your hardware and library set-up. Then remove or comment-out the DHT related code from the Blynk sketch and replace it with BME code.
You may think that the prospect of doing this is daunting, but you will learn an enormous amount along the way.

Take the same approach to battery voltage measurement. You will need a voltage divider made with 2 resistors of suitable values, and you will need to use an analogue pin on your board.
Once again, the code examples and ways of calculating the correct resistor values are all out there. Get these working without Blynk, the add it into your Blynk sketch.

Pete.

1 Like

You just follow the valuable suggestion of @PeteKnight and be successful.

Just a little help here for you to start are two sample codes of DHT and BME280 doing exactly the same, so that you’ll know how and where to change:

DHT11

#include <DHT.h>

#define DHTPIN 2          // 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);

void printValues() 
{
  Serial.print("Temperature = ");
  Serial.print(dht.readTemperature());
  Serial.println(" *C");

  Serial.print("Humidity = ");
  Serial.print(dht.readHumidity());
  Serial.println(" %");

  Serial.println();
}

void setup() 
{
  Serial.begin(115200);
  dht.begin();
}

void loop() 
{
  printValues();
  delay(10000);
}

and BME280

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>

#define BME_SCK 13
#define BME_MISO 12
#define BME_MOSI 11
#define BME_CS 10

Adafruit_BME280 bme; // I2C
//Adafruit_BME280 bme(BME_CS); // hardware SPI
//Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI


void printValues() 
{
  Serial.print("Temperature = ");
  Serial.print(bme.readTemperature());
  Serial.println(" *C");

  Serial.print("Humidity = ");
  Serial.print(bme.readHumidity());
  Serial.println(" %");

  Serial.println();
}

void setup() 
{
  Serial.begin(115200);
  bme.begin();  
}

void loop() 
{
  printValues();
  delay(10000);
}


I have the sketch currently working now I just need it to report the temp in Fahrenheit instead of Celsius. Below is what I am using. I have searched around the internet with no luck. Could you please help me understand how to make this work. @PeteKnight you have been helping me a good bit also. Is this a simple fix ?

{
  Wire.begin(); //just added
  float t = bme.readTemperature();
  float p  = (bme.readPressure() / 100.0F);
  float h =  bme.readHumidity();
  float a =  bme.readAltitude(SEALEVELPRESSURE_HPA);
  if (isnan(t) || isnan(p) || isnan(h) || isnan(a)) {
    Serial.println("Failed to read from BME sensor!");
    return;
  }

To convert C to F, use this simple formula

F = ((C * 9) / 5 ) + 32

So to have degree F, use

float t = ( ( bme.readTemperature() * 9 ) / 5 ) + 32;

Thanks ! @khoih