ESP8266 NodeMCU failed to connect to thingspeak

I bought today a new NodeMCU and at first, it was working fine and communicating well with the thingspeak server and sending data from my photoresistor sensor, but after a 20-30 mins, it won't communicate with the api.thingspeak.com and prompt a "connection failed" message.

I've tried using the ThingSpeak example for a single writefield but I am prompted with an error -301 which also meant that the communication to the thingspeak server has failed.

I've also tried changing the host with the added https://, still no luck.

This is the code that I am using:

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
 
const char* ssid     = "jmySSID";
const char* password = "myPass";
 
const char* host = "api.thingspeak.com";
const char* APIKey = "myAPI";
 
void setup()
{
    Serial.begin(9600);
    delay(10);
    //WiFi.mode(WIFI_STA);
 
    // We start by connecting to a WiFi network
 
    Serial.println();
    Serial.println();
    Serial.print("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("IP address: ");
    Serial.println(WiFi.localIP());
}
 
int value = 0;
 
void loop()
{
    delay(5000);
    value = analogRead(A0);
 
    Serial.print("connecting to ");
    Serial.println(host);
 
    // Use WiFiClient class to create TCP connections
    WiFiClient client;
    const int httpPort = 80;
    if (!client.connect(host, httpPort)) {
        Serial.println("connection failed");
        return;
    }
 
    // We now create a URI for the request
    String url = "/update";
    url += "?api_key=";
    url += APIKey;
    url += "&field1=";
    url += value;
 
    Serial.print("Requesting URL: ");
    Serial.println(url);
 
    // This will send the request to the server
    client.print(String("GET ") + url + " HTTP/1.1\r\n" +
                 "Host: " + host + "\r\n" +
                 "Connection: close\r\n\r\n");
    unsigned long timeout = millis();
    while (client.available() == 0) {
        if (millis() - timeout > 5000) {
            Serial.println(">>> Client Timeout !");
            client.stop();
            return;
        }
    }
 
    // Read all the lines of the reply from server and print them to Serial
    while(client.available()) {
        String line = client.readStringUntil('\r');
        Serial.print(line);
    }
 
    Serial.println();
    Serial.println("closing connection");
}

Here is the error message:
image

I assume you are sending the actual API Key provided by ThingSpeak since you are sometimes successful. Here are a few thoughts:

  • You might be exceeding the data limit imposed on free ThingSpeak accounts.
  • The ThingSpeak server returns an error code which may prove helpful in figuring out what is wrong.
  • You could try using the ThingSpeak library.

Don

Thanks for replying,

  • For the data limit, I am not sure but I've only used thingspeak services today and as shown here the daily message limit is 8,129 messages.
    image
    and I've only got 109 today since I started.

  • I already tried using the ThingSpeak library with the example and it returns an HTTP error -301 and I run through the documentation and it says there that -301 error is Failed to connecto to thingspeak as shown below:

You are attempting to send data every 5 seconds but with the free ThingSpeak account there must be a minimum 15 seconds between data points.

ThingSpeak™ Licensing FAQ
9. What are the benefits of purchasing one of the paid license over using a free license?

The paid license options offer the ability to send and process more data on ThingSpeak. The paid license options also offer the ability to have more channels on ThingSpeak, which allows you to connect more devices to ThingSpeak. They offer reduced message update interval limit of one second, allowing you to send data to ThingSpeak more frequently (the free option limit is 15 seconds). The academic and standard paid options also offer technical support, and a longer timeout for MATLAB calculations. In addition, the Standard license option offers the ability build commercial projects with ThingSpeak.

Don

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.