Automated Hydro.. How to use virtual pins? SOLVED!

Hi,

I´m new at this forum and trying make myself an simple automated hydrophonics system. Code is not finished yet and i have some newbie question here.

I´m using Blynk to control it and the first task is to check 1 virtual switch state in Blynk, that control do i give my plants watermix1 or watermix2. Sensor will say when the watering procedure will start/stop.

The question:

Should i just use IF/ELSE commands or is there a easier/better way to make "BLYNK_WRITE(V3)" -kind of code? There is some simple LED-code now there, but i can make the watermix1 and watermix2-selection same way if i only know where and how. Code should be simple to you.

Could someone help a little? Thanks.

//
 /**************************************************************

  Hydrophonics Automation V1.00
   
   App project setup:
     Value Display widget attached to V5
     Value Display widget attached to V6

 **************************************************************/


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


// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "----"; //Enter the Auth code which was send by Blink

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "-----";  //Enter your WIFI Name
char pass[] = "-----";  //Enter your WIFI Password
bool isFirstConnect = true;

int LED1 = D2;   //Only for a test.. going to remove this


// Set water sensor analog pin

float water_sensor_a = A0;
float sensorValue = 0;

// Set DHT11 pin

#define DHTPIN 2          // Digital pin D4

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


//SimpleTimer timer;            //Timer??
BlynkTimer timer;              //  Timer??

static int sensorSoilMoisture = 60;
static int sensorAirHumidity = 50;
static int wateringAmount = 5;
static int wateringTimer = -1;
static bool isNotificationSent = false;

void sendUptime()
{
  // 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.

  Blynk.virtualWrite(V5, millis() / 1000);
}


void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // 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);     //V5 is for Humidity
  Blynk.virtualWrite(V6, t);     //V6 is for Temperature

}

void setup()
{
  Serial.begin(9600); // See the connection status in Serial Monitor
  pinMode(LED1, OUTPUT);
  Blynk.begin(auth, ssid, pass);
  dht.begin();
  delay(5000); // give time to settle

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

}


BLYNK_CONNECTED() {
  if (isFirstConnect) {
    Blynk.syncAll();

    isFirstConnect = false;
  }
}



BLYNK_WRITE(V3)
{
  int pinValue = param.asInt(); // Assigning incoming value from pin V3 to a variable
  if (pinValue == 1) {
    digitalWrite(LED1, HIGH); // Turn LED on. MEANS THIS ONE FOR FLOWERING.
  } else {
    digitalWrite(LED1, LOW); // Turn LED off. MEANS THIS ONE FOR VEGETATIVE
  }
}


void start_watering()

{


}





void loop()
{
  Blynk.run(); // Initiates Blynk
  timer.run(); // Initiates SimpleTimer



  //Moisture sensor A related
  {

    sensorValue = analogRead(water_sensor_a);
    Serial.println(sensorValue);

    if (sensorValue >= 151)

    {
      //      Blynk.virtualWrite(sensorValue);

      Blynk.virtualWrite(V1, "LOW Water!");
      
      
      // start_watering():        //GO START WATERING???

      }

      else if (sensorValue >= 71 && sensorValue < 150)

    {

      //   Blynk.virtualWrite(sensorValue);

      Blynk.virtualWrite(V1, "MID Water");

    }

    else if (sensorValue < 70)

    {

      {
        //       Blynk.virtualWrite(sensorValue);

        Blynk.virtualWrite(V1, "FULL Water");



      }

    }


  }

}
1 Like

SORRY THIS WAY TO ADD CODE

Please help us and yourself by Auto Formatting the code in the IDE then posting it here using code tags as described in read this before posting a programming question

You might also want to consider removing your WIFI name and password and your Blynk authorisation code.

Or is "pinValue" now a variable i can use normally inside a loop? So if i just check pinValue 1 or 0 and choose watermix1/2 like that? Does Blynk app now update the pinValue-state automatically?

I solved it by using "Blynk.syncVirtual()"-command in loop to call back the BLYNK_WRITE(V3). Now i can choose the Watermix1 or 2. Great!

//
//
/**************************************************************
 Sasmsara Exit Hydrophonics V1.0

   App project setup:
     Value Display widget attached to V5
     Value Display widget attached to V6

 **************************************************************/


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


// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "------"; //Enter the Auth code which was send by Blink

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "-----";  //Enter your WIFI Name
char pass[] = "--------";  //Enter your WIFI Password
bool isFirstConnect = true;

