Auto/manual Mode on Blynk using Wemos D1 mini

Hi Blynker, please help
I am doing a project as below:

  • Reading temperature and humidity.
  • Control Pump in auto/manual mode on blynk.
    Code:
#define BLYNK_PRINT Serial
#include <Blynk.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include "DHTesp.h"

#ifdef ESP32
#pragma message(THIS EXAMPLE IS FOR ESP8266 ONLY!)
#error Select ESP8266 board.
#endif

DHTesp dht;
//**********************************************
char auth[] = "";
char ssid[] = "";
char pass[] = "";
//********************************************
const int ledPin1 = D1;
const int ledPin2 = D7;
const int btnPin1 = D5;
const int btnPin2 = D6;

//*******************************************
BlynkTimer timer;
//******************************************
void ManualMode();

int ledState1 = LOW;
int ledState2 = LOW;
int btnState1 = HIGH;
int btnState2 = HIGH;
// Every time we connect to the cloud...
BLYNK_CONNECTED() {
  // Request the latest state from the server
  Blynk.syncVirtual(V2);
  Blynk.syncVirtual(V3);

  // Alternatively, you could override server state using:
  //Blynk.virtualWrite(V2, ledState);
}

// When App button is pushed - switch the state
BLYNK_WRITE(V2) {
  ledState1 = param.asInt();
  digitalWrite(ledPin1, ledState1);
}

BLYNK_WRITE(V3) {
  ledState2 = param.asInt();
  digitalWrite(ledPin2, ledState2);
}

void ManualMode()
{
  if (digitalRead(btnPin1) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btnState1 != LOW) {

      // Toggle LED state
      ledState1 = !ledState1;
      digitalWrite(ledPin1, ledState1);

      // Update Button Widget
      Blynk.virtualWrite(V2, ledState1);
    }
    btnState1 = LOW;
  } else {
    btnState1 = HIGH;
  }
  //*********************************************
  if (digitalRead(btnPin2) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btnState2 != LOW) {

      // Toggle LED state
      ledState2 = !ledState2;
      digitalWrite(ledPin2, ledState2);

      // Update Button Widget
      Blynk.virtualWrite(V3, ledState2);
    }
    btnState2 = LOW;
  } else {
    btnState2 = HIGH;
  }
}


void setup()
{
  Serial.begin(115200);
  Serial.println();
  Serial.println("Status\tHumidity (%)\tTemperature (C)\t(F)\tHeatIndex (C)\t(F)");
  String thisBoard= ARDUINO_BOARD;
  Serial.println(thisBoard);

  // Autodetect is not working reliable, don't use the following line
  // dht.setup(17);
  // use this instead: 
  dht.setup(D4, DHTesp::DHT11); // Connect DHT sensor to GPIO 17
  Blynk.begin(auth, ssid, pass);
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(btnPin1, INPUT_PULLUP);
  pinMode(btnPin2, INPUT_PULLUP);
  digitalWrite(ledPin1, ledState1);
  digitalWrite(ledPin2, ledState2);
}

void loop()
{
  delay(dht.getMinimumSamplingPeriod());

  float humidity = dht.getHumidity();
  float temperature = dht.getTemperature();

  Serial.print(dht.getStatusString());
  Serial.print("\t");
  Serial.print(humidity, 1);
  Serial.print("\t\t");
  Serial.print(temperature, 1);
  Serial.print("\t\t");
  Serial.print(dht.toFahrenheit(temperature), 1);
  Serial.print("\t\t");
  Serial.print(dht.computeHeatIndex(temperature, humidity, false), 1);
  Serial.print("\t\t");
  Serial.println(dht.computeHeatIndex(dht.toFahrenheit(temperature), humidity, true), 1);
  delay(2000);
  Blynk.virtualWrite(V5, humidity);
  Blynk.virtualWrite(V6, temperature);
  Blynk.run();
  timer.run();
  Blynk.syncAll();
  
}

@IoTcreator 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.

Mr. Pete, thanks so much! please advise me about how I can create auto/manual mode, also SelectMode.

I would start by reading this, as your current code breaks every Blynk rule in the book:

Once you’ve trestructured your code and have it working correctly then I’d suggest that you re-post your updated code (correctly formatted this time) along with a detailed description of exactly what it is that you’re trying to achieve and what you’ve tried already.

Pete.

Mr.Pete,

  • With ManualMode worked well.
  • Now I want to make AutoMode as temperature 7 humidity.
  • and a select mode for change from auto to manual.

As I said, we look forward to seeing your revised code and detailed description of exactly what it is you are trying to achieve.

Pete.

Dear Mr.Pete,

  • I tried to code this, but it not working,
    ‘temperature’ was not declared in this scope

