Help with Sensor + Motor with Virtual Pins (Beginner) (NodeMCU)

Hey guys I am very new to coding and would love some help with my project. So I have a NodeMcu connected to a DC motor and Humidity sensor. Basically, I want to read the sensor data and if it’s below a certain number then turn on the motor for 10 seconds and send a notification to my phone that the motor is on. Preferably I would like to take advantage of the Blynk virtual pins for the humidity sensor. I would love some help with this project (keep in mind I’m a complete beginner).

Sensor: A1

Motor: D1

Here is my code so far (it makes no sense and probably doesn’t do anything):

[Unformatted code removed by moderator]

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

Don’t put code that does work in the void loop.

Make a routine to do the work and set a timer that runs the code or use interrupts to run the code.

This code will have a problem as the analogRead will cause a disconnect due to too many requests to the server.

Here is the formatted code



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

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

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "xxxxxxxx";
char pass[] = "xxxxxxxx";
int humidity = A0;
BLYNK_WRITE(V1)
{
  int pinValue = param.asInt();
  Serial.print(" V1 ");
  Serial.println(pinValue);
}

void setup()
{
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);
  pinMode(humidity, INPUT);
}

void loop()
{
  int h = analogRead(humidity);
  if (h < 40)

  {
    Blynk.virtualWrite(1, HIGH);
  }
  else {

    Blynk.virtualWrite(1, LOW);
  }
  Blynk.run();
}```

Read this…

http://help.blynk.cc/en/articles/2091699-keep-your-void-loop-clean

Pete.

Okay so here is the code that works without blynk. Now I want to do the exact same thing but with blynk. Help would be great.

int motorPin = 5;      // select the pin for the LED
int sensorValue = 0;  // variable to store the value coming from the sensor
int dryValue = 40;
int wetValue = 50;

void setup() {
  // declare the ledPin as an OUTPUT:
  pinMode(motorPin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  // read the value from the sensor:
  sensorValue = analogRead(sensorPin);
  if (sensorValue < dryValue) {
    Serial.println("motor on");
    digitalWrite(motorPin, HIGH);

  }
  else {
    digitalWrite(motorPin, LOW);
    Serial.println("motor off");
  }

}```

So read the “keep your void loop clean” document that I linked to, move the code out of your void loop and into a function, and call it with a timer.

Pete.

how do I call it with a timer?

It’s all explained in the “keep your void loop clean” document that I linked to earlier. Have you read it?

Hint - it’s in section 2

Pete.

Okay is this correct?



#include <SPI.h>
#include <WiFi.h>
#include <BlynkSimpleWifi.h>

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

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

int sensorPin = A0;    // select the input pin for the potentiometer
int motorPin = 5;      // select the pin for the LED
int sensorValue = 0;  // variable to store the value coming from the sensor
int dryValue = 40;
int wetValue = 50;
BlynkTimer timer; // Announcing the timer

void setup()
{
  // Debug console
  Serial.begin(9600);
  pinMode(motorPin, OUTPUT);
  Blynk.begin(auth, ssid, pass);
  timer.setInterval(1000L, motor); //timer will run every sec
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);
}

void loop()
{
  Blynk.run();
  timer.run();        // run timer every second
}

void motor()
{
  sensorValue = analogRead(sensorPin);
  if (sensorValue < dryValue) {
    Serial.println("motor on");
    digitalWrite(motorPin, HIGH);
    Blynk.notify("Watering Plants");
  }
  else {
    digitalWrite(motorPin, LOW);
    Serial.println("motor off");
    Blynk.notify("Plants Watered");
  }
}```

Great, except that you’ve now added a Blynk.notify in there in each of the if conditions.

This means that all the time the device is powered-on, it will attempt to send you a notification once every second. I say “attempt” because the documentation for the Push Notification widget says…

Limitations:

  • Maximum allowed body length is 120 symbols;
  • Every device can send only 1 notification every 5 seconds;

As the notification process uses third part service (provided by Google if I remember correctly), you’ll probably find that your device gets blocked from sending notifications - at least temporarily.

You need to use a flag to indicate that each of the two different notification types have been sent, and not re-send them if this is the case. The flag will be reset once the opposite ‘if’ statement becomes true.

Pete.

so I can move the notifications under another void so that I can call on it to run every 10 seconds or so. Does that work?

Why would you want to be bombarded with notifications every 10 seconds, 24 hours per day, telling you that your plants are either being watered or that they aren’t being watered?

I’ve explained that you need to use two flags, one to indicate that the “watering commenced” alert has been sent and the other to tell you that the “watering ended” alert has been sent, and that these flags need to be checked (with an if statement) before another alert is sent.

Pete.

So here is the fixed code and it seems to be working perfectly. But I just have one last question. How do I make the button in the Blynk app have priority over this code below? That would be great, thank you once again!

#include <BlynkSimpleEsp8266.h>

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

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

int sensorPin = A0;    // select the input pin for the potentiometer
int motorPin = D5;      // select the pin for the LED
int sensorValue = 0;  // variable to store the value coming from the sensor
int dryValue = 40;
int wetValue = 50;
int notified = 0;
BlynkTimer timer; // Announcing the timer

void setup()
{
  // Debug console
  Serial.begin(9600);
  pinMode(motorPin, OUTPUT);
  Blynk.begin(auth, ssid, pass);
  timer.setInterval(1000L, motor); //timer will run every sec
  timer.setInterval(1000L, notification); //timer will run every sec
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);
}

void loop()
{
  Blynk.run();
  timer.run();        // run timer every second
}

void motor()
{
  sensorValue = analogRead(sensorPin);
  if (sensorValue < dryValue) {
    Serial.println("motor on");
    digitalWrite(motorPin, HIGH);

  }
  else {
    digitalWrite(motorPin, LOW);
    Serial.println("motor off");

  }
}

void notification()
{
  if (sensorValue < dryValue && notified == 0){ // check to see if flag is also set
  notified = 1; 
  Blynk.email("bandrewstanger@gmail.com", "ALARM", "Plant's Watering");
  Blynk.notify("Plant's Watering");
} else if(sensorValue > wetValue){ // set lower than 60... to stop the 59.9-60.0 bouncing.
  Blynk.notify("Plant's Watered"); 
  Blynk.email("bandrewstanger@gmail.com", "ALARM", "Plant's Watered");
  notified = 0;
}
}```

Which button in the Blynk app?
What is its function?
What pin is it connected to?

Pete.

Also probably don’t publish your actual passwords, ssid and auth tokens on the open network.