MicroPython - MQTT tutorial on ESP32

แชร์
ฝัง
  • เผยแพร่เมื่อ 2 มิ.ย. 2024
  • What if you want to remote control a drone, or create a smart home lighting system, or collect sensor data from many devices? Enter MQTT: a robust and flexible IoT protocol that lets us do all that and more!
    Written version of this tutorial: bhave.sh/micropython-mqtt
    (boot.py WiFi connection code is included in here)
    Download mosquitto broker: mosquitto.org/download/
    Awesome Micropython tutorials: bhave.sh
    Banner design and video by the amazing Natalie Plociennik: natalie.gallery
    0:00 Intro
    0:56 MQTT Architecture
    3:18 Installing mosquitto
    4:58 Configuring mosquitto
    7:11 Publish & subscribe
    9:16 ESP32 hardware setup
    10:04 Select ESP32 in Thonny
    11:15 Connect ESP32 to WiFi
    11:51 Connect to MQTT broker
    15:33 Publish from ESP32
    20:27 Button publish demo
    21:34 Subscribe from ESP32
    26:39 Subscription callback demo
    28:03 Set up second ESP32
    29:11 Final project demo
    30:10 MQTT benefits
    30:56 Join for more MicroPython!

ความคิดเห็น • 35

  • @tanimashraf
    @tanimashraf 16 วันที่ผ่านมา

    Loved the 10 million subs thing :D

  • @mystereit73
    @mystereit73 ปีที่แล้ว +1

    So well explained! Thank you!

  • @unrealcompanymusic
    @unrealcompanymusic ปีที่แล้ว +1

    really helped me to create an LED smart light for my room! Great going Bhavesh, more more more

  • @premprasad3511
    @premprasad3511 ปีที่แล้ว

    Hi Bhavesh, thanks a lo for your tutorials and in particular for responding to my request with detailed answers. I followed your advice to use a usb serial adapter for the uart port and finally managed to get the data flowing over the serial port. Thanks a lot

  • @MichaelRegner
    @MichaelRegner ปีที่แล้ว

    Thank you. This was very helpful

  • @igrik1982
    @igrik1982 2 ปีที่แล้ว +1

    Thank you! It helped me a lot.

  • @premprasad3511
    @premprasad3511 ปีที่แล้ว

    You are so good ; I really hope you will have 10 million subscribers soon

  • @SONUKOL1
    @SONUKOL1 ปีที่แล้ว

    very nice

  • @case485
    @case485 ปีที่แล้ว

    Great video man keep it up! Question for you can you run the broker off a 3rd esp32 as well?

    • @BhaveshKakwani
      @BhaveshKakwani  ปีที่แล้ว

      Yeah looks like you can! I found this Arduino library which pretty much does all of it for you. It's not Micropython but, hey, looks like it works: github.com/hsaturn/TinyMqtt

  • @laurentstrodiot8434
    @laurentstrodiot8434 2 ปีที่แล้ว +1

    Congratulations for your video !!! This is great quality and I have learned a lot about using mqtt with micropython. I have a micropython project with neopixel matrix connected to esp8266 (connected to wifi) and I was wondering if the same protocol could be used to send json files or arrays ? Many thanks and looking forward to your next video. best.

    • @BhaveshKakwani
      @BhaveshKakwani  2 ปีที่แล้ว +1

      Yes it totally can be used for that! You need to convert your data (whether json or array) into *bytes* - this process is known as "serialization" of data. For e.g., if your data is json you can use the _ujson_ library (mpython.readthedocs.io/en/master/library/pythonStd/ujson.html) to "dump" your json into a string.
      Then you can send that to your ESP using MQTT. On the ESP side, deserialize the data by "load"ing the json. Probably there will be a max limit of how big your data can be, due to the small RAM of the ESP. But you should try it and let me know if you have more questions during the process :D

    • @laurentstrodiot8434
      @laurentstrodiot8434 2 ปีที่แล้ว

      @@BhaveshKakwani Many thanks for your reply. I will test it on my neopixel display (8x8). I will send the array containing all rgb values to my esp8266 from my mac. I will also test with json files. Thanks again for your video.

    • @laurentstrodiot8434
      @laurentstrodiot8434 ปีที่แล้ว

      @@BhaveshKakwani I made it successfully thanks to you. Best.

  • @ozzyd971
    @ozzyd971 ปีที่แล้ว

    100000000th (binary) subscriber!

  • @safwanshaib9106
    @safwanshaib9106 ปีที่แล้ว

    Nice work 👏
    I was trying to use AWS broker online but have been having issues with building the connection, especially the certificate and private key. It would very helpful if I could connect to an online IOT cloud.

    • @BhaveshKakwani
      @BhaveshKakwani  ปีที่แล้ว

      It looks like Adafruit IO has what you need and they do support insecure MQTT connections for testing things out (i.e. no certificate or key needed). They have a free plan too, and easy to follow documentation, check it out: io.adafruit.com/

  • @premprasad3511
    @premprasad3511 ปีที่แล้ว +1

    How to send data from micropython on esp32 to python running on pc using pyserial module ? And vice versa ?
    Can you do this quickly plesae ? I tried . It works on arduino, on microbit but not when esp32 is running micropython !!

    • @BhaveshKakwani
      @BhaveshKakwani  ปีที่แล้ว +1

      Could you describe your hardware setup to me? Are you connecting the ESP32 to the PC via USB cable or via UARTUSB cable? If it's UART, then which port are you using?
      If you could also share your code so far, it will really allow me to help you :)

    • @premprasad3511
      @premprasad3511 ปีที่แล้ว

      @@BhaveshKakwani I connected ESP32 to the PC via USB cable: Even with usb cable the program gives error when we do not specify the uart port.
      Here is my code
      On the esp side:
      from machine import Pin, PWM, UART #type: ignore
      import time
      led=Pin(2,Pin.OUT)
      uart = UART(0, 115200) (tried 1, and 2 as well)
      uart.init()
      while True:
      if uart.any():
      msg = uart.read()
      print(msg)
      On the pc side
      import serial, time
      ser = serial.Serial(
      port='COM8',
      baudrate = 115200,
      parity=serial.PARITY_NONE,
      stopbits=serial.STOPBITS_ONE,
      bytesize=serial.EIGHTBITS,
      timeout=1)
      while True:

      msg = input()
      ser.write(msg.encode('utf-8'))
      print(ser.readline())
      time.sleep(.1)

    • @BhaveshKakwani
      @BhaveshKakwani  ปีที่แล้ว

      @@premprasad3511 I will reply personally to you to help resolve the issue :)

  • @omerberkaykaynar7400
    @omerberkaykaynar7400 2 ปีที่แล้ว

    great but i want to redirect remotely.
    for example: I want to know the temperature in my house from my workplace. how can do this? your current local connection

    • @BhaveshKakwani
      @BhaveshKakwani  2 ปีที่แล้ว +1

      Hi Ömer! Yes it is possible to do this, and there are two ways:
      1. If you have a Raspberry Pi at home, run the mosquitto broker on that. Sign up for the service 'pagekite' which allows your Pi to be accessed from the internet on a custom domain name (something.something). Now you can run an MQTT subscriber at your workplace that connects to the broker on this custom domain instead of the local IP address.
      2. Other option if you don't have a Raspberry Pi - sign up for a cheap VPS server from Linode. Run the mosquitto broker on it, and give its IP address to your ESP32 and to your workplace laptop. They should now be able to communicate with each other :) Personally I find this option much easier.
      I recognize that doing this is not that easy, depending on how much past web development experience you have.
      Can you let me know if you are interested in learning the 2nd option? I may consider making a tutorial.

    • @omerberkaykaynar7400
      @omerberkaykaynar7400 2 ปีที่แล้ว

      @@BhaveshKakwani thank you so much. There are very few resources on this subject. For this reason, I would like to say that I am waiting for a video like this 🙂

    • @omerberkaykaynar7400
      @omerberkaykaynar7400 2 ปีที่แล้ว

      @@BhaveshKakwani sir, can you give me like this code example or resource. I need it for graduation project :)

    • @BhaveshKakwani
      @BhaveshKakwani  2 ปีที่แล้ว

      @@omerberkaykaynar7400 Here you go. It uses DigitalOcean service instead of Linode, but that doesn't matter - they're basically the same. It's a good step-by-step tutorial randomnerdtutorials.com/cloud-mqtt-mosquitto-broker-access-anywhere-digital-ocean/

    • @omerberkaykaynar7400
      @omerberkaykaynar7400 ปีที่แล้ว

      @@BhaveshKakwani I would like to say that I am looking forward to the video :)

  • @mansurbhamani3905
    @mansurbhamani3905 ปีที่แล้ว

    I tried it on Raspberry Pi PICO temperature measurement, it works. But if broker goes offline then PICO does not connect back to the broker. What could be the solution?

    • @BhaveshKakwani
      @BhaveshKakwani  ปีที่แล้ว

      If the broker goes offline, then nothing can connect to it while it's down. When the broker comes back online, you can try connecting to it again. Maybe try having some basic logic in your main loop to detect when the connection is down, and try calling mqttc.connect() again to reconnect

    • @mansurbhamani3905
      @mansurbhamani3905 ปีที่แล้ว

      @@BhaveshKakwani What I meant was PICO does not connect back automatically after the broker is up. Is there any code that will help PICO to rejoin when broker comes back online?

    • @BhaveshKakwani
      @BhaveshKakwani  ปีที่แล้ว +1

      @@mansurbhamani3905 Yes, you can use the built-in umqtt.robust library instead of the umqtt.simple library I showed in the video. Here's an example code where the umqtt.robust library is used, and it automatically reconnects to the broker if for whatever reason the connection had dropped: github.com/micropython/micropython-lib/blob/master/micropython/umqtt.robust/example_check_msg_robust.py

    • @mansurbhamani3905
      @mansurbhamani3905 ปีที่แล้ว

      @@BhaveshKakwani Great, thank you 👍

  • @DJNilu
    @DJNilu 2 ปีที่แล้ว +2

    lol 10 million subscribers! ....in binary