Arduino IoT Cloud

This example demonstrates code for communicating with the Arduino IoT Cloud. Its purpose is to connect to the Arduino IoT Cloud and interact with cloud variables. Here, we send the temperature values read from the DHT11 sensor to the Arduino IoT Cloud, allowing us to monitor it from the cloud.

../_images/02_cloud.png

Required Components

In this project, we need the following components.

It’s definitely convenient to buy a whole kit, here’s the link:

Name

ITEMS IN THIS KIT

LINK

Elite Explorer Kit

300+

Elite Explorer Kit

You can also buy them separately from the links below.

COMPONENT INTRODUCTION

PURCHASE LINK

Arduino Uno R4 WiFi

-

Breadboard

BUY

Jumper Wires

BUY

Humiture Sensor Module

BUY

Wiring

../_images/02_arduino_iot_cloud_bb.png

Schematic

../_images/02_arduino_iot_cloud_schematic.png

Install Arduino Create Agent

  1. Visit the address https://create.arduino.cc/getting-started/plugin/welcome.

  2. Click START.

../_images/02_install_agent_2.png
  1. Choose the version that suits your computer, and it will download an installation package.

../_images/02_install_agent_3.png
  1. Install the agent.

../_images/02_install_agent_4.png
  1. After installation, go back to your browser, and you will see the following interface.

../_images/02_install_agent_5.png
  1. Click NEXT, and then you can GO TO the web editor.

../_images/02_install_agent_6.png

Using Arduino IoT Cloud

  1. First, you need to log in or register with Arduino.

  1. Once logged in, click on IoT Cloud in the upper right corner.

    ../_images/02_iot_cloud_2.png
  2. Create a new thing.

    ../_images/02_iot_cloud_3.png
  3. Associate your device.

    ../_images/02_iot_cloud_4.png
  4. Set up a new device.

    ../_images/02_iot_cloud_5.png
  5. Choose your Arduino board.

    ../_images/02_iot_cloud_6.png
  6. Wait for a moment, and your UNO R4 WiFi will be detected. Continue by clicking configure.

    ../_images/02_iot_cloud_7.png
  7. Give your device a name.

../_images/02_iot_cloud_8.png
  1. Make your device IoT-ready, and remember to save the secret key.

../_images/02_iot_cloud_9.png
  1. Wait for a few minutes.

../_images/02_iot_cloud_10.png
  1. Configure WiFi.

../_images/02_iot_cloud_11.png
  1. Here you will need to enter your WiFi password and secret key.

../_images/02_iot_cloud_12.png
  1. Add a variable.

../_images/02_iot_cloud_13.png
  1. Here, we want to display the temperature in IoT Cloud, so we configure a read-only float variable.

../_images/02_iot_cloud_14.png
  1. After completion, go to the sketch.

../_images/02_iot_cloud_15.png
  1. Open the full editor.

../_images/02_iot_cloud_16.png
  1. Click on Libraries on the right side, then Library Manager.

../_images/02_iot_cloud_17.png
  1. Search for the DHT sensor library and check it.

../_images/02_iot_cloud_18.png
  1. Now, we need to edit the code. You can see that the editor has already prepared the IoT Cloud-related code for you. You just need to add the specific functionality you need. In this example, we added code to read the temperature using the DHT11 sensor.

// DHT sensor library - Version: Latest
#include <DHT.h>
#include <DHT_U.h>

/*
Sketch generated by the Arduino IoT Cloud Thing "Untitled"
https://create.arduino.cc/cloud/things/260edac8-34f9-4e2e-9214-ba0c20994220

Arduino IoT Cloud Variables description

The following variables are automatically generated and updated when changes are made to the Thing

float temperature;

Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
which are called when their values are changed from the Dashboard.
These functions are generated with the Thing and added at the end of this sketch.
*/

#include "thingProperties.h"

#define DHTPIN 11
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

void setup() {
    // Initialize serial and wait for port to open:
    Serial.begin(9600);
    // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
    delay(1500);

    dht.begin();

    // Defined in thingProperties.h
    initProperties();

    // Connect to Arduino IoT Cloud
    ArduinoCloud.begin(ArduinoIoTPreferredConnection);

    /*
        The following function allows you to obtain more information
        related to the state of network and IoT Cloud connection and errors
        the higher number the more granular information you’ll get.
        The default is 0 (only errors).
        Maximum is 4
    */
    setDebugMessageLevel(2);
    ArduinoCloud.printDebugInfo();
}

void loop() {
    ArduinoCloud.update();
    // Your code here

    float temp = dht.readTemperature();
    temperature = temp;

}
  1. Upload the code. You may be prompted to update; follow the prompts to complete.

../_images/02_iot_cloud_20.png
  1. Return to IoT CLOUD.

../_images/02_iot_cloud_21.png
  1. Click on the menu in the top left corner.

../_images/02_iot_cloud_22.png
  1. Click on the dashboard.

../_images/02_iot_cloud_23.png
  1. Create dashboard.

../_images/02_iot_cloud_24.png
  1. There are many widgets available; here, we choose a value widget for displaying the temperature.

../_images/02_iot_cloud_25.png
  1. After clicking, a widget settings interface will appear, where you can connect the widget to the cloud variable you created earlier.

../_images/02_iot_cloud_26.png
  1. Now, you can view the sensor readings on Arduino IoT Cloud.

../_images/02_iot_cloud_27.png

How it works?

After configuring the IoT Cloud (device setup, network setup, creating cloud variables), you will notice that the sketch on the cloud updates automatically. So, most of the code is already written for you.

Open the editor, and you will see that this sketch contains four files:

main.ino: Used to initialize the Arduino and perform the main loop tasks. Additionally, it includes logic for connecting and communicating with the Arduino IoT Cloud.

thingProperties.h: This file is used to define variables and functions in the Arduino IoT Cloud. It contains declarations of cloud variables and their associated callback functions. In the provided code, it is used to initialize cloud properties (e.g., the temperature variable) and connect to the Arduino IoT Cloud.

Secret: Used to store sensitive or private information, such as WiFi passwords or API keys. This sensitive information is typically not exposed directly in the code but is stored in the Secret file to enhance security.

ReadMe.adoc: Contains project documentation or other relevant information for easier understanding and use of the project. This file usually does not contain executable code but rather documents and descriptive information.

We need to add some code for the DHT11 sensor. This code is identical to what you would use on your local IDE. The only difference is that you need to assign the value read from the DHT11 to the cloud variable temperature.

(Note: You should never modify thingProperties.h and Secret. They will be modified when you make changes using the Thing editor.)