NEED HELP arduino project

Before creating the topic

  1. Search forum for similar topics
  2. Check http://docs.blynk.cc and http://help.blynk.cc/
  3. Add details :
    • Hardware model + communication type. For example: Arduino UNO with Ethernet Shield
    • Smartphone OS (iOS or Android) + version
    • Blynk server or local server
    • Blynk Library version
    • Add your sketch code. :point_up:Code should be formatted as example below.

Simply paste your code between ``` If you don’t format your code, your topic can be deleted by moderators.
Hey I am making this project where I take data from capacitive soil 1.2 sensor,Ii need help taking this sketch and sending it in Blynk app also when it falls below a certain value I want it to trigger a relay which while run for a specific time. Can some one help me. Thanks.

this is arduino sketch I have so far

const int AirValue = 650;   //you need to replace this value with Value_1
const int WaterValue = 390;  //you need to replace this value with Value_2
int intervals = (AirValue - WaterValue)/3;   
int soilMoistureValue = 0;
void setup() {
  Serial.begin(9600); // open serial port, set the baud rate to 9600 bps
}
void loop() {
soilMoistureValue = analogRead(A0);  //put Sensor insert into soil
if(soilMoistureValue > WaterValue && soilMoistureValue < (WaterValue + intervals))
{
  Serial.println("Very Wet");
}
else if(soilMoistureValue > (WaterValue + intervals) && soilMoistureValue < (AirValue - intervals))
{
  Serial.println("Wet");
}
else if(soilMoistureValue < AirValue && soilMoistureValue > (AirValue - intervals))
{
  Serial.println("Dry");
}
delay(50);
}

Start by reading this:

Then looking at some of the example sketches in Sketch Builder:
https://examples.blynk.cc/

Pete.

1 Like

First thank you.Yes I got that working but how would I make it trigger the relay if it reaches certain value. Again thanks.

Something like this?

const int VAL_PROBE = 0; // Analog pin 0
const int MOISTURE_LEVEL = 250; // the value after the LED goes ON
 
void setup() {
    Serial.begin(9600);
}
 
void LedState(int state) {
    digitalWrite(13, state);
}
 
void loop() {
    int moisture = analogRead(VAL_PROBE);
 
    Serial.println(moisture);
 
    if(moisture > MOISTURE_LEVEL) {
        LedState(HIGH);
    } else   {
        LedState(LOW);
    }
    delay(100);
}

or something like this?

#define BLYNK_PRINT DebugSerial
int relay = 8;
volatile byte relayState = LOW;
const int AirValue = 650;
const int WaterValue =390;
int soilMoistureValue =0;
// You could use a spare Hardware Serial on boards that have it (like Mega)
#include <SoftwareSerial.h>
SoftwareSerial DebugSerial(2, 3); // RX, TX

#include <BlynkSimpleStream.h>

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


void setup()
{
  pinMode(8,OUTPUT);
  pinMode(A0,INPUT);

  // Debug console
  DebugSerial.begin(9600);

  // Blynk will work through Serial
  // Do not read or write this serial manually in your sketch
  Serial.begin(9600);
  Blynk.begin(Serial, auth);
}

void loop()
{
  if (WaterValue >=400){
    digitalWrite (relay, HIGH);
  }
  else {
    digitalWrite (relay, LOW);   
  }
  Blynk.run();
  delay(100);
}

this is a clean loop :wink:

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

so your sketch must look like :

void setup()
{
  pinMode(8,OUTPUT);
  pinMode(A0,INPUT);

  // Debug console
  DebugSerial.begin(9600);

  // Blynk will work through Serial
  // Do not read or write this serial manually in your sketch
  Serial.begin(9600);
  Blynk.begin(Serial, auth);

  timer.setInterval(1000L, relay);
}

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

void relay(){
 if (WaterValue >=400){
    digitalWrite (relay, HIGH);
  }
  else {
    digitalWrite (relay, LOW);   
  }
}

Thank you, let me run it and see if it works. Is the relay part right in the sketch?

it is just an example
you have to incorporate your

int moisture = analogRead(VAL_PROBE);

and some other stuff

Is this correct? Thanks

on this sketch I get this error
‘int relay’ redeclared as different kind of symbol

int relay = 8;
volatile byte relayState = LOW;
const int AirValue = 650;
const int WaterValue =390;
int soilMoistureValue =0;
// You could use a spare Hardware Serial on boards that have it (like Mega)
#include <SoftwareSerial.h>
SoftwareSerial DebugSerial(2, 3); // RX, TX

#include <BlynkSimpleStream.h>

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

void setup()
{
  pinMode(8,OUTPUT);
  pinMode(A0,INPUT);

  // Debug console
  DebugSerial.begin(9600);

  // Blynk will work through Serial
  // Do not read or write this serial manually in your sketch
  Serial.begin(9600);
  Blynk.begin(Serial, auth);

  timer.setInterval(1000L, relay);
}

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

void relay(){
 if (WaterValue >=400){
    digitalWrite (relay, HIGH);
  }
  else {
    digitalWrite (relay, LOW);   
  }
}

Since we can’t see the entire sketch (all your Blynk libraries are missing) then we can’t tell if you have another declaration or not.

There is also an unlikely possibility that the name relay is being used as a keyword or something elsewhere? Try relayPin

BTW, if you are going to do this, then perhaps use const int and then actually use the name in lew of the number throughout the sketch…

pinMode(8,OUTPUT);

should be…

pinMode(relay,OUTPUT);

Also… this is unnecessary as the default nature of analog pins is INPUT

Sorry I didn’t understand

Still get the same thing.

int relay = 8;
volatile byte relayState = LOW;
const int AirValue = 650;
const int WaterValue =390;
int soilMoistureValue =0;
// You could use a spare Hardware Serial on boards that have it (like Mega)
#include <SoftwareSerial.h>
SoftwareSerial DebugSerial(2, 3); // RX, TX

#include <BlynkSimpleStream.h>

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

void setup()
{
  pinMode(relay,OUTPUT);
  pinMode(A0,INPUT);

  // Debug console
  DebugSerial.begin(9600);

  // Blynk will work through Serial
  // Do not read or write this serial manually in your sketch
  Serial.begin(9600);
  Blynk.begin(Serial, auth);

  timer.setInterval(1000L, relay);
}

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

void relay(){
 if (WaterValue >=400){
    digitalWrite (relay, HIGH);
  }
  else {
    digitalWrite (relay, LOW);   
  }
}

Don’t understand what? Life, the Universe and Everything? Or some particular part of my response?

Ah, sorry I found it… this should be at the top of the sketch.

I already have that in the sketch.

Yep, I thought so… you are using the same name relay for BOTH the pin and the function

Yes, but normally they are located at the TOP… thus when I didn’t see it there… :stuck_out_tongue_winking_eye:

Ok now its telling me
“timer” was not declared in this scope

int relayPin = 8;
volatile byte relayState = LOW;
const int AirValue = 650;
const int WaterValue =390;
int soilMoistureValue =0;
#include <BlynkSimpleStream.h>
// You could use a spare Hardware Serial on boards that have it (like Mega)
#include <SoftwareSerial.h>
SoftwareSerial DebugSerial(2, 3); // RX, TX

#include <BlynkSimpleStream.h>

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

void setup()
{
  pinMode(relayPin,OUTPUT);
  pinMode(A0,INPUT);

  // Debug console
  DebugSerial.begin(9600);

  // Blynk will work through Serial
  // Do not read or write this serial manually in your sketch
  Serial.begin(9600);
  Blynk.begin(Serial, auth);

  timer.setInterval(1000L, relay);
}

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

void relay(){
 if (WaterValue >=400){
    digitalWrite (relay, HIGH);
  }
  else {
    digitalWrite (relay, LOW);   
  }
}

Because it isn’t :wink: you need this BlynkTimer timer; … as per various examples using the BlynkTimer

And now you have the library referenced twice… and still NOT at the top of the sketch

Thanks, it seems it’s working. let me upload it and check it out. Thank you so much.

#include <BlynkSimpleStream.h>
#include <SoftwareSerial.h>
BlynkTimer timer;

int relayPin = 8;
volatile byte relayState = LOW;
const int AirValue = 650;
const int WaterValue =390;
int soilMoistureValue =0;
// You could use a spare Hardware Serial on boards that have it (like Mega)
SoftwareSerial DebugSerial(2, 3); // RX, TX


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

void setup()
{
  pinMode(relayPin,OUTPUT);
  pinMode(A0,INPUT);

  // Debug console
  DebugSerial.begin(9600);

  // Blynk will work through Serial
  // Do not read or write this serial manually in your sketch
  Serial.begin(9600);
  Blynk.begin(Serial, auth);

  timer.setInterval(1000L, relay);
}

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

void relay(){
 if (WaterValue >=400){
    digitalWrite (relay, HIGH);
  }
  else {
    digitalWrite (relay, LOW);   
  }
}