Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By iamastic
#92226 Hi!

I connected the NodeMCU to a Capacitive Soil Moisture Sensor to send Humidity levels. I connected it to ThingSpeak successfully and was able to record the data every 30 seconds.

I then tried to do the same with Firebase. I ran into a few issues and was unsure why it did not work. So I reuploaded the code I used for ThingSpeak only to learn that it has suddenly stopped working as well.

Whenever I upload the ThingSpeak code to my NodeMCU, it connects to the WiFi successfully, records the first piece of data and sends it to ThingSpeak. Afterwards, in the loop, it attempts to reconnect to the WiFiClient but constantly fails.

When I hit the reset button on the NodeMCU, it again sends the data successfully but stops after the first time.

I am struggling to figure out why it is able to send the data the first time, the client is able to connect the first time, but all the subsequent requests are not going through?

Here is the short code I have been using (that worked prior to me testing out with Firebase):

Code: Select all#include <ESP8266WiFi.h>

String apiKey = "XXXXX";     //  Enter your Write API key from ThingSpeak

const char *ssid =  "XXXXX";     // replace with your wifi ssid and wpa2 key
const char *pass =  "XXXXX";
const char* server = "api.thingspeak.com";

WiFiClient client;

int sensor_pin = A0;
int output_value;


void setup()
{
  Serial.begin(115200);
  delay(10);


  Serial.println("Connecting to ");
  Serial.println(ssid);


  WiFi.begin(ssid, pass);

  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");

}

void loop()
{

  output_value = analogRead(sensor_pin);
  output_value = map(output_value, 550, 0, 0, 100);

  if (client.connect(server, 80))  //   "184.106.153.149" or api.thingspeak.com
  {

    String postStr = apiKey;
    postStr += "&field1=";
    postStr += String(output_value);

    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: " + apiKey + "\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);

    Serial.print("Humidity: ");
    Serial.print(output_value);
    Serial.print("%");
    Serial.println("   Send to Thingspeak.");
  } else {
      Serial.println("Did not connect to thingspeak");
    }
  client.stop();

  Serial.println("Waiting...");

  // thingspeak needs minimum 15 sec delay between updates, i've set it to 30 seconds
  delay(10000);
}


This code was working well before and now is no longer working (apart from the very first instance during every reset). How can I successfully reconnect to the WiFiClient the second time? Is this an issue with the NodeMCU module?

Thanks!
User avatar
By iamastic
#92239 UPDATE: It starts working and stops working randomly. If I leave it running for roughly 3-5 minutes, it starts connecting and working as usual again. I realised in my code that I have an error with the delay time, and have fixed it to 30 seconds instead of 10.

The issue still persists, where in the beginning it fails to connect for a few minutes. Why is this?