Soil Sensor with wifi unit

Hello everyone,
I am a newbie to some of the world of sensors and IoT for the arduino, and I am stumped.

I am trying an IoT Gardening project, ESP8266-ESP01 and I am using a simple soil sensor , DHt11 for temperature and humidity, and a Photoresistor for light readings. I am outputting it to Thinkspeak to create a digital dashboard.

I am struggling to get my soil sensor to change readings. It is in A0.
I have calibrated my soil sensor with the upper and lower values. In previous projects I have gotten the soil sensor to take accurate readings witout the wifi unit.

Also, I can get the DHT11 to read/write display in my Serial port, and on my web dashboard no problem. It is the soil sensor that I cannot get to change readings.

I am stumped. Does anyone have any suggestions to get the soil sensor to change/read correctly?
Any assistance anyone has would be greatly appreciated!

My code is here:
+++++++++++++++++++++++++++++++++++++++++++++++

#include <BearSSLHelpers.h>
#include <CertStoreBearSSL.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiAP.h>
#include <ESP8266WiFiGeneric.h>
#include <ESP8266WiFiGratuitous.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266WiFiScan.h>
#include <ESP8266WiFiSTA.h>
#include <ESP8266WiFiType.h>
#include <WiFiClient.h>
#include <WiFiClientSecure.h>
#include <WiFiClientSecureAxTLS.h>
#include <WiFiClientSecureBearSSL.h>
#include <WiFiServer.h>
#include <WiFiServerSecure.h>
#include <WiFiServerSecureAxTLS.h>
#include <WiFiServerSecureBearSSL.h>
#include <WiFiUdp.h>


#include <dummy.h>


#include <DHT.h>


#define DHTPIN 2          //DHT11 is connected to GPIO Pin 2


int sensor_pin = A0;
int output_value;




String apiKey = "________________";     //  Enter your Write API key from ThingSpeak
const char* ssid =  "________________";     // Enter your WiFi Network's SSID
const char* pass =  "_______________"; // Enter your WiFi Network's Password
const char* server = "api.thingspeak.com";
 
float humi;
float temp;
 
DHT dht(DHTPIN, DHT11);
WiFiClient client;
 
void setup() 
{
       Serial.begin(9600);
       delay(10);
       dht.begin();
       
       Serial.println("Connecting to ");
       Serial.println(ssid);
 
 
       WiFi.begin(ssid, pass);
 
      while (WiFi.status() != WL_CONNECTED) 
     {
            delay(100);
            Serial.print("*");
     }
      Serial.println("");
      Serial.println("***WiFi connected***");
 
}
 
void loop() 
{
  
      humi = dht.readHumidity();
      temp = dht.readTemperature();
      
      output_value = analogRead(sensor_pin);
      output_value = map(output_value,1023,165,0,100);
 
      if (client.connect(server,80))   //   "184.106.153.149" or api.thingspeak.com
      {  
              String sendData = apiKey+"&field1="+String(temp)+"&field2="+String(humi)+"&field3="+String(output_value)+"\r\n\r\n"; 
       
       //Serial.println(sendData);


       client.print("POST /update HTTP/1.1\n");
       client.print("Host: api.thingspeak.com\n");
       client.print("Connection: close\n");
       client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
       client.print("Content-Type: application/x-www-form-urlencoded\n");
       client.print("Content-Length: ");
       client.print(sendData.length());
       client.print("\n\n");
       client.print(sendData);


       Serial.print("Temperature: ");
       Serial.print(temp);
       Serial.print(" deg C. Humidity: ");
       Serial.print(humi);
       
       Serial.print(" Soil Moisture: ");
       Serial.print(output_value);
       
      
       Serial.println(" Connecting to Thingspeak.");
       }
      
      client.stop();


      Serial.println("Sending....");
  
 delay(10000);
}

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

@mskvasik

Other post/duplicate DELETED
Please do NOT cross post / duplicate as it wastes peoples time and efforts to have more than one post for a single topic.

Continued cross posting could result in a time out from the forum.

Could you also take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

Then you can modify your first post to more readable !

Thanks Bob for clarifying. Didn't mean to cross-post. :slight_smile:

Thanks!

Please edit your post to add code tags.

