【0531更新教程】Blynk+M5timer_camera,易于实现部署的家居检测-Arduino中文社区 - Powered by Discuz! Archiver

沧海笑1122 发表于 2021-5-13 22:09

【0531更新教程】Blynk+M5timer_camera,易于实现部署的家居检测

本帖最后由 沧海笑1122 于 2021-5-31 19:15 编辑

   Blynk+M5timer_camera(esp32 core),易于实现部署的家居监测(0531更新详见四楼)【背景故事】本教程源于B站《自制 ESP32+Blynk 无线 Wi-Fi 监控系统,实时画面》 作者:乐鑫物联网学院作者说明 原始搬运自:https://www.youtube.com/watch?v=PaiDT1t07DU

本人用M5stack timer camera(ESP32)进行复现。 先看一下效果。Blynk+M5timer_camera(esp32 core),易于实现部署的家居监测_哔哩哔哩 (゜-゜)つロ 干杯~-bilibili
M5stack timer camera链接:m5-docs(m5stack.com)基本思路:手机部分采用blynk,blynk里面有一个获取网络图片的组件,M5 camera和blynk app在一个网段内,触发拍摄后,给bkynk server 一个图片链接,app访问并且显示。关于触发拍摄按钮,是在blynk 构建一个按钮,关联gpio14,即可形成一个按钮事件,放置在程序主循环里,当按钮按动时,即触发一个拍摄,把图片链接通过虚拟pinV1 推送给blynk server ,从而由app获取并且显示在手机上。
   一、软硬件硬件:安卓手机一部+ M5timer_camera(esp32core)软件:arduino IDE 1.8.13
      Blynk以及arduino 库二、代码/*************************************************************
Download latest Blynk library here:
    https://github.com/blynkkk/blynk-library/releases/latest

Blynk is a platform with iOS and Android apps to control
Arduino, Raspberry Pi and the likes over the Internet.
You can easily build graphic interfaces for all your
projects by simply dragging and dropping widgets.

    Downloads, docs, tutorials: http://www.blynk.cc
    Sketch generator:         http://examples.blynk.cc
    Blynk community:            http://community.blynk.cc
    Follow us:                  http://www.fb.com/blynkapp
                              http://twitter.com/blynk_app

Blynk library is licensed under MIT license
This example code is in public domain.

*************************************************************
This example runs directly on ESP32 chip.

Note: This requires ESP32 support package:
    https://github.com/espressif/arduino-esp32

Please be sure to select the right ESP32 module
in the Tools -> Board menu!

Change WiFi ssid, pass, and Blynk auth token to run :)
Feel free to apply it to any other example. It's simple!
*************************************************************/
//2021-05-03
//blynk+m5stack timer_cam ,it works
//https://www.bilibili.com/video/BV1Si4y1L7gZ
//https://www.youtube.com/watch?v=PaiDT1t07DU

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#include "esp_camera.h"

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include "camera_pins.h"
String local_IP;
int count = 0;
void startCameraServer();

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
const char* auth = "your auth";


// Your WiFi credentials.
// Set password to "" for open networks.
const char* ssid = "your ssid";
const char* pass = "your pass";
char domain[]="blynk.mixly.org";

#define PHOTO 14      //ESP32 CAM 1 拍摄按钮

#define LED 2//定义led灯,拍摄时闪动
void takePhoto()
{
digitalWrite(LED, HIGH);//led闪动,表明一个拍摄过程
delay(200);
uint32_t randomNum = random(50000);
Serial.println("http://"+local_IP+"/capture?_cb="+ (String)randomNum);
Blynk.setProperty(V1, "urls", "http://"+local_IP+"/capture?_cb="+(String)randomNum); //将cam拍摄的图片通过V1发送至blynk server,从而由blynk图片组件获取后访问并且显示在页面
digitalWrite(LED, LOW);
delay(1000);
}

void setup()
{

Serial.begin(115200);
Serial.setDebugOutput(true);
Serial.println();
pinMode(LED, OUTPUT);
digitalWrite(LED, HIGH);
//cam初始化
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;
config.frame_size = FRAMESIZE_UXGA;
config.jpeg_quality = 4;//default=10
config.fb_count = 2;

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

sensor_t * s = esp_camera_sensor_get();
//initial sensors are flipped vertically and colors are a bit saturated
s->set_vflip(s, 1);//flip it back
s->set_brightness(s, 1);//up the blightness just a bit
s->set_saturation(s, -2);//lower the saturation

//drop down frame size for higher initial frame rate
s->set_framesize(s, FRAMESIZE_QVGA);

//建立wifi连接,主要为了获取local_IP
   WiFi.begin(ssid, pass);

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

startCameraServer();
    Serial.print("Camera Ready! Use 'http://");
Serial.print(WiFi.localIP());

local_IP = WiFi.localIP().toString();//获取local_IP
Serial.println("' to connect");
Blynk.begin(auth, ssid, pass,"Blynk.mixly.org",8080);
}

