So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By vinicivs
#81871 Hi guys! My first post here!
I was following some tutorials, trying to send data from soil moisture, DHT22 and LDR to Thingspeak. My first experiment, without the LDR functions very well. But since I need to use it (and try to "multiplex" with the soil moisture sensor, I am getting just a dead board. Absolutely nothing happens after flash the code. The code compiles without any error, and I also tried to find the problem, both in the code and in the board wiring.
Please, could someone give a look? I really don't know why I am doing wrong.

Code: Select all// Library DHT22
#include <DHT.h>

// Library ESP
#include <ESP8266WiFi.h>


// Thingspeak API key,
String apiKey = "";                                     //fill w/ api key from thingspeak
const char* ssid = "";                          //fill w/ wifi name
const char* password = "";                       //fill w/ wifi password
const char* server = "api.thingspeak.com";

// DHT22
#define DHTPIN D3 // what pin we’re connected to
DHT dht(DHTPIN, DHT22); //why 15? dht 22, 15

WiFiClient client;

// Variables
int sensorPin = A0;    // input for LDR and rain sensor
int enable1 = D1;      // enable reading LDR
int enable2 = D2;      // enable reading Soil sensor


int sensorValue1 = 0;  // variable to store the value coming from sensor LDR
int sensorValue2 = 0;  // variable to store the value coming from sensor Soil sensor


// Setup
void setup() {

pinMode(enable1, OUTPUT);
pinMode(enable2, OUTPUT);
 
Serial.begin(115200);
delay(10);

dht.begin();

WiFi.begin(ssid, password);

Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
Serial.print("..........");
Serial.println();
WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
delay(500);

}
Serial.println("WiFi connected");
Serial.println();

}


void loop() {
// DHT22

float h = dht.readHumidity();
float t = dht.readTemperature();

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

Serial.print("Temperature:      ");
Serial.print(t);
Serial.print(" degrees Celcius ");
Serial.println();

Serial.print("Humidity:         ");
Serial.print(h);
Serial.print("%");
Serial.println();


// LDR
digitalWrite(enable1, HIGH);
  sensorValue1 = analogRead(sensorPin);
  sensorValue1 = constrain(sensorValue1, 300, 850);
  sensorValue1 = map(sensorValue1, 300, 850, 0, 1023);
  Serial.print("Light intensity: ");
  Serial.println(sensorValue1);
  digitalWrite(enable1, LOW);
  delay(100);
  // novo bloco que pega o input via tradicional: Sensor 2 (Soil Moisture)
 digitalWrite (enable2, HIGH);
  delay (500);
  sensorValue2 = analogRead (sensorPin);
  sensorValue2 = constrain (sensorValue2, 380, 0);
  sensorValue2 = map(sensorValue2, 380, 0, 0, 100);
  Serial.print("Sensor 2 value:  ");
  Serial.println (sensorValue2);
  Serial.println ();
  digitalWrite (enable2, LOW);
  delay (2000);

// Thingspeak
   if (client.connect(server,80)) { // "184.106.153.149" or api.thingspeak.com
   {
     String postStr = apiKey;
     postStr += "&field1=";
     postStr += String(t);
     postStr += "&field2=";
     postStr += String(h);
     postStr += "&field3=";
     postStr += String(sensorValue1);
     postStr += "&field4=";
     postStr += String(sensorValue2);
     postStr += "\r\n\r\n\r\n\r\n";
   
     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(postStr.length());
     client.print("\n\n\n\n");
     client.print(postStr);
     delay(1000);
   }
   
   client.stop();

          // thingspeak needs minimum 15 sec delay between updates
delay(20000);
   }
}   
You do not have the required permissions to view the files attached to this post.
User avatar
By Bonzo
#81884 It is interesting what you did for the sensor readings, I ended up buying an ADC and switching mine on and off with a MOSFET. Your diode's look wrong as you have them in opposite directions both on the output from the LDR. On a voltage divider the output is supposed to go between the two resistors.

Also why is there a resistor on the GND from the soil sensor?

I Would be interested in what results you get from your soil sensor. I have two capacitive ones an the readings are all over the place.
User avatar
By vinicivs
#81936
Bonzo wrote:It is interesting what you did for the sensor readings, I ended up buying an ADC and switching mine on and off with a MOSFET. Your diode's look wrong as you have them in opposite directions both on the output from the LDR. On a voltage divider the output is supposed to go between the two resistors.

Also why is there a resistor on the GND from the soil sensor?

I Would be interested in what results you get from your soil sensor. I have two capacitive ones an the readings are all over the place.


Thank you by responding me Bonzo. Led Zpeppelin fan here?
I don't think the diodes are wrong. I checked again at least three wiring schemas and all looks the same regarding the diode position. You are write about the resistor on the ground wire, but as my electronics knowledge is very poor, I just decided to follow my examples. But, let me say you what happened: I decided to change the board, and suddenly everything works fine, with exception of wifi signal. I do not now but can't have any signal message on serial monitor and ThingSpeak as well. So I am still struggling to have the thing done! I am planning to change my resistive sensor for capacitive like yours.
User avatar
By Bonzo
#81969 It is strange about the wifi. I have a setup with multiple inputs and wifi and it works; although I only have one item on A0: viewtopic.php?f=11&t=19458

Looking again at your layout the and the resistor I questioned on the soil sensor must be for a voltage divider. I do not know why the manufacturers do not put one into the breakout board as they have a ground there. I have just decided to add one to my setup and will see if it has any effect.

As far as I know there is only the one wifi library and I would start from scratch and see where it fails. Add one item at a time; although that could be problematic if your code will not run without one item. I would start with the temperature sensor as the code may not run without that one. All you would need to do is unplug the A0 jumper.