Go Down

Topic: Nodemcu clone can't connect to wifi! Help here bros! (Read 126 times) previous topic - next topic

vinicivs

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, so I expect some support here! hope someone can help me.
I will appreciate all the help.
I will post in the messages ahead the code that connects.
Right here is the code that doesn't connects:

Code: [Select]

#define SW_VERSION " ThinkSpeak.com" // SW version will appears at initial 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();
}
 

PaulRB

So that's the code that doesn't work? Also post the code that did work (the version before you added the LDR).



Quote
I expect some support here!
I know English is not your first language. So you can be forgiven. But those words sound very arrogant. You have no right to expect support from anyone here. It's ok to say you hope for some support or you would appreciate some support.

vinicivs

Sorry about my English. I just wanted to post a newbie issue. I will edit the text and paste the old code. Thanks.

vinicivs

Here is the code that connects:


Code: [Select]

#define SW_VERSION " ThinkSpeak.com"

/* 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 <Adafruit_Sensor.h> */
#include <DHT.h>
/* #include <DHT_U.h> */

#define DHTPIN D3  
#define DHTTYPE DHT22
 
/* DHT_Unified dht(DHTPIN, DHTTYPE); */
DHT dht(DHTPIN, DHTTYPE);
float hum = 0;
float temp = 0;

/* Soil Moister */
#define soilMoisterPin A0
#define soilMoisterVcc D4
int soilMoister = 0;

void setup()
{
  pinMode (soilMoisterVcc, OUTPUT);
  Serial.begin(115200);
  delay(10);                          
  dht.begin();
  connectWifi();
  timer.setInterval(1000L, getDhtData);          
  timer.setInterval(3000L, getSoilMoisterData);  
  timer.setInterval(4000L, sendDataTS);
  digitalWrite (soilMoisterVcc, LOW);
}

void loop()
{
  getDhtData();
  getSoilMoisterData();
  displayData();
  delay(2000); // Delay for getting DHT22 data
  timer.run(); // Initiates SimpleTimer
}

/***************************************************
 * Get DHT data
 **************************************************/
void getDhtData(void)
{
  float tempIni = temp;
  float humIni = hum;
  /* sensors_event_t event;
  temp = dht.temperature().getEvent(&event);
  hum = dht.humidity().getEvent(&event); */
  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;
  }
}

/***************************************************
 * Get Soil Moister Sensor data
 **************************************************/
void getSoilMoisterData(void)
{
  soilMoister = 0;
  digitalWrite (soilMoisterVcc, HIGH);
  delay (500);
  int N = 3;
  for(int i = 0; i < N; i++) // read sensor "N" times and get the average
  {
    soilMoister += analogRead(soilMoisterPin);  
    delay(150);                                    
  }
  digitalWrite (soilMoisterVcc, LOW);
  soilMoister = soilMoister/N;
  Serial.println(soilMoister);
  soilMoister = map(soilMoister, 380, 0, 0, 100);
}

/***************************************************
 * Display data at Serial Monitor
 **************************************************/
void displayData(void)
{
  Serial.print(" Temperature: ");
  Serial.print(temp);
  Serial.print("oC   Humidity: ");
  Serial.print(hum);
  Serial.println("%");
  Serial.print(" Moisture: ");                        
}

/***************************************************
 * 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(soilMoister);
     postStr += "\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");
     client.print(postStr);
     delay(1000);
   }
   sent++;
   client.stop();
}


PaulRB

Oh. Now both versions of the code are described as "code that connects". Can you please clarify which does not connect?

vinicivs

Oh. Now both versions of the code are described as "code that connects". Can you please clarify which does not connect?

Sorry. Fixed. The one in first message doesn't work.

PaulRB

The code for connecting to the WiFi and for connecting to the server are very similar. There are these differences:

Code: [Select]
     postStr += "\r\n\r\n";
compared to
Code: [Select]
     postStr += "\r\n\r\n\r\n\r\n";
and
Code: [Select]
     client.print("\n\n");
compared to
Code: [Select]
     client.print("\n\n\n\n");
I don't know why these differences would cause any problems.

rpurser

A few things to experiment with:
  • Does it work if you disconnect the LCD?  I'm guessing there's an interaction between the LCD and the wi-fi chip.
  • Can you reach any website?  ThingSpeak has kept its ingest API stable for a long time, and hasn't had any outages recently.
Visit www.mathworks.com/iot to learn more about analyzing your IoT data.

Go Up