Hi there,
I try to do a node mcu project, to read humidity and temp data from DHT (this thing works just great) and read my plant soil moisture (problem point)
Moisture sensor is connected to A0 pin and returns me ~200 in water and 400 dry, how to remap it to virtual pin to show data from 0 when dry to 100 when wet?
Here is my code
#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include "DHT.h"
#include <I2CSoilMoistureSensor.h>
#define DHTPIN 2 // The pin you connect to
#define DHTTYPE DHT11 // DHT 11 Change this if you have a DHT22
DHT dht(DHTPIN, DHTTYPE,16); // Change this to
int Sensorvalue=0;
int sensorPin=A0;
int sensorValuePercent=0;
SimpleTimer timer;
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "d4d23273fdcc459e85aff835ae30db28";
void setup()
{
Serial.begin(9600);
Blynk.begin(auth, "YK", "ykompan!");
timer.setInterval(2000, sendDHT);
}
void sendDHT()
{
//Read the Temp and Humidity from DHT
float h = dht.readHumidity();
float t = dht.readTemperature();
int hum = (int) h;
int tem = (int) t;
//Write values to V04 and V05
Blynk.virtualWrite(4, hum);
Blynk.virtualWrite(5, tem);
}
void Moisture()
{
Sensorvalue = analogRead(A0);
Sensorvalue = map(Sensorvalue, 0, 1023, 1023, 0);
sensorValuePercent = (Sensorvalue / 1023) * 100; // In Prozent
Blynk.virtualWrite(V1, Sensorvalue);
}
void loop()
{
Blynk.run();
timer.run();
}
Thank you in advance, im steel a noobie, and learning.
That guy did not post answer, and he haв it from 0 to 1230, he just wanted to turn it round, and I need to change a scale somehow, also tries mapping from that post, but receive no value to virtual pin 1 at all
#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include "DHT.h"
#include <I2CSoilMoistureSensor.h>
#define DHTPIN 2 // The pin you connect to
#define DHTTYPE DHT11 // DHT 11 Change this if you have a DHT22
DHT dht(DHTPIN, DHTTYPE,16); // Change this to
int x = analogRead(A0);
SimpleTimer timer;
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "d4d23273fdcc459e85aff835ae30db28";
void setup()
{
Serial.begin(9600);
Blynk.begin(auth, "YK", "ykompan!");
timer.setInterval(2000, sendDHT);
}
void sendDHT()
{
//Read the Temp and Humidity from DHT
float h = dht.readHumidity();
float t = dht.readTemperature();
int hum = (int) h;
int tem = (int) t;
//Write values to V04 and V05
Blynk.virtualWrite(4, hum);
Blynk.virtualWrite(5, tem);
timer.setInterval(2000, moisture);
}
void moisture(){
int x = analogRead(A0);
int y = map(x, 600, 230, 0, 100);
if (y > 100)
{
y = 100;
}
Blynk.virtualWrite(V1, y);
}
void loop()
{
Blynk.run();
timer.run();
}`Preformatted text`
Glad we got there and thanks for posting your working sketch.
Edit: you shouldn’t need this bit:
if (y > 100)
{
y = 100;
}
as the map() function restricts the range of 0 to 100. I was using the x variable (not y) in the if statement as I don’t have a sensor so I was just simulating the values.
In my simulation I now have the following, using 250 as it makes the +50 easier to work with.
int x = 250; // global variable
int y = map(x, 600, 250, 0, 100);
if(x < 600){
x= x + 50;
}
else{
x = 250;
}