Unable to show ThinkSpeak data, after uploading code

Hello everyone,

My task aims to show temperature and humidity level using DHT22 temperature sensor. However, after connecting Wifi NodeMCU module and upload it, I am unable to show the ThinkSpeak data. After sending to ThinkSpeak through the output serial monitor, ThinkSpeak data does not detect it and does not show any graphs.

I am using NodeMCU and DHT22 temperature sensor.

Any ideas how to solve this problem?
I will show my Arduino coding here.
Attached a photo of ThinkSpeak graph, that does not detect the coding.

#include <ESP8266WiFi.h>
#include "DHT.h"

#define DHTPIN D2       // Pin D2
#define DHTTYPE DHT22   // DHT 22 

// Wi-Fi Settings
const char* ssid = "JM";              // your wireless network name (SSID)
const char* password = "12345678";    // your Wi-Fi network password

DHT dht(DHTPIN, DHTTYPE);

WiFiClient client;                              
const int channelID = 1241864;                  // ThingSpeak Settings
String writeAPIKey = "I1KWNDJ4I4MI9GT0";        // write API key for your ThingSpeak Channel
const char* server = "api.thingspeak.com";

void setup() 
{
       Serial.begin(115200);
       delay(10);
       dht.begin();
 
       Serial.println("Connecting to ");
       Serial.println(ssid);
 
       WiFi.begin(ssid, password);
 
      while (WiFi.status() != WL_CONNECTED) 
     {
            delay(500);
            Serial.print(".");
     }
      Serial.println("");
      Serial.println("WiFi connected");
      Serial.println("");
}

void loop() {  
  if (client.connect(server, 80)) {
    
    // Measure Analog Input (A0)
    float h = dht.readHumidity();
    float t = dht.readTemperature(); 

    // Construct API request body
    String body = "&field1=";
           body += String(t);
           body += "&field2=";
           body += String(h);
          

    client.println("POST /update HTTP/1.1");
    client.println("Host: api.thingspeak.com");
    client.println("User-Agent: ESP8266 (nothans)/1.0");
    client.println("Connection: close");
    client.println("X-THINGSPEAKAPIKEY: " + writeAPIKey);
    client.println("Content-Type: application/x-www-form-urlencoded");
    client.println("Content-Length: " + String(body.length()));
    client.println("");
    client.print(body);

    Serial.print("Temperature: ");
    Serial.print(t);
    Serial.print(" degrees Celcius, Humidity: ");
    Serial.print(h);
    Serial.println("%. Send to Thingspeak.");
  }
  client.stop();
  Serial.println("Waiting...");
  delay(8000);

}

Thank You!!

a longer time ago I donwloaded a demo-code from random nerd tutorials.

I tried to find it online again. But I had no luck.

So I post the demo-code here. You need to install the ThingSpeak-Library

RNT has some more thingspeak-demos

/*
  WriteMultipleFields
    Description: Writes values to fields 1,2,3,4 and status in a single ThingSpeak update every 20 seconds.
    Hardware: ESP8266 based boards
    !!! IMPORTANT - Modify the secrets.h file for this project with your network connection and ThingSpeak channel details. !!!
    Note:
  - Requires ESP8266WiFi library and ESP8622 board add-on. See https://github.com/esp8266/Arduino for details.
  - Select the target hardware from the Tools->Board menu
  - This example is written for a network using WPA encryption. For WEP or WPA, change the WiFi.begin() call accordingly.
  
  ThingSpeak ( https://www.thingspeak.com ) is an analytic IoT platform service that allows you to aggregate, visualize, and 
  analyze live data streams in the cloud. Visit https://www.thingspeak.com to sign up for a free account and create a channel.  
  
  Documentation for the ThingSpeak Communication Library for Arduino is in the README.md folder where the library was installed.
  See https://www.mathworks.com/help/thingspeak/index.html for the full ThingSpeak documentation.
  
  For licensing information, see the accompanying license file.
  
  Copyright 2018, The MathWorks, Inc.
*/

#include "ThingSpeak.h"
#include "secrets.h"
#include <ESP8266WiFi.h>

char ssid[] = SECRET_SSID;   // your network SSID (name) 
char pass[] = SECRET_PASS;   // your network password
int keyIndex = 0;            // your network key Index number (needed only for WEP)
WiFiClient  client;

unsigned long myChannelNumber = SECRET_CH_ID;
const char * myWriteAPIKey = SECRET_WRITE_APIKEY;

// Initialize our values
int number1 = 0;
int number2 = random(0,100);
int number3 = random(0,100);
int number4 = random(0,100);
String myStatus = "";

void setup() {
  Serial.begin(115200);  // Initialize serial

  WiFi.mode(WIFI_STA); 
  ThingSpeak.begin(client);  // Initialize ThingSpeak
}

