我需要 ESP32 项目的帮助 - 相机 - 与 Node-red 的通信

问题描述 投票:0回答:0

我正在寻找可以帮助我完成这个项目的人。我必须创建一个物联网应用程序,其中包括连接到互联网(Wifi 或以太网,由您选择)的 ESP32-cam,能够接收消息并通过 MQTT 发送它们。如果有人能帮助我,我将不胜感激。

我可以按照您认为合适的方式组织与消息相关的主题。

ESP32 上的应用程序将有一个文本 ID 来识别安装传感器的房间。 (例如“厨房”)。

ESP32 会周期性地在 Node-red 上以 Tcam 周期发送摄像头图像,这将显示在 DashBoard 上。此外,事件也被写在用作调试的端口(串行)上,格式如下“ID - 图像发送 - 大小”

环境光由周期为 Tpwm 和 DutyCyle D 的 PWM 信号控制。(使用 LED 模拟环境光)。

照明参数(Tpwm,D)将从 Node-red 发送,值可以从仪表板设置。

在仪表板的其他设置中,必须有一个开关(完全打开和关闭照明)。

Node-red 仪表板必须:

使用按钮(在仪表板上),允许恢复应用程序初始化时使用的 IP 地址和 ID。 (在仪表板上查看)。

有了足够数量的控件,允许在 ESP32-cam 上设置以下值:Tcam、Tpwm、DutyCyle

使用按钮,允许获取“额外”图像。

图形控件必须能够随时间显示占空比值。

对 node-red 的检查必须允许发送将写入串口的警报消息,并将在 ESP32 led 上设置闪烁现象。

警报状态只能通过仪表板上的命令重置,或在 25 秒后自动重置。

拍摄的图像,除了显示在仪表盘上外,还必须适当地保存在PC上(node-red上有专门的控件)。

我开始创建这个文件,但我不知道如何继续:

#include <WiFi.h>
#include <PubSubClient.h>
#include "esp_camera.h"


// Pin definition for CAMERA_MODEL_AI_THINKER
#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


// Variables
camera_fb_t * fb = NULL;

// Replace the next variables with your SSID/Password combination
const char* ssid = "ITTMarconi";
const char* password = "B3InTheFuture";

// Add your MQTT Broker IP address, example:
const char* mqtt_broker = "192.168.42.90";  // Marconi
const int   mqtt_port        = 1884;   // porta del Broker

WiFiClient   espClient;
PubSubClient client(espClient);

void setup() {
  Serial.begin(115200);
 
  setup_wifi();
  client.setServer(mqtt_broker, mqtt_port);
  client.setCallback(callback);
 
 
  //Camera config
  camera_config_t config;
  config.ledc_channel = LEDC_CHANNEL_0;
  config.ledc_timer = LEDC_TIMER_0;
  config.pin_d0 = Y2_GPIO_NUM;
  config.pin_d1 = Y3_GPIO_NUM;
  config.pin_d2 = Y4_GPIO_NUM;
  config.pin_d3 = Y5_GPIO_NUM;
  config.pin_d4 = Y6_GPIO_NUM;
  config.pin_d5 = Y7_GPIO_NUM;
  config.pin_d6 = Y8_GPIO_NUM;
  config.pin_d7 = Y9_GPIO_NUM;
  config.pin_xclk = XCLK_GPIO_NUM;
  config.pin_pclk = PCLK_GPIO_NUM;
  config.pin_vsync = VSYNC_GPIO_NUM;
  config.pin_href = HREF_GPIO_NUM;
  config.pin_sscb_sda = SIOD_GPIO_NUM;
  config.pin_sscb_scl = SIOC_GPIO_NUM;
  config.pin_pwdn = PWDN_GPIO_NUM;
  config.pin_reset = RESET_GPIO_NUM;
  config.xclk_freq_hz = 20000000;
  config.pixel_format = PIXFORMAT_JPEG;

  if (psramFound()) {
    config.frame_size = FRAMESIZE_UXGA;
    config.jpeg_quality = 10;
    config.fb_count = 2;
  } else {
    config.frame_size = FRAMESIZE_SVGA;
    config.jpeg_quality = 12;
    config.fb_count = 1;
  }

  // Init Camera
  esp_err_t err = esp_camera_init(&config);
  if (err != ESP_OK) {
    Serial.printf("Camera init failed with error 0x%x", err);
    return;
  }
}

void setup_wifi() {
  delay(10);
  // We start by connecting to a WiFi network
  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.println("IP address: ");
  Serial.println(WiFi.localIP());
}


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

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect("ESP32cam-pippo","testuser","testpass")) {
      Serial.println("connected in");
      // Subscribe
      client.subscribe("esp32/output");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}



void callback(char* topic, byte* payload, unsigned int length) {
    Serial.print("Message arrived [");
    Serial.print(topic);
    Serial.print("] ");
    for (int i = 0; i < length; i++) {
        Serial.print((char)payload[i]);
    }
    Serial.println();
   
    esp_camera_fb_return(fb);
   
    // Take Picture with Camera
    fb = esp_camera_fb_get();  
    if(!fb) {
      Serial.println("Camera capture failed");
      return;
    } else {
      Serial.println("Camera capture successful!");
    }
     
   
    client.beginPublish("Marconi_dB/99/image", fb->len, false);    
    client.write(fb->buf, fb->len);

    // Now we're done!
    client.endPublish();

    Serial.println("end MQTT");

    esp_camera_fb_return(fb);

    Serial.println("camera ret ");
    client.disconnect ();
  }