void loop()
{
// put your main code here, to run repeatedly:

    Blynk.run();
//用户在app上按动拍摄按钮,则触发本事件
if(digitalRead(PHOTO) == HIGH){
    takePhoto();
    }
}
一、后续
后续我会利用一个二自由度云台,加上m5的舵机驱动模块,形成一个简单的可以调整摄像头拍摄角度的家具监测。感谢裘师兄提供blynk.mixly.org服务器。感谢M5提供好玩有趣的极客工具。感谢arduino.cn提供平台。最重要感谢原创者:https://www.youtube.com/watch?v=PaiDT1t07DU
以及乐鑫物联网学院在B站的搬运。
已经立夏,万物勃发。
沧海抱拳。
代码如下:


topdog 发表于 2021-5-14 00:31

谢谢沧海笑1122老师的分享,一起学习一起进步!:)

沧海笑1122 发表于 2021-5-14 10:50

topdog 发表于 2021-5-14 00:31
谢谢沧海笑1122老师的分享,一起学习一起进步!

感谢师兄支持鼓励:handshake

沧海笑1122 发表于 2021-5-31 19:13

本帖最后由 沧海笑1122 于 2021-5-31 19:16 编辑

【0531更新】
通过舵机控制板+二自由度舵机云台实现了简易的家居监测。用户在手机blynk APP上通过滑动棒来改变舵机的水平旋转或者仰角角度,从而对不同的家居目标进行拍摄,在设置好摄像头位置后,按动拍摄按钮,即可从blynk APP上观察到家居目标的拍摄情况。先看一下效果。
https://www.bilibili.com/video/BV1Ef4y1Y75S/
l基本思路:手机部分安装blynk APP,blynk里面有一个获取网络图片的组件,M5 camera和blynk app在一个网段内,用户触发拍摄后,M5Stack Timer Camera将建立一个照片服务,给blynk server发送一个图片链接,blynk APP访问此链接并且显示。触发拍摄按钮,就是在blynk 构建一个按钮,关联M5Stack Timer Camera的gpio14,即可形成一个按钮事件,放置在程序主循环里,当按钮按动时,触发一个拍摄行为,M5StackTimer Camera把图片链接通过虚拟pinV1 推送给blynkserver ,从而由app获取并且显示在手机上。    舵机控制部分的用户界面也在blynk APP上,通过两个slider(滑动棒)组件进行参数设置,很方便拖动设置舵机旋转角度,通过V2和V3下发给仰角以及水平旋转舵机。l核心知识点:1、 blynk的图片组件,可以以虚拟针的方式从M5Stack Timer Camera(内核是esp32)读取照片的链接,展示在手机APP上。2、 M5Stack Timer Camera在摄像头拍摄照片后,实际上建立了一个基于网络的照片服务器,使得其他客户端可以通过各种形式来访问刚刚拍摄的照片。3、 M5Stack Timer Camera与M5StickC之间用json格式来传送舵机控制指令,算是这个小玩具的第三个知识点。下图是Blynk的主要界面以及参数设计: l难度以及有趣指数评估:难度指数:★★有趣指数:★★★☆同样是一个易于上手的小项目,便于加深对M5Stack Timer Camera照片服务以及blynk通信机制的理解。
【制作过程】一、软硬件硬件:

内容参数及版本备注
1安卓手机一部安卓4.44以上含blynk APP
2M5Stack Timer Cameraesp32 core

38舵机控制板含750mah电池M5Stack出品
4M5StickC
舵机控制的执行元件
5二自由度舵机云台9G舵机

软件:

内容参数及版本备注
1arduino IDE 1.8.13

2BlynkAPP 以及arduino 库



连线:M5StickC与8舵机控制板直接插接即可,二自由度舵机云台的水平旋转舵机以及垂直仰角舵机分别接8舵机控制板的CH1和CH2。二自由度舵机云台与摄像头的装配

二、逻辑示意图
三、代码:/*舵机控制的json数据格式
{
"CH": 1,
"value":90
}
s_json="{\"CH\":+str(1)+",\"value\":+str(pinValue1)}"


*/

//编写调试:2021-05-03
//整理于2021-05-26
//blynk+m5stack timer_cam ,it works
//https://www.bilibili.com/video/BV1Si4y1L7gZ
//https://www.youtube.com/watch?v=PaiDT1t07DU

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#include "esp_camera.h"

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include "camera_pins.h"
String local_IP;
int count = 0;
String s_json1;
String s_json2;
#include "HardwareSerial.h"
HardwareSerial MySerial(1);

void startCameraServer();

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
const char* auth = "your auth";

// Your WiFi credentials.
// Set password to "" for open networks.
const char* ssid = "your ssid";
const char* pass = "your psw";
char domain[]="blynk.mixly.org";

#define PHOTO 14      //ESP32 CAM
#define LED 2

