ESP32CAM的图片用MQTT也能传?物联网平台来帮你

  ESP32是乐鑫在ESP8266后推出的一款集成WiFi功能的微控制器,相较于ESP8266,ESP32拥有更强大的性能,可用于复杂应用的开发。

  ESP32CAM在ESP32的基础上扩展了摄像头模组,体积极小,可广泛应用于各种物联网场合,适用于家庭智能设备、工业无线控制、无线监控、QR无线识别,无线定位系统信号以及其它物联网应用,是物联网应用的理想解决方案。

  MQTT是一种基于发布/订阅模式的轻量级通讯协议,它的特点是可使用极少的代码与有限的带宽为远程连接的设备提供实时可靠的消息订阅服务,被广泛应用于物联网运用。MQTT单条报文允许的最大体积为 256 M,ESP32CAM拍摄的图片的体积大小远小于这个数值,可以考虑使用MQTT协议来传输图片。

  需要使用的零件如下:

零件名称 数量
1 ESP32CAM 开发板 1
2 USB-TTL模块 1
3 杜邦线 若干

ESP32CAM的图片用MQTT也能传?物联网平台来帮你_第1张图片
  那么,要如何将ESP32CAM获取的图片通过MQTT进行传输呢?可参考以下信息进行设置。
ESP32CAM的图片用MQTT也能传?物联网平台来帮你_第2张图片
  使用Arduino IDE 编译并上传以下Arduino程序。该程序使用的是esp32开发板,需要在Arduino IDE中添加安装该开发板环境,并在编译时选择开发板环境为ESP32系列开发板。在Arduino IDE中添加ESP32开发板环境。注意烧录时ESP32CAM的开发板中IO0针脚需要进行接地操作,否则会导致刷写失败。

#include "WiFi.h"
#include "esp_camera.h"
#include "esp_timer.h"
#include "img_converters.h"
#include "Arduino.h"
#include "soc/soc.h"           // Disable brownour problems
#include "soc/rtc_cntl_reg.h"  // Disable brownour problems
#include "driver/rtc_io.h"
//#include "StringArray.h"

#include 
#include 
#include 


//esp32-cam针脚定义 拍照相关
constexpr int kCameraPin_PWDN   =  32;
constexpr int kCameraPin_RESET  =  -1;  // NC
constexpr int kCameraPin_XCLK   =   0;
constexpr int kCameraPin_SIOD   =  26;
constexpr int kCameraPin_SIOC   =  27;
constexpr int kCameraPin_Y9     =  35;
constexpr int kCameraPin_Y8     =  34;
constexpr int kCameraPin_Y7     =  39;
constexpr int kCameraPin_Y6     =  36;
constexpr int kCameraPin_Y5     =  21;
constexpr int kCameraPin_Y4     =  19;
constexpr int kCameraPin_Y3     =  18;
constexpr int kCameraPin_Y2     =   5;
constexpr int kCameraPin_VSYNC  =  25;
constexpr int kCameraPin_HREF   =  23;
constexpr int kCameraPin_PCLK   =  22;

// 此处需替换连接的无线网络为自己使用的无线网络
const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";

// 此处需替换MQTT服务端为自己使用的服务端
const char* mqtt_server = "YOUR_MQTT_BROKER_HOSTNAME";
const int mqtt_port = 1883;
const char* mqtt_user = "YOUR_MQTT_BROKER_USERNAME";
const char* mqtt_password = "YOUR_MQTT_BROKER_PASSWORD";

//设置主题
const char* mqtt_TopicName = "/devices/esp32/data";

//设置相机帧的分辨率大小
framesize_t resolution_ = FRAMESIZE_QVGA;


//定义延时时间1000=1s
#define SLEEP_DELAY 60000 //延迟60s
#define FILE_PHOTO "/photo.jpg"

// OV2640 相机模组的针脚定义
#define PWDN_GPIO_NUM     32
#define RESET_GPIO_NUM    -1
#define XCLK_GPIO_NUM      0
#define SIOD_GPIO_NUM     26
#define SIOC_GPIO_NUM     27
#define Y9_GPIO_NUM       35
#define Y8_GPIO_NUM       34
#define Y7_GPIO_NUM       39
#define Y6_GPIO_NUM       36
#define Y5_GPIO_NUM       21
#define Y4_GPIO_NUM       19
#define Y3_GPIO_NUM       18
#define Y2_GPIO_NUM        5
#define VSYNC_GPIO_NUM    25
#define HREF_GPIO_NUM     23
#define PCLK_GPIO_NUM     22


// 选择相机型号
#define CAMERA_MODEL_WROVER_KIT
//#define CAMERA_MODEL_ESP_EYE
//#define CAMERA_MODEL_M5STACK_PSRAM
//#define CAMERA_MODEL_M5STACK_WIDE
//#define CAMERA_MODEL_AI_THINKER


//#define ESP32_CLIENT_ID = WiFi.macAddress()
//const char* esp_client_id = WiFi.macAddress()
WiFiClient mqttClient;
PubSubClient client(mqttClient);

