NODEMCU (ESP8266) goes offline

Hi. I use an NODEMCU (ESP8266) to control a peristaltic pump. I have created a Thing in IoT cloud and control it trough Iot remote app from my phone. I am able to start and stop the pump using a switch from my phone by changing a cloud variable. The problem i have is that the NodeMCU goes offline after a while and then I can't control it anymore. How can i prevent it of going offline or is there a way once it goes to bring it back online? What is the reason that it goes offline and after how long is this happening? The program also includes functionality to start the pump every day at 11:00 for 5 minutes. Here is my code

#include "thingProperties.h"
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <ESP8266WiFi.h> // Include the ESP8266 WiFi library

// Define the pump pin
const int pumpPin = 4; // GPIO 4 (D2 on NodeMCU)
unsigned long previousMillis = 0; // Variable to store the last time the pump was started
const long interval = 24 * 60 * 60 * 1000; // 24 hours in milliseconds
const int pumpDuration = 5 * 60 * 1000; // 5 minutes in milliseconds
bool scheduledPumpRun = false; // Flag to indicate if a scheduled pump run is ongoing

// Define NTP Client to get time
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org", 0, 60000);

void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  delay(1500);

  pinMode(pumpPin, OUTPUT);
  digitalWrite(pumpPin, LOW); // Ensure the pump is off initially

  initProperties(); // Defined in thingProperties.h

  ArduinoCloud.begin(ArduinoIoTPreferredConnection);

  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();

  // Initialize the NTP client
  timeClient.begin();
}

void loop() {
  // Reconnect if disconnected from WiFi
  if (WiFi.status() != WL_CONNECTED) {
    reconnect();
  }

  ArduinoCloud.update();
  timeClient.update();

  unsigned long currentMillis = millis();
  int currentHour = timeClient.getHours();
  int currentMinute = timeClient.getMinutes();

  // Check if it's time to start the pump
  if (!scheduledPumpRun && currentMillis - previousMillis >= interval && currentHour == 11 && currentMinute == 0) {
    startPump();
  }

  // Check if the pump duration has elapsed
  if (scheduledPumpRun && currentMillis - previousMillis >= pumpDuration) {
    stopPump();
  }
}

void onPumpChange() {
  // Control the pump based on the cloud variable 'pump'
  if (pump) {
    startPump();
  } else {
    stopPump();
  }
}

void startPump() {
  digitalWrite(pumpPin, HIGH);
  previousMillis = millis(); // Record the start time
  scheduledPumpRun = true;   // Set flag to indicate scheduled run
}

void stopPump() {
  digitalWrite(pumpPin, LOW);
  scheduledPumpRun = false;  // Reset flag
}



void reconnect() {
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }

  // Reinitialize NTP client
  timeClient.begin();
}

Hi @dkera013. Thing sketches use the "ArduinoIoTCloud" library to handle the connection and communication with the Arduino Cloud server. This library prints debugging information to the board's serial port. This information might give a clue to the reason why the Device is unexpectedly going into an "offline" state.

I'm going to ask you to provide the output that is printed to Cloud Editor's Serial Monitor tool by your sketch when it goes into the offline state.


:exclamation: This procedure is not intended to solve the problem. The purpose is to gather more information.


Please do this:

  1. Open your sketch in Arduino Cloud Editor.
  2. Start Arduino Create Agent if it is not already running.
  3. Connect the Arduino board to your computer with a USB cable.
  4. Wait for the USB symbol icon to appear on the Cloud Editor toolbar, indicating the serial port of your board is recognized.
  5. If the cloud shaped icon ("Over-The-Air") is selected on the Cloud Editor toolbar, click the USB symbol icon.
    This configures Cloud Editor to communicate with the Arduino board via the serial port instead of the OTA port.
  6. Click on "Serial Monitor" in the Cloud Editor toolbar.
    A Serial Monitor window will open.
  7. Wait for your sketch to finish printing all the startup information.
  8. Press the Ctrl+A keyboard shortcut (Command+A for macOS users).
    This will select all the text in the Serial Monitor window.
  9. Press the Ctrl+C keyboard shortcut (Command+C for macOS users).
    This will copy the selected text to the clipboard.
  10. Open a forum reply here by clicking the "Reply" button.
  11. Click the <CODE/> icon on the post composer toolbar.
    This will add the forum's code block markup (```) to your reply to make sure the error messages are correctly formatted.
    Code block icon on toolbar
  12. Press the Ctrl+V keyboard shortcut (Command+V for macOS users).
    This will paste the copied text into the code block.
  13. Move the cursor outside of the code block markup before you add any additional text to your reply.
  14. Click the "Reply" button to post the output.

