Arduino project with blynk via usb

Hello,
I have an issue regarding the conection between Arduino Uno and Blynk via USB.
I am trying to project an irrigation system, using an electrical pump, a relay, DHT11 sensor , soil moisture sensor and a sensor for water level. I tried to use this code

// See the Device Info tab, or Template settings
#define BLYNK_TEMPLATE_ID           "TMPLxxxxxx"
#define BLYNK_DEVICE_NAME           "Device"
#define BLYNK_AUTH_TOKEN            "YourAuthToken"


// Comment this out to disable prints and save space
#define BLYNK_PRINT SwSerial

#include <SoftwareSerial.h>
SoftwareSerial SwSerial(10, 11); // RX, TX

#include <BlynkSimpleStream.h>
char auth[] = "a8q7LrCYekGdx8z2mWd0aJ7C6WxR275g";




//senzor umititate sol:
int Pin_Releu = 8;
int Pin_Senzor = A1;  
int valoare_iesire;


//senzor nivel apa+buzzer:
#define POWER_PIN  7
#define SIGNAL_PIN A0

int value = 0; // variable to store the sensor value

const int buzzer = 9; //buzzer to arduino pin 9


#include <DHT.h>
#define DHTPIN A3          // What digital pin we're connected to
#define DHTTYPE DHT11     
DHT dht(DHTPIN, DHTTYPE);

BlynkTimer timer;

void senzorUmidSol()
{
  pinMode(Pin_Releu, OUTPUT);
  pinMode(Pin_Senzor, INPUT);
  Serial.println("Se citesc valorile de la senzori...");
  Serial.println(" ");

 valoare_iesire = analogRead(Pin_Senzor);
 //valoare_iesire = map(valoare_iesire,0,1023,0,100);

  Serial.println(valoare_iesire);

 if( valoare_iesire>600){
  digitalWrite(Pin_Releu, LOW);
  Serial.println("Asteptati, solul este udat..");
 }
 else
 {
  digitalWrite(Pin_Releu, HIGH);  
 Serial.println("Solul este umed.");    
 }

Blynk.virtualWrite(V1, valoare_iesire);
  
}

void senzorNivelApal()
{
  
  digitalWrite(POWER_PIN, HIGH);  // turn the sensor ON
  delay(10);                      // wait 10 milliseconds
  value = analogRead(SIGNAL_PIN); // read the analog value from sensor
  digitalWrite(POWER_PIN, LOW);   // turn the sensor OFF

  Serial.print("nivelul apei din vas: ");
  Serial.println(value);

  pinMode(buzzer, OUTPUT); // Set buzzer - pin 9 as an output
   


  if(value>=620)
        Serial.println("Este suficienta apa in vas");
        
  if(value>=600 && value<620)
        Serial.println("Vasul de apa este aproape gol");
        
  if(value<600){
        Serial.println("Umpleti vasul cu apa");

        tone(buzzer,5000); // Send 1KHz sound signal...
        delay(2000);        // ...for 1 sec
        noTone(buzzer);     // Stop sound...
        delay(2000);        // ...for 1sec
  }
  

 Blynk.virtualWrite(V0, value);
}

void senzorPicaturi()
{
  int sensorReading = analogRead(A2);

  if(sensorReading >= 0 && sensorReading < 300) {
    Serial.println("Ploaie puternica");
    Blynk.notify("Ploaie puternica");
  }
  if(sensorReading >= 300 && sensorReading < 768 ){
    Serial.println("Ploaie");
    Blynk.notify("Ploaie");
  }
  
  if(sensorReading >= 768 && sensorReading < 1024) {
    Serial.println("Nu ploua");
    Blynk.notify("Nu ploua");
  }

  Blynk.virtualWrite(V2, sensorReading);
 }

void senzorDHT()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);
}



void setup()
{
  // Debug console
  SwSerial.begin(115200);

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

  dht.begin();

  // Setup a function to be called every second
 
timer.setInterval(2000L,senzorUmidSol);
delay(1000);
timer.setInterval(2000L, senzorNivelApal);
delay(1000);
timer.setInterval(2000L,senzorPicaturi);
delay(1000);
timer.setInterval(2000L,senzorDHT);
  delay(1000);
 
}

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

but when i run it on Arduino , Blynk does not show the values…
Without Blynk, the code works very well, so I think my problem is the connection with Blynk application.
When i run the code, if the soil is dry , the pump is running, but the values are not showing in the application. I think the problem is with “timer.setInterval” but i am not sure.
I would be gratefull if somebody can help me fixing this issue. Thank you!

*After i run the code în Arduino, i am calling the serial port (com3) in blynk-ser.bat ,to connect arduino with blynk app…

Please edit your post and add triple backticks ( ``` ) before and after your whole sketch.

1 Like

Is this your auth token, or one that’s left over from somewhere else?

If you’re copy/pasting this data from the Blynk web console…

then that line of code ought to be changed to:

char auth[] = BLYNK_AUTH_TOKEN;

Have you edited the USB connection script to connect to the Blynk IoT server by changing thois line:

set SERV_ADDR=blynk-cloud.com

to this:

set SERV_ADDR=blynk.cloud

Also, this is very bad…

You should read the “Staggering Timers” section of this tutorial…

Pete.

2 Likes

Thank you!!