I have a project which sends soil moisture, temperature, air humidity data to ThingSpeak. It works perfectly. My problem is on my second project, when I add an LDD, which shares A0 pin with soil moisture through multiplexing. I can get all data over the serial monitor, but the board can't connect to TS. For the first project that runs ok, I am using a Nodemcu v2 "official" board. For the second one, it is a clear clone bought on taobao, which picture I am sharing. I suspect that 1) can be something related with a different library to connect wifi or 2) something related with the timer. None of them I am sure about.
I will appreciate all the help.
#define SW_VERSION " ThinkSpeak.com" // SW version will appears at innitial LCD Display
/* ESP12-E & Thinkspeak*/
#include <ESP8266WiFi.h>
WiFiClient client;
const char* MY_SSID = "xxx";
const char* MY_PWD = "xxx";
const char* TS_SERVER = "api.thingspeak.com";
String TS_API_KEY ="xxx";
int sent = 0;
/* TIMER */
#include <SimpleTimer.h>
SimpleTimer timer;
/* DHT22*/
#include "DHT.h"
#define DHTPIN D3
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
float hum = 0;
float temp = 0;
/* Soil Moister */
int sensorPin = A0; // analog input for both sensors
int enable1 = D1; // enable reading Sensor 1
int enable2 = D2; // enable reading Sensor 2
int ldrValue1 = 0; // variable to store the value coming from sensor 1.
int soilValue2 = 0; // variable to store the value coming from sensor 2.
void setup()
{
Serial.begin(115200);
delay(10);
dht.begin();
connectWifi();
timer.setInterval(1000L, getDhtData);
//timer.setInterval(3000L, getSoilMoisterData);
timer.setInterval(4000L, sendDataTS);
pinMode(enable1, OUTPUT);
pinMode(enable2, OUTPUT);
}
void loop()
{
// Sensor DHT22
getDhtData();
// Sensor 1 LDR
digitalWrite(enable1, HIGH);
ldrValue1 = analogRead(sensorPin);
ldrValue1 = constrain(ldrValue1, 300, 850);
ldrValue1 = map(ldrValue1, 300, 850, 0, 1023);
Serial.print("Light intensity: ");
Serial.println(ldrValue1);
digitalWrite(enable1, LOW);
delay(1500);
// Sensor 2 SOIL MOISTURE
digitalWrite(enable2, HIGH);
delay(500);
soilValue2 = analogRead(sensorPin);
soilValue2 = constrain(soilValue2, 300, 0);
soilValue2 = map(soilValue2, 300, 0, 0, 100);
Serial.print("Soil moisture: ");
Serial.println(soilValue2);
Serial.println();
delay(1500);
digitalWrite(enable2, LOW);
displayData();
delay(2000); // delay for getting DHT22 data
timer.run(); // Initiates SimpleTimer
}
/***************************************************
* Get DHT data
**************************************************/
void getDhtData(void)
{
float tempIni = temp;
float humIni = hum;
temp = dht.readTemperature();
hum = dht.readHumidity();
if (isnan(hum) || isnan(temp)) // Check if any reads failed and exit early (to try again).
{
Serial.println("Failed to read from DHT sensor!");
temp = tempIni;
hum = humIni;
return;
}
}
// display data
void displayData(void)
{
Serial.print(" Temperature: ");
Serial.print(temp);
Serial.print("oC Humidity: ");
Serial.print(hum);
Serial.println("%");
}
/***************************************************
* Connecting WiFi
**************************************************/
void connectWifi()
{
Serial.print("Connecting to "+ *MY_SSID);
WiFi.begin(MY_SSID, MY_PWD);
while (WiFi.status() != WL_CONNECTED)
{
delay(1000);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi Connected");
Serial.println("");
}
/***************************************************
* Sending Data to Thinkspeak Channel
**************************************************/
void sendDataTS(void)
{
if (client.connect(TS_SERVER, 80))
{
String postStr = TS_API_KEY;
postStr += "&field1=";
postStr += String(temp);
postStr += "&field2=";
postStr += String(hum);
postStr += "&field3=";
postStr += String(soilValue2);
postStr += "&field4=";
postStr += String(ldrValue1);
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: " + TS_API_KEY + "\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);
}
sent++;
client.stop();
}