BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Soil Moisture Sensor with gauge

Right, post the full code and lets have a look.

Hello

the is the code for blynk

#include <SoftwareSerial.h>
SoftwareSerial DebugSerial(2, 3); // RX, TX
#define BLYNK_PRINT DebugSerial
#include <BlynkSimpleStream.h>

int Sensorvalue=0;
int sensorPin=A0;
int sensorValuePercent=0;

WidgetLED led1(V1);
WidgetLED led2(V2);


char auth[] = "6044f1cd5a54cf287de89";

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

  Serial.begin(9600);
  Blynk.begin(Serial, auth);
}
void loop()
{
 Sensorvalue = analogRead (A0); 
 Sensorvalue = map (Sensorvalue,160,1023,1023,0); 
sensorValuePercent = (300/1023) * 100; // In Prozent
    
  Blynk.run();
  
 }

#upset
/Fettkeewl

Make sure you use THREE (3) backticks for the code… not ONE.

```
CODE
```

not

`
CODE
`

Right, first mistake is putting the sensor code into the loop, never a good idea and blynk will flood.

Use Simpletimer for reading the sensor every say 20-30mins (its soil moisture after all, it does not change every nanosecond, :slight_smile: )

Edit: Give me a few minutes to go collect a visitor then I’ll rewrite the code.

Oh not delay is used since blynk gets a fault so it does not work

Not exactly. He’s not performing Blynk.virtualWrites() so he not flooding anything. His arduino hardware will be happy enough performing an ADC read and map() in the loop.

The issue is he is relying on the app to read the hardware determined in the widget settings in the app. The INTERVAL time. Default is usually 1 second.

This is okay… but its kinda slack and only works while the app is open. So only good to play with.

@markop, you need to use some virtualpins. Copy the PushData.ino Example (also in your IDE menu). Set it up on your network, with your auth code etc and get it working.

Then change the widget to a gauge.

Then change this part of the code from this:

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

to this:

Blynk.virtualWrite(V5, analogRead(A0) );

Or just add a new line and change the V5 to V6 etc… then have both (why not eh).

This will update the gauge every 1 sec on the HARDWARE side… far more reliable.
Work from this example from now on and you will be okay.

Leave loop() as it is. Just the 2 lines.

This topic by @Costas is a great place to start! Blynk for Beginners and help with your project

Sorry, you’re right, the code was not tagged so chrome has not shown all of it, grrrr.

instead of

Sensorvalue = analogRead (A0);
Sensorvalue = map (Sensorvalue,160,1023,1023,0);
sensorValuePercent = (300/1023) * 100; // In Prozent

Blynk.run();

But

Blynk.virtualWrite(V5, analogRead(A0) );
Sensorvalue = map (Sensorvalue,160,1023,1023,0);
sensorValuePercent = (300/1023) * 100; // In Prozent
Blynk.run();

No, follow my example above!

Would it be possible that you please me the whole code mailen would be nice beginners on the area

You need to use some virtualpins. Copy the PushData.ino Example (also in your IDE menu). Set it up on your network, with your auth code etc and get it working.

Then change the widget to a gauge.

Then change this part of the code from this:

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

to this:

Blynk.virtualWrite(V5, analogRead(A0) );

#include <SoftwareSerial.h>
SoftwareSerial DebugSerial(2, 3); // RX, TX
#define BLYNK_PRINT DebugSerial
#include <BlynkSimpleStream.h>

int Sensorvalue=0;
int sensorPin=A0;
int sensorValuePercent=0;

Blynk.virtualWrite(V5, analogRead(A0) );

WidgetLED led1(V1);
WidgetLED led2(V2);


char auth[] = "6044f1c889";

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

  Serial.begin(9600);
  Blynk.begin(Serial, auth);
}
void loop()
{
 Sensorvalue = analogRead (A0); 
 Sensorvalue = map (Sensorvalue,160,1023,200,0); 
sensorValuePercent = (300/1023) * 100; // In Prozent
    
  Blynk.run();
  
 }

#upset
/Fettkeewl

1 Like

you will make people upset if you don’t format your code properly with three ``` backticks at beginning and end…

1 Like

The code is so correct for gauge
Because in the case of a soil moisture sensor, it indicates dryness instead of 0 1023 and in the case of moist 0 instead of 1023

#include <SoftwareSerial.h>
SoftwareSerial DebugSerial(2, 3); // RX, TX
#define BLYNK_PRINT DebugSerial
#include <BlynkSimpleStream.h>
#include <SimpleTimer.h>

int Sensorvalue=0;
int sensorPin=A0;
int sensorValuePercent=0;

SimpleTimer timer;

char auth[] = "6044f1c85";

void setup()
{
  // Debug console
  DebugSerial.begin(9600);
timer.setInterval(3000L, blinkLedWidget);
  Serial.begin(9600);
  Blynk.begin(Serial, auth);
}

void blinkLedWidget()
{
Blynk.virtualWrite(V5, analogRead(A0) );
 Sensorvalue = map(Sensorvalue, 0,1023,1023,0);
sensorValuePercent = (300/1023) * 100; // In Prozent
  }
void loop()    
{
  Blynk.run();
  timer.run();
 }

^ Comment inline with your code.

Also does not work the same

Sensorvalue = map(Sensorvalue, 1023,0,1023,0);

or

Sensorvalue = map(Sensorvalue, 1023,0,0,1023);

Also

Should this:

sensorValuePercent = (300/1023) * 100;

Not be:

sensorValuePercent = (Sensorvalue/1023) * 100;

Mark, what is the raw values for analogread(A0) dry and wet?

dry is 1023 instead of 0

wet is 0 instead of 1023

#include <SoftwareSerial.h>
SoftwareSerial DebugSerial(2, 3); // RX, TX
#define BLYNK_PRINT DebugSerial
#include <BlynkSimpleStream.h>
#include <SimpleTimer.h>

int Sensorvalue=0;
int sensorPin=A0;
int sensorValuePercent=0;


WidgetLED led1(V1);
WidgetLED led2(V2);
SimpleTimer timer;

char auth[] = "6044f1c858a547d5a54cf14a1287de89";

void setup()
{
  // Debug console
  DebugSerial.begin(9600);
timer.setInterval(3000L, blinkLedWidget);
  Serial.begin(9600);
  Blynk.begin(Serial, auth);
}

void blinkLedWidget()
{
Blynk.virtualWrite(V5, analogRead(A0) );
 Sensorvalue = map(Sensorvalue, 0,1023,0,1023);
sensorValuePercent = (Sensorvalue/1023)* 100; // In Prozent
  }
void loop()    
{
  Blynk.run();
  timer.run();
 }

Does not work with blynk