Arduino Cloud: Three Key Starter Projects
by CapeGeek in Circuits > Arduino
519 Views, 1 Favorites, 0 Comments
Arduino Cloud: Three Key Starter Projects
If you were excited about using the Arduino Cloud to make IoT projects, but ran into early problems, I have shared your frustrations. Even though I am an experienced Arduino user, I found it difficult and time consuming to get the basic examples running. In this Instructable I've tried to cover details I missed at first, along with some visual representations, to help you quickly build a working Thing that implements three useful capabilities:
- Switch on IoT Remote ---> sends --> Device LED: A virtual switch on an IoT Remote (e.g. iPhone) sending commands to a component on a hardware device (e.g. LED).
- Switch on Device --> sends --> IoT Remote LED. The reverse case, where a switch on a hardware device sends commands to a virtual LED on an IoT Remote
- Reading Pot Values on a Device ---> sends ---> Dashboard on IoT Remote: This is reading a value from a hardware sensor/device (e.g. potentiometer) sending the values to an IoT Remote Widget (e.g. gauge, graph).
Example 1 is described in some detail in Steps 1-7. Then the implementation of Examples 2 and 3 follow in steps X to Y, but in less detail. These examples show where elements go in the IoT Sketch to set up activities that happen when the Cloud Variables change and setting up activities that occur periodically.
Once you have successfully built these three examples you can easily extend them for other projects. The LED in Example 1 could be replaced with actuators, from buzzers, to solenoids. The hardware switch in Example 2 could be replaced with a magnetic switch, proximity sensor, etc. Finally, you could replace the potentiometer in Example 3 with any sensor that changes voltage values.
Finally, if you need more beginner-level details, I have included links to additional information at the end. A copy of the final sketch is included at the end.
I hope this Instructable helps unravel some of the mystery for early developers to ease your path to full scale projects.
Supplies
- Arduino account: https://cloud.arduino.cc/
- Arduino Create Agent web app(downloadable from the same site)
- Arduino IoT Remote App running in your smart phone or laptop from Arduino Store
- A cloud compatible microcontroller board, such as Arduino MKR WiFi 1010 or NANO , or compatible 3rd party board
- SPST Mini Toggle or Slide Switch
- 10K Potentiometer
- Breadboard, jumper wires, and power supply https://amzn.to/4bK5XJg
To build an Arduino Cloud project you will need an online Arduino user account and a compatible Arduino board. By default, you can start with a free plan. While it limits some capabilities, those are not a problem for early projects. For reference, you can only have one Thing associated with a Device. You can have up to 2 things in a free account and the data can be stored for up to 1 day. Arduino has multiple paid options that allow you to add more things and retain data for longer periods.
I have used both the MKR WiFi 1010 https://amzn.to/4c5VgBc and the NANO 33 IoT: https://amzn.to/3Vq8fbI
The steps below assume the reader is familiar with the basics of Arduino Sketches and the Arduino Cloud, and has set up the Arduino Create Agent using their Arduino user account. As a result, I've intentionally tried to keep the steps fairly terse to speed up your process. But if you need more information, I have added more details in the last Steps.
Device Wiring
The first step is to wire up a very simple breadboard that has an Arduino device and a couple of simple components. In this case I am using an Arduino MKR WiFi 1010, a hardware switch, and a 10k potentiometer. These are wired as shown above. Note that the potentiometer is connected to the Vcc pin, not the 5v pin for safety.
Example 1 will use a virtual switch in the IoT Remote and the on-board LED on the MKR device, so a separate LED is not wired on the breadboard. Example 2 will use the hardware switch shown here and a virtual LED in the IoT Remote. Example 3 uses the potentiometer shown on the board, sending values to a virtual Gauge on the IoT Remote.
Create Skeletal Thing for Example 1: IoT Remote Switch ->sends-> Device LED
The first step is to use the Arduino Create Agent to make a skeletal IoT Thing that will be expanded to implement Examples 1-3, described above. Since examples 1 and 2 are similar to the regular beginner example of the LED Blink project, but these are IoT versions where the devices are controlled by an IoT Remote (e.g. your phone). Use the name "Blinky" for the IoT Thing you will be creating here. This first example uses a virtual switch on the IoT Remote (e.g. iPhone) to send commands to a LED on a hardware device (IoT Remote Switch ---sends--> Device LED).
The specific steps below are also shown in the numbered diagram above:
- The steps begin by clicking on Create New
- Then click on Thing
- This takes you to Setup
- Next, look in the top left corner for Things > Untitled. This is where you click on rename to change the name to your project name, in this case, Blinky
- .Next click on Associate Device, which takes you off to either select a device if you have already set one up, or set up a new device. If setting up a new device it many take a few minutes and ask you to click Configure. It will then give you an auto-generated name for your device. This is not particularly important to change.
- After you click, that box of the Create Agent should show your devices with an auto generated name.
- Click on Configure Network and save your WiFi name and password. At this time, skip the Smart Home Integration and Webhook buttons.
This completes what can be called the skeletal Blinky Thing. In the next step you will add information about the Cloud Variable that will be shared between the IoT Remote and hardware device.
Adding an IoT Cloud Variable
This step describes how to add the Cloud Variables needed for Blinky using the Create Agent. Cloud Variables are variables in the Cloud that provide a shared, common link between the hardware device and virtual Things s.
At this point, you should be in Step 8 in the Thing window of the Create Agent. To add your first Cloud Variable, click on the Add variable button you should see in the right side of that Window. Once in that window, shown above, you can choose the name, data type, and interaction policy for your variable by completing:
- Name: For the IoT version of Blinky, use the name remoteSwitch for the variable name
- Sync With Other Things: Not used in this example. But could be used to sync with variables in another Thing.
- Type: Since the LED we want to control only has two states (on/off), use the pull down menu to select the Boolean type (bool) for the Cloud Variable
- Variable Permission: Since you want to modify the value through a cloud connection, set the variable permission to Read & Write.
- Variable Update Policy: For this example, select On Change. In other examples you can update periodically.
Finally, click the ADD Variable button. That will take you back up a level to the Thing window.
Once you complete the above steps the Create Agent will automatically generate the skeletal Blinky Sketch. You will see this in more detail in the next Step.
Examining the Skeletal Sketch
You should be back in the Thing window and ready for Step 9 in that diagram. Click on Sketch in the top, far right row. This will take you to a new frame in the Thing window so you can finish the upgrading the skeletal Sketch. When you open the tab you are actually in a Sketch editor, like the Arduino IDE, and the skeletal Blinky Sketch is already loaded. This skeletal Sketch includes all of your additions for device connection, WiFi info, and variable specs, as well as automatically added functionality to update the Cloud variables as they change. The skeletal sketch still needs you to manually add code to the to make it have the Blinky functionality, which will be done in the next sketch.
Before doing that in the next Step, it is helpful to examine what the skeletal sketch contains. You will notice that the cloud variable is added, as bool remoteSwitch. It has a property initialization file link, as would a regular Arduino Sketch.
The Sketch also has the usual void setup () ( function that runs once. In this example, that function has details about setting up the WiFi link, serial monitor, and debugging. But note these were set up by filling in the blank in the Thing window, not by adding code manually. Later examples will have code you will add in setup, but none is necessary yet. Next is the void loop () { function that continuously to run continuously as long as the device is powered. This is where you will add code in the next step to give the skeletal sketch the functionality to turn the LED on/off when the switch state changes. But at this point, it is only an empty pace holder.
Adding Blinky Functionality to the Skeletal Sketch
To give you some orientation, the diagram above has the familiar sketch for a regular Arduino Blink project that turns an LED on/off depending on the state of a switch. In this regular/non-IoT example, the LED and switch are both hardware components connected to the Arduino device. If not familiar, take time to understand this basic conditional code first, as you will be adding much the same conditional logic in the your new IoT Sketch. The only difference is that the regular sketch uses "switchPin" for the variable, and in Blinky you are using "remoteSwitch". But in the first Blinky example, the LED will still be on the hardware device, but turned on/off depending on a virtual switch in the IoT Remote.
Looking at the skeletal Sketch, skim down to find: void onRemoteSwitchChange() {
This is called the callback function for the remoteSwitch Cloud Variable you created earlier. This was set up automatically in the sketchwhen you set the policy for the Cloud Variable to get attention when it's value changes. The Create Agent set then added the callback function automatically to perform some behind the scene magic for you whenever the Cloud modifies the value of the variable. In this case, when the switch changes state.
Right after, you see, "add your code here", add the conditional code that is the same as in the regular sketch. This may be confusing to first time users, so to clarify, this is where you need to copy/fill-in the functional lines to give the Thing the desired functionality to make your Think work, as shown below:
if (remoteSwitch == HIGH){
digitalWrite(LED_BUILTIN, HIGH);
}
else {
digitalWrite(LED_BUILTIN, LOW);
}
Now whenever the device receives an update from the cloud that the variable has changed, it means the switch was pressed, so it will turn on the LED. (Note, the built-in LED is being used for simplicity, but it could be one you add and assigned to a pin.)
Since you are in a Sketch editor (like the IDE), you can click the verification tab at this point with the check mark "✓". When confirmed that it is working, you can click the upload button "->" to compile and upload your finished IoT Sketch to the hardware device you have it assigned.
At this point the Sketch part of the Blinky Thing for the Example 1 is completed, but you still need a Dashboard to interact with your device.
Adding Widgets to a Dashboard and Controlling Blinky
Now that you have configured the device and network, created variables, and uploaded your Sketch for your Blinky Thing. The next step is to create a Dashboard. The Dashboard will be your control center for your cloud applications that make it easily to monitor & control your Arduino boards from a web browser or the Arduino IoT Cloud Mobile App.
Dashboards are composed of widgets, which are directly linked to a Cloud Variable in your Thing. Your sketch will likely have other non-share/non-Cloud variables, but whenever a Cloud Variable is updated, the Widget value is updated as well. A widget is a dashboard control or monitoring item, such as a gauge or push button. There are many different widgets available including:
- Gauge - displaying data in a "gauge" style.
- Switch - for switching a boolean, i.e. turning something on/off.
- Map - display the location of your Thing.
- Messenger - to display or send strings to your board.
A full list of widgets and how to use them is available here https://docs.arduino.cc/arduino-cloud/cloud-interface/dashboard-widgets/#list-of-widgets
Creating a Dashboard for Blinky is fairly simple compared to making the Thing. Start by going back to the top level of the Create Agent. As shown in the diagram above, click on the link in the left hand ribbon to 1. Dashboard. On the top right, also click on the + Dashboard button to enter a dashboard editor.
In that window, first 2. click untitled, to change the name to Blinky. FYI, you will be adding to this same dashbord for all 3 examples, so you can leave it named Blinky.
Next, 3. click, the Add button. That will take you to a frame where you select the type of Widget, also in 3. In this case Push Button (could be slide or toggle switch). When you click on the Push Button icon, a window shows an unnamed push button icon. You can rename it "Remote Switch" and click to select the variable from the Blinky Thing listed, click 4. to add select the variable you want to create a link between the widget and the variable in the Thing. In this case, remoteSwitch. Click on remoteSwitch and an icon will show up. In 4. click Link Variable, then click Done.
Finally, 5. click on the crossed-arrow button to open up a window where you can use your mouse to move the symbol (in the case push button) to position it in a smartphone display.
At this point the Widget is finished. As soon as you click Done, the widget will be placed on the dashboard accessable on your laptop or IoT Remote, described in the next step.
Using the IoT Remote to Control Blinky
Dashboards have two modes, edit and view. For creating and configuring the dashboard, you use the edit mode. This Dashboard is very simple, but will give you an idea of where to begin.
Once done, you use view mode to control Blinky from either your computer screen or the mobile version of the IoT Remote App in your phone.
You can now use the dashboard to control the device LED by clicking on switch icon to toggle it.
Congratulations, your Blinky Thing has now been successfully implemented.
Adding Example 2: Switch on Device -->sent--> IoT Remote LED
Now that you have implemented the Blinky Thing for Example 1, it is easy to add capabilities for Example 2, because you will be adding to Example 1, not starting over. Note that Example 2 is the reverse case of Example 1, where the state of a switch on the hardware device sends commands to an IoT Remote that has a virtual LED on the Dashboard. This is easily done the following steps in the list above.
- Go back and open the Blinky Thing on the Create Agent
- click to add a new Cloud Variable "button_state"
- set it up as type "bool", read/write, and on change, and click on Add Variable
- Now button_state should show up in the list of Cloud Variables
- While in the Thing window, click on the top right Sketch tab
- After-"include "thingProperties.h” add the following:
//Pin for the hardware push button connection (FYI, this is a new non-cloud variable: buttonPin)
const int buttonPin = 5;
- skip down to after void setup() {
- add:
// buttonPin is set as input with pullup resistor
pinMode(buttonPin, INPUT_PULLUP);
- after-void loop() {
- add:
//Read button_state from buttonPin (negating it since the switch is tied to ground, not +)
button_state =!(digitalRead(buttonPin));
This completes the additions to the Sketch for Example 2. Since you are in a Sketch editor (like the IDE), you can click the verification tab at this point with the check mark "✓". When confirmed that it is working, you can click the upload button "->" to compile and upload your extended IoT Sketch to the hardware device you have it assigned.
Your Blinky Thing should now have the added functionality of Example 2 in addition to that for Example 1.
To finish Example 2, you need to repeat the simple steps you did above to add a second Widget in the Dashboard.
- In the Create Agent, click on the link in the left hand ribbon to 1. Dashboard.
- Click on the Blinky dashboard you made for Example 1 to enter the dashboard editor.
- In that window, you can keep the same title (top row): Blinky
- Next, click, the edit button in the top row. That will take you to a frame with an ADD button, to select your type of Widget
- In this case, scroll down to the LED icon.
- When you click the icon, a new window will ask you to rename and select the variable you want to create a link to.
- In this case, name it "Remote LED" and select button_state.
- Click Link Variable, and then click Done.
- Finally, click on the crossed-arrow button to open up a window where you can use your mouse to move the symbol to position it in a smartphone display, then click DONE in the top.
At this point the 2nd Widget is finished. As soon as you click Done, the LED widget will be placed on the dashboard and it will be ready for action when you click on the View mode icon. You should be able to see it on your phone's IoT Remote app.
Test your new Blinky2 Thing and make sure that when you change the hardware switch on your device that the LED changes state on your IoT Remote Dashboard. It may take a few minutes to update.
Adding Example 3- Pot Value --> Sent --> Dashboard on IoT Remote
Now that you have implemented Example 1 and 2, this should be getting a little easier. So go back to the the Create Agent to add capabilities for the final Example 3. In this example you will read the value of a hardware potentiometer connected to your device, map that value to an angle, send it to the cloud, and displaying it on a gauge on the IoT Remote Dashboard. The new code is added to the sketch you have made for Example 1 and 2, The new code follows:
- Starting in the same Blinky Thing on the Create Agent, click again to add a 3rd Cloud Variable "pot_value"
- But this time, instead of a bool type, since the potentiometer (and most other sensors) have more than on/off state, set the variable type to integer
- In this case you need to read only and update on change
- Click Add Variable and now it show up as the 3rd variable in the list
- While still in the Thing window, click on the Sketch tab in the top right
- In the declarations section (before Setup) add code to define the pin the potentiometer is connected to:
// Define pin for potentiometer
const int potPin = A1;
- In the setup section add:
pinMode(potPin, INPUT);
- After the loop section add:
// Get Potentiometer Value
pot_value = analogRead(potPin);
That completes the new additions to the Sketch for Example 3.
Now from the Thing window, from the top left, go to the Dashboard window
- Click on Add Widget
- Change the name to: Remote Pot Value
- Select the widget showing the Guage
- Link it to the variable: pot_value
- Edit the range so it is 0 to 1023 ( the max output of the A/D converter)
- Edit the position of the guage icon on on the Dashboard to a convenient place
- Return to the Viewing screen icon at the top of the Dashboard
Your Blinky Thing should now have the added functionality of Example 3 in addition to that for Examples 1 and 2.
Now go to the IoT Remote on your phone and see check if the virtual Gauge display changes when you turn the pot dial. This is basic setup can be easily customized by adding a mapping function to translate the pot values to angles, percentages, etc.
This completes the implementation of Examples 1, 2, and 3 and should give you an understanding of how these examples can be reused and modified in your own Arduino IoT Cloud projects.
Thing Summary
The table above shows an overall view of the elements all of the three examples. Each example has a different interaction modality and those require different additions to the sketch. This diagram will show you a little more clearly, what is different about the three implementations to make it easier for you to decide which case to build upon.
Basic Arduino Cloud IoT Concepts
For reference, a very brief summary of Arduino Cloud IoT concepts used in this Instructable is given below. I put these at the end since they will be familiar to some users and make the text unnecessarily lengthy in that case. Links to even more details are reverenced at the end..
The Internet of Things (IoT) has been around since the 80's to build projects that sense, control devices, and visualize data, connected remotely over the Internet https://en.wikipedia.org/wiki/Internet_of_things. Unfortunately, most approaches required mastering complex networking protocols and communication technologies until Arduino integrated the capabilities into their Arduino Cloud. Ideally, the Arduino Cloud provides a powerful, all-in-one solution, to construct fairly complex IoT projects without having to deal with the underlying technology.
The core concept for IoT devices is an IoT Thing. Things basically enable the connection and communication between devices through the Internet. The concept of a Thing may be new to you, but they are not really complicated. A very helpful metaphor is to think of "IoT Things" as just containers for all the components needed to mechanize an IoT project. So a Thing needs to include the following four components:
- IoT Device: An Arduino Cloud-compatible hardware microcontroller board that the Create Agent has configured. In this example, a MKR WiFi 1010
- WiFi Network Credentials
- IoT Sketch
- Contains the traditional void setup and void loop function, just like a traditional, regular Arduino Sketch
- Contains variables, just like a regular Sketch
- Code that you add to the void loop function that defines the specific functionality of your IoT construction
- Automatically generated code that adds Cloud-specific initialization and a variable updating
- IoT Variables: Like regular, typed Sketch variables, but with these enhancements-
- They are the shared connecting link between your Arduino board and the Arduino Cloud
- Has a read/write permission options about sending/receiving Cloud data
- Policy about variable updating based on when Cloud values changes or periodically
Setting Up the Arduino Cloud Create Agent
A clever aspect of Arduino's simplification for the developer is a browser application called the Create Agent. This serves as your Home Base to seamlessly configure, edit, and upload all the necessary components into a Thing. The Create Agent also enables making Dashboard visual displays to monitor and control the IoT Things on an IoT Remote device, like a smart phone or your laptop.
After downloading and installing the Arduino Cloud Agent from the Arduino website, it will show up in your browser ribbon as a grey circle with the Arduino infinity - + symbol, as shown here. The key items you will be using are accessed in the left action bar: Things, Devices, Sketches, and Dashboards.
From the user perspective, you can think of the Create Agent as a convenient interface between the Arduino Sketch Editor (IDE), familiar to regular Arduino developers, and the Arduino Cloud. But the Create Agent also serves as a "protection" interface between the user and the Arduino Cloud to prevent it from being unintentionally corrupted by user error.
Links and Differemces Between a Regular Sketch and an IoT Sketch
Arduino Cloud introductions typically dive right into walking you through the process of using the Create Agent to assist you in making a basic IoT project with only a few steps. On the one hand, the Create Agent is an elegant success at streamlining what the user needs to know to produce a working IoT project. But, an unintended consequence is that this simplification leaves you wondering about a few unexplained details about what is created automatically, and what and were you needed to manually add your own code. This is sort of like modern car dashboards that have successfully replaced gauges, with idiot lights. It is great while all the lights stay green, but how do you decide what to do when the car goes dead and some of the lights come on red?
This Instructable starts by taking a step back to start you in familiar territory by briefly reviewing the structure of a regular Arduino project, to clarify the link and similarities. The example is the classic Hello World project for Arduino beginners, the Blink project. Blink adds a basic Sketch to an Arduino microcontroller board to endlessly flash an LED on and off, as shown in the Sketch above. Blink has the classic three component Sketch structure.
First are hardware initialization and variable definitions (library, pin assignments, etc.). In this case, for the switchPin variable set to 1 for a pin assignment for the hardware switch.
Second, the void setup() { } function that includes code between the curly brackets to do a specific task that runs once at the start. This is the code that is the heart of determining any functionality to be computed. In this case it sets how the pin assignments interact with the external environment, as either input or output .
Third, the void loop() {..} function repeats forever as long as the Arduino is on. In this case, it contanis a conditional that determines if the LED is turned on or off, depending on the switch state.
The IoT Sketch in an IoT Thing and a regular Sketch are the same in that they both have the same three component skeletal structure. But they are different in that the inputs and outputs do not have hardware connections. In the IoT, some or all of the inputs and outputs are connected by Internet connections. In the case of the first example here, the switch is on an IoT Remote (e.g. iPhone), instead of a hardwired switch, sending commands to a hardware device (e.g. LED). A key insight is that the link in the IoT is mechanized by sharing a common variable in the cloud, called a Cloud Variable. Cloud Variables will be set up in the Create Agent.
Another difference between regular and IoT Sketches is that the Create Agent adds code to the IoT Sketch to send updates to the Arduino Cloud when there is a change in the variable value either at the device or the IoT Remote. This will become obvious in the nest Step when a Skeletal Thing and Sketch is created. Hopefully, this explanation will make the purpose of new code make more sense.
Additional Arduino Information
Arduino has provided a lot of helpful tutorial information about using the Create Agent, but some details were scattered. Below is a collection that I have found helpful. Please update in the comment section here if you have additions or corrections to help other newcomers.
- Not all USB cables are equal. Make sure you USB cable is a data cable and not a power-only cable. This is a major time waster for many new users because a power only cable will not download data.
- The Create App can be finicky and will often not see a new device when connected until several tries. The usual list of fixes include: re-plugging the USB cable, pushing the reset button on the board 2x quickly, when all else fails, unplug, restart your computer, and start all over, just keep repeating until the system sees your device.
- You may want to go with the device name automatically generated, even though it may seem senseless. New names do not always stick.
- Deleting dashboards or widgets does not impact the functionality of your Thing & device. But that does remove the historical data of that Widget.
- Once you upload a sketch to a device, even after it says it has uploaded, it can take some time for the device to start behaving as specified. So give it a moment.
- The yellow blinking light on MKR board has to do with a charging circuit. It is Nothing to be concerned about when it starts blinking, but also don't confuse it with an attempt to make your onboard LED blink;-)
More Resources
Here are keys link to the Arduino Cloud info on their website:
https://docs.arduino.cc/arduino-cloud/
https://docs.arduino.cc/arduino-cloud/#arduino-iot-cloud-api
In addition to Arduino's online information, here are several additional resources I found very helpful:
I believe the following book is currently the best extensive resource overall: Arduino IoT Cloud for Developers. https://amzn.to/3VnWXo8
https://dronebotworkshop.com/iot-cloud-fundamentals/
Below is an excellent tutorial for a similar first time project on a NANO insetead of the MRK board:
You can find a good comparison with other current options (2024) here: https://community.element14.com/technologies/internet-of-things/b/blog/posts/a-comparison-of-arduino-iot-cloud-to-amazon-aws-google-cloud-and-microsoft-azure
You can find more information about those and other compatible boards at: https://store-usa.arduino.cc/pages/cloud-compatible-boards?selectedStore=us