NodeMCU can't connect to wifi and send to TS. | ESP Wi-Fi | Forum

Avatar

Please consider registering
Guest

sp_LogInOut Log In sp_Registration Register

Register | Lost password?
Advanced Search

— Forum Scope —






— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

sp_Feed sp_TopicIcon
NodeMCU can't connect to wifi and send to TS.
No permission to create posts
April 21, 2019
5:15 am
Avatar
Vinicivs
New Member
Members
Forum Posts: 3
Member Since:
April 5, 2019
sp_UserOfflineSmall Offline

Hi guys!
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. Thanks!
--
#define SW_VERSION " ThinkSpeak.com" // SW version will appears at innitial LCD Display
/* ESP12-E & Thinkspeak*/
#include

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 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 += "

";

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

");
client.print(postStr);
delay(1000);
}
sent++;
client.stop();
}

April 23, 2019
8:06 am
Avatar
cstapels
Moderator
Members


Moderators
Forum Posts: 861
Member Since:
March 7, 2017
sp_UserOfflineSmall Offline

Can you describe the behavior you are seeing? Do you have any theories about what might be going on?
That is a lot of code you have posted for us to parse. Perhaps you could show what you are seeing on the serial monitor.

Forum Timezone: America/New_York

Most Users Ever Online: 166

Currently Online:
20 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

rw950431: 272

Vinod: 240

piajola: 95

turgo: 70

vespapierre: 63

Adarsh_Murthy: 62

Member Stats:

Guest Posters: 1

Members: 8665

Moderators: 1

Admins: 2

Forum Stats:

Groups: 3

Forums: 14

Topics: 1600

Posts: 5760

Newest Members:

briskpanda, Dejw0089, Yassine Ben Salah, anis, 43994554AKS, suda

Moderators: cstapels: 460

Administrators: Hans: 405, lee: 457