Need help with code for greenhouse automation

Khoih I have a question. I am just using the basic sketch for the DHT11 for my greenhouse right now. I am wanting to put this in deep sleep with a 10 minute delay. It is on a ESP8266 D1 Mini board. The sketch I am using is from the blynk sketch browser. Could you please tell me how I could go about doing this ?

You can just use the SmartFarm-DeepSleep code as is, then select DHT11 as the sensor.

If you’d like and have time + energy, you can either

  1. copy the DeepSleep code portion to your basic sketch
  2. or delete the unnecessary code

Thats where I was getting confused. New to this and didnt know where the deep sleep code needed to be placed in the sketch. I will also have to place a jumper to the rst pin correct ?

I’m sorry I’m so busy right now and can’t help you with the code. You have to start trying the best you can, then someone will help guide you to finish.
I don’t know if @Madhukesh can help you now.

Its okay… I will search youtube and give it a go. I appreciate all the help you have given me to this point. Thanks !

I am afraid to help you on this, because i have not worked on deep sleep. But i will give it a try.

As @khoih said delete the lines which are not necessary. Each section has been commented with info on what it does, if you give enough time and study the code you can get it working.

Yes connect RST pin to GPIO16

void deep_sleep() {
Blynk.disconnect();
WiFi.mode(WIFI_OFF);
ESP.deepSleep(60000000); \\ 60 Seconds
}

Call this function once you have got the readings from the sensor.
This is all you need to put the ESP in deep sleep and wake up again at set interval.

I would say better start with a simple deep sleep sketch and once you get it working you can add it to Blynk. Once you have a basic code you can post it here and we can help you.

@khoih correct me if am wrong.

1 Like

This is the code that I am currently running. It goes into deep sleep but it never send the temp or humidity values to blynk.

[Unformatted code removed by moderator]

You’ve not done the triple backtick thing again, so I’ve removed your code.

Please keep the discussion about this subject to one topic, rather than the two that you have created. I’ll merge the other topic into this one to keep everything together.

Pete.

I am in need of some help. I am monitoring the temp and humidity in a greenhouse and I got the deep sleep to work on my ESp8266 D1 mini board but it does not send the sensor data using this code. Can anyone please help me with this ? Any help would be appreciated.

  Download latest Blynk library here:
    https://github.com/blynkkk/blynk-library/releases/latest

  Blynk is a platform with iOS and Android apps to control
  Arduino, Raspberry Pi and the likes over the Internet.
  You can easily build graphic interfaces for all your
  projects by simply dragging and dropping widgets.

    Downloads, docs, tutorials: http://www.blynk.cc
    Sketch generator:           http://examples.blynk.cc
    Blynk community:            http://community.blynk.cc
    Follow us:                  http://www.fb.com/blynkapp
                                http://twitter.com/blynk_app

  Blynk library is licensed under MIT license
  This example code is in public domain.

 *************************************************************

  This example shows how value can be pushed from Arduino to
  the Blynk App.

  WARNING :
  For this example you'll need Adafruit DHT sensor libraries:
    https://github.com/adafruit/Adafruit_Sensor
    https://github.com/adafruit/DHT-sensor-library

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

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

  dht.begin();

  // Setup a function to be called every second
  timer.setInterval(60000L, sendSensor);

  ESP.deepSleep(600 * 1000000,WAKE_RF_DEFAULT);   //try the default mode
  delay(100);
}

void loop()
{
  Blynk.run();
  timer.run();
}

@swtrpeter please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Pete.

Sorry about that

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