Paul Langdon
Published © CC BY-SA

Classroom Greenhouse with Blynk

An easy to user experiment toolkit for the classroom to measure temperature and humidity and observe from your smartphone.

BeginnerWork in progress4 hours2,754
Classroom Greenhouse with Blynk

Things used in this project

Hardware components

SparkFun Blynk Board - ESP8266
SparkFun Blynk Board - ESP8266
×1

Software apps and online services

Blynk
Blynk

Hand tools and fabrication machines

Socker Greenhouse

Story

Read more

Schematics

Wiring Diagram

Diagram for wiring Blynk and Fan

Code

Blynk Code

C/C++
#define DEBUG // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include "SparkFunHTU21D.h"

////////////////////
// Blynk Settings //
////////////////////
char BlynkAuth[] = "yourBlynkAuthCodeHere";
char WiFiNetwork[] = "yourSSID";
char WiFiPassword[] = "yourWiFiPassword";
char emailAddress[] = "yourEmail@server";

///////////////////////
// Hardware Settings //
///////////////////////
#define FAN_PIN 5
HTU21D thSense;

// Virtual pins
#define TEMP V9
#define HUMID V10
#define TEMP_LIMIT V12
#define FAN_ON V13



// Globals
uint32_t lastPush = 0;
float tempF = 0;
float humidity = 0;
int tempLimit = 90;
boolean fanOn = false;
boolean tempAlertSent = false;

BLYNK_READ(TEMP) {
  Blynk.virtualWrite(TEMP, tempF); // Update Blynk virtual value
}

BLYNK_READ(HUMID) {
  Blynk.virtualWrite(HUMID, humidity); // Update Blynk virtual value
}


BLYNK_READ(FAN_ON) {
  Blynk.virtualWrite(FAN, fanOn); // Update Blynk virtual value
}



void setup()
{
  // Initialize hardware
#ifdef DEBUG
  Serial.begin(9600); // Serial for debugging
#endif
  rgb.begin(); // RGB LED
  pinMode(FAN_PIN, OUTPUT);
  digitalWrite(FAN_PIN, LOW);
  thSense.begin();
  // Initialize Blynk
  Blynk.begin(BlynkAuth, WiFiNetwork, WiFiPassword);
}

void loop()
{
  // Execute Blynk.run() as often as possible during the loop
  Blynk.run();
  float tempC = thSense.readTemperature(); // Read from the temperature sensor
  tempF = tempC * 9.0 / 5.0 + 32.0; // Convert to farenheit
  humidity = thSense.readHumidity(); // Read from humidity sensor

  // See if we need to send an email alert re: high temp
  if (tempF > tempLimit) {
    if (tempAlertSent == false) { // only send if we haven't already
      Blynk.email(emailAddress, "High temperature.", "Temperature is above your chosen threshold." + tempF + "F");
      tempAlertSent = true;
      digitalWrite(FAN_PIN, HIGH); //Turn On Fan
      fanOn = true;
    }
  }
  else {
    tempAlertSent = false; // reset the alert flag if the temp has been sent
    digitalWrite(FAN_PIN, LOW); //Turn Off Fan
    fanOn = false;
  }


  // Force a push if we haven't had one for a minute
  if (millis() - lastPush > 60000 || millis() < lastPush) {
    lastPush = millis();
    Blynk.virtualWrite(TEMP, tempF); // Update Blynk virtual value
    Blynk.virtualWrite(HUMID, humidity); // Update Blynk virtual value
  }

  // For debugging:
#ifdef DEBUG
  Serial.print(" humidity: ");
  Serial.print(humidity);
  Serial.print(" temp: ");
  Serial.print(tempF);
  Serial.print(" tempAlertSent: ");
  Serial.println(tempAlertSent ? "Yes" : "No ");
  Serial.print(" fanOn: ");
  Serial.println(fanOn ? "Yes" : "No ");
  
#endif
}

Credits

Paul Langdon

Paul Langdon

49 projects • 319 followers
Working as a cloud architect for an IoT hardware company

Comments