void loop() {

  // Connect or reconnect to WiFi
  if(WiFi.status() != WL_CONNECTED){
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(SECRET_SSID);
    while(WiFi.status() != WL_CONNECTED){
      WiFi.begin(ssid, pass);  // Connect to WPA/WPA2 network. Change this line if using open or WEP network
      Serial.print(".");
      delay(5000);     
    } 
    Serial.println("\nConnected.");
  }

  // set the fields with the values
  ThingSpeak.setField(1, number1);
  ThingSpeak.setField(2, number2);
  ThingSpeak.setField(3, number3);
  ThingSpeak.setField(4, number4);

  // figure out the status message
  if(number1 > number2){
    myStatus = String("field1 is greater than field2"); 
  }
  else if(number1 < number2){
    myStatus = String("field1 is less than field2");
  }
  else{
    myStatus = String("field1 equals field2");
  }
  
  // set the status
  ThingSpeak.setStatus(myStatus);
  
  // write to the ThingSpeak channel
  int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
  if(x == 200){
    Serial.println("Channel update successful.");
  }
  else{
    Serial.println("Problem updating channel. HTTP error code " + String(x));
  }
  
  // change the values
  number1++;
  if(number1 > 99){
    number1 = 0;
  }
  number2 = random(0,100);
  number3 = random(0,100);
  number4 = random(0,100);
  
  delay(20000); // Wait 20 seconds to update the channel again
}

best regards Stefan

You should definitely use the ThingSpeak library or at least look at the examples in the doc. I'm pretty sure the error has to do with the body variable. It shouldn't start with an ampersand. The first parameter needs a question mark for a GET request but I don't think there is a separator at all in the POST format. Start with 'field1..." instead of "&field1 ..."

StefanL38:
a longer time ago I donwloaded a demo-code from random nerd tutorials.

I tried to find it online again. But I had no luck.

So I post the demo-code here. You need to install the ThingSpeak-Library

RNT has some more thingspeak-demos
ESP8266 Web Server with Arduino IDE | Random Nerd Tutorials
ESP8266 Daily Task - Publish Temperature Readings to ThingSpeak | Random Nerd Tutorials
ESP8266 NodeMCU HTTP POST with Arduino IDE (ThingSpeak and IFTTT.com) | Random Nerd Tutorials

/*

WriteMultipleFields
   Description: Writes values to fields 1,2,3,4 and status in a single ThingSpeak update every 20 seconds.
   Hardware: ESP8266 based boards
   !!! IMPORTANT - Modify the secrets.h file for this project with your network connection and ThingSpeak channel details. !!!
   Note:
 - Requires ESP8266WiFi library and ESP8622 board add-on. See GitHub - esp8266/Arduino: ESP8266 core for Arduino for details.
 - Select the target hardware from the Tools->Board menu
 - This example is written for a network using WPA encryption. For WEP or WPA, change the WiFi.begin() call accordingly.
 
 ThingSpeak ( https://www.thingspeak.com ) is an analytic IoT platform service that allows you to aggregate, visualize, and
 analyze live data streams in the cloud. Visit https://www.thingspeak.com to sign up for a free account and create a channel.  
 
 Documentation for the ThingSpeak Communication Library for Arduino is in the README.md folder where the library was installed.
 See ThingSpeak Documentation for the full ThingSpeak documentation.
 
 For licensing information, see the accompanying license file.
 
 Copyright 2018, The MathWorks, Inc.
*/

#include "ThingSpeak.h"
#include "secrets.h"
#include <ESP8266WiFi.h>

char ssid[] = SECRET_SSID;   // your network SSID (name)
char pass[] = SECRET_PASS;   // your network password
int keyIndex = 0;            // your network key Index number (needed only for WEP)
WiFiClient  client;

unsigned long myChannelNumber = SECRET_CH_ID;
const char * myWriteAPIKey = SECRET_WRITE_APIKEY;

// Initialize our values
int number1 = 0;
int number2 = random(0,100);
int number3 = random(0,100);
int number4 = random(0,100);
String myStatus = "";

void setup() {
 Serial.begin(115200);  // Initialize serial

WiFi.mode(WIFI_STA);
 ThingSpeak.begin(client);  // Initialize ThingSpeak
}

void loop() {

// Connect or reconnect to WiFi
 if(WiFi.status() != WL_CONNECTED){
   Serial.print("Attempting to connect to SSID: ");
   Serial.println(SECRET_SSID);
   while(WiFi.status() != WL_CONNECTED){
     WiFi.begin(ssid, pass);  // Connect to WPA/WPA2 network. Change this line if using open or WEP network
     Serial.print(".");
     delay(5000);    
   }
   Serial.println("\nConnected.");
 }

// set the fields with the values
 ThingSpeak.setField(1, number1);
 ThingSpeak.setField(2, number2);
 ThingSpeak.setField(3, number3);
 ThingSpeak.setField(4, number4);

// figure out the status message
 if(number1 > number2){
   myStatus = String("field1 is greater than field2");
 }
 else if(number1 < number2){
   myStatus = String("field1 is less than field2");
 }
 else{
   myStatus = String("field1 equals field2");
 }
 
 // set the status
 ThingSpeak.setStatus(myStatus);
 
 // write to the ThingSpeak channel
 int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
 if(x == 200){
   Serial.println("Channel update successful.");
 }
 else{
   Serial.println("Problem updating channel. HTTP error code " + String(x));
 }
 
 // change the values
 number1++;
 if(number1 > 99){
   number1 = 0;
 }
 number2 = random(0,100);
 number3 = random(0,100);
 number4 = random(0,100);
 
 delay(20000); // Wait 20 seconds to update the channel again
}




best regards Stefan

Thank You Stefan for providing the sample code, I have code it with dht22 temperature sensor and further troubleshoot the codes. It WORKS! Thanks to you!