Coder Social home page Coder Social logo

fnandomaia / esp32cam-mqtt-2 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from abish7643/esp32cam-mqtt

0.0 0.0 0.0 1.02 MB

ESP32Cam with Adafruit IO to send image using PubSubClient library & Python program to subscribe to the same topic.

C++ 76.86% Python 15.42% C 7.71%

esp32cam-mqtt-2's Introduction

ESP32CAM With Adafruit.io

Dependencies

  • Hardware
    • AI Thinker ESP32 CAM
    • USB UART Programmer (CP2102 or FTDI)
  • Libraries

Contents

  • Publish to Adafruit.IO
    • Using Platform IO
    • Using Arduino IDE
  • Customizing The Credentials
  • Expected Output
  • Subscribe to Topic Using Python

Publish to Adafruit.IO

  1. Using Platform IO

    In the file platformio.ini, before the library dependencies, add a build flag to modify the MQTT Packet Size to the required size. The default packet size defined in PubSubClient Library is 128 Bytes. It's increased to 36000 Bytes inorder to satisfy the requirements.

    build_flags = -DMQTT_MAX_PACKET_SIZE=36000
    
    lib_deps =
    [email protected]

    For additional information, go to Platform IO build_flags.

  2. Using Arduino IDE

    Install the PubSubClient Library from Tools -> Manage Libraries. We used the 2.7 version of the same. After installing the library go to the directory of Libraries related to Arduino, and navigate to PubSubClient Library.

    • In Linux

      user@user:~/Arduino/libraries/PubSubClient$ pwd
      /home/user/Arduino/libraries/PubSubClient
    • In Windows

      Navigate to user/Documents/Arduino/libraries/PubSubClient

    Go to PubSubClient.h residing in the src folder, Find the following codeblock.

    // MQTT_MAX_PACKET_SIZE : Maximum packet size
    #ifndef MQTT_MAX_PACKET_SIZE
    #define MQTT_MAX_PACKET_SIZE 128
    #endif

    And modify according to requirements.

    // MQTT_MAX_PACKET_SIZE : Maximum packet size
    #ifndef MQTT_MAX_PACKET_SIZE
    #define MQTT_MAX_PACKET_SIZE 36000
    #endif

Adding The Credentials

In the main.cpp file (If Arduino IDE, ESP32Cam_MQTT.ino),

  1. Replace the variables with your SSID/Password combination

    const char *ssid = "WiFi_SSID";
    const char *password = "WiFi_Password";
  2. Add MQTT Credentials.

    const char *mqtt_server = "io.adafruit.com";
    const char *mqtt_clientid = "mqtt_clientid";
    const char *mqtt_username = "mqtt_username";
    const char *mqtt_password = "mqtt_password";
    const char *mqtt_publish_topic = "username/feeds/camera";
    • Replace mqtt_clientid with a unique random id.
    • Replace mqtt_username with the Adafruit Username.
    • Replace mqtt_password with the Adafruit AIO Key.

Expected Output

Connecting to Dhanish
.......
WiFi connected
IP address: 
192.168.1.5
Attempting MQTT connection...connected
Camera Captured
Buffer Length: 
22008
Publishing...Published
Camera Captured
Buffer Length: 
21960
Publishing...Published

...

Subscribe to Topic Using Python

A basic python code to subscribe to the topic username/feeds/camera and updates realtime.

Requirements:

  • Python3
  • Paho MQTT Client
  • PIL (Python Imaging Library)
  • PyGame

Adding Credentials

  1. Subscribe to Camera Topic in the on_connect function,

    def on_connect(client, userdata, flags, rc):
        print("Connected with result code "+str(rc))
        client.subscribe("username/feeds/camera")
  2. Replace MQTT Credentials,

    client.username_pw_set(username="mqtt_username",password="mqtt_password"
    client.connect("io.adafruit.com", 1883, 60)
  3. Run using python3

    python3 test_subscriber.py 

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.