Code compiles, board does not run.
Posted: Thu Apr 18, 2019 8:36 am
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.
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);
}
}