Problem uploading sensor data using Thingspeak library

Hey!
I am trying to use ThingSpeak libraries on a test project to upload my sensor data (DHT11).

My board is an Arduino UNO, with a WiFi- module esp8266 (no shields) to connect to internet.

Even though my sketch gives no errors, the data is not uploaded.

I’ve just tried to use the library example but in vain.

I get the first set of sensor value on the serial monitor but after that nothing happens.

here is my code :

#include "DHT.h"
#define DHTPIN 8   

#include <WiFi101.h>
#include "ThingSpeak.h"
#define USE_WIFI101_SHIELD
//#define USE_ETHERNET_SHIELD
#define DHTTYPE DHT11 
DHT dht(DHTPIN, DHTTYPE);

#if !defined(USE_WIFI101_SHIELD) && !defined(USE_ETHERNET_SHIELD) && !defined(ARDUINO_SAMD_MKR1000) && !defined(ARDUINO_AVR_YUN) && !defined(ARDUINO_ARCH_ESP8266)
 #error "Uncomment the #define for  USE_WIFI101_SHIELD "
#endif


#if defined(USE_WIFI101_SHIELD) 
 //Use WiFi
   
    #include <SPI.h>
  #include <WiFi101.h>

    char ssid[] = "XXXX";    //  your network SSID (name) 
  char pass[] = "XXXXXXX";   // your network password
int status = WL_IDLE_STATUS;
    WiFiClient  client;
 
#endif


unsigned long myChannelNumber = YYYYY;
const char * myWriteAPIKey = "YYYYYYYY";

void setup() {

 Serial.begin(9600);
  Serial.println("DHTxx test!");
  dht.begin();
   
   #ifdef  defined(USE_WIFI101_SHIELD) 
    WiFi.begin(ssid, pass);
  #endif
   
  ThingSpeak.begin(client);
  
}

void loop() {

   delay(2000);

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);

 // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
 Serial.println("Failed to read from DHT sensor!");
    return;
  }
  // Read the input on each pin, convert the reading, and set each field to be sent to ThingSpeak.
  // On Uno,Mega,Yun:  0 - 1023 maps to 0 - 5 volts
  Serial.println("Humidity: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.println("Temperature: ");
  Serial.print(t);
  Serial.print(" *C ");
  //float f1 = t;
  float f2 = h;

 
  // Write the fields that you've set all at once.  
  
 // ThingSpeak.writeField(myChannelNumber, 1, t, myWriteAPIKey);
  ThingSpeak.writeField(myChannelNumber, 2, f2, myWriteAPIKey);
 
 delay(20000); // ThingSpeak will only accept updates every 15 seconds. 
}

I have just started working on arduino .

Any help will be appreciated.

delay(2000);

// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds ‘old’ (its a very slow sensor)

That is NOT why the reading may be old. The reading may be old because of that stupid delay().

  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);

Do you really NOT have a clue whether you want the temperature in F or C?

  float f2 = h;

Why? What is the point of making another copy of the value?

 delay(20000); // ThingSpeak will only accept updates every 15 seconds.
}

THAT is no excuse for using delay?

Do you REALLY expect the temperature to change much in 20 seconds? If you do, then using Thingspeak to host the data is pretty stupid. Stand up your own server, flood it with all the data you want, and get past all the issues with Thingsqueek.

@PaulS

I’ve made the above mentioned changes.

Still no progress.

here is the modified code:

#include "DHT.h"
#define DHTPIN 8   

#include <WiFi101.h>
#include "ThingSpeak.h"
#define USE_WIFI101_SHIELD
//#define USE_ETHERNET_SHIELD
#define DHTTYPE DHT11 
DHT dht(DHTPIN, DHTTYPE);

#if !defined(USE_WIFI101_SHIELD) && !defined(USE_ETHERNET_SHIELD) && !defined(ARDUINO_SAMD_MKR1000) && !defined(ARDUINO_AVR_YUN) && !defined(ARDUINO_ARCH_ESP8266)
#error "Uncomment the #define for  USE_WIFI101_SHIELD "
#endif


#if defined(USE_WIFI101_SHIELD) 
//Use WiFi
  
   #include <SPI.h>
 #include <WiFi101.h>

   char ssid[] = "XXXXXX";    //  your network SSID (name) 
 char pass[] = "XXXXXXX";   // your network password
int status = WL_IDLE_STATUS;
   WiFiClient  client;

#endif


unsigned long myChannelNumber = YYYYYYY;
const char * myWriteAPIKey = "YYYYYYY";

void setup() {

Serial.begin(9600);
 Serial.println("DHTxx test!");
 dht.begin();
  
  #ifdef  defined(USE_WIFI101_SHIELD) 
   WiFi.begin(ssid, pass);
 #endif
  
 ThingSpeak.begin(client);
 
}

void loop() {

  
 float h = dht.readHumidity();
 // Read temperature as Celsius (the default)
 float t = dht.readTemperature();
 
// Check if any reads failed and exit early (to try again).
 if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
   return;
 }

 Serial.println("Humidity: ");
 Serial.print(h);
 Serial.print(" %\t");
 Serial.println("Temperature: ");
 Serial.print(t);
 Serial.print(" *C ");

 // Write the fields that you've set all at once.  
 
 ThingSpeak.writeField(myChannelNumber, 1, t, myWriteAPIKey);
 ThingSpeak.writeField(myChannelNumber, 2, h, myWriteAPIKey);
 
delay(3600000);  
}

Still no progress.

Is it so difficult to put a Serial.print() statement before and after the calls to the Thinksqueak class methods, to see if that is where the problem is?

I had already tried that.

I’m getting the output on the serial monitor but the same is not uploaded on the thingspeak channel .

I had already tried that.

I'm getting the output on the serial monitor

That is NOT what you said your problem was at the beginning.

The problem I'm facing is that even when the code shows no errors on compilation, the sensor data is not uploaded on the website.

I had tried other examples from the Thingspeak library as well , but I'm still unable to upload the data.