ESP32 IoT Tutorial 50 - Control RGB LED from anywhere in the world | SunFounder's ESP32 kit

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hi! welcome to tutorial from Robojax  in this video we are going to control   the color of an RGB LED using esp32 over  Wi-Fi from a remote location anywhere in   the world using MQTT protocol and IO service  from Ada fruit this is a screen that I can   select the color change it to any color  that I want as you can see as as I move   the slider the color is changing mqtt this  is a service from Ada fruit and this is a   Serial monitor where I can move the slider the  color changes also we can see it on the serial monitor or we can use a color pecker to just  select any color we want let's get started with this esp3 2 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   or Theo 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 the 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 in the previous lessons we learned how  to uh use and control the color of an RGB L with   esp32 this is esp32 with the extension Boost from  SunFounder we are going to use it this time over   Wi-Fi and we are going to use the MQTT protocol  from uh Ada fruit and we have three sliders we   can select r g or v r and based on the color that  we select we can change the color and also we are   going to use the Color Picker and in mqtt protocol  is so we are going to subscribe to a topic on the   mqtt and this will read the subscription the this  portion is called a publisher we are publishing   values to MQTT broker and this is a subscriber  to that topic where we can get it so this pulls   the subscription value and whatever is a value it  sets the RGB LED color this lesson is very useful   we are going to learn how to use a slider uh with  Adafruit IO and then we are going to read how we   are going to learn how to read that particular  feed uh as a subscriber with esp32 and pull it   individually each slider has separate value and we  can set the colors for this but also I created the   Color Picker where we can send where this from the  Color Picker where we can send the hex value of R   g&b and then we should be able to pull it extract  and separate all the three colors and set the   color so this is a good learning project in terms  of programming and in terms of extracting MQTT value let's get introduced to MQTT MQTT stands for  massage 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 elements 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 thing 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 cued 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 pressure and if we   use a microcontroller such as Arduino or esp32  this in this case we use it as a publisher and   we have MQTT Broker in this case we are using  add 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 temperature   sensor is Red by ESP 32 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 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 Ada 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 mqtt MQTT 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 to 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 now let's click on Ada fruit 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 our account log in now once you logged in you can click  on dashboard here click on dashboard and   you'll see all your dashboards I'm  creating a new one so you can see it RGB less than 50 I just named it less than 50 so this has has been now created  now click on it so you can see this   blank screen don't worry and here from  this gear click and create new block   now we could do uh blog and also we can  we could do group I will prefer group   let's go to the feed and let's in here we  can create new group for RGB color for the feed so this is just a feed group here  the feed have been created from here   we create the feeds so they are being  grouped and they are being named like   this dot green so click on this  plus sign and then let's name it green you see as soon as I created green it added  RGB LED less than feed. green so it added this RGB   and feds and then we can create uh green red  it should have been RGB I forgot the order so blue so this these are the key that we are going  to use in our um code now go to dashboard and   click on the uh appropriate dashboard in this case  ours click here and create new block and then we   need to uh create three sliders for red green and  blue click and from here we can see the feeds so   create red and then go next from here we can and  just type here red and the zero the maximum is 255   because we know the RGB color is 8 Bits And then  create red is been created create block slider   scroll down red green this type neck this time go  to green and type green this is just a name for   and the value minimum is zero maximum is 255 if  you want to limit the value so it doesn't go below   certain value also set your minimum to something  else so that's green and then create a new block blue the minimum is zero maximum  is 255 so we have create these   three go to the feed and then let's create Color Picker so this has done now let's go to  dashboard listen and here create new block   and this time this is Color Picker select it  and then from this screen select this Color Picker and let's name it Color Picker and done so we  have created all these this is done if you need   to know the name of this click on feeds and here  you can see the names blue green red and you can   change them how you can change the key let's  say you not happy with the key click on blue   and you will see this screen come here feed  info and here change that blue to whatever   you want and not the name but the key change it  here but change this uh feed click here this is   the feed go to here go to the and this is the  feed name if you're not happy with the uh uh   initial feed name change it from here so this  is the prefix to the feed and if I show you here so RGB LED list and feed  this after everything is done   you can click here and get the key  I'm not worried that if you see this   key I can simply click and and generate  a new key and all other projects will be   not uh working so this is a username and  this is the key that you use within your code when you use esp32 with Sounders esp32  camera extension module like this it comes   also with a battery on the package and it  comes a built-in charger so you can connect   it and charge it and this disconnect and later  on you can use it with a lot of power so you   can power up your application very easily and  here is the wiring I've picked this RGB LED and   as you can see the second pin is uh the longest  one so if you turn it around now it will be the   second from this side so make sure it's the  second from the left so the first pin is red   the long pin is the common and then green  and blue I'm just inserting it like this and from this point one pin is I've connected  220 Ohm resistor to this point and then the   the common does not have anything then then  the green one resistor 220 ohm and then the   blue 220 ohm to this point where from the  red it is connected the red is connected   to pin 27 this is 26 and then the blue is 25  the one pin is not connected it is connected   to the ground and ground is on this side  not in here on that side ground is in here   and these are the three wires 27 6 25 27  26 and 25 so the red is connected to 26   you can connect it in here if you're  using RGB board like this connect it just now let me explain our code this is the  same as before for the RGB this portion we are   defining a red pin green pin blue pin 27 26 25  and then these are the channels for p password   modulation Channel 0 1 and two where are setting  the operation frequency we have already spoken   about this we have defined these three variables  that are holding the color R color R color G color   B so these are uh Global variables outside any  function about this so this is MQTT at a fruit   MQTT and MQTT Clan if if you haven't watched the  previous lesson let's let me show you to install   it uh let's copy that text go to library right  click and paste it here and then Adafruit MQTT   library you will see this button as install click  to install it so this is the only library that   you need then you will need to enter your Wi-Fi  SSID and then your Wi-Fi password Wi-Fi is case   sensitive if you enter for example instead  of book if you enter lower case b it will   not work you will get error so respect that and  this is the path for the io uh add the fruit IO   server this is a port which is a secure port  and here we are getting our username and the   key from Adafruit which I'm going to show  you so you will need to enter it as is this portion and here we are passing all the  values that we set user password port and   path all of them we're creating from this MQTT  client and object call called MQTT and we pass   these values with the key and username so the  object is created for the connection then we   need to use other fruit uh root certificate  of authority this is for security the secure   certificate uh this certificate might  expire by the time that you uh use the   code so let's replace it I'm going to show  you click on file examples and scroll down click on file examples and scroll down  until you see a series of EX example   from Custom libraries a series of other  fruit because we installed the library   this is now shown if you don't see it  restart Arduino IDE close it and open   it again and make sure that the library is  installed the way I've shown it after that   you should be able to see it go to Adafruit  mqtt library and select at a fruit secure esp32 open it and then go around line  50 this is a certificate select the   certificate go down and up to the end  line up to here right click copy I'm   closing it I don't need it and then select  the same certificate which is around line   64 or maybe up and down doesn't matter  so you know this is a certificate select it and then right click paste so the certificate  must be updated if you are getting connectivity   error and then we are created we have created  these three objects for subscription and this   is MQTT subscribe I call the subscription  object color R color G color B these are   each color separate that we receive from these  sliders RG and B and then for the Color Picker   because it sends this three color all in one I  called it color RGB so remember these are our   objects and we are subscribing using MQTT uh  uh uh object and username this will be robojax   for/ fe/ RGB color. red so this is my key on Ada  fruit and this is RGB color. green. BL so these   are individual color and the last one I called it  Color Picker within my user name which is robojax   this should be unique uh and if you have multiple  of this just put some kind of number here and on   other fruit so you can have multiple values for  different applications because I did not have   more uh colors so more projects related to color  color this is fine so these are very important   if they don't uh match you will not be able to  pull up the subscription value and then this as   this is for RGB we are attaching uh LED setup  we are setting the Channel with frequency and   resolution for the RGB are the three channels  which we defined at the top of the code after   that we are attaching pin channel to the pin  this is a 26 for example to channel 1 25 and   27 and so forth for the three channels this is  initializing the serial monitor these are all   text this is initializing the Wi-Fi delay of two  seconds this y Loop checks if the connection is   not equal through every 500 millisecond or half  a second it prints a DOT until the connection   contion happens otherwise it will just print once  the connection happens the loop will exit it will   print a new line this will print a text and  this IP address that you have connected in your   local network and then this is initializing the  certificate for the client that we are connecting   this is now these two lines are for red color  so we are getting the subscription object set   call back and this is a function that I have  written color R call back and then then after   that we we pass this object to our subscribe MQTT  subscription as a reference so color R is here so   remember this is for R and this is G and then I  put here G and G these two are for green these   two are for blue and these two are for RGB and  these are functions now let's check inside the   loop this runs the MQTT connection and this is  just printing r r color these three variables   are at the top just printing R and then R so if  I open the serial monitor you will see the this is so this is coming from this this  line r g and B so we print R and then   the R value and then G and then the  G value and B and then the v b value then we run MQTT process packet so this re a  subscription every 500 millisecond if you do   need it faster slow it down by increasing this  number like 2,000 3,000 which is 2,000 means 2   seconds and 3,000 means 3 seconds and this  is setting the color job of this one is to   go and set the color based on the value  so now this is another function fun so AR color this color R call back and then color G  call back and color B they are all the same I'm   going to explain one of them we are receiving  the message and this is just printing the value   that we received from the internet and then  we convert this message to string and this   line is getting an integer and we check if  the color R is greater than 20 that 255 or   less than zero which is not allowed this line is  just keeping it within the bound if it is out of   the bound it will set it to zero the same  way for the green and the same way for the blue so that's done then for RGB it gets  a little complicated the color is printed   but here we get the color as a string so we  are converting it to string now this is new   color also we check if the length is equal  7 this was not necessary I just put it for precaution and first we get the new color and  extract the this portion this is an example if   the color was B2 A4 uh 82 then we extract only  B2 this portion it says start from one and go   to three so this is 0 1 2 but so this goes from  one and gets the third one so B2 is extracted   this goes from three which is A4 will be extracted  so this will extract 8 D and every time they are   extracted it they're being stored in a variable  called the r color so this was new color this is   called the r color and and the g color and the B  color and then because this is string we need to   convert it to the decimal and there hex value  so string to hex is a function that gets this   value and converts it to C string and it goes to  this function and this will give us a number uh   like between 0 to 255 this hex string to hex  is a function which I've taken from this URL   very simple it comes and it's being converted to  integer and returns Now set color is from RGB LED   project where we set the channel and then the  color this is global global variable and those   also this is so this will set for red Channel  green Channel and blue Channel and we see the   color the connection MQTT is a built-in function  which I used from Ada fruit this is just trying   to connect and it initially sets it to three it  tries every time tries a connection waits for   5 second if it doesn't happen it decrements at  3 2 1 and zero if it is zero then it will stay   forever and after three tries this will not try  any longer and if the connection is established   it will print this otherwise it will be stuck  uh in this while loop in here so that's a full code now let's see how we can select the esp32  board we can click here under the select board   and type here esp32 de 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 esp32 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 other is USB serial device and   here now it's connected if I disconnect this one  of them disappear the one that disappeared 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 com it now   we have successfully selected the board and the  port and this is very important it must be done first and here is the demonstration I've opened  the serial monitor where the serial monitor also   prints the color r g andb so we can see it I've  also brought this screen from Ada fruit where we   can move the sliders and also see the color here  the default color as it seems is 0 0 0 so it's   all off but as soon as I make any changes the  color is reflecting you see R is z g is z b is   173 and we are reading it the latency depends  on your internet it's very fast and if your   internet connection is fast you will get very low  latency and then let's change the G as green as   zero and let's change the RG and B all of them  255 255 so we are getting almost let's set it   to Green as you can see green is 255 and that R  and G are R and B are zero red and now red is 25   five green and blue are zero so it's perfectly  responding and also you can see it on the serial Monitor and we can do also from Color Picker that's the color if you don't see the  color properly it's due to two reason one is   my camera second is the lighting and and  also the closeness of the camera uh causes   to so you can see the color together but  the reflection shows very close color to this so this color is the same similar to  this color and also you can see the values perfect for RGB LED we have three LEDs in one which  I'm going to explain it one by one and we   are going to connect them and to understand  RGB LED we first uh let's have a look at the   Led Led is light emiting diode it has two pins  one pin is called anode which is positive the   positive is always long the anode pin and and  then the other pin is called cathode and it is short here is the symbol for the Same Led we  have a triangle with a line and uh most of the   time they put one or two Arrow here to show  it is light the anode is on the back side of   this uh triangle and the side that has line it's  called cathode and to turn the LED on we can not   connected directly to the power we have to put a  resistor to reduce the amount of voltage that is   coming so we are using it as a voltage divider  right like this the resistor can be connected   between anode and positive like this and the  cad is going to the negative and this is the   actual shape so this is 220 Ohm resistor you will  connect it this pin must be the anode pin the long   pin so it has been cut and it is connected so the  LED will turn on we can have the resistor on the   other side so the anode is connected directly  to the positive and the resistor can be on the   cathode and the effect is the same there is  no difference and here is the RGB LED RGB LED   however has three LEDs in one so red green blue  RGB they're connected like that if the anodes   are all connected together it's called common  anode and we have one pin for the positive and   then we have uh separate pin for the cathode so  this is called common anode or we can have it as   like this common cathode which the common will  be connected to the negative and the anode are   separate so we can control each RGB LED is sold  like this with four pins or as a surface mounted   device like this sometimes you can see it as  a module which all the resistors are already   uh mounted so you have all the four pins and you  will just connect it so we have common and then   red green blue for the common anode the resistors  are connected at the cathode because the because   the anode is common so we are connecting each  resistor for each Ed in this package or we can   if it is common cathode then the cathode will  be connected to the ground and the resistors   will be connected to the anode of each resistor  to divide the voltage and allow two volts for   the LED here's the actual uh RGB and here is the  picture with a proper explanation from SunFounder   so if we divide it in two uh pin of the right  two pin on the left the two pin on the left the   second one from the left is common which is  the longest one and here we can see this is   the longer pin and that is our common and then  from the left we have red green blue r g and B   and here this is our R and then G and then B so  you cannot make mistake if you put it like this   then the longest on the right side and that's  incorrect so we are going to use it like this now I'm on the Bing search engine just type  RGB Color Picker with slider and the first   one that comes click on it to understand  the color of RGB we have to first know   that all colors can be created or any  color can be created from combination   of r g and B this area shows the actual  color that we have created and because   there is no color the red is zero  the value is red is zero green is   zero and blue is zero so we see black uh  colors now if I move this you see some red here and this is maximum value why 255 because  it's 2 to the power8 8 so we have 8 bit color   resolution which means from here to here we  have 255 steps to add or adjust the color of   course there are higher bit rates that can  be done for our purpose we are using 8 Bits   so this is red now if I slide it back the value  you just read it here pay attention and this is   green Max maimum green light green and then  dark and then this is blue so we got these   three colors now the value could be shown also  in HEX pay attention this is the hex value but   those are RGB but we are paying attention  to this now let's have red and let's put   green so this is red this is green and if you  combine it you get yellow color and if if we   have red and if you put blue then we get pink  color so this is the combination of red and blue now I'm bringing that down this is  green now green is the maximum now if I   bring red you know it it was red and yellow but  blue and green will create the Sapia uh this color and if I bring all the colors it will  be white this is interesting if you combine   the right proportion of r g and B red green and  blue you will create a white color and now if we   pay attention this is 255 255 255 now this color  is red is zero and then we have green and blue   255 255 that's why you are getting and if you  want pink so red is maximum 255 and then 255   is a blue if you put them at the middle you  will create um a darker pink because we are   moving these two together 82 82 or 83 so they  will be the same now the funest starts when   you play with these values and you create like  different color orange you see somewhere here   if we have full red and blue is zero green  is at the middle that's uh you see we are   creating orange if I bring that here and this we  have different kind of blue so we with all this   we can create different color that we want and  we are going to practically test it and see it here
Info
Channel: Robojax
Views: 795
Rating: undefined out of 5
Keywords: robojax, sun, founder, sunfounder, course, tutorial, kit, learn, learning, step by step, esp32, arduino, iot, wifi, RGB, LED, MQTT, iOT, control, change, set, color, colour, diode, Adafruit, IO, cloud
Id: -9q1GfGsnr0
Channel Id: undefined
Length: 39min 56sec (2396 seconds)
Published: Mon Feb 19 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.