#define BLYNK_PRINT Serial
#include <Blynk.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include "DHTesp.h"

#ifdef ESP32
#pragma message(THIS EXAMPLE IS FOR ESP8266 ONLY!)
#error Select ESP8266 board.
#endif

DHTesp dht;
//**********************************************
char auth[] = "";
char ssid[] = "";
char pass[] = "";
//********************************************
const int ledPin1 = D1;
const int ledPin2 = D7;
const int btnPin1 = D5;
const int btnPin2 = D6;

//*******************************************
BlynkTimer timer;
//***************************************
void ManualMode();

int ledState1 = LOW;
int ledState2 = LOW;
int btnState1 = HIGH;
int btnState2 = HIGH;
// Every time we connect to the cloud...
BLYNK_CONNECTED() {
  // Request the latest state from the server
  Blynk.syncVirtual(V2);
  Blynk.syncVirtual(V3);

  // Alternatively, you could override server state using:
  //Blynk.virtualWrite(V2, ledState);
}

// When App button is pushed - switch the state
BLYNK_WRITE(V2) {
  ledState1 = param.asInt();
  digitalWrite(ledPin1, ledState1);
}

BLYNK_WRITE(V3) {
  ledState2 = param.asInt();
  digitalWrite(ledPin2, ledState2);
}

void ManualMode()
{
  if (digitalRead(btnPin1) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btnState1 != LOW) {

      // Toggle LED state
      ledState1 = !ledState1;
      digitalWrite(ledPin1, ledState1);

      // Update Button Widget
      Blynk.virtualWrite(V2, ledState1);
    }
    btnState1 = LOW;
  } else {
    btnState1 = HIGH;
  }
  //*********************************************
  if (digitalRead(btnPin2) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btnState2 != LOW) {

      // Toggle LED state
      ledState2 = !ledState2;
      digitalWrite(ledPin2, ledState2);

      // Update Button Widget
      Blynk.virtualWrite(V3, ledState2);
    }
    btnState2 = LOW;
  } else {
    btnState2 = HIGH;
  }
}


void setup()
{
  Serial.begin(115200);
  Serial.println();
  Serial.println("Status\tHumidity (%)\tTemperature (C)\t(F)\tHeatIndex (C)\t(F)");
  String thisBoard= ARDUINO_BOARD;
  Serial.println(thisBoard);

  // Autodetect is not working reliable, don't use the following line
  // dht.setup(17);
  // use this instead: 
  dht.setup(D4, DHTesp::DHT11); // Connect DHT sensor to GPIO 17
  Blynk.begin(auth, ssid, pass);
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(btnPin1, INPUT_PULLUP);
  pinMode(btnPin2, INPUT_PULLUP);
  digitalWrite(ledPin1, ledState1);
  digitalWrite(ledPin2, ledState2);
}

void AutoMode();
int settemp = 30;

void AutoMode()
{if (temperature>setemp){
   btnState1 = LOW;
  } else {
    btnState1 = HIGH;
}

void loop()
{
  delay(dht.getMinimumSamplingPeriod());

  float humidity = dht.getHumidity();
  float temperature = dht.getTemperature();

  Serial.print(dht.getStatusString());
  Serial.print("\t");
  Serial.print(humidity, 1);
  Serial.print("\t\t");
  Serial.print(temperature, 1);
  Serial.print("\t\t");
  Serial.print(dht.toFahrenheit(temperature), 1);
  Serial.print("\t\t");
  Serial.print(dht.computeHeatIndex(temperature, humidity, false), 1);
  Serial.print("\t\t");
  Serial.println(dht.computeHeatIndex(dht.toFahrenheit(temperature), humidity, true), 1);
  delay(2000);
  Blynk.virtualWrite(V5, humidity);
  Blynk.virtualWrite(V6, temperature);
  Blynk.run();
  timer.run();
  Blynk.syncAll();
  
}

Henry.

I’ve given you some advice, which you’ve chosen not to follow, so I’ll take a step back and see if any other forum members want to step in and try to convince you that your void loop is the first thing you need to tackle.

Pete.

Sorry, I am a new member.
if have any problem, please advise me!
thanks so much!

I suggest start by doing as Pete says and read the best practices documentation.

It looks like you have declared timer but I don’t see any timers in your code. If you want something to happen every so many milliseconds you should be doing that using timers. You need to get rid of all calls to delay in “Loop()”. That would be a good start.

You also shouldn’t need to do a Blynk.syncAll() in your loop. Typically a sync is only done after the device first connects to Blynk. For example inside of BLYNK_CONNECTED function.

1 Like

You can also have a look at this similar project

Thanks so much! I will rework step by step it. I hope that could find what’s wrong and how can I fix the problem.
thanks All

Thanks so much,