DHT11 with moisture sensor using ESP8266

Please Help, Im trying to connect DHT11 with a Moisture sensor the code up loads DH11 shows the reading on the gauge but the moisture sensor doesnt show anything on the gauge when placed into a cup of water, this is my code

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <DHT.h>


char auth[] = "*************"; //Auth code 


char ssid[] = "********";  //WIFI Name
char pass[] = "***********";  //WIFI Password

#define DHTPIN 2          // Digital pin 4
#define soilPin 0

// Uncomment whatever type you're using!
#define DHTTYPE DHT11     // DHT 11

DHT dht(DHTPIN, DHTTYPE);
SimpleTimer timer;

void sendSensor()
{
  float h = dht.readHumidity();    // in %
  float t = dht.readTemperature(); // in C

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
}

  Blynk.virtualWrite(V5, h);  //V5 is for Humidity
  Blynk.virtualWrite(V6, t);  //V6 is for Temperature
}

void sendsoilSensor()
{
   float s = analogRead(soilPin);

   if (isnan(s)) {
     Serial.println("Failed to read from Gas sensor!");
     return;
}
  
   Blynk.virtualWrite(V7, s);
}

void setup()
{
  Serial.begin(9600); // See the connection status in Serial Monitor
  Blynk.begin(auth, ssid, pass);

  dht.begin();

  timer.setInterval(1000L, sendSensor);
}

void loop()
{
  Blynk.run(); // Initiates Blynk
  timer.run(); // Initiates SimpleTimer
}
1 Like

Please properly format all posted code in this forum… I fixed your already…

Blynk%20-%20FTFC

You don’t appear to be calling that function with anything… thus no worky :stuck_out_tongue_winking_eye:

Add in this (adjusted 50ms off, so that the timing is not coinciding with your other sensor)…

  timer.setInterval(1050L, sendsoilSensor);
1 Like

I got the sensor to work now, however its reading 1024 when dry and when wet it drops down to 500 something, i would like to make it to 0 dry and max 100 when wet any ideas how to do this ?

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <DHT.h>


char auth[] = "b3abafaab77243a88a0"; //Auth code 


char ssid[] = "BEL";  //WIFI Name
char pass[] = "E4E49";  //WIFI Password

#define DHTPIN 2          // Digital pin 4
#define soilMoisterPin A0
#define soilMoisterVcc D4
int soilMoister = 0;



// Uncomment whatever type you're using!
#define DHTTYPE DHT11     // DHT 11

DHT dht(DHTPIN, DHTTYPE);
SimpleTimer timer;

void sendSensor()
{
  float h = dht.readHumidity();    // in %
  float t = dht.readTemperature(); // in C

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
}

  Blynk.virtualWrite(V5, h);  //V5 is for Humidity
  Blynk.virtualWrite(V6, t);  //V6 is for Temperature
}

void getSoilMoisterData(void)
{
  soilMoister = 0;
  digitalWrite (soilMoisterVcc, HIGH);
  delay (500);
  int N = 3;
  for(int i = 0; i < N; i++) // read sensor "N" times and get the average
  {
    soilMoister += analogRead(soilMoisterPin);   
    delay(150);
  }
  digitalWrite (soilMoisterVcc, LOW);
  soilMoister = soilMoister/N; 
  Serial.println(soilMoister);
  soilMoister = map(soilMoister, 0, 1023, 1023, 0);
  
   
}


void setup()
{
  Serial.begin(9600); // See the connection status in Serial Monitor
  Blynk.begin(auth, ssid, pass);

  dht.begin();

  timer.setInterval(1000L, sendSensor);
}

void loop()
{
  Blynk.run(); // Initiates Blynk
  timer.run(); // Initiates SimpleTimer
}

Once again, you’ve posted your code without the necessary backticks, despite @Gunner explaining how it should be done:
Blynk%20-%20FTFC

Click the pencil icon at the bottom of your latest post and edit it to include back ticks.

Pete.

1 Like