int LED1 = D2;



// Set water sensor analog pins

float water_sensor_a = A0;
//int water_sensor_b = A2;
float sensorValue = 0;

// Set DHT11 pin

#define DHTPIN 2          // Digital pin D4

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


//SimpleTimer timer;
BlynkTimer timer; // Initiating timer to perform repeating event

static int sensorSoilMoisture = 60;
static int sensorAirHumidity = 50;
static int wateringAmount = 5;
static int wateringTimer = -1;
static bool isNotificationSent = false;

void sendUptime()
{
  // 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.

  Blynk.virtualWrite(V5, millis() / 1000);
}


void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // 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);  //V5 is for Humidity
  Blynk.virtualWrite(V6, t);  //V6 is for Temperature

}

void setup()
{
  Serial.begin(9600); // See the connection status in Serial Monitor
  pinMode(LED1, OUTPUT);
  Blynk.begin(auth, ssid, pass);
  dht.begin();
  delay(5000); // give time to settle

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

}


BLYNK_CONNECTED() {
  if (isFirstConnect) {
    Blynk.syncAll();

    isFirstConnect = false;
  }
}



BLYNK_WRITE(V3)
{
  int pinValue = param.asInt(); // Assigning incoming value from pin V3 to a variable
  if (pinValue == 1) {
    watermixBLOOM();

    //    digitalWrite(LED1, HIGH); // Turn LED on. KUKITUS.
  } else {

    watermixVEG();
    //    digitalWrite(LED1, LOW); // Turn LED off. VEGETYS
    //  }
  }
}


void watermixBLOOM()
{

  sensorValue = analogRead(water_sensor_a);
  Serial.println(sensorValue);

  if (sensorValue >= 151)

  {
    //      Blynk.virtualWrite(sensorValue);

    Blynk.virtualWrite(V1, "Watering BLOOM-mix");
    digitalWrite(LED1, HIGH); // Turn LED on. KUKITUS.
    delay (1000);
    digitalWrite(LED1, LOW); // Turn LED on. KUKITUS.
    delay (1000);

    digitalWrite(LED1, HIGH); // Turn LED on. KUKITUS.
    delay (1000);
    digitalWrite(LED1, LOW); // Turn LED on. KUKITUS.
    delay (1000);
    // watermixBLOOM CODE HERE

  } else {

  }

}

void watermixVEG()
{

  sensorValue = analogRead(water_sensor_a);
  Serial.println(sensorValue);

  if (sensorValue >= 151)

  {
    //      Blynk.virtualWrite(sensorValue);

    Blynk.virtualWrite(V1, "Watering VEG-mix");

    digitalWrite(LED1, HIGH); // Turn LED on. KUKITUS.
    delay (500);
    digitalWrite(LED1, LOW); // Turn LED on. KUKITUS.
    delay (500);

    digitalWrite(LED1, HIGH); // Turn LED on. KUKITUS.
    delay (500);
    digitalWrite(LED1, LOW); // Turn LED on. KUKITUS.
    delay (500);
    digitalWrite(LED1, HIGH); // Turn LED on. KUKITUS.
    delay (500);
    digitalWrite(LED1, LOW); // Turn LED on. KUKITUS.
    delay (500);

    digitalWrite(LED1, HIGH); // Turn LED on. KUKITUS.
    delay (500);
    digitalWrite(LED1, LOW); // Turn LED on. KUKITUS.
    delay (500);
    // watermixVEG CODE HERE

  } else {

  }

}
void loop()
{
  Blynk.run(); // Initiates Blynk
  timer.run(); // Initiates SimpleTimer



  //Moisture sensor A related
  {

    sensorValue = analogRead(water_sensor_a);
    Serial.println(sensorValue);

    if (sensorValue >= 151)

    {
      //      Blynk.virtualWrite(sensorValue);

      Blynk.virtualWrite(V1, "LOW Water!");
      Blynk.syncVirtual(V3);
    }


    else if (sensorValue >= 71 && sensorValue < 150)

    {

      //   Blynk.virtualWrite(sensorValue);

      Blynk.virtualWrite(V1, "MID Water");

    }

    else if (sensorValue < 70)

    {

      {
        //       Blynk.virtualWrite(sensorValue);

        Blynk.virtualWrite(V1, "FULL Water");



      }

    }


  }



}