Hello I am trying to compile the code to my ESP32 and I keep getting an error message

the error massege is ''ArduinoIoTPreferredConnection' was not declared in this scope''
I am trying to conntrol 3 relays using ArduinoCloud
this is my code:

#include "arduino_secrets.h"
#include "thingProperties.h"
#include <ArduinoIoTCloud.h>

bool relay1;
bool relay2;
bool relay3;

const int RELAY1_PIN = 2;
const int RELAY2_PIN = 3;
const int RELAY3_PIN = 4;

void setup() {
  Serial.begin(9600);
  for(unsigned long const serialBeginTime = millis(); !Serial && (millis() - serialBeginTime > 5000); ) { }
  initProperties();
  pinMode(RELAY1_PIN, OUTPUT);
  pinMode(RELAY2_PIN, OUTPUT);
  pinMode(RELAY3_PIN, OUTPUT);

  // Initialize Arduino IoT Cloud library
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);

  // Set debug message level
  setDebugMessageLevel(DBG_INFO);
  ArduinoCloud.printDebugInfo();
}

void loop() {
  ArduinoCloud.update();
  
  // Control relay 1
  digitalWrite(RELAY1_PIN, relay1);

  // Control relay 2
  digitalWrite(RELAY2_PIN, relay2);

  // Control relay 3
  digitalWrite(RELAY3_PIN, relay3);
}

void onRelay1Change() {
  Serial.print("Relay 1 set to ");
  Serial.println(relay1);
}

void onRelay2Change() {
  Serial.print("Relay 2 set to ");
  Serial.println(relay2);
}

void onRelay3Change() {
  Serial.print("Relay 3 set to ");
  Serial.println(relay3);
}

Welcome to the forum

Please post the full error message, using code tags when you do

here is the whole error massege;

C:\Users\Jure\Desktop\zaklucnaDejansk\zaklucnaDejansk.ino: In function 'void setup()':
C:\Users\Jure\Desktop\zaklucnaDejansk\zaklucnaDejansk.ino:22:22: error: 'ArduinoIoTPreferredConnection' was not declared in this scope
   ArduinoCloud.begin(ArduinoIoTPreferredConnection);
                      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~

exit status 1

Compilation error: 'ArduinoIoTPreferredConnection' was not declared in this scope

Hi @soulslayer. Please post the contents of the "thingProperties.h" tab of your sketch.

When you set up a Thing on Arduino Cloud, it automatically adds a declaration for ArduinoIoTPreferredConnection in the thingProperties.h file it generates. So this error message is unexpected.

of course here it is

#ifndef THINGPROPERTIES_H
#define THINGPROPERTIES_H

#include "ArduinoIoTCloud.h"
#include <Arduino_ConnectionHandler.h>

#if defined(BOARD_HAS_SECRET_KEY)
  #define BOARD_ID "2c898927-043f-4d86-8ebf-06f96a70421f"
#endif


extern bool relay1;
extern bool relay2;
extern bool relay3;
void onRelay1Change();
void onRelay2Change();
void onRelay3Change();
void initProperties(){

  ArduinoCloud.addProperty(relay1, READWRITE, ON_CHANGE, onRelay1Change);
  ArduinoCloud.addProperty(relay2, READWRITE, ON_CHANGE, onRelay2Change);
  ArduinoCloud.addProperty(relay3, READWRITE, ON_CHANGE, onRelay3Change);

}

#endif /* THINGPROPERTIES_H */

Sure enough, the declaration/instantiation of ArduinoIoTPreferredConnection is missing. Did you write this code by hand? It doesn't look like the generated code from the Thing sketches that are automatically generated when you initialize a Thing via the Arduino Cloud website.

It might be best to create a new Thing on Arduino Cloud. You should be able to copy the code from your .ino file into the new sketch as long as you make sure to use the same names for the Cloud Variables.

There is a tutorial for setting up an Arduino Cloud Thing here:

https://docs.arduino.cc/arduino-cloud/cloud-interface/things/

thank you so much that was the fix :sweat_smile:

You are welcome. I'm glad it is working now.

Regards,
Per

1 Like

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