Use Arduino Cloud with ESP8266 without Connection Handler

Hi, I have an issue usong cloud with ESP8266 board. I have created my own connection manager (not a custom handler in terms of your classes) and initialized the Arduino cloud with ArduinoCloud.begin(); version without any parameters. I suppose, such an approach is possible as this overload of the begin method does exist.
I have also left out the ArduinoCloud.update(); call from loop, as the docs says, that it is needed only for watch dog functionality and ESP8266 is not a supported device for this (as per the list on GitHub).

And the result is, that even if my ESP-01 board connects to my wifi, but does not interact with the Arduino Cloud at all :frowning::

Here are the core parts of my sketch:

#include <ArduinoIoTCloud.h>
// #include <Arduino_ConnectionHandler.h>
#include "ESPWiFiManager.h" //my custom wifi connection manager due to need of ESP8266WiFiMulti class 

#define SECRET_DEVICE_KEY "**********************************" //masked out just for the purpose of this forum
#define SECRET_OPTIONAL_PASS "**********"
#define SECRET_SSID "***************"
const char DEVICE_LOGIN_NAME[] = "***********************";
const char SSID[] = SECRET_SSID;
const char PASS[] = SECRET_OPTIONAL_PASS;
const char DEVICE_KEY[] = SECRET_DEVICE_KEY;

// WiFiConnectionHandler ArduinoIoTPreferredConnection(SSID, PASS);

bool btnState;
void onBtnStateChange()
{
  digitalWrite(LED_BUILTIN, btnState);
}

void setup()
{
  DEBUGINIT(115200); //opens the hw serial etc.

  ArduinoCloud.setBoardId(DEVICE_LOGIN_NAME);
  ArduinoCloud.setSecretDeviceKey(DEVICE_KEY);
  ArduinoCloud.addProperty(btnState, READWRITE, ON_CHANGE, onBtnStateChange);

  wifiMgr.SetupWifi("ArduinoCloud", LED_BUILTIN); //sets up wifi connection, device host name and LED to blink for several infos
  wifiMgr.ConnectWifi(); //connects to my wifi

  ArduinoCloud.begin();
}

void loop()
{
    //ArduinoCloud.update();
}

What am I missing?

Take a look at https://docs.arduino.cc/arduino-cloud/guides/esp32 and https://support.arduino.cc/hc/en-us/articles/360016495559-Add-and-connect-a-device-to-Arduino-Cloud

1 Like

It is essential to regularly call ArduinoCloud.update() in your Arduino Cloud Thing sketches.

It is true that calling this function resets the watchdog timer, but that is not the only thing calling the function does. This function is what handles the syncing of your Arduino Cloud Variables between the device and the Dashboard. So if you don't call it then there is no communication at all with Arduino Cloud, as happened with your sketch.

The whole point of the watchdog timer is to allow the device to recover from a situation where the sketch hangs and stops calling ArduinoCloud.update().

In case you want to have a deeper technical understanding of what the function does, here is the source code:

https://github.com/arduino-libraries/ArduinoIoTCloud/blob/1.13.0/src/ArduinoIoTCloudTCP.cpp#L245-L284

void ArduinoIoTCloudTCP::update()
{
  /* Feed the watchdog. If any of the functions called below
   * get stuck than we can at least reset and recover.
   */
#if defined (ARDUINO_ARCH_SAMD) || defined (ARDUINO_ARCH_MBED)
  watchdog_reset();
#endif


  /* Run through the state machine. */
  State next_state = _state;
  switch (_state)
  {
  case State::ConnectPhy:           next_state = handle_ConnectPhy();           break;
  case State::SyncTime:             next_state = handle_SyncTime();             break;
  case State::ConnectMqttBroker:    next_state = handle_ConnectMqttBroker();    break;
  case State::SendDeviceProperties: next_state = handle_SendDeviceProperties(); break;
  case State::SubscribeDeviceTopic: next_state = handle_SubscribeDeviceTopic(); break;
  case State::WaitDeviceConfig:     next_state = handle_WaitDeviceConfig();     break;
  case State::CheckDeviceConfig:    next_state = handle_CheckDeviceConfig();    break;
  case State::SubscribeThingTopics: next_state = handle_SubscribeThingTopics(); break;
  case State::RequestLastValues:    next_state = handle_RequestLastValues();    break;
  case State::Connected:            next_state = handle_Connected();            break;
  case State::Disconnect:           next_state = handle_Disconnect();           break;
  }
  _state = next_state;

  /* This watchdog feed is actually needed only by the RP2040 Connect because its
   * maximum watchdog window is 8389 ms; despite this we feed it for all 
   * supported ARCH to keep code aligned.
   */
#if defined (ARDUINO_ARCH_SAMD) || defined (ARDUINO_ARCH_MBED)
  watchdog_reset();
#endif

  /* Check for new data from the MQTT client. */
  if (_mqttClient.connected())
    _mqttClient.poll();
}
1 Like

Hi, this solves it:

1 Like

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