这是用于连接 Node-red 的 json 文件:

[{
"id": "ebca07135e55aa5f",
"type": "tab",
"label": "MQTT-image",
"disabled": false,
"info": "",
"env": []
},
{
"id": "266f665a0896f870",
"type": "aedes broker",
"z": "ebca07135e55aa5f",
"name": "",
"mqtt_port": "1884",
"mqtt_ws_bind": "port",
"mqtt_ws_port": "",
"mqtt_ws_path": "",
"cert": "",
"key": "",
"certname": "",
"keyname": "",
"persistence_bind": "memory",
"dburl": "",
"usetls": false,
"credentials": {
"username": "",
"password": "",
"certdata": "",
"keydata": ""
},
"x": 270,
"y": 100,
"wires": [
[
"bf508974d709ef2c"
],
[]
]
},
{
"id": "8e550c5d8987dbc6",
"type": "mqtt in",
"z": "ebca07135e55aa5f",
"name": "",
"topic": "Marconi_dB/0/size",
"qos": "2",
"datatype": "auto-detect",
"broker": "321d2fb789e1f051",
"nl": false,
"rap": true,
"rh": 0,
"inputs": 0,
"x": 250,
"y": 260,
"wires": [
[
"17406ed885b28fce"
]
]
},
{
"id": "ab09a7389c81da1c",
"type": "mqtt in",
"z": "ebca07135e55aa5f",
"name": "",
"topic": "Marconi_dB/+/width",
"qos": "2",
"datatype": "auto-detect",
"broker": "321d2fb789e1f051",
"nl": false,
"rap": true,
"rh": 0,
"inputs": 0,
"x": 250,
"y": 340,
"wires": [
[
"92108f158f8654d0"
]
]
},
{
"id": "bde5b65d069fd26d",
"type": "mqtt in",
"z": "ebca07135e55aa5f",
"name": "",
"topic": "Marconi_dB/+/height",
"qos": "2",
"datatype": "auto-detect",
"broker": "321d2fb789e1f051",
"nl": false,
"rap": true,
"rh": 0,
"inputs": 0,
"x": 250,
"y": 440,
"wires": [
[
"558df1a4ebdc777e"
]
]
},
{
"id": "4b4bbc676afda4a1",
"type": "mqtt in",
"z": "ebca07135e55aa5f",
"name": "",
"topic": "Marconi_dB/+/image",
"qos": "2",
"datatype": "auto-detect",
"broker": "321d2fb789e1f051",
"nl": false,
"rap": true,
"rh": 0,
"inputs": 0,
"x": 250,
"y": 560,
"wires": [
[
"a82b7766d007bafc",
"66fbd29f5df8aa98",
"d3725ae315b0f009"
]
]
},
{
"id": "17406ed885b28fce",
"type": "debug",
"z": "ebca07135e55aa5f",
"name": "debug 5",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"statusVal": "",
"statusType": "auto",
"x": 500,
"y": 260,
"wires": []
},
{
"id": "92108f158f8654d0",
"type": "debug",
"z": "ebca07135e55aa5f",
"name": "debug 7",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"statusVal": "",
"statusType": "auto",
"x": 500,
"y": 340,
"wires": []
},
{
"id": "558df1a4ebdc777e",
"type": "debug",
"z": "ebca07135e55aa5f",
"name": "debug 8",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"statusVal": "",
"statusType": "auto",
"x": 500,
"y": 440,
"wires": []
},
{
"id": "a82b7766d007bafc",
"type": "debug",
"z": "ebca07135e55aa5f",
"name": "debug 9",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"statusVal": "",
"statusType": "auto",
"x": 740,
"y": 560,
"wires": []
},
{
"id": "273c9eff94c3482e",
"type": "mqtt out",
"z": "ebca07135e55aa5f",
"name": "",
"topic": "Marconi_dB",
"qos": "",
"retain": "",
"respTopic": "",
"contentType": "",
"userProps": "",
"correl": "",
"expiry": "",
"broker": "321d2fb789e1f051",
"x": 1070,
"y": 360,
"wires": []
},
{
"id": "bf508974d709ef2c",
"type": "debug",
"z": "ebca07135e55aa5f",
"name": "debug 10",
"active": false,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 500,
"y": 100,
"wires": []
},
{
"id": "2b7fabb931f85654",
"type": "inject",
"z": "ebca07135e55aa5f",
"name": "",
"props": [{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "",
"payloadType": "date",
"x": 820,
"y": 300,
"wires": [
[
"273c9eff94c3482e"          ]
]
},
{
"id": "66fbd29f5df8aa98",
"type": "image",
"z": "ebca07135e55aa5f",
"name": "",
"width": 160,
"data": "payload",
"dataType": "msg",
"thumbnail": false,
"active": false,
"pass": false,
"outputs": 0,
"x": 840,
"y": 660,
"wires": []
},
{
"id": "d3725ae315b0f009",
"type": "imageSimple",
"z": "ebca07135e55aa5f",
"imageData": "payload",
"imageType": "msg",
"autoresize": false,
"original": false,
"localfile": false,
"flipX": false,
"flipY": false,
"width": 200,
"height": 200,
"rotate": 0,
"crop": false,
"cropX": 0,
"cropY": 0,
"cropWidth": 200,
"cropHeight": 200,
"x": 490,
"y": 740,
"wires": [
[
"44b8bc767a397086"
]
]
},
{
"id": "1fc19507bc2e93de",
"type": "template",
"z": "ebca07135e55aa5f",
"name": "",
"field": "payload",
"fieldType": "msg",
"format": "handlebars",
"syntax": "mustache",
"template": "<img width=\"320px\" height=\"240px\" src=\"data:image/jpg;base64,{{{payload}}}\">",
"output": "str",
"x": 880,
"y": 740,
"wires": [
[
"6db755c900c112c3"
]
]
},
{
"id": "44b8bc767a397086",
"type": "base64",
"z": "ebca07135e55aa5f",
"name": "",
"action": "str",
"property": "payload",
"x": 660,
"y": 740,
"wires": [
[
"1fc19507bc2e93de"
]
]
},
{
"id": "6db755c900c112c3",
"type": "ui_template",
"z": "ebca07135e55aa5f",
"group": "add6797b.ef9dc",
"name": "",
"order": 1,
"width": 7,
"height": 7,
"format": "<div ng-bind-html=\"msg.payload\"></div>",
"storeOutMessages": true,
"fwdInMessages": true,
"resendOnRefresh": false,
"templateScope": "local",
"className": "",
"x": 1060,
"y": 760,
"wires": [
[]
]
},
{
"id": "4b8880ab424704d9",
"type": "inject",
"z": "ebca07135e55aa5f",
"name": "ON",
"props": [{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "on",
"payloadType": "str",
"x": 170,
"y": 860,
"wires": [
[
"2fa21b786f437bb4"
]
]
},
{
"id": "2fa21b786f437bb4",
"type": "mqtt out",
"z": "ebca07135e55aa5f",
"name": "",
"topic": "esp32/input",
"qos": "",
"retain": "",
"respTopic": "",
"contentType": "",
"userProps": "",
"correl": "",
"expiry": "",
"broker": "321d2fb789e1f051",
"x": 530,
"y": 880,
"wires": []
},
{
"id": "8ab9c30554d4e1c7",
"type": "inject",
"z": "ebca07135e55aa5f",
"name": "OFF",
"props": [{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "off",
"payloadType": "str",
"x": 170,
"y": 900,
"wires": [
[
"2fa21b786f437bb4"
]
]
},
{
"id": "9a30ed32a9cefc0e",
"type": "ui_switch",
"z": "ebca07135e55aa5f",
"name": "",
"label": "switch",
"tooltip": "",
"group": "bd30f67a543c4007",
"order": 3,
"width": 0,
"height": 0,
"passthru": true,
"decouple": "false",
"topic": "esp32/output",
"topicType": "msg",
"style": "",
"onvalue": "on",
"onvalueType": "str",
"onicon": "",
"oncolor": "",
"offvalue": "off",
"offvalueType": "str",
"officon": "",
"offcolor": "",
"animate": false,
"className": "",
"x": 170,
"y": 980,
"wires": [
[
"2fa21b786f437bb4"
]
]
},
{
"id": "321d2fb789e1f051",
"type": "mqtt-broker",
"name": "",
"broker": "192.168.42.90",
"port": "1884",
"clientid": "",
"autoConnect": true,
"usetls": false,
"protocolVersion": "4",
"keepalive": "60",
"cleansession": true,
"birthTopic": "",
"birthQos": "0",
"birthPayload": "",
"birthMsg": {},
"closeTopic": "",
"closeQos": "0",
"closePayload": "",
"closeMsg": {},
"willTopic": "",
"willQos": "0",
"willPayload": "",
"willMsg": {},
"userProps": "",
"sessionExpiry": "",
"credentials": {}
},
{
"id": "add6797b.ef9dc",
"type": "ui_group",
"name": "Image from camera ",
"tab": "922045aa0aea388d",
"order": 1,
"disp": true,
"width": "9",
"collapse": false,
"className": ""
},
{
"id": "bd30f67a543c4007",
"type": "ui_group",
"name": "Group 1",
"tab": "c13a34cfbff65b09",
"order": 1,
"disp": true,
"width": 6
},
{
"id": "922045aa0aea388d",
"type": "ui_tab",
"name": "Image GUI",
"icon": "dashboard",
"disabled": false,
"hidden": false
},
{
"id": "c13a34cfbff65b09",
"type": "ui_tab",
"name": "MQTT",
"icon": "dashboard",
"order": 5,
"disabled": false,
"hidden": false
}
]
node.js arduino esp32 node-red arduino-c++
© www.soinside.com 2019 - 2024. All rights reserved.