Sending Data From An Arduino To A NodeMCU | ArduinoJSON 5.13.2 | Serial Communication | DHT11

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone so in this video we're going to have  a look at how to transfer data from an arduino   to a node mcu via the serial port or the  serial line now this sort of approach   would be useful if you had multiple sensors  connected to your arduino and you wanted to   transfer that data to the node mcu and thereby  push the data onto the cloud or push it online   so without waiting any longer let's get  right into it now if you guys like this   video don't forget to give me a thumbs up  by clicking the like button below and if   you haven't done so already please subscribe  i'm Chams and welcome to that's engineering right so there are multiple methods that can be  used to transfer data from one device to another   for example i2c or serial communication which in my opinion is one of the  easiest and most convenient methods   and it is what we will be looking at in this video   so for demonstration purposes i will be  using a dht11 temperature and humidity sensor now this is solely for the purpose of collecting  some sensor data so that it can be transferred   from the arduino to the node mcu so let's have  a look at the flow of data so we would start by   collecting our sensor readings via our arduino  and placing that data on the serial port   this will then be sent to the node mcu and  from the node mcu then data can be pushed   online or to the cloud via the esp12 wi-fi module  housed inside the nodemcu this can also work in   reverse where you can pull data from the cloud  to the node mcu and then place the data on the   serial port and send it back to the arduino  for some sort of activation of any device   now this sort of concept or methodology would be  most useful if you were implementing an iot or a   cloud connect based system or following something  regarding industry 4.0 so before we get on to the   physical connections and looking at the code let's  have a very quick look at the overall connections   between our three devices so first of all all  three devices have to have a common ground they   have to be grounded to each other for the dht11  one pin which is the vcc will be connected to   plus five volts of the arduino and the data pin or  the out pin will be connected to any data pin on   the arduino i usually use pin number four and you  will see this reflected when discussing the code   now the important part is the connection  between the arduino and the node mcu   so because this is a serial connection we would  have two data connections one would be receiving   rx and one would be for transmitting tx  most people would use the rx and t exports   on the arduino and node mcu respectively but  i think it's easier to just use any two pwm   ports on the arduino and the respective node mcu  ports so i used pin 5 on the arduino connected to   pin d5 on the node mcu followed by pin 6 on  the arduino connected to pin d6 on the node mcu   now these pins have to be assigned as  receiving rx and transmitting tx pins   how you want to assign them is up  to you but what you should remember   is that they should be opposite for the node mcu  and arduino what i mean by this is that if pin 5   is assigned as rx on the arduino then it should  be assigned as tx on the node mcu and vice versa this is because when the node mcu is transmitting  the arduino has to receive and when that's   transmitting the node mcu has to receive it  doesn't really matter when you're making the   physical connections but you have to keep this  in mind when writing the code so now let's have a   look at the physical connections the components  you will need are a bunch of connecting wires the dht11 temperature and humidity sensor an arduino uno and the node mcu right so the node mcu is not breadboard  friendly what that means is you cannot   directly fix it onto a single breadboard  instead you can buy a holder which would be   the more practical or professional thing to do  i however don't have that so what i've done is   i've connected half of it to one breadboard  and the other half to another breadboard right so let's go ahead and wire up the  connections now we'll take positive five   volts from the arduino and connect it to a  common pin and i'm going to round the arduino   to the next pin so that's going to be the  common ground i've already grounded the node mcu   to the common ground right so for  the software serial connection   i will be using pins 5 and six so pin number five  from the arduino will go to pin d5 on the node mcu and pin six on the arduino will  go to pin d6 on the nodemcu right the final thing is  to wire up the dht11 sensor   so the cornermost pin will go to ground the  common ground the vcc will go to plus five volts   and the data pin will go to pin number four  on the arduino you're free to use whatever   data pin you like i'll be using pin four  right so those are the physical connections right so now let's have a look at the code two  programs are written one for the arduino and   one for the node mcu as they are both separate  programming boards now i will leave a link in the   description down below where you can access these  codes so do have a look at that so i started off   by including the dht library now this is for the  arduino site code i then included the library for   software serial to enable serial communication  between the two devices i also included the   arduino json library this was to create a json  object which would store the sensor readings being   transferred from one device to the other so to  obtain this library go to tools manage libraries and search for json right and you would obtain this sort of library  arduino json by benue blanchon i hope i'm   pronouncing his name correctly so go ahead and  install this library i've already installed it right i then enabled the serial connection by  specifying the rx and tx ports on the arduino side   so pin 5 for rx pin 6 for tx this bit  of code initializes the dht 11 sensor   it was connected to pin 4 and two variables  were allocated for temperature and humidity under the loop i created a static json buffer  so you can create a static or a dynamic buffer   if you create a static buffer it has a  predefined size which i set to be 1000   so based on that you can i created a json  object using the attribute create object   and i named it data so this json object  is what will store the sensor readings   as different values within the object i then  ran a function to obtain the sensor readings   so this function is down here simply  i obtained humidity and temperature   using the read humidity and read temperature  attributes and printed it on the serial monitor   so once the humidity and temperature readings were  obtained they were assigned to the json object in   this format next the json object was placed on the  serial line using the print to command or print to   attribute and the json buffer was then cleared to  create space to allow for the next set of humidity   and temperature readings so that's the arduino  code now let's have a look at the node mcu code right so this is the code for the node mcu  similarly it also has the software serial   library and the arduino json library in this  case the software serial has to be initialized   with d6 being the receiving pin rx  and d5 being the transmitting pin tx   in the loop a second static json buffer was  created and instead of creating a new data   of a new json object the object placed on the  serial port by the arduino was passed or read   by the node mcu using the pass object command i  use the same naming data to avoid any confusion   i then had a small if if uh function so it checks  to see if the json object is valid or invalid   if it's invalid which means either the temperature  and humidity readings have not been sent or they   have not been sent correctly it would print it  to the serial monitor it would clear the json   buffer and return to the beginning of the  loop so this would continue until a valid   set of readings are obtained given a valid set of  readings are obtained if you jump to this command   where it would print that the json object  has been received and then go ahead and   print the humidity and temperature on the  serial monitor so you can simply obtain   the readings using this sort of a command you have  to allocate a variable and read the attribute of   the json object data so in this case it's  humidity and in this case it's temperature   so that's the code let's go ahead  and run it and see how it works right so now let's see how this code runs i have  already uploaded the code but before you upload   it make sure to check what your board is make  sure that the correct arduino board is selected   and check your com port so for me the arduino  is on com3 and the nodemcu is on com4 right   so i've already uploaded the arduino  code so if i go to the serial monitor i can see the temperature and humidity  readings coming every two seconds   so this is on the arduino now these readings  have to be put into the json object and sent   to the node mcu via the serial port so let's  go ahead close this right so this is the code   for the nodemcu now again i have already  uploaded the code but before you upload it   make sure that your board is selected to nodemcu  so you can do that by actually scrolling down   and you can find nodemcu over here make sure your  upload speed is at this particular value over here   and the correct com port is selected so my node  mcu is on com4 i've already uploaded the code so   let's go ahead and look at the serial monitor  right so the json object is received and the   received humidity and the received temperature is  as such and this will keep going every two seconds   as long as the arduino is sending that  particular json object with the data inside it all right everyone so that's all for this video i  hope you guys enjoyed it and learned something now   if you guys have any questions or clarifications  on the work we just did please feel free to leave   a comment and i will do my best to get back to you  if you guys like this video don't forget to give   me a thumbs up by clicking the like button below  and if you haven't done so already please click   that subscribe button thank you all for watching  stay safe and i'll see you in the next video
Info
Channel: ThatsEngineering
Views: 5,311
Rating: undefined out of 5
Keywords: Arduino, NodeMCU, Serial, Communication, Port, DHT11, DHT, ThatsEngineering, Thats Engineering, That's Engineering, Cloud, IoT, Sending, Data, JSON, Software, Uno
Id: CuZXrv-EKUk
Channel Id: undefined
Length: 13min 11sec (791 seconds)
Published: Fri Oct 30 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.