Ok its done now sorry first time on here, if I short the two legs of the moisture sensor is goes to 0 but when I remove short it goes to 1024 I would like to make it the opposite and using value 100. No matter what I do in the mapping section it doesn’t change a thing

That is why there is a Welcome Topic for new users to read so they don’t use the “new here” excuse :stuck_out_tongue_winking_eye:

As for your issue… we don’t really teach programming here… but try to focus on helping new users understand how Blynk works.

Not exactly sure if related to your issue… but you can reverse a Widgets MIN/MAX settings so they respond to reverse input. Or even negative settings, etc… experiment and see.

ok ty

1 Like

I see you edited your post :wink: … but FYI, I moderate the “trolls” that beg for help or get cranky when silly things like “rules” interfere :stuck_out_tongue_winking_eye:

1 Like

Heheh happens after programming for 7 hours anyways I did get it to read 100 now with your previous comment but the gauge is fully highlighted at 0 do you have any fixes for that ?

1 Like

Sorry, I am not picturing exactly what data and settings you are using… I am multitasking with my own programming and doing poorly at both :blush:

Are you still reversing the data here as well as in the widget?

1 Like

so now I put it to soilMoister = map(soilMoister, 0, 100, 100, 0); and on the widget i made i pressed the button inbetween 0 100 the squiggly line thingy :stuck_out_tongue: but its still reading 100 if i reverse it it reads 0 but the widget is to 100%

1 Like

Just never use that squiggly thing :upside_down_face: … it rarely seems to work properly.

map() is the way to go.

Again, sorry but my mind is not picking up this simple issue… Long day programming blinky LEDs with lambda functions… very worthy stuff :rofl:

Not sure if it is related, but long ago I did some strangeness with widgets like this

  LVLval = map(analogRead(2), 0, 1023, -150, 150); // Mapped from POT
  Blynk.virtualWrite(V28, LVLval);  // Mapped sensor simulation value
  Blynk.virtualWrite(V26, LVLval);  // Upper Bar (0 to 150)
  Blynk.virtualWrite(V27, LVLval);  // Lower Bar (-150 to 0)

image

The key for this widget was the Flip Axis… perhaps it will work better than your gauge?

1 Like

lol idk what its called its in the screen when you edit widget but i got it to work now it reads 0%, when i put in the probe the moisture rises. BUT the issue now is the gauge is at max when reading 0 moisture and reading 75% moisture its at a quarter way up the gauge. and what is a lambda function like i said i have no clue what any of these things are everything is trial and error for me

1 Like

It is called Data Mapping… and best ignored from now on (IMHO :wink: ) especially if also using map()

Sound like issues between the map() reversing and the “squiggly” thing… forget them and focus on the Widgets MIN/MAX values… if you set it to 100-0 and not bother with even map() reversing, what happens?

Not relevent :smiley: but this is what the end result was - C++ Blynk (Legacy) - Code Examples for Basic Tasks - #52 by Gunner

1 Like

when i set it to 100-0 and not bother with map() reversing the gauge reads 100% at 0 and 0% at 100

1 Like

technically speaking it works it just bugs me that gauge is reading 100% when the value is 0

1 Like

Well… it is doing what you ask, I think… the Gauge Widget doesn’t have a “flip axis” option if that is what you are looking for (AKA fill from right hand side instead of left hand). For that you would have to use the Level Widget

1 Like

gauge works fine i will just keep the moisture probes in the dirt so it will always read properly i just don’t know why my mapping isn’t changing anything i will have to trouble shoot code thank you for your help i really appreciate it

1 Like

Hmmm… I would have to built a sketch to test this… but it seems that this is your issue, as how can the map() adjust the variable soilMoister at the same time as reading the same variable?

I always have a 2nd variable to handle the mapping result

mapedSoilMoister = map(soilMoister, 0, 1023, 1023, 0);
1 Like

I just tried that it didnt work maybe i did it wrong tho ? but if you have the time and you get it going ill be checking this thread, once again thank you for all your help!

1 Like