The map function explains the arguments as:
map(value, fromLow, fromHigh, toLow, toHigh)
Your settings are:
output_value = map(output_value,1023,165,0,100);
So, your calculated values (114 - 116) are out of range (0 - 100)
Values from 1023 (low) to 165 (high) should map to values 0 to 100.
There are 859 discrete steps in the raw value, 101 steps in the mapped value, so each mapped step equals ~8.5 raw steps.
The mapped value 115 equates to a raw value of 165-(15*8.5) = 37.5 and it's fairly consistent.
Are you sure you are measuring the analogue output of the moisture device and not some other input?

As an aside, you will find it useful to comment your code verbosely, and it would help us understand what your code should be doing. Using descriptive variable names helps too. The variable "output_value" is not intuitive, and we shouldn't have to figure out which variable collects the input of concern. Please note we also have no idea how you have connected things, and there could be an insight to the issue in how you have wired things.

Hello everyone,
I am kind of new at this, but I am trying to get a DHT11 to talk with an ESP8266 ESP-01. I am able to connect to wifi, and. I am trying to chart my sensor data to the site Thinkspeak.

I am having a slight problem with my sensor though. My DHT11 is reading 0 for temperature, and 0 for humidity in my Serial monitor, and on Thinkspeak. I have checked and rechecked the wiring, and it is correct. I am stumped. I have posted my code here. Do I need a begin statement in my void setup for the DHT11? If so, what would that look like.

I have tested my DHT as it is wired with an example reading sketch, and it works just fine. It is just not working with this code below.

Any insights that you might have would be appreciated.

#include <SoftwareSerial.h>
#include <dht11.h>
#define RX 2
#define TX 3
#define dht_apin 11 // Analog Pin sensor is connected to
dht11 dhtObject;
String AP = "APNAME";       // AP NAME
String PASS = "PASSWORD"; // AP PASSWORD
String API = "APIKEY";   // Write API KEY
String HOST = "api.thingspeak.com";
String PORT = "80";
int countTrueCommand;
int countTimeCommand; 
boolean found = false; 
int valSensor = 1;
  
SoftwareSerial esp8266(RX,TX); 
  
void setup() {
  Serial.begin(9600);
  esp8266.begin(115200);
  sendCommand("AT",5,"OK");
  sendCommand("AT+CWMODE=1",5,"OK");
  sendCommand("AT+CWJAP=\""+ AP +"\",\""+ PASS +"\"",20,"OK");
}

void loop() {
  
 String getData = "GET /update?api_key="+ API +"&field1="+getTemperatureValue()+"&field2="+getHumidityValue();
 sendCommand("AT+CIPMUX=1",5,"OK");
 sendCommand("AT+CIPSTART=0,\"TCP\",\""+ HOST +"\","+ PORT,15,"OK");
 sendCommand("AT+CIPSEND=0," +String(getData.length()+4),4,">");
 esp8266.println(getData);delay(1500);countTrueCommand++;
 sendCommand("AT+CIPCLOSE=0",5,"OK");
}


String getTemperatureValue(){

   dhtObject.read(dht_apin);
   Serial.print(" Temperature(C)= ");
   int temp = dhtObject.temperature;
   Serial.println(temp); 
   delay(50);
   return String(temp); 
  
}


String getHumidityValue(){

   dhtObject.read(dht_apin);
   Serial.print(" Humidity in %= ");
   int humidity = dhtObject.humidity;
   Serial.println(humidity);
   delay(50);
   return String(humidity); 
  
}

void sendCommand(String command, int maxTime, char readReplay[]) {
  Serial.print(countTrueCommand);
  Serial.print(". at command => ");
  Serial.print(command);
  Serial.print(" ");
  while(countTimeCommand < (maxTime*1))
  {
    esp8266.println(command);//at+cipsend
    if(esp8266.find(readReplay))//ok
    {
      found = true;
      break;
    }
  
    countTimeCommand++;
  }
  
  if(found == true)
  {
    Serial.println("OYI");
    countTrueCommand++;
    countTimeCommand = 0;
  }
  
  if(found == false)
  {
    Serial.println("Fail");
    countTrueCommand = 0;
    countTimeCommand = 0;
  }
  
  found = false;
 }

I would like to recommend using the the DHT sensor library by Adafruit. See how to use the library on Arduino - DHT11 tutorial

Is this a coincidence or did you just create a new profile?

https://forum.arduino.cc/index.php?topic=718457.0

Threads merged.