Can you quantify what you mean by "a while"? Is it a matter of seconds, minutes, hours, days?

Have you been able to correlate the change to the offline state with any specific event (e.g., turning the pump on)?

hi this is the output i get from the serial monitor. i uploaded the code and tested the pump while running.
first when i started the serial output was:

���***** Arduino IoT Cloud - configuration info *****
Device ID: 782f7d39-e410-4b35-87aa-40fa5d8499a9
MQTT Broker: iot.arduino.cc:8884
WiFi.status(): 3
Connected to "Nova_2.4G_SNUkbPd"
Connected to Arduino IoT Cloud
Thing ID: b013ea57-550e-404c-902c-bbc106bcf8eb
Connected to Arduino IoT Cloud

then these were also printed;

ArduinoIoTCloudTCP::handle_Disconnect MQTT client connection lost
Disconnected from Arduino IoT Cloud
Connected to Arduino IoT Cloud
Thing ID: b013ea57-550e-404c-902c-bbc106bcf8eb
Connected to Arduino IoT Cloud
Thing ID: b013ea57-550e-404c-902c-bbc106bcf8eb
ArduinoIoTCloudTCP::handle_Disconnect MQTT client connection lost
Disconnected from Arduino IoT Cloud
ArduinoIoTCloudTCP::handle_Disconnect MQTT client connection lost
Disconnected from Arduino IoT Cloud
Connected to Arduino IoT Cloud
Thing ID: b013ea57-550e-404c-902c-bbc106bcf8eb
Connected to Arduino IoT Cloud
Thing ID: b013ea57-550e-404c-902c-bbc106bcf8eb
Connected to Arduino IoT Cloud
Thing ID: b013ea57-550e-404c-902c-bbc106bcf8eb
Connected to Arduino IoT Cloud
Thing ID: b013ea57-550e-404c-902c-bbc106bcf8eb

after some more tests this also was printed:

Device ID: 782f7d39-e410-4b35-87aa-40fa5d8499a9
MQTT Broker: iot.arduino.cc:8884
����������������������������������������������������������������iFi.status(): 3
Connected to "Nova_2.4G_SNUkbPd"
ArduinoIoTCloudTCP::attachThing could not subscribe to /a/t/b013ea57-550e-404c-902c-bbc106bcf8eb/e/i
Check your thing configuration, and press the reset button on your board.
ArduinoIoTCloudTCP::attachThing could not subscribe to /a/t/b013ea57-550e-404c-902c-bbc106bcf8eb/e/i
Check your thing configuration, and press the reset button on your board.
ArduinoIoTCloudTCP::handle_Disconnect MQTT client connection lost
Disconnected from Arduino IoT Cloud
Connected to Arduino IoT Cloud
Thing ID: b013ea57-550e-404c-902c-bbc106bcf8eb

it goes offline after some minutes and then in order to get back online i need to upload the code again. for sure, it goes offline when i unplug it from the usb and leave it connected to the dc power adapter (12V, 1A) only.

no, i haven't correlated the change to any specific event because i test the pump several times by opening and closing it several times and then when i leave it on power at some point it goes offline. the maximum time that managed to stay online was almost 1 h.

one last thing that is interesting is that, when it is offline, if i turn the switch on and off from the iot remote app, then it is logged as latest activity on the arduino cloud device details page. however, the device is neither getting online nor turns the pump on.