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

Displaying a percentage from my sensor

hi there
I needing a little help with my moisture sensor, everything works well except I want the gauge widget to show my results in a percentage between 0, 100. The code works well in Arduino, serial monitor showing correct percentage. When I try and transfer that to a widget in blynk all I get is either the raw data from the sensor (which I can understand why it does that) or absolutely nothing. I have tried many different ways trying to learn what others have done but I’m finding myself getting more confused when i believe its something simple. your help will be most great full

#define BLYNK_PRINT Serial


#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>

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

const int dry = 589;
const int wet = 295;

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



  Blynk.begin(auth);
  // You can also specify server:
  //Blynk.begin(auth, "blynk-cloud.com", 80);
  //Blynk.begin(auth, IPAddress(192,168,1,100), 8080);
  // For more options, see Boards_Ethernet/Arduino_Ethernet_Manual example
}

void loop()
{
  Blynk.run();

  int sensorVal = analogRead(A0);
  
  int percentageHumidity = map(sensorVal, wet, dry, 100, 0);

  Serial.print(percentageHumidity);
  Serial.println("%");
  delay(100);
}
BLYNK_WRITE(V1){
int sensorVal = param.asInt();
}

First of all, you shouldn’t have this in your void loop…

Instead, it should be in a separate function called by a timer. Read this:

Secondly, BLYNK_WRITE(V1) does not write data to virtual pin V1. You need to use Blynk.virtualWrite(V1, percentageHumidity) to do this.

Pete.

Thanks Pete
you are a legend thanks for pointing me in the right direction its working, this is what ive done

#define BLYNK_PRINT Serial


#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>

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

BlynkTimer timer;

const int dry = 589;
const int wet = 295;

void sendSensor()
{
int sensorVal = analogRead(A0);
  
  int percentageHumidity = map(sensorVal, wet, dry, 100, 0);
  Blynk.virtualWrite(V1, percentageHumidity);
  Serial.print(percentageHumidity);
  Serial.println("%");
  delay(100);
}
void setup()
{
  // Debug console
  Serial.begin(9600);



  Blynk.begin(auth);
  // You can also specify server:
  //Blynk.begin(auth, "blynk-cloud.com", 80);
  //Blynk.begin(auth, IPAddress(192,168,1,100), 8080);
  // For more options, see Boards_Ethernet/Arduino_Ethernet_Manual example

    // Setup a function to be called every second
  timer.setInterval(1000L, sendSensor);
}

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

One last thing to top this off, when I disconnect the power and power it back up I loose connection to my blynk application, I have to re-load the code for it to work again? I would like it to automatically connect back up. Can you point me in the best place to learn how this is achieved
thanks
spillamatt

That shouldnt be happening.
You do seem to be missing some code related to the Ethernet shield, where you define the CS pin (GPIO4) as an output pin then set it HIGH to disable the SD card.
The SD card and the Ethernet port share the same interface, so this is necessary to switch the SD card off so that the Ethernet port can be used.
If you take a look at the sketchbuilder you’ll see code like:

#define W5100_CS  10
#define SDCARD_CS 4
[in void loop......]
  pinMode(SDCARD_CS, OUTPUT);
  digitalWrite(SDCARD_CS, HIGH); // Deselect the SD card

Maybe this is the issue?

Having said that, I tried to persevere with the Arduino Ethernet shield for a long time, but couldn’t get around random lockups that would require the board to be reset to overcome the issue.
Have a read of this…

Pete.

thanks Pete
I added the code back into my sketch hopefully that will help.

thanks for the added information everything in there is so true I do own a
Arduino mega
nodemcu
esp01
wemos mega esp8266
I have no worries with the nodemcu you I use that often when im working with one sensor or one code at a time. the esp01 and the wemos mega im having trouble understanding how to do a firmware flash, but I havnt had time to fully investigate it. Im using the Ethernet shield for now just cause its easy and it works. Ill get there, thanks heaps
matt

1 Like