MQTT Image Transfer: Send and Receive images using MQTT | Android app x python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to high voltages in this  exciting video we will learn how to seamlessly   send receive and store images using the mqtt  protocol but before we begin let's address   two burning questions can we really use the  mqtt protocol for sending and receiving images   the answer is a resounding yes however  the next question is is it recommended   surprisingly the answer is no it's not now  you might be wondering why not let's explore   the key challenges that make transmitting  images over MQTT are less than ideal choice   payload size MQTT is optimized for small  payload not large files like images   bandwidth considerations transferring images  over mqtt can consume excessive bandwidth   latency MQTT low latency design may be compromised  when transmitting large images quality of service   higher levels of quality of service in mqtt for  large images can introduce additional overhead   and latency while mqtt might not be the optimal  choice for image transfer due to its limitations   in payload size and bandwidth considerations don't  lose hope just yet you can still use MQTT to send   images for personal use as long as you understand  and accept these limitations you can experiment   with transmitting smaller images over mqtt in an  ample Network however for efficient and reliable   image transfer other protocols like HTTP or  specialized image transfer Protocols are more   suitable our goal is Crystal Clear to send and  receive images using the mqtt protocol in this   video we will demonstrate how to send and receive  images through mqtt brace yourself as we walk you   through creating an Android app using MIT App  Inventor to select and send images along with   writing python code for image transmission  and reception so let's see how we could send   image using mqttt protocol our journey begins with  connecting to an mqtt broker followed by selecting   or reading an image then we will convert the image  into base64 format base64 is a binary to text   encoding scheme that is commonly used to represent  binary data in text format it is used to encode   binary data such as images into a format that can  be easily transmitted over text based protocols or   stored as text finally we will publish the base64  data to the desired topic for image transmission   now let's switch roles and become the recipient  we will connect to the mqtt broker subscribe to   the topic where the image is sent and eagerly  await for its arrival once the messages arrived   we will extract the base64 code decode  it and voila the image will be revealed   so let's get started and write python code  to send an image using mqtt protocol we are   in the visual studio code window and we have this  image that we are going to publish it is the logo   of high voltages talking about logo make sure  to subscribe high voltages now I have created   a file to write code for sending images using  mqtt first let's import the necessary libraries   we will need the time module for adding delays  the base64 module for encoding the image and   the power mqtt module for mqtt functionality  you could check out my previous videos on mqtt   and python to learn more about these libraries  now let's set up the mqtt broker information we will use the broker hivemq.com which is a  free broker with the default Port 1883 and we   will publish our image on the images topic  you can modify this according to your mqtt   broker setup make sure to change the client  ID to establish an mqtt connection we Define   the connect mqtt function inside this function  we set up the connection parameters and Define   the onconnect Callback function which will be  called when the client successfully connects to   the mqtt broker in my previous video in which I  have explained how to use mqtt protocol in Python   I have explained all these things in very detail  you could check out previous videos now we will   write a publish function and in it we open the  image file using the with statement logo.jpg is   our file that we are going to publish and now  we will read the content of this image file   and store it in the file content variable then  we will use the base64 function to convert the   image content to a base64 encoded format this  ensures that we can send the image as a string   over mqtt to publish the image we will call  the client.publish function and we will pass   the topic and the base64 content as parameters  the client.publish function returns a result   which we will store in the result variable we  check the status of the message by accessing   the result variable if the status is 0 it means  the message wasn't successfully otherwise we   display a failure message to control the flow of  the program we introduce a main function inside   this function we establish an mqtt connection  using the connect mqtt function and now we   will start the mqtt loop we will then call the  publish function to send the image after a five   second delay using time.sleep function we stop the  mqtt loop and now we will call our main function and that's it we have successfully written a   python script to send images  over mqtt now let's test this you could see message sent to topic images now  let's write python code for receiving image using   mqtt protocol now we are writing code to receive  image using mqtt I have created a file and copied   the same code from the other file since the  broker information and connect function remains   same make sure to change this client ID and now  we will write code to subscribe to the topic and   decode image first we will have a function called  subscribe which takes a mqtt client as a parameter   this function sets up the mqd client to  subscribe to a specific topic and defines   a callback function for handling incoming  messages inside the Subscribe function we   have a nested function called on message  this function will be called whenever a   new message is received it take parameters  such as client user data and message which   provide information about the received message now  let's write code to process the received message   in this part the code first decode the payload  of the received message from bytes to a string   using the decode method then it converts the  string back to bytes by encoding it with ASCII   using the encode ASCII method then the resulting  encoded image data is stored in the image variable   next the code generates a unique file name for  the received image using the current date and   time this presents the file name that will be  assigned to the image file now let's explore how   to store image in this part the code first opens a  new file in binary write mode using the generated   file name it then decodes the image variable which  contains the encoded image data using the base64   function from the base64 module the decoded  image data is returned to the file using the   right method finally the file is closed using  the close method coming back to the Subscribe   function after setting up the message handling  function the code proceeds to subscribe to a   specified mqd topic and assign the on message  function as the callback for incoming messages   lastly we have the main function in the main  function we will first connect to the mqtt   client by calling connect mqtt function  then we will call the Subscribe function   and pass the client as a parameter finally we  will call a forever Loop so we can continuously   process incoming messages now we will have to  call the main function now let's run the code and you see we are successfully connected to the  mqtt broker and now let's run code to send image and you will see the file is generated with  the date time so this is how you could send   and receive images using Python and mqtt  currently I was using the same machine to   run both scripts but you can run these scripts  in different machines and different locations   to send image from one machine to another  and finally we can use MIT App Inventor   to make Android app and send image using  mqtt okay so we are in MIT App Inventor   and we will start by clicking on start new  project and we will give our project a name and you will see a window like this now to use  mqtt and base64 we will need two extra extension   you can download the mqtt extension from this  website I will put the link in the description   and for base64 you could download it from here  so now let's download these two extensions we have explored these extension in  my previous video in which I I made   a tutorial about MIT App Inventor  and mqtt so now we will go down   here and click on extension and we  will click on import extension and   this is for mqtt we will import this extension  and then we will import the other base64 extension then we will have to drag these  extension into the Mobile screen and you will be able to see these  extension in the components and now   we will click on mqtt extension and  we will have to set our broker here and we will have to set our client ID the port is 183 we are not using any password  and username so that's it now we will create a   basic user interface we will go into layout  we will drag this vertical arrangement we   will set the height to fill parent with  to fill parent and now we will import   some components for example  we will name it this image   and we will need this image picker we will change  the name of this image speaker to select image and then we will get a button and  we will change the button text to   send and we will make it invisible  and we will create a label here and for now we will keep it empty so that is  our user interface now we will go to blocks   and here we can program our Android application  so first thing we will create a variable   we'll call it image and I will set this  to empty string and then we will connect   to mgrity broker so I will click on the screen  one and I will say when screen one initialize   then I'll go to this extension mqtt  and I will call this function connect   I will set the clean session to false so when our  screen one will initialize we will connect to mqtt   broker then I will call this image picker and  I will call this function when image speaker we   have already picked our image so I will click  on this image and we will set the image here and we will select selection of image speaker  and then I will I will encode this image   so I will go here and I will call this function  image to base64 and here we will select our image so this function will convert our image into  base64 so now we will go back to here and we will   call a function that when the image is already  converted to base64 we will set the variable image and we will get the base64 data and we  will set it to the image variable and then   we will make our button visible and we will set the label text to image ready to send and now when button is clicked  we will call this ambiguity function and we will   publish the image so for the topic we will we are  using the images topic and message the variable   image in which we have stored the base64 data  so that's it now we will build our application so now the APK has been created I will download  this APK and then I will open this emulator   and I will drag this apk here it will be installed and our app is here so now  we will select our image and you could see the image here and it says  image ready to send and when I will click on send   you see the image has been added here so  that's how you can send image from Android to   python using mqtt thank you for watching  please like share and subscribe for more   such videos and I would love to  hear your comments about this
Info
Channel: High Voltages
Views: 2,929
Rating: undefined out of 5
Keywords: image using mqtt, could we use mqtt to send images ?, mqtt, mqtt protocol, send image using mqtt, receive image using mqtt, mqtt python, internet of things, mit app inventor, mqtt tutorial, image transfer tutorial, mqtt image transfer, mqtt image communication, android image transfer, python image transfer, mqtt image sending, mqtt image receiving, mqtt image tutorial., message queuing telemetry, what is mqtt, story of mqtt
Id: SFuaNcyWV8g
Channel Id: undefined
Length: 15min 2sec (902 seconds)
Published: Fri Jul 14 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.