I cant connect my board to iotCloud

#include <Arduino_ESP32_OTA.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <ArduinoIoTCloud.h>
#include <Arduino_ConnectionHandler.h>

#define AOUT_PIN_MQ2 13
#define AOUT_PIN_FLAME 12
#define BUZZER_PIN 5
#define LED_RED_PIN 22
#define LED_GREEN_PIN 23

// Declare SSID and PASS as global variables
const char SSID[] = "FAM_2.4";    // Network SSID (name)
const char PASS[] = "Password";    // Network password (use for WPA, or use as key for WEP)
WiFiConnectionHandler ArduinoIoTPreferredConnection(SSID, PASS);
void setup() {
  Serial.begin(115200);
  pinMode(BUZZER_PIN, OUTPUT);
  pinMode(LED_RED_PIN, OUTPUT);
  pinMode(LED_GREEN_PIN, OUTPUT);

  ledcSetup(0, 5000, 8);
  ledcAttachPin(BUZZER_PIN, 0);

  digitalWrite(LED_RED_PIN, HIGH);
  Serial.println("Warming up sensors...");
  connectToWiFi();

  if (WiFi.status() == WL_CONNECTED) {
    Serial.println("WiFi connected.");
    Serial.print("WiFi connection result: ");
    Serial.println(WiFi.status());

    // Add a delay to stabilize the WiFi connection
    delay(2000);

    int cloudConnection = ArduinoCloud.begin(ArduinoIoTPreferredConnection);
    Serial.print("Arduino IoT Cloud connection result: ");
    Serial.println(cloudConnection);

    if (cloudConnection != 0) {
      // Unable to connect to Arduino IoT Cloud
      Serial.println("Error connecting to Arduino IoT Cloud");
      setDebugMessageLevel(4);
      ArduinoCloud.printDebugInfo();
      while (1);
    }

    // Turn on the green LED when connected
    digitalWrite(LED_RED_PIN, LOW);
    digitalWrite(LED_GREEN_PIN, HIGH);
    Serial.println("Connected, Sensors are ready!");
  } else {
    Serial.println("WiFi connection failed. Sensors may not work correctly.");
  }

  // Initialize IoT Cloud properties
  initProperties();

  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500);
}


void loop() {
  ArduinoCloud.update();
  
  // Read smoke (MQ2) sensor
  int smokeValue = analogRead(AOUT_PIN_MQ2);
  Serial.print("Smoke Value: ");
  Serial.println(smokeValue);

  // Read flame sensor from A0
  int flameValue = analogRead(AOUT_PIN_FLAME);
  Serial.print("Flame Value: ");
  Serial.println(flameValue);

  // Check for smoke and activate buzzer with different tones
  if (smokeValue > 500) {
    tone(BUZZER_PIN, 1000);  // Adjust frequency for smoke detection
    Serial.println("Smoke detected! Buzzer sounding (Smoke tone).");
    digitalWrite(LED_RED_PIN, HIGH);
    digitalWrite(LED_GREEN_PIN, LOW); // Alternate red and green LEDs
    delay(3000);  // Adjust duration for tone to be audible
    noTone(BUZZER_PIN);  // Turn off the buzzer
  }

  // Check for flame and activate buzzer with different tones
  if (flameValue < 1000) {
    tone(BUZZER_PIN, 900);  // Adjust frequency for flame detection
    digitalWrite(LED_RED_PIN, HIGH);
    digitalWrite(LED_GREEN_PIN, LOW); // Alternate red and green LEDs
    Serial.println("Flame detected! Buzzer sounding (Flame tone).");
  } else {
    noTone(BUZZER_PIN);
    digitalWrite(LED_RED_PIN, LOW);
    digitalWrite(LED_GREEN_PIN, HIGH);
  }

  delay(1000);  // You can adjust the delay based on your application requirements
}

void connectToWiFi() {
  Serial.print("Connecting to WiFi");
  WiFi.begin(SSID, PASS);

  int attempts = 0;
  while (WiFi.status() != WL_CONNECTED && attempts < 20) {
    delay(500);
    Serial.print(".");
    attempts++;
  }

  if (WiFi.status() == WL_CONNECTED) {
    Serial.println("\nConnected to WiFi");
  } else {
    Serial.println("\nFailed to connect to WiFi");
  }
}

