ESP32 Tutorial 30 - Control Servo Over the web using MQTT using Adafruit IO Service

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we are going to use  esp32 to control the position of a   Servo using MQTT over the web using add  a fruit IO service I'm positioning it at 33° 180 zero or any angle uh that you want esp32 starter kit from SunFounder this is  the best esp32 learning kit from SunFounder it   has this esp32 microcontroller which has built in  Wi-Fi and Bluetooth this board can do everything   Arduino Uno can do or many other Arduinos can do  plus extra more features because we have Wi-Fi and   Bluetooth the possibilities are endless you can  get connected to the cloud or do the control or   read information or values via your mobile device  or your desktop or over the cloud from a far location it comes with a camera extension  kit where you can stream the video over   Wi-Fi either through the cloud or to your  network and also it comes with micro SD   card where you can save images on the micro  SD card or you can write from the device any   information log that information on the device  and read it back you can power the board using   this included 18650 lithium battery and it has  built-in charger where you can connect micro USB   and charge the battery the kit comes with 320  pieces of component that you can learn tons of projects there are many instances that you need  to control the position of a servo motor may be   attached to a an arm a robotic application  or whatever it is and you want to be able   to control it remotely over the internet  and doesn't matter the barrier we will be   connected via Wi-Fi and this device is not  connected to anything it's powered up with a   battery uh this is the uh SunFounder extension  board with a battery with built-in charger so   you're just putting it alone and you just need  to charge the battery it has a via this can be   charged via micro USB so you can uh send  uh to any angle that you want we have in   previous lesson we learned how to control  the position of a Servo with esp32 but in   this video we are going to use other fruit  cloud service to control the position of this let's get introduced to MQTT mqtt stands for  message queuing Telemetry transport uh why we need   it because it's lightweight and efficient MQTT is  bidirectional MQTT can be scaled to millions of   things and it uses reliable messaging delivery  system it is secure and it is persistent over   unreliable Network so first we have to understand  the element of MQTT we always need to have a   broker and we have MQTT subscriber we have MQTT  publisher and the most important thing in the   broker is the topic uh for which the messaging  is being published or subscribed now this is   Internet of Things From mt.org and we have client  broker and another client which is subscriber   in this case we have a temperature sensor sends  information to the broker and the temperature is   listed or queued on the cloud and our subscriber  can read or fish a particular Topic in this case   temperature the topic is temperature and we read  the temperature and if we use a microcontroller   such as Arduino or esp32 this uh in this case we  use it as a publisher and we have mq broker in   this case we are using Ada fruit IO and then we  have MQTT client in this case a subscriber a lap   uh mobile device or a computer so the temperature  from a temperature sensor is read by esp32 and   because this acts as a publisher we are publishing  it as a topic of temperature to the broker and   it is being list and queued there it just Waits  and until the request comes or uh the device is   connected so we can read the temperature on the  screen that's was how it is used as a publisher   now let's see how we can use it as a subscriber  we have add a fruit uh MQTT broker service and   then esp32 is used now here as a subscriber and we  have subscribed to a topic called LED and then uh   mut qtt client is publisher is publishing on off  uh to the MQTT broker so when they touch or click   on the icon on or off the information is cued here  for the LED topic remember this is a topic and we   can have millions of topic so one topic is called  LED and because we have subscribed to that topic   of led the information comes either that topic  says high or low or on or off depending on that   we interpret and read the information and we turn  on and off the LED based on the uh action that is   taking on the screen case we have MQTT Broker at  the fruit and we have MQTT client subscriber esp32   and we have MQTT client publisher this is now  our mobile device or whatever it is that you're   connecting so we we are using this slider to set  the angle it goes to the specific Servo angle feed   on MQTT and uh esp32 reads it and it will move  the servo to that particular angle that we have subscribed now let's click on Adafruit IO we're  going to create an account click here on the sign in let's create an account here by create sign up so once you open an account log in now once you logged in you can click  on dashboard here after logging in to   your dashboard of creating an account  click on dashboard and let's create a new dashboard I'm naming it Servo angle one  you can put some description here the   dashboard have been created click  and in here let's create a block   create new block and from here  select slider and here name is it whatever name you want I I call it  Servo angle one because I might try   different uh elements create so this  has been now created select next and here Servo minimum is zero maximum if your Sero  maximum is 100 than 80 step size if you set it   to 10 it will go by the increment of 10 let's set  it to one slider label I'm going to the web and getting the gree symbol so this is the symbol I copied it you can put degree 400 degree and then it has been  created now again click here edit layout   and then you can increase the size if you  wish save layout now let's go to feed and   click on Servo angle this name this name must  match if you want to change it click here on   this screen you will see the values  and in here feed info click and you   can change the name the key here the name  doesn't matter Servo angle but this key is   the one that we are using in our code and or  doin know code so save dashboard now this is ready now let me do the wiring for the wiring  we need to have M to male this is female and   we cannot attach it here get me to me wires uh  so I got this me to me from you can insert them   like this the wire will be ready but because  this will not hold to make sure to tighten it   or put a tape I have this Dupont wire I'm going  to connect it I selected the same color so I'm   going to insert it to the connector and I have  the same color I'm not going to make mistake   so the wiring is very simple uh look here ground  I mentioned ground is ground is connected to the   ground from from the last second and then  5 Vol is connected to 5 volts this red the   last pin is 5 Vols when you hold a USB this  way and then the signal pin is connected to 25 like that and the wiring is completed I'm opening Arduino IDE let's open our project by clicking  on file open on the left side click on   downloads then esp32 starter kit main on the  right side double click open it double click   on C double click on codes and here is  a code I've started the code from I've   started The Code by using the iot 6 Ada fruit  I've modified it and also I've used the servo   swing this 4.3 I've combined them I've written  the code this line is for esp32 Servo you need   to install the library I'm just copying it  go to library paste here and you will see   esp32 Sero by Kevin Harington and click  install mine shows it is outdated so we   could just update it here and install it you  will see install and the rest we need to have the MQTT we need to install MQTT I'm selecting  it and copying it going in here and at the fruit   MQTT you will see the install here install it and  that is done other than that you need to install   the driver for the esp32 which I've explained it  on the first or second lesson of this course so   from this asp32 Servo we are defining an object  called my Servo pin 25 is defined for Servo so it   can be connected we are selecting a default angle  when we turn on the uh esp32 it goes to this angle   this variable is holding the initial angle and  later on it can get updated this is the minimum   and maximum pulse width you can play with this  if your Servo does not respond but generally with   the mini Servo this value Works to set them this  include Wi-Fi is for the Wi-Fi this is for secure   client this is for MQTT from Ada fruit and this  is for MQTT client enter here your SS ID for the   your router and remember if your SSID is book and  if you type with lowercase book it will not work   respect the case and then enter the password this  line is for the io service and this is a port we   are not changing them here we're entering our user  ID from Adafruit and here we're using the key that   which I just shown you this is for Wi-Fi client  and then we are creating from Ada fruit MQTT   client we are creating an object we are passing  the server the server Port username and key all   the values that we have here to initialize that  uh this uh object and then we need the certificate   for other fruit this certificate that by the time  that you install it might be outdated so I will   suggest to update it by opening file examples and  once you install the Ada fruit library mq2 library   MQTT library go to Adafruit you see multiple  Ada fruit and go to Adafruit secure esp32 open it and find the certificate here from line 50 in  here maybe it will change depending on the timing   so select it and then right click copy it come  to this code here for the servo and then paste it   right click here and paste so this will be updated  and here we are creating a a Servo for subscribe   for our subscription this is our object which we  later on will refer to and we are passing this is   this is the feed that we have just created on Ada  fruit this name must match otherwise it will not   work so this this AI username will be Robojax  here Robojax Feats and then Servo angle one or   whatever name that you set so this must match  otherwise it will not work and inside the setup   using my Servo we are attaching the servo pin to  the minimum maximum so the this is attaching this   is uh the period Hertz for the servo these are in  this is initializing the serial monitor printing   the text this is printing the SSID for the Wi-Fi  and this is initializing the Wi-Fi and this line   is checking if the Wi-Fi is not connected every  500 millisecond it will print a DOT and finally   it will pass if the connection is successful  it will print this will print a new line and   this will print the IP address and then this is  for connecting the to add a fruit uh with secure   certificate and this is setting Servo set call  back and it goes to this function which is at   the bottom of the code and then we are subscribing  using uh my uh MQTT sub Sero that we just defined   this is being passed here so we are subscribing  now inside the loop we use mt2 connection to   connect which is at the bottom of the code and  then this is just for test it's printing the   servo angle this variable is at the top of the  code and then every 500 millisecond it checks   so there is a delay of 500 millisecond or uh 0.5  second if it's 500 millisecond and this has been   taken from uh this was a code that I use for  lesson 17 and this line Servo right micro this   will write the value to the servo and Servo move  moves to this angle in terms of uh micro period   and there is 15 millisecond delay but this  does not have any effect because we have 500   millisecond wait time anyways now Servo call back  is here and we are getting the message the this   is printing the buffer message the whole message  that comes and this is getting the actual message   and we set the message as a string stored in a  variable because it's passed as a string and and   then using this method we are converting it to  integer of Servo angle sometimes you might get   the value greater than 180 or less than zero so  this is just preventing that and setting it to   zero my my mqt connect is a function that gets  connected we don't really have it works there   is so this y Loop checks and it tries every 5  Second until it gets connected Ed otherwise it   will be stuck uh in here it will it will it will  try three times and every time it decrements 3 2   1 and if it is zero it stays here and it will not  get connected otherwise after connection it shows   connected so that's the whole code and this  was a function that I wrote and modified for   this Purpose with the additional of the codes that  that you see on the uh upper section of the code now let's see how we can select the esp32 board  we can click here under the select board and type   here esp32 D as soon as you type Dev you will see  Dev board you can select it and click okay so the   board have been selected now we have to select  the port the other way to select the board is   Click On Tools board esp3 2 and select the esp32  dev module now we have to select the port if I   click here it shows two port and I don't know  which one belongs to my device sometimes you   will see you will not see the number properly  so the best way to be sure the right click on   the start menu go to the device manager and you  will see here the ports if I click on this Arrow   it will show me the ports one is USB serial ch340  one the the other is USB serial device and here   now it's connected if I disconnect this one of  them disappear the one that disappears is my board so six stays and it disappeared if I connect it so it is my comport ch340 now it is my  comport and I can select it or I can click   on tool port and here you will see it you can  select whichever you want ours is com8 now we   have successfully selected the board and the  port and this is very important it must be done first here is a demonstration the esp32  is not connected to anything except the   battery and it is now connected via Wi-Fi  this angle is not reflecting because we   have just booted and it goes by default  to 90° I just clicked it so it goes to 90° now let me just disconnect this one wire  just this yellow and let's move it ooh it's   holding now I can move it so if I press this  it goes to 90° because the default is 90 and   I set the 90 in here by moving the this arm  and let's go to zero if this connected 0 180   is on the other side so now because the value  might have been out of range so it sets at 290 160 after half a second it goes to the angle  it's responding very well with a a little   delay but generally it works fine unless  there is some delay in your Wi-Fi or in the network and now I have connected external 5 volts  the positive and negative from my power supply   is now connected the positive to the positive of  the servo the negative is connected to the common   USB is also connected and as you can see the  angle is shown if I select any angle it doesn't   turn off or doesn't affect the USB connection or  disconnection as you can see it's responding very well 180 so you need to connect external power   even the battery was not able to  supply the proper current for the servo
Info
Channel: Robojax
Views: 942
Rating: undefined out of 5
Keywords: robojax, sun, founder, sunfounder, course, tutorial, kit, learn, learning, step by step, esp32, arduino, iot, wifi, mqtt, broker, susbscriber, servo, angle, position, adafruit, IO
Id: T4DhWNg2Rb8
Channel Id: undefined
Length: 23min 29sec (1409 seconds)
Published: Sun Feb 18 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.