void takePhoto()
{
digitalWrite(LED, HIGH); //板载led闪动,表明一次拍摄过程
delay(200);
uint32_t randomNum = random(50000);
Serial.println("http://"+local_IP+"/capture?_cb="+ (String)randomNum);
Blynk.setProperty(V1, "urls", "http://"+local_IP+"/capture?_cb="+(String)randomNum); //将本次拍摄照片的网络链接发送给V1,用于blynk APP展示
digitalWrite(LED, LOW);
delay(1000);
}
//垂直仰角舵机控制CH2---V2
BLYNK_WRITE(V2) {
int pinValue2 = param.asInt(); // assigning incoming value from pin V2 to a variable
// process received value
Serial.print("V2: ");
Serial.println(pinValue2);
//生成一个json串,带有通道号以及旋转角度等参数
s_json2="{\"CH\":"+String(2)+",\"value\":"+String(pinValue2)+"}";
MySerial.println(s_json2);
delay(50);
}

//水平旋转舵机控制CH1---V3
BLYNK_WRITE(V3) {
int pinValue1 = param.asInt(); // assigning incoming value from pin V3 to a variable
// process received value
Serial.print("V3: ");
Serial.println(pinValue1);
   //生成一个json串,带有通道号以及旋转角度等参数
s_json1="{\"CH\":"+String(1)+",\"value\":"+String(pinValue1)+"}";
MySerial.println(s_json1);
delay(50);
}

void setup()
{

Serial.begin(115200);
//自定义硬串口
MySerial.begin(9600, SERIAL_8N1, 13, 4);//rx 13white   tx 4 yellow
Serial.setDebugOutput(true);
Serial.println();
pinMode(LED, OUTPUT);
digitalWrite(LED, HIGH);
//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;
config.frame_size = FRAMESIZE_UXGA;
config.jpeg_quality = 4;//default=10
config.fb_count = 2;

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

sensor_t * s = esp_camera_sensor_get();
//initial sensors are flipped vertically and colors are a bit saturated
s->set_vflip(s, 1);//flip it back
s->set_brightness(s, 1);//up the blightness just a bit
s->set_saturation(s, -2);//lower the saturation

//drop down frame size for higher initial frame rate
s->set_framesize(s, FRAMESIZE_QVGA);

WiFi.begin(ssid, pass);
//wifi连接并且返回一个local_IP,用于传递图片服务器链接
while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");

startCameraServer();
    Serial.print("Camera Ready! Use 'http://");
Serial.print(WiFi.localIP());

local_IP = WiFi.localIP().toString();
Serial.println("' to connect");
//Blynk Server连接
Blynk.begin(auth, ssid, pass,"Blynk.mixly.org",8080);

}

void loop()
{
// put your main code here, to run repeatedly:
    Blynk.run();
//判断是否拍摄按钮在Blynk APP中被按下,photo Pin=gp14
if(digitalRead(PHOTO) == HIGH){
    takePhoto();
    }
}
C端代码
/*
M5StickC端控制程序(来自M5Stack Timer Camera的uart)
M5StickC端舵机控制json数据格式
{
"CH": 1,
"value":90
}
整理:2021-05-26
*/
#include <M5StickC.h>
#include "IIC_servo.h"
#include <ArduinoJson.h>
#include "HardwareSerial.h"
HardwareSerial MySerial(1);
// the setup routine runs once when M5StickC starts up
void setup(){
// Initialize the M5StickC object
M5.begin();
// LCD display
M5.Lcd.println("IIC_servo");
//自定义硬串口
MySerial.begin(9600, SERIAL_8N1, 32, 33);//rx 32 yellow   tx33 white
IIC_Servo_Init();   //sda0   scl26
M5.Lcd.println("begin :");

}

// the loop routine runs over and over again forever
void loop() {
while (!MySerial.available())
   delay(100);
// Deserialize the JSON document
const size_t capacity = JSON_OBJECT_SIZE(4) + 50;//定义来自arduinojson.org的助手生成
DynamicJsonDocument doc(capacity);
DeserializationError error = deserializeJson(doc, MySerial); //解析来自MySerial的数据流
// Test if parsing succeeds.
if (error) {
    Serial.print(F("deserializeJson() failed: "));
    Serial.println(error.c_str());
    return;
}
int i_ch=doc["CH"]; //解析第X(1或2)路舵机
int i_value=doc["value"]; //解析角度值
Servo_angle_set(i_ch,i_value);    //执行,对CHX设定角度value°
Serial.print(i_ch);
Serial.print("*");
Serial.println(i_value);
delay(100);
}

一、后记Blynk+M5Stack Timer Camera+M5StickC以及一个二自由度云台(当然少不了M5Stack的舵机驱动模块),就形成一个简单的可以调整摄像头拍摄角度的家具监测。感谢裘师兄提供blynk.mixly.org服务器。最重要感谢原创者:https://www.youtube.com/watch?v=PaiDT1t07DU
以及乐鑫物联网学院在B站的搬运。
最好人生是小满,花未全开月未圆。沧海抱拳。
页: [1]
查看完整版本: 【0531更新教程】Blynk+M5timer_camera,易于实现部署的家居检测