// Code generated by Arduino IoT Cloud, DO NOT EDIT.

const char DEVICE_ID[] = "YOUR_DEVICE_ID";
const char DEVICE_KEY[] = "YOUR_DEVICE_KEY"; // Secret device password

float mq2_sensor;
int flame_sensor;

void initProperties() {
  ArduinoCloud.setBoardId(DEVICE_ID);
  ArduinoCloud.setDeviceId(DEVICE_KEY);
  ArduinoCloud.addProperty(mq2_sensor, READ, ON_CHANGE, NULL);
  ArduinoCloud.addProperty(flame_sensor, READ, ON_CHANGE, NULL);
}

I'm using the Arduino IDE, not the online version. My board is the ESP32 WROOM, but in the IDE, I'm uploading to the 'DOIT ESP32 DEVKIT V1. im sure im putting the right credential in the "ID" and "secret key"

Hi @senser12. I'm going to ask you to post the output that is printed to the Arduino IDE Serial Monitor tool by your sketch. Arduino Cloud Thing sketches print information about the connection process to Serial Monitor when the sketch program starts running and this information might give us some helpful clue about the cause of the problem.


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


Please do this:

  1. If there is a "Serial Monitor" tab in the bottom panel of the Arduino IDE window, hover the mouse pointer over the tab and then click the X icon to close the tab.
    This is done to make sure there won't be any distracting irrelevant content in the output.
  2. Select Tools > Serial Monitor from the Arduino IDE menus.
    The "Serial Monitor" will open in the bottom panel of the Arduino IDE window.
  3. Select "115200" from the baud rate menu on the Serial Monitor toolbar.
  4. Wait for your sketch to finish printing all the startup information.
  5. If the "Toggle Autoscroll" feature is enabled, as indicated by the background being highlighted behind the image icon on the Serial Monitor toolbar, click the icon to disable autoscrolling.
    The icon's background highlight will disappear:
  6. If not all the relevant output text is visible in the output field of the Serial Monitor panel, hover the mouse pointer over the border between the bottom panel of the Arduino IDE window and the editor panel.
    The mouse pointer will change to the resize style:
    image
    Then click and drag the border up until all the text is visible.
  7. Click at the top of the text in the output field of the Serial Monitor panel and then drag the mouse down until all the text is selected.
  8. Press Ctrl+C (Command+C for macOS users).
    This will copy the selected text to the clipboard.
  9. Open a forum reply here by clicking the "Reply" button.
  10. 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 tags icon on toolbar
  11. Press Ctrl+V.
    This will paste the compilation output into the code block.
  12. Move the cursor outside of the code tags before you add any additional text to your reply.
  13. Click the "Reply" button to post the output.
1 Like
14:39:58.721 -> Error connecting to Arduino IoT Cloud
14:39:58.721 -> ***** Arduino IoT Cloud - configuration info *****
14:39:58.721 -> Device ID: 
14:39:58.721 -> MQTT Broker: mqtts-up.iot.arduino.cc:8884

the Device ID: is empty i dony know why

Remove these credentials?

I see you are calling initProperties after ArduinoCloud.begin. I didn't check the library code so I'm not sure whether or not this is a problem but initProperties is always called first in the standard sketches. Please move the initProperties above the ArduinoCloud.begin call to see if that makes a difference.

ArduinoCloud.setDeviceId is wrong here in your sketch.

If "YOUR_DEVICE_KEY" is the "Secret Key" that was shown when you set up your ESP32 board as an Arduino Cloud device:

Then it should be passed via the ArduinoCloud.setSecretDeviceKey function instead of ArduinoCloud.setDeviceId

ArduinoCloud.setBoardId is actually just an alias for ArduinoCloud.setDeviceId:

https://github.com/arduino-libraries/ArduinoIoTCloud/blob/1.13.0/src/ArduinoIoTCloudTCP.h#L90

    inline void setBoardId        (String const device_id) { setDeviceId(device_id); }

so what your code does is set the device ID to "YOUR_DEVICE_ID" and then immediately changes it to "YOUR_DEVICE_KEY"

1 Like

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