Arduino IoT Cloud 2021 - Getting Started with Arduino & ESP32

Rek XIX
40 followers
27 Video Views·Oct 31, 2022
Arduino IoT Cloud 2021 - Getting Started with Arduino & ESP32
Today in the workshop we're getting started with the Arduino IoT cloud.
We'll learn how to build a thing, connect it to a device,
and build a dashboard to control our Arduino through the cloud.
We'll also use the Arduino IoT cloud with an ESP32.
We have our head in the cloud today, so welcome to the workshop.
Well hello and welcome to the workshop,
and today we are going to be working up in the cloud,
specifically the Arduino IoT cloud.
The Arduino IoT cloud is a product that lets you use Arduino boards
and some third-party boards as well and control them through the cloud
so we can build our own Internet of Things devices
using Arduino coding that we're very familiar with.
Now the Arduino IoT cloud has been around for a little while,
but it recently underwent a big change in the way that things work on the cloud
and the way that plans work as far as joining the cloud
and pricing and that has all been reworked.
So you've probably seen a few videos or read a few articles about the IoT cloud.
I just want to let you know if those videos and articles were made before 2021,
chances are they are a little out of date and not exactly correct.
Now one of the advantages about the updates that Arduino has made recently
is that they've changed the pricing structure on the cloud.
There is still a free plan and a number of paid plans
and in the free plan you can now use third-party boards
which you weren't able to do previously.
So we don't have to restrict ourselves to Arduino boards,
we can also use devices like ESP32s
and I'll be showing you that a little later on in this video.
Now I'm going to be using the cloud with my account and my account is a paid account.
However, everything I'm doing today is applicable on the free account.
I'm not going to be using any paid account features.
So you can sign up for the cloud for free and follow along in the experiments.
So before we get started, let's go and examine exactly what the Arduino IoT cloud is.
This is an IoT application platform created and maintained by Arduino.
It supports several Arduino processors as well as some third-party microcontrollers.
Some Arduino processors permit over-the-air updates,
allowing you to update your code over the internet
without a direct connection to the microcontroller.
The cloud has a mobile app as well that is available for both Android and iOS.
An advanced feature of the cloud is an API that allows you to create
web and desktop applications to control your microcontroller.
The API has a software development kit for JavaScript, Python, and Go.
The Arduino IoT cloud also supports webhooks
and this will allow integration with other existing IoT services.
Your historical variable data can be retained in the cloud.
The length of time it is retained depends upon which cloud plan you have signed up for.
You'll work with the Arduino IoT cloud using an online web editor.
The web editor allows you to create sketches
and also has a library manager that is equivalent of the library manager on the desktop IDE.
You'll be using that web editor to build a Thing,
the basic unit in an Arduino IoT cloud application.
You'll connect devices to your Thing, devices being microcontrollers,
and you will connect your Thing to your network, in this case being your local Wi-Fi in most cases.
You can add I O devices to your microcontroller
and control them through variables added to your Thing.
You can also create a dashboard in order to control and read data from your Thing.
The Arduino IoT cloud allows you to connect a compatible microcontroller to the cloud.
You can control that microcontroller with an online dashboard.
You can also use a mobile dashboard as well.
You can also use a second microcontroller to control the first one
and you can do this with or without a dashboard.
You can control existing IoT connected devices
and you can use IoT controllers from Amazon to interact with your microcontroller.
To start working with the IoT cloud,
first you'll connect a microcontroller up to your local workstation.
You'll then connect to the cloud
and the cloud itself will also connect to the microcontroller.
If your microcontroller supports over-the-air updates,
you can remove the connection directly to the controller
and just do your updates through the cloud.
In order for all of this work,
you'll have to install an Arduino agent on your workstation.
This agent is available for Linux, Windows, or Mac.
The purpose of the agent is to interface the USB port
on your workstation with the web editor.
You can use the cloud with a number of compatible microcontrollers.
This will include the Arduino Nano 33 IoT,
the Arduino Nano RP2040 Connect,
most of the Arduino MKR series of boards,
from the Arduino Pro series, the Portentia H7,
as well as the Expressive ESP32 and ESP8266 boards.
You'll also need to set up a user account in order to use the IoT cloud.
You can start with a free account,
and if you want more features,
there are a number of low-cost paid accounts
that you can subscribe to as well.
So let's set up an account and start working with the Arduino IoT cloud.
Now in order to use the Arduino IoT cloud,
you're going to need to create an account,
and Arduino has four different levels of account.
You can start with the free account,
and I would suggest that just so that you can start getting used to the IoT cloud.
After that, you can move over to another one of these accounts,
and they have the entry-level account at $2.99 US a month,
the maker-level account at $6.99 a month,
and that's the plan that I'm currently on,
and the maker-plus plan, which is $23.99 a month.
Now let's see what the differences are between all of these plans.
Now the total number of sketches that you can create
is unlimited for all of the plans,
but the sketch storage will vary.
It's unlimited for all of the paid plans,
but it is limited to 100 megabytes for the free plan,
and 100 megabytes is pretty generous.
The compilation time refers to the fact
that you're going to be compiling your sketches using the Arduino Web IDE,
and therefore you're going to be compiling online.
Again, you're limited to 200 seconds of compile time a day for the free plan,
and it's unlimited for the other plans,
but in order to put that in perspective,
I've been doing a number of experiments with the cloud,
and I've yet to use 200 seconds in one day,
so that's a pretty high limit.
Now the number of things you can build
is the restriction that you'll have to look at
if you want to update to a bigger plan.
You're only allowed to build two things with the free plan,
and that can certainly be enough in order to get your interest going.
However, for practical applications,
you're probably going to want to build a number of things,
and the number of things increases depending upon which plan you're using.
Another thing is the number of variables
that you're allowed to use in each one of your things.
On the free plan, you're allowed to use five variables,
and five variables is actually not a bad number.
Now these are cloud-connected variables.
On the other plans, you can go to 10 variables with the next plan up,
and the two higher plans have an unlimited number of variables.
You have an unlimited number of dashboards with all of the plans.
Now dashboard sharing allows you to permit you
to share your dashboard with other Arduino users.
I probably won't be doing that anyway,
but it is included on my plan.
The API is not available on the free plan.
It's a more advanced feature,
and we won't be discussing it today anyway.
With the other plans, you're limited to the number of requests
per second you can make to the API.
Webhooks allow you to hook other existing IoT services
to the Arduino cloud and use them with your device,
and this is available on all the plans.
Over-the-air updates, which is a cool feature we've already discussed
that allows you to update the code without connecting directly to the Arduino,
is not available on the free plan,
but it's available on the other plans.
The amount of time you retain data also differs between the different plans.
You'll retain it for one day on your free plan,
and it goes up to one year on the top plan over here.
All of the plans now have ESP32 and ESP8266 support,
and they all allow you to use the mobile app.
And so you'll have to select the plan,
and once you do, we can start using the Arduino IoT cloud.
Now, one other task that you're going to need to perform
before you can start working with the Arduino IoT cloud
is to install the Arduino Create Agent onto your workstation.
Now, the Create Agent is a small program that runs in the background,
and it provides an interface between your local USB port
and the web-based utilities in the Arduino IoT cloud.
And of course, you're going to need to be able to do that
if you have a microcontroller connected to your computer via USB.
Now, there is an agent available for all of the major operating systems
for Linux, for Windows, and for Mac.
I'm on a Linux machine,
and the installation is pretty well the same on all three of them.
You download the component,
you extract the file, and you run the installation program.
It's as simple as that.
Now, once the agent has been installed,
it's going to put a little icon onto your taskbar.
And here's mine over here, an Arduino icon.
It gives me the version of the agent.
It allows me to go directly to Arduino Create
or to open a debug console.
I can also pause or quit the agent.
Also, during the installation, it'll modify your startup a bit.
So let's go take a look at mine.
Here's my startup applications.
And as you can see, the Arduino Create agent
has already installed itself in there
so that it will start up every time that I reboot my workstation.
And again, for Windows and for Mac, it will perform a similar function.
So once you have this agent installed,
and you have an account,
we are ready to use the Arduino IoT Cloud.
Now, for our first experiments with the Arduino IoT Cloud,
we are going to start with the basics.
I'm going to take an Arduino Nano 33 IoT.
I'm going to attach an LED and a temperature sensor to it.
And we'll see how we can control the LED from our cloud console
and how we can read the temperature data back to the cloud console.
And we're going to take this step by step
so that you can grasp all of the basic concepts of working with the cloud.
Now, if you don't happen to have an Arduino Nano 33 IoT,
you can use another cloud compatible Arduino device,
and it'll work pretty much the same.
And that goes true for most of the experiments
that we're going to be doing today.
You could substitute a different controller and get the same results.
So let's go and see how we hook it up.
And then I'll show you how we can build our first thing.
For our first experiment,
I'm going to be using an Arduino Nano 33 IoT Connect board.
However, any board compatible with the Arduino Cloud would work.
So for example, you could also use an Arduino Nano RP2040 Connect.
I'm also using a DHT22 temperature and humidity sensor.
You could use a DHT11 sensor.
If you don't have a DHT22, you will have to modify the code slightly.
You'll need an LED and a dropping resistor for that LED.
We'll start by connecting the anode of our LED to one side of the dropping resistor.
And we'll connect the other side of that resistor to data pin D8.
We'll connect the LED's cathode to one of the ground connections on the microcontroller.
Next, we'll connect the data output from the temperature sensor to data pin D7.
We'll connect the ground connection on the temperature sensor to the Arduino's ground.
And finally, the VCC on the temperature sensor
will be connected to the 3.3 volt output from the Arduino.
And this completes our wiring.
So now that we've created an account on the Arduino IoT Cloud,
and we've added the Arduino Connect agent to our workstation,
we are ready to create our first thing.
Now I've cleaned everything out of my console
in order to let you see what it looks like the very first time that you come onto the IoT Cloud.
And you get an option to create your first thing.
You also get an option over here to go and build dashboards.
And we'll be doing that in a little while.
Devices let you set up and configure microcontrollers in order to control them through the cloud.
And integrations let you work with APIs.
Now if you have the free plan, this will not be available to you.
And we won't be looking at APIs today.
So I'm going to go back to Things and create our first thing.
So I'll click the Create Thing button.
And the Thing Editor comes up.
And you'll notice that it has a Setup tab, a Sketch tab, and a Serial Monitor tab.
It's got a title, which is currently untitled.
And we've got a section for Variables, Devices, and Networks.
So let's go and give this a title first of all.
And now that we've titled it, we can look at the other things.
Now devices are the devices that we are going to connect to, the microcontrollers.
And so I have my Arduino Nano 33 IoT connected right now to my USB port.
So I'm going to select a device over here.
And I get a choice of either Arduino devices or third-party devices.
I'm going to set an Arduino device because that's what I've got.
And it should go and detect my device hopefully.
And there it is.
It's figured out that I have an Arduino Nano 33 IoT.
I'll have to hit the Configure button now.
Now we have to give it a name.
And it's going to suggest a name.
It usually seems to be a female name, but I'll give it a different name.
And you'll notice that I've used dashes in my names.
You cannot use spaces in a device name.
And so I'll hit Next over here.
And it's going to try to configure my device for the Arduino IoT Cloud.
Now it needs to dump a bit of code onto it, so it can take a few minutes
because it has to go and grab the code from the internet.
But it does happen pretty quickly, as you can see.
And now we are done.
So now we have a device over here.
Now we can start adding some variables.
Now variables are the cloud variables.
The variables that we exchange between our device and the cloud.
And so we have on our board, of course, an LED and a temperature and humidity sensor set up.
We're going to start working with the LED right now just to get a feel for this.
And then we'll come back to the temperature and humidity sensor.
So hit Add Variable.
And we'll give our variable a name.
Now you can name it anything you like.
I'm naming mine Red LED because I have a red LED.
Obviously, if yours was yellow, you might choose a different name.
Now we need a type of a variable.
And there are many, many different types that we can choose from,
including a number of types you haven't seen before, such as acceleration, angle, area, etc.
Now there are some of these so-called fancy variable types
available that would work with our LED.
There's one called Light, for example.
However, I have found that with the mobile app,
that the dashboards don't work properly unless you use a more standard variable type.
Now that's probably just a bug that will be resolved eventually.
But for now, I'm going to just observe that and use a Boolean,
which is a standard variable type.
And it will do the job because all I want to do is either have my LED on or off.
And so this Boolean will represent the state of the LED, whether it is on or off.
Now you also need to set permissions for your variables.
And you can give it two different types of permission, read and write or read only.
We need read and write because this is an LED and we need to be able to write to it.
Generally, when you have an output, you're going to have to set it to read and write,
even though we can't read back from it, we need to be able to write.
For a sensor, such as our temperature and humidity sensor,
we could set that up as read only because we don't have to send it any data.
But in this particular case, read and write is the correct choice.
The update policy says when is the variable going to be updated?
Is it going to be updated every time something changes?
Or is it going to be updated periodically,
such as once a second or once every minute or something?
We're going to do it on change because we want to build a dashboard
that will allow us to control the state of this LED with something like a switch.
And so this is set correctly, we're going to add our variable.
Okay, now we've added the variable and you notice the network button is now available to us as well.
And this is the network or Wi-Fi that we're going to need to configure.
So let's click on that.
And we'll have to give it the name.
And we'll have to give it a password.
And we will save that.
Okay, so I've got my network configured.
I've got my device configured and variables configured.
Now we can move over to the sketch tab.
And the sketch tab will give us a sketch that we're going to need to upload
to our Arduino or our connected device in order to make everything work.
And you'll notice there's already a lot of things in the sketch.
Now we can edit it here, or we can click a button here for the full editor.
I'm going to stay here and I'll show you the full editor a little bit later on.
Now up in the declaration section,
you'll notice that it's already mentioned that there is a Boolean called redled.
And we don't need to declare it in this sketch because it's been declared in the cloud.
We also include in every one of these sketches a file called thing properties.
And these are the unique properties for our thing.
Now we've got in our setup, it's already set up a serial monitor for us.
It's initialized the thing properties and it's initialized and connected to the cloud.
We've also set some debugging levels over here.
Now the loop is continuously running something called our Arduino cloud update.
It needs to do that to stay connected to the cloud.
You can also add extra code to your loop.
And you'll notice it's made a function here on redled change.
And that's because we set up a variable that is set to activate on change.
So whenever it changes, this function will be called and we can do something.
And in our case, that's going to be either turn on or off the LED.
So we're going to add a bit of code over here in order to make all of this work.
Now of course we have an LED that's connected to data pin 8.
So we need to let it know about that.
Okay, so I've got an integer called myled that represents the pin I'm connected to.
Now down here in the setup, I'm going to need to set that up as an output with a pin mode command.
We've got a little error over here.
Let's fix that.
You'll notice it did try to put the bracket in the closing bracket for me.
I accidentally over typed it.
So I've got a pin mode command.
And so now our LED is set up.
We're not going to do anything in the loop.
But we do want to do something when the LED changes.
So before we do anything, let's just do a serial print out of what the value of the boolean is.
Okay, and we're going to print our boolean value, which is called redled.
And again, it put the brackets there, but it doesn't put the ending semicolon on there.
And now we need to actually build some code that will make the LED turn on or off.
And an if statement would do that for us.
So if it's a red LED, let's do something then.
Okay, so if it is actually existing, then we'll turn the LED on by sending that pin high.
And over here, we can throw in an else statement.
And I've got something a little off over here.
Okay, fix my spelling errors.
And so as you can see, I've got an if it's a red LED, I'll turn my L and that should be an ed
high else it's the digital right for my LED low.
And so that looks reasonable over here.
We can actually go and verify to see if that works.
And it's going to do a verification.
You'll notice on the bottom of the screen, you get some indication about what's going on.
So let's just see if I made any syntax errors.
And we're good.
And so now I can upload this to my Arduino.
Okay, that's on the Arduino.
And head over here to the serial monitor.
And we'll see that the serial monitors come back and told me I'm connected to the Arduino IoT cloud.
So that's great.
Now what I need to do is be able to control the thing I did.
And for that, we're going to need to create a dashboard.
So let's go into dashboards.
And we'll build a dashboard.
Now we come up with a blank dashboard called untitled.
And it also comes up in view mode.
We have to put this into edit mode in order to do something with it.
So let's put it in edit mode.
Now you'll notice we get an add button.
And we can edit the title of this.
So let's give it a, we'll give it a title.
And we can add widgets to it.
Now widgets are controls we can add to our dashboard.
And the very first one over here looks like it might be a good one to use.
It's a switch.
And we can use that to switch our LED on and off.
So we get the editor for this switch.
Now the name over here is simply going to be what it displays on the top.
So I'm going to just call this LED because it's an LED control.
And we need to link the switch to a variable, to a cloud variable.
So we'll link it to a variable.
We'll look at our things.
We only have one thing.
And the only variable available right now is one we created, red LED.
And it tells us it's a boolean.
Its last result was false, et cetera.
And so we'll link that variable to our switch.
And so now this switch is linked to that variable.
And it will control it.
And we can say done.
And there we go.
We've got our dashboard created.
And we can work with the dashboard either at edit mode or view mode.
So we can look at it over here.
And we can look at it over there.
And so now that we've got our dashboard and our code.
Let's go and see if this is actually working.
All right.
So as you can see, I've got my LED connected over here to my microcontroller.
I've got my dashboard, which I just noticed I spelt bassboard.
But I suppose I could correct that if I wanted to.
But anyway, let's see if the switch works.
I'm going to turn this on.
And our LED comes on.
And if I turn it off, the LED goes off.
Now I've also got the mobile app open over here.
And if I use it, I can turn the LED on.
And I can turn the LED off.
And notice that my dashboard or dasboard is also corresponding to what I'm doing on the mobile.
So when I work the mobile, it is also working on the regular dashboard.
And the reverse is true as well.
If I go and click this over here, you'll see the mobile one corresponds.
And so you can have your dashboard open on multiple devices and work with it.
And it will always reflect the current state no matter what device you're on.
And so there we've built our first thing with the Arduino IoT Cloud.
Now let's go and see if we can modify this to include the temperature and humidity sensor.
All right, now that we have our red LED working with our thing,
let's add the temperature and humidity sensor to it.
So I'm back in my thing editor.
I'm going to add some more variables to represent temperature and humidity.
We'll start with the temperature.
And I gave it a name over here.
And as for the variable type, again, I want to use one of the basic variable types for this.
And so a floating point number looks like it would be a good thing to use.
So I'm going to use a float.
And I'm going to call it board temp.
Now in this particular case, it only has to be read only.
Because I'm just reading this value back.
But we will do this on change.
And I'm going to leave the threshold empty over here right now and set it as it is.
So that's my variable.
And I'll make another one exactly like it for the humidity.
Okay, so now I've got my two variables defined over here.
Let's go and take care of them now in the sketch.
Because as you can see, the sketch has now been modified.
Now the sketch has been modified.
You can see it's added some floats, etc. over here that I've just defined.
It's down in this section over here.
It hasn't really done too much more because basically it was already set up.
Now I'm going to go into the full editor right now though.
Because I'm going to need to include a library in order to work with the DHT22.
So let's open the full editor.
And the full editor, as you can see, has a lot of features.
It looks in many respects just like the editor on the dashboard.
At least it has a lot of the same features.
And here's our code in it over here.
And you can also see the other files that are included with these projects.
The thing properties file.
The secret file is the one which has my username and password for the LAN.
And a readme file which is kind of neat.
Because basically it has just given me the format for a readme
that I could edit right now and include with my file.
So that's a pretty nice little feature.
So at any rate, we need to add a library over here.
Because our DHT needs to work with a library.
So if we go to libraries over here, we get a complete library manager.
And we can search for our library.
Let's search for DHT.
And we got the DHT sensor library over here.
Now you can go and hit more info.
That will open up the page on GitHub that actually tells you all about the library itself.
You can go down and see some examples for this library.
What I'm going to do is just include it.
And the include button will let you include it.
There's an arrow beside the include button.
And you can also even include earlier versions of the library if you wish.
So that's a nice handy feature.
So I'm just going to say include.
And it's included the library.
Now as you can see, it's added some code over here.
I'm just going to move that because I like it to be in a different spot.
We'll put that down over here with this include.
Okay, it's included the DHT sensor for me.
Now I need to do a little bit of typing in order to make this sensor actually work.
First of all, we have to define the DHT pin.
Okay, and we've got it connected to IO pin number seven.
And we also need to define the type.
Okay, and I've defined mine as a DHT22.
Now if you're using a DHT11, you would obviously want to change that particular line.
And then we're going to go actually create an object.
I don't need to put a semicolon on the end of that.
Okay, so now we've defined an object called DHT.
And we've defined it with the DHT pin and the DHT type.
Okay, let's go down into the setup.
In the setup, we need to initialize it.
So we'll do that somewhere here.
And we'll do a DHT begin, although I didn't put it.
There we go.
DHT dot begin should initialize everything.
And we're going to go modify our code in the loop over here.
Because we want to just continually read the DHT sensor.
Now the DHT sensors are older capacitive style sensors.
And they can only update their data about every two seconds.
We're going to start a loop with a delay.
And we'll put a two second delay in to start everything off.
So we give the sensor time to stabilize.
And then we're going to assign some variables over here.
So you remember our two cloud variables are board temp and board humid.
So okay, DHT read temperature.
Let's make that a little prettier.
And we'll do the same thing for the humidity.
Okay, there we go.
So now we can go up here, we can save it.
And we can upload that to our board.
Oop, and I got an error.
Where is my A is not declared in this scope.
And I don't even know how the letter A got in there.
Somewhere in my cutting and pasting, no doubt.
Let's give that another try.
And it's done uploading.
Okay, so now we've got a code uploaded.
We will have to go into our dashboard next.
And modify that in order to read it.
So let's go back to the IoT cloud.
I'll leave the page.
We can go to our dashboards.
Go to my first dashboard.
And let's go and add some things in order to let us monitor the temperature.
So let's go down over here.
And we've got a gauge over here.
That'll probably be pretty good for temperature.
So the name I'm going to change it to temp.
And I'll link a variable to that one.
And I can have board temp and board humid are available for it.
Board temp is what we want.
We'll link that variable.
And the maximum and minimum over here.
The maximum is 100, which is a little high for the temperature in Celsius.
I'm going to make that 40.
And there we go.
We have a temperature gauge.
And as you can see, it's already up and working.
It's reading the temperature here in the workshop.
Let's do the same thing for humidity.
Let's go and add another one.
Could use a gauge, but why not percentage?
Because that's how humidity is generally displayed anyway.
Give it a different label.
We'll link another variable to it.
Board humid.
We'll link that variable.
And it's done.
And there we go.
And our dashboard is all running and working.
You'll notice you can use the dashboard in the edit mode or in the view mode.
Doesn't matter.
And it still works with the LED switch.
It still has my temperature and my humidity displayed on the dashboard.
So we have built a thing that is both able to control an LED
and to read back temperature and humidity.
Now in our first experiment, we took an Arduino Nano 33 IoT.
And we built a thing.
And we used a dashboard to control that thing.
But in our next experiment, we won't be using a dashboard.
Instead, we are going to build two things.
And I'm going to build them out of Arduino Nano RP2040 Connect boards.
But you could use different boards.
In fact, they don't even have to be the same board.
As long as they're boards that are compatible with the cloud.
Now we're going to put an LED and a push button on each board.
And use the push button on one board to control the LED on the other.
And while that may be simple, it illustrates a very important concept.
And remember, these two boards don't have to be on the same Wi-Fi network.
Although they're both on the same Wi-Fi network in my office here in Montreal right now.
I could have one of them here in Montreal.
And use it to control another board that's in Mumbai or Melbourne or Minneapolis.
It doesn't really matter as long as they're both connected to the internet.
So this is a very powerful technique.
So let's go and hook up a couple of RP2040 Connect boards.
And we'll see how we can use them to control each other through the Arduino IoT cloud.
Now here's our hookup.
Remember you're going to need to duplicate this hookup.
You'll need an Arduino Nano RP2040 Connect board.
Or another cloud compatible Arduino board.
We'll need a momentary contact push button switch.
An LED, any color that you like.
And a dropping resistor for that LED.
I use 220 ohms but anything in the 150 to 330 ohm range will work.
We'll start by connecting data pin D7 to one side of the resistor.
And then we'll connect the other side of the resistor to the anode of the LED.
We'll connect the LED's cathode to a ground point.
We'll connect data pin 2 to one side of the push button.
And the other side of the push button will be connected to ground.
And this completes our circuit.
Again remember you will need to build two of these circuits for this experiment.
Now the example that we're working with today is actually one that Arduino has posted to
their website.
And I have a link to that article in the article in the dronebotworkshop.com website that accompanies
this video.
Now you should know that they have one tiny error in their article.
And I'll point that out as we go along.
But otherwise it's a great article and you can follow along to repeat this experiment
yourself.
So what I did to start is I took my two Arduino Nano RP2040 boards and I registered them both
as devices by just going into the device tab and doing add device.
And here they are and I've named them device 1 and device 2 as they have named them in
the original article.
And then we created a couple of things.
One for each device.
There's remote light 1 and remote light 2.
Let's go into remote light 1 first.
Now on remote light 1 I've connected it to device 1.
I've obviously added my network credentials.
And then I've added a couple of variables.
One called switch 1 which is an integer and one called switch 2 which is a boolean.
Now there was no real reason to use a boolean for one and an integer for the other.
They're both interchangeable.
And I think they just did this in order to illustrate that you could use one or the other
variable.
These variables were set up pretty well in the standard fashion.
Now let's go into device number 2, into thing number 2, excuse me.
And I'll show you something with the variables.
They also have the same variables.
But the thing is these variables are actually synchronized.
And I'll show you how you do that.
If you were to add a variable right now, there is a link here that says sync with other things.
If you hit that link, you'll see all of the different variables that you have available
in all your different things.
And you can click on one of these variables and have your variable sync with it.
So the state of that variable and the other thing will match the state of the variable
on your thing.
And so that's what we've done is we've created a couple of synchronized variables within
our things.
Now I'm going to go back into remote light 1 and we'll go and we'll take a look at the
code there.
So let's go into the sketch over here and we'll open that in the full editor so we can
see it a bit better.
Okay, here we are in the full editor.
And I'll show you the modifications that had to be made to the sketch that was generated
for our thing.
Now the first modification, and this is true for both of the sketches, is we have integers
that represent both the LED and the push button pins.
And we've got them connected to the button on pin 2 and the LED on pin 7.
Then in setup we have to initialize those.
And you'll note that we use a pin mode command and that the button pin is initialized with
an input pull-up.
And that way we didn't need to wire a pull-up resistor to our push button.
The LED of course is just an output.
Then we'll go into the loop.
And in the loop what we do is create an integer called button state.
And now we're looking at device number 1.
So if the button state is low, which means the button has been pushed, what we do is
we invert the current value of this cloud variable that we have called switch 1.
So if switch 1 was for example at a 1, it would be a 0.
If it was at a 0, it would be a 1, etc, etc.
And then we put a small delay in there just as a debounce.
So that takes the status of our switch.
Now there are two things over here that were generated by the thing when we generated it.
Two functions on switch 1 change and on switch 2 change.
This is actually where they had the error on the original Arduino article.
They had put everything into the switch 1 change.
On device number 1 you need to put it on the switch 2 change because you're monitoring
switch number 2 on the other device.
So on switch 2, if switch 2 is high, then we'll put the LED high.
Else we'll put it low.
So that just drives the LED.
Now if we go back and close this right now.
And yes we'll say we can close this.
And if we go back to our things and go to remote light number 2,
we will see basically the identical sketch over here.
The only difference at the end over here.
First of all, we're looking at the button state on switch 2 and not the button state
on switch 1 like we did in the first one.
And this is on switch 1 change, not on switch 2 change like it was on the other one.
And of course we're dealing with switch 1 instead of switch 2.
But otherwise they're very, very similar sketches.
And so once you've loaded the sketches to the two devices, you're actually finished
the project because we don't have a dashboard for this.
These two things are going to just talk to one another.
So let's go and take a look at them doing that right now.
Okay, here's our demonstration.
I've got it set up with device 1 and device 2.
And you will notice that I'm powering both of my devices using these USB power banks.
So we are not connected to a computer in any fashion.
The only connection we're making is through the Wi-Fi connection
on the Arduino RP2040 Connect boards.
Now, I also want you to keep in mind that the only requirement is that both of these
devices are connected to a network that connects to the internet.
Now, in this case, of course, they're both connected to my homeland,
but they could be very well connected to two different networks
in two different parts of the world.
As long as they're both on the internet, this should function correctly.
So as you can see, it's basically a push button LED on each one.
I'll hit the push button on device 1 and device 2's LED toggles on.
And if I hit the push button again, I can turn it off.
And the same thing goes with the push button here.
I can do this and turn on the LED on device 1.
And I can toggle it off by pressing it again.
And so what we've done is we've illustrated that we can control
both of these two devices through the Arduino IoT cloud.
Now, remember, if you were to do anything such as delete
one of the things associated with one of these devices,
this would stop working because it is making use of the cloud
to pass the status information about the two different variables.
And so this is a great demo.
It's simplistic, but I think you can see a lot of applications for this.
Now, in the experiments that we've run so far,
we've been using Arduino compatible boards made by Arduino.
But we don't have to restrict ourselves to Arduino boards
because the Arduino IoT cloud also has support for a number of third party boards,
including the ESP8266 and the ESP32.
So for this experiment, we're going to create a thing using an ESP32.
For our hookup, we'll need an ESP32 module.
And pretty well any ESP32 module will do.
Just make certain you have a pinout diagram of your module
as it may not be identical to the one that I used.
We'll also need a potentiometer.
Any linear tape or pot of 5k or over will suffice.
I used a 10k pot.
We'll need an LED.
I used a yellow one this time, but you could use any color you like.
And a dropping resistor for that LED.
Any value from 150 to 330 ohms should work good.
I used a 220 ohm resistor.
We'll begin by connecting the GPIO2 output to one side of the resistor.
And then we'll connect the other side of the resistor to the anode of the LED.
We'll connect the LED's cathode to one of the ground terminals on the ESP32 module.
We'll connect the 3.3 volt output of the ESP32 to one side of the potentiometer.
We'll connect the GPIO pin 34 to the wiper of the pot.
And the other side of the pot will be connected to ground.
And this completes our wiring.
So now that we've wired up our ESP32, we'll need to create a thing for it.
So let's create a new thing.
And we'll give our thing a title.
And that's a pretty creative title over there.
And now let's set up variables.
And the LED, we're going to use a boolean again,
because we're just representing whether it's on or whether it's off.
And it's read and write, and we do it on chain.
So we'll add our variable.
There we go.
And we need a second variable for the potentiometer.
And for that, I think an integer would be a good choice.
And it needs to be read only.
We're not going to write to this.
And we'll set this up here.
And so now we've got our two variables.
Now we have to add our device, our ESP32.
And that's where things are a bit different than what we've seen before.
So we'll select device.
And we'll set up a new device.
And it's going to be a third-party device.
And it's an ESP32, which you'll notice has a little mark beside it
that says it's still in experimental phase.
And we're going to pick the WR Rover, because that's a type of ESP32 that I have.
And I'll continue that.
And we'll give it a name, which is good enough over here.
And now we come to a different part.
We need to create a secret key.
And it's been created for us.
We'll need to keep this key, because this key is going to be used in setting up our Wi-Fi.
I've just copied that to my clipboard.
Let's just make certain it's there.
Now, under normal circumstances, you probably shouldn't show the world your secret key.
But in this particular case, it's only for a demo
that I'm probably going to take down right after I film it.
So it's not a problem.
I've saved my secret key.
And I can continue.
And now I'm all set.
And I have a new device over here, the ESP32 board.
And now we go into our network setup.
And here's where things are a bit different.
You will notice in the network setup that we have a place for the secret key.
And so I can paste that secret key that I had before into here.
And then for the name and SSID, it's just as it is before.
And we'll save that.
And so now we've got our thing set up.
Let's go and take a look at our sketch that goes with the thing.
And then we'll build a dashboard to control our ESP32 device.
All right.
So now that we've constructed our thing, let's take a look at the code for the thing.
And I've opened it up in the full editor so you can see it better.
And I've also entered all the code so you don't have to spend time watching me type it in.
Now, of course, the thing has created a lot of the code for us.
And all we need to do is add a couple of extra entries in order to make everything work.
And the first entries are up here in the declarations.
It's just some integers we've defined for the two pins we're using.
The pin for the LED on GPIO2.
And the pin for the potentiometer on pin 34.
Then we go into the setup.
And in the setup, we need to set the LED up as an output.
Since we'll be using GPIO pin 34 as an analog input,
we don't need to do anything with it in this setup.
Then we go into the loop.
And that is where we use the GPIO pin.
We assign the value of the cloud variable ESP32 pot to an analog read of the potentiometer.
Now, remember that the ESP32 uses a 12-bit A2D converter,
as opposed to the 10-bit one that you're used to in the Arduino Uno.
So we're going to expect values of 0 to 4095 for ESP32 pot.
We add a small delay after that to avoid bombarding everything with data.
So every half a second, we'll take a reading over here.
And then we have the function that the thing has created for us for the LED change.
And this is pretty well identical to what we saw in the first experiment.
We take a look at the cloud Boolean ESP32 LED.
If it is true, then we send the pin high to turn the LED on.
Else, we send it off.
And so that's the code.
Now let's go and build ourselves a dashboard for our project.
So I'm going to go into the dashboards.
And I'm going to build a dashboard.
And we're going to go into edit mode, of course.
And we'll give our dashboard a title.
How's that?
And we're going to add a few things.
Now for the LED, a switch seems to be appropriate as we did before.
So let's just give it a name.
And we will link the variables.
So we'll click the variable.
And we'll look at the ESP32 thing.
And there's an ESP32 LED available for us.
And so we're going to link that variable to it.
And that is done.
Now let's add something for the potentiometer.
And there's a number of different things we can choose,
including some of the ones we've seen before.
But I'm going to use a value over here.
And for the value, I'm going to again change this to label it.
And then I'm going to link my variable.
And again, in the ESP32, I'm going to find an ESP32 pot variable.
And I'm going to link that variable to it right now.
And we've actually got a value in there.
And we are done right now.
And here's our dashboard.
So let's go and do a quick demo of this.
All right, you can see my demonstration hooked up on this breadboard, the potentiometer.
There's a yellow LED over here.
It's a little hard to see because the breadboard itself is kind of yellow.
And of course, the ESP32 module.
And so now if you look on our dashboard, we're getting a value for the pot.
Let's go and turn it all the way to one end.
And we've got a value of 4095, which is what one would expect
from a 12-bit analog to digital converter.
And we'll turn it down to the other end.
And the values are dropping.
And they drop all the way down to zero.
And so we can read our pot values on our dashboard quite easily.
And now we'll give the switch a try.
And as we expected, the yellow LED came on.
You'll also notice another LED came on over here on the module.
And that's because my ESP32 module has a built-in LED on GPIO2.
And a lot of ESP32 modules have that.
So as you can see, we can control an ESP32 device through the IoT Cloud using a dashboard.
All right. Well, this concludes our look at the Arduino IoT Cloud for today.
And I'll be doing more experiments with the Arduino IoT Cloud in the future.
And if you want to catch those experiments,
the best way to do that is to subscribe to the channel.
And you just need to hit that little subscribe button.
And when you do, also click on the bell notification.
And as long as you've got notifications enabled in your YouTube account,
you will get notified every time I make a new video.
Now, if you want to learn more about the IoT Cloud or grab some of the code that I used today,
there is, as always, an article that accompanies this video on the DroneBotWorkshop.com website.
And there's a link to that article right below the video.
When you're on the website, you can sign up for the newsletter if you haven't already.
And occasionally, you'll be getting some email from me.
It's not sales mail by any means.
It's just my way of letting you know what is going on here in the workshop.
And finally, if you want to discuss your experiences on the Arduino IoT Cloud,
the DroneBot Workshop forums is the place to go.
And there's a link to the forum below this video.
So if you haven't signed up yet, please go and do that.
It's free.
And there's a lot of great people on the forum who can help you out with your projects.
Now, of course, the Arduino IoT Cloud isn't the only game in town.
There are other things such as Blink that also perform similar functions
and may have some advantages over the cloud, especially in the number of devices they support.
And we'll be talking about Blink in a future video.
But until then, please take care of yourself.
Please stay safe.
And we'll see you again very soon here in the DroneBot Workshop.
Goodbye for now.
