Converting Analog Data to Percentage

Hi fellow Blynkers,

I am quite new to the whole Blynk experience but have had some reasonable success getting started. I would like to build a plant soil moisture system that does not rely on MQTT and Blynk looks right for the job, I am however running into some problems with converting the data from the analog sensor into percentage.

Hardware:

  • WEMOS D1 Mini
  • Capacitive soil moisture sensor(generic) versio 1.0

I am able to read the sensor data using the following sketch:

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


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

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

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

// Use Virtual pin 5 for uptime display
#define PIN_UPTIME V5
#define PIN_CALCULATION V6

// This function tells Arduino what to do if there is a Widget
// which is requesting data for Virtual Pin (5)
BLYNK_READ(PIN_UPTIME)
{
  // This command writes Arduino's uptime in seconds to Virtual Pin (5)
  Blynk.virtualWrite(PIN_UPTIME, millis() / 1000);
  
}

void setup()
{
  // Debug console
  Serial.begin(9600);
  float moisture = PIN_UPTIME;
  Serial.println(moisture);
  
  Blynk.begin(auth, ssid, pass);
  // 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();
  
 
}

What I am struggling with is converting this data to a percentage. The data range is between 540(dry) and 320(wet) and I would like to display this as percentage. So far my attempts to create a variable from PIN_UPTIME have not worked.

float soilMoisture = PIN_UPTIME

Does not return a value.

Thanks in advance.

You should be reading the value from the analogue pin, using an analogRead command the writing this to Blynk virtual pins, not attempting to read a time based value from the Blynk digital pin.

Pete.

1 Like