const int LED_BUILTIN = 4;


void setup_camera() {

     // OV2640 camera module
     camera_config_t config;
    config.pin_pwdn     = kCameraPin_PWDN;
    config.pin_reset    = kCameraPin_RESET;
    config.pin_xclk     = kCameraPin_XCLK;
    config.pin_sscb_sda = kCameraPin_SIOD;
    config.pin_sscb_scl = kCameraPin_SIOC;
    config.pin_d7       = kCameraPin_Y9;
    config.pin_d6       = kCameraPin_Y8;
    config.pin_d5       = kCameraPin_Y7;
    config.pin_d4       = kCameraPin_Y6;
    config.pin_d3       = kCameraPin_Y5;
    config.pin_d2       = kCameraPin_Y4;
    config.pin_d1       = kCameraPin_Y3;
    config.pin_d0       = kCameraPin_Y2;
    config.pin_vsync    = kCameraPin_VSYNC;
    config.pin_href     = kCameraPin_HREF;
    config.pin_pclk     = kCameraPin_PCLK;
    config.xclk_freq_hz = 20000000;
    config.ledc_timer   = LEDC_TIMER_0;
    config.ledc_channel = LEDC_CHANNEL_0;
    config.pixel_format = PIXFORMAT_JPEG;
    config.frame_size   = FRAMESIZE_SVGA;
    config.jpeg_quality = 10;
    config.fb_count     = 1;

  esp_err_t err = esp_camera_init(&config);
  Serial.printf("esp_camera_init: 0x%x\n", err);

  // sensor_t *s = esp_camera_sensor_get();
  // s->set_framesize(s, FRAMESIZE_QVGA);   
  }

String msg;
int timeCount = 0;
void getimg(){//拍照分段发送到mqtt

    camera_fb_t *fb = esp_camera_fb_get();
    if (fb){
        Serial.printf("width: %d, height: %d, buf: 0x%x, len: %d\n", fb->width, fb->height, fb->buf, fb->len);
        char data[4104];
        //client.publish(mqtt_TopicName, "0");
        for (int i = 0; i < fb->len; i++){

            sprintf(data, "%02X", *((fb->buf + i)));
            msg += data;
           // if (msg.length() == 4096){
           //     timeCount += 1;
           //     client.beginPublish(mqtt_TopicName, msg.length(), 0);
           //     client.print(msg);
           //     client.endPublish();
           //     msg = "";
           // }
        }
        if (msg.length() > 0){
            client.beginPublish(mqtt_TopicName, msg.length(), 0);
            client.print(msg);
            client.endPublish();
            msg = "";
        }
        //client.publish(mqtt_TopicName, "1");
        timeCount = 0;
        esp_camera_fb_return(fb);
    }
}

void setup_wifi() {
  delay(10);
  // 连接WIFI
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.print("IP address : ");
  Serial.println(WiFi.localIP());
}

//MQTT重连
void reconnect() {
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    if (client.connect("ESP32Client", mqtt_user, mqtt_password)) {
      Serial.println("connected");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      delay(5000);
    }
  }
}

void setup() {
  Serial.begin(115200);
  Serial.setDebugOutput(true);
  Serial.println();
  setup_camera(); //设置相机
  setup_wifi();		//连接WIFI
  client.setServer(mqtt_server, mqtt_port); //连接MQTT Broker
  if (client.connect("ESP32Client", mqtt_user, mqtt_password)) {
      Serial.println("mqtt connected");
    } 
}

void loop() {
  getimg();

  if (!client.connected()) {
    reconnect();
  }

  delay(10000);
}

  使用FastWeb网页开发平台的示例来接收,可查看ESP32CAM传输的界面。ESP32CAM的画面使用十六进制文本进行传输,MQTT接收到传输的信息后,使用专用的解析函数可将十六进制的内容重新转换成图片。以上的示例使用的界面可自行搭建,只要几分钟就可以搭建完成。

ESP32CAM的图片用MQTT也能传?物联网平台来帮你_第3张图片

  UGCM.HexToJPG(apayload,"profile-image.jpg");
  UgImage01.Picture.LoadFromFile("profile-image.jpg");

  如果您需要了解更多当前展示范例的详细信息,可点击以下链接查看。

  • 产品:https://www.isoface.cn/isoface/production/software/fastweb/web
  • 范例:https://fastweb.isoface.cn:1443/?module=f9bcaeb1-3015-4778-818a-fa2ee225d118&userkey=d795b2b5-67c0-4674-9961-4aa574311c8d
  • 范例视频:https://www.isoface.cn/isoface/component/k2/video-tutorial/fastweb/w-eq-dem-2009
  • 范例手册:https://www.isoface.cn/isoface/doc/fastweb/demo/mqtt-esp32-cam/
  • 产品手册:https://www.isoface.cn/isoface/doc/fastweb/main/
  • 公司网址:https://www.isoface.cn
  • QQ群:309174897

你可能感兴趣的:(物联网运用,物联网,单片机)