Controlling Water Flow with a Solenoid and Arduino IoT Cloud Part 2
You can check out commercial for pay Design Services, electronic Design Services at forcetronics.com. All right lets get started. Okay, heres a slide. I showed in part one so Ill just go over it quickly, but heres our whole setup. We have our esp32 board, which is our brains behind it. It can connect to the Arduino iot Cloud which we can set up a dashboard to control the esp32 and with the right solenoid Drive circuit, which we set up in part one. We can control the solenoid, essentially turning water or gas, or oil line on or off so in part. Two were, of course, going to focus on setting up the iot cloud, the code that were going to load on our asp32 and creating the dashboard. This slide gives a quick bulleted list on the things we need to set up high level on the iot cloud. The first thing is setting up our device setting up our esp32, our network, Wi Fi information, and things like that for doing that. Instead of me trying to cover it in here and maybe having a part, three Im just going to recommend you to a great tutorial that Ill have a link for in the description of this video, and I recommend doing at least steps one. Two and three: you can actually do the whole tutorial its pretty simple and sort of give you an overview of all the steps Im going to start with creating our thing so Im going to assume that youve looked at tutorial first and you have a login, you Have your network information in there and you have your esp32 or equivalent Wi Fi Arduino board set up in iot cloud.
All right lets talk about how we can set up our things then well, look at our our code and then well. Look at setting up the dashboard and seeing everything in action. Okay, here I am in the Arduino iot Cloud youre, looking at it through a web browser Im logged in already and Im at the devices tab, and you can see. I already have my esp32 underscore test tied to my account. We can see its status is online. The board manager profile Im using is the sp32 dev module. You can use, of course, whatever matches the ASP 32 board youre using, but anyway you should have this part at least already done so lets look at setting up our things, so the things are variables you set up that is used by your dashboard in the cloud And also by your sketch to tie common variables together so thats what were going to set up first and you can set up different sections of things for different dashboards or different code sets. I named mine, solenoid control. So if you want to create one, you just hit this create button, and the first thing you need to do is name. It then add some variables and well look at the variables that I added you want to associate your device. This is your device that you already set up your esp32 remember. Mine was esp32 test. I could associate with this Im, not because I already have my things set up but thats.
How you do it and for the variables were going to use two different types of variables from the basic types were going to use a Boolean variable and then, from the time types were going to use a scheduler so thats. What were going to do Im actually going to just get out of this, because I already have mine set up, you can see it. It did save the Untitled Im going to delete that. But if you go through that process that I just showed you to create it and then add your variables, we want to add two variables: the bullion which Ill Ill. Show you how I set it up. I named mine, solenoid underscore State Boolean variable it auto generates a name which you can use. I didnt like it how it auto generated the same name as the thing name, so I just said solenoid no underscore State. You want it to be a read and write variable and then you know variable update policy on change and then save it: green, water, scheduler and heres. The types read and write on change, wire and well show you how to set up the scheduler too, but thats. Our two variables were going to use for this tutorial and once again the bullion is going to allow us to create a switch in our dashboard. So we can manually turn it on and off the solenoid. That is from anywhere right anywhere with an internet connection, and then the water scheduler allows us to send set up timed intervals, for when the solenoid turns on and then turns off again and thats, with no interference from us itll just do it automatically.
Then, of course, you have to associate your device with your things and my thing is solenoid underscore control. I only have one device right now, so the esp32 test, and now at this point we can go to the sketch. So a lot of the sketch is auto generated Im actually going to open this sketch in the full editor. Okay, here we are in the full editor heres, our sketch Im, not going to show you my secret, but this is going to contain some of the information you know, such as your Wi Fi network and secret key, and things like that. This things properties.h was automatically generated and you can see this has some of the information that we created in our things portion so on water schedule change. This is a function. Thats called when the scheduler fires on solenoid State change. This is a function that will be called when the solenoid State changes we have uh. You know some variables the variables we declared for our things are declared in this dot h file. This init properties is something were going to call in our sketch, but this is once again all auto generated by Arduino. Once you do your things setup now, heres the actual sketch some of this is auto generated. Some of this is what I wrote and Ill paste a copy of this uh, probably on my blog, if you want to see it in detail, but this includes the things properties.
h which we just looked at, that was auto generated. I defined my solenoid pin that Im using to control the solenoid from my esp32, its pin 21 on the esp32 I set up that PIN for output and I initially set it to low this. Initiate properties was Auto generated by things properties.h, and you know they automatically put it in the sketch heres another auto generated function, which is the Arduino Cloud which is going to handle connecting to the cloud and maintaining that connection heres some debug functions. I havent really studied exactly what these do, but these allow you to capture debug messages for errors. Then here we are in the main Loop and Arduino Cloud dot update is auto generated and thats just probably checking our connection with the cloud, maintaining it. The simple, if else statement is something I did add. So when I looked up the water schedule a variable and looked at some of the functions that are available for it, one of it is is active. So when I call the variable dot is active, this will return true if the water scheduler is set to be on or false, if its not and then I have an or statement, and then I have solenoid state, which is our bullion character. So if either of these are on the digital pin, writes the solenoid control pin to high, or else you know it puts it to low to turn the water off so thats our simple code setup for controlling the solenoid.
I also share these functions. These were auto generated. In fact, I used this function at first for testing, but this function is like an interrupt function. Thats called whenever theres a state change in the solenoid variable and well show you how to do that in the dashboard. So this function is sort of like an interrupt function and then a similar interrupt type function is on water. Scheduler change, which means the water scheduler turned on because of the timing or turned off this function would be called, and you know if I had a more complex sketch, I probably would be using these functions, but I just put the control simply in the loop for Now, okay lets go back and look at our dashboard. Okay, so I have a dashboard. I named it solenoid control and once again, if you dont have a dashboard, yet you can create one here and its very intuitive, what to name it and things like that and what to tie it to and the widgets to add which Ill show you in my Dashboard, so this switch, which I named water flow, is a widget, its an on off switch and the idea is the widgets. You can then tie to your thing variables, and so your sketch, you know, changes your communicates with your thing variables. So does your dashboard so by changing the switch Im changing that Boolean variable, which is tied to this widget and which my code will detect, that there is a change and then control the solenoid to do what its supposed to same thing with the scheduling.
So right now were in the dashboard View and you can also go into look at it from a smart device. Point of view were going to look at mine from a computer point of view. But if we click here we can go to edit and we can add widgets so that first one I have is a switch, so you would just click on that to add it, and then, when were in this edit View, we can edit our widgets. So I named mine water flow, so this is where you can name it, and then you have to link it to a variable, so you can see and things solenoid control group. I link it to solenoid state, which is our bullion character, so thats. How simple it is just to link it to our code. Okay, Im done here, and then we have the scheduler, which I named scheduler. I have it linked the same way except to our water scheduler variable, so real easy to set up the dashboard. Its really just clicking and some easy steps to link it to the thing variable you want it to be to either control or read from okay and our scheduler is real easy to edit. We can just click on it and, once again were still in the edit mode theres a couple things we can do. First, we can, you know, set the start date. We can set the start time. So this is the time its going to turn on mine, says America, slash Denver because Im on Mountain Time, we could select the duration, for example purposes.
I just set it for seconds. How often do we want it to repeat? I have it repeating every day and then end of reoccurrence. How often do we want to just keep cycling? I just have it set for never, then we go back to the dashboard View and now lets look at our dashboard in action. Okay, so this is going to be a similar setup that we saw in part one except were going to control it from our dashboard instead of locally by a switch. So here we are at my sync. I have a hose thats connected to the water line outside thats turned on right now the solenoid is off, so no water is Flowing from the nozzle. We already went over the hardware setup in part, one so Im not going to go over that again and heres. Our Cloud setup, so this is just what I just showed you and all Im going to do is turn off to on. You should have heard that click, and now we can see on our power supply that we now have current flowing, signifying that the solenoid is open and water can flow through the valve so Ill show that by holding that down spraying out some water, then Ill go Back and Ill turn it off, we can see no current flowing and then, when I squeeze the nozzle theres still a little water left in the hose, but we can see its cut off by the solenoid now Im going to go to the scheduler.
So first thing Im going to do is kind of show the time on my computer and then Im going to go to the scheduler to set up a time thats shortly in the future. There we go. We have our time typed in 245 and 30 seconds. So Im doing it down to the the second type resolution. Duration is 10 seconds and Ill fast forward here. To get us up to the time we want to be at were getting closer to the time for it to turn on water is not on, and now it should be on so well, look at the power supply, we see our current is flowing and we can Turn it on so Ill, just let it run until the 10 seconds is up. Water is still on now its off, so our 10 seconds is up and no more water is flowing. When I pull the trigger so thats how easy it is to set up. You know a cloud dashboard to control water flow or to really control any type of actuator or to read sensor data and use that to control an actuator. So if you know already know how to use the Arduino IDE or how to write Arduino code, setting up this iot dashboard is really easy.