【搬运】M5StickV串口通讯——图像发送-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 2854|回复: 0

【搬运】M5StickV串口通讯——图像发送

[复制链接]
发表于 2019-10-30 14:03 | 显示全部楼层 |阅读模式
本帖最后由 vany5921 于 2019-10-30 21:32 编辑

      本案例结合M5StickV与M5StickC实现拍照并将图片通过WIFI发送至Line客户端(参考链接:https://qiita.com/nnn112358/items/5efd926fea20cd6c2c43
1.端口设置
================M5StickV部分的配置==================

        M5StickV的Grove口为34,35,之前的案例多次提到端口配置,不再赘述
[mw_shl_code=python,true]from fpioa_manager import fm
from machine import UART
fm.register(35, fm.fpioa.UART2_TX, force=True)
fm.register(34, fm.fpioa.UART2_RX, force=True)[/mw_shl_code]

        配置串口数据通讯的基本信息
[mw_shl_code=python,true]uart_Port = UART(UART.UART2, 115200,8,0,0, timeout=1000, read_buf_len= 4096)
data_packet = bytearray([0x00,0x00,0x00,0x00,0x00])
uart_Port.write(data_packet )[/mw_shl_code]
=================M5StickC部分的配置================


ESP32一共有三个串口,其中一个被USB占用,M5StickC的grove接口为32,33,所以将这两个端口配置为硬件串口2
[mw_shl_code=arduino,true]HardwareSerial serial_ext(2);
serial_ext.begin(115200, SERIAL_8N1, 32, 33);[/mw_shl_code]

如果串口接受到数据读取10个字节
[mw_shl_code=arduino,true]if (serial_ext.available()) {
uint8_t rx_buffer[10];
int rx_size = serial_ext.readBytes(rx_buffer, 10);
}[/mw_shl_code]

2.数据收发


=================M5StickV串口发送==============
通过UART通信发送大数据时,为了知道数据包的起始位置,必须要加上数据头加上标识。对于JPG格式来说标识为FF、D8,我们用0xFF,0xD8、0xEA标识数据包data_packet,添加到要发送的数据前,以便它可以检测数据的起始位置。图像数据大小存储在识别包中。由于图像数据通常比较大,不适合用1个字节来表示,因此将其分为3个字节。
[mw_shl_code=python,true]img_size1 = (img.size()& 0xFF0000)>>16
img_size2 = (img.size()& 0x00FF00)>>8
img_size3 = (img.size()& 0x0000FF)>>0
data_packet = bytearray([0xFF,0xD8,0xEA,0x01,img_size1,img_size2,img_size3,0x00,0x00,0x00])
uart_Port.write(data_packet)
uart_Port.write(img_buf)[/mw_shl_code]

===================M5StickC串口接收==============
当M5StickC接收到数据时首先要判断数据头标识,然后根据数据大小进行解码

[mw_shl_code=arduino,true]static const uint8_t packet_begin[3] = { 0xFF, 0xD8, 0xEA };

if (serial_ext.available()) {
    uint8_t rx_buffer[10];
    int rx_size = serial_ext.readBytes(rx_buffer, 10);
    if (rx_size == 10) {   //packet receive of packet_begin
      if ((rx_buffer[0] == packet_begin[0]) && (rx_buffer[1] == packet_begin[1]) && (rx_buffer[2] == packet_begin[2])) {
        //image size receive of packet_begin
        jpeg_data.length = (uint32_t)(rx_buffer[4] << 16) | (rx_buffer[5] << 8) | rx_buffer[6];
        int rx_size = serial_ext.readBytes(jpeg_data.buf, jpeg_data.length);
      }
    }
  }[/mw_shl_code]


3.完整代码


=================M5StickV代码==============
[mw_shl_code=python,true]## This Source is M5StickV MaixPy
import network, socket, time, sensor, image,lcd
from Maix import GPIO
from fpioa_manager import fm, board_info
from machine import UART

#M5StickV Camera Start
clock = time.clock()
lcd.init()
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
lcd.rotation(2)
sensor.run(1)
sensor.skip_frames(time = 2000)
#M5StickV GPIO_UART
fm.register(35, fm.fpioa.UART2_TX, force=True)
fm.register(34, fm.fpioa.UART2_RX, force=True)
uart_Port = UART(UART.UART2, 115200,8,0,0, timeout=1000, read_buf_len= 4096)
#M5StickV ButtonA
but_a=GPIO(GPIO.GPIO1, GPIO.IN, GPIO.PULL_UP)
while True:
    clock.tick()
    img = sensor.snapshot()
    lcd.display(img)
#   IF Button A Push Then Image Send UART
    if but_a.value() == 0:
        img_buf = img.compress(quality=70)
        img_size1 = (img.size()& 0xFF0000)>>16
        img_size2 = (img.size()& 0x00FF00)>>8
        img_size3 = (img.size()& 0x0000FF)>>0
        data_packet = bytearray([0xFF,0xD8,0xEA,0x01,img_size1,img_size2,img_size3,0x00,0x00,0x00])
        uart_Port.write(data_packet)
        uart_Port.write(img_buf)
        time.sleep(1.0)
#   Send UART End
uart_Port.deinit()
del uart_Port
print("finish")[/mw_shl_code]

=================M5StickC代码=================

[mw_shl_code=arduino,true]#include <M5StickC.h>
#include <WiFi.h>
#include <ssl_client.h>
#include <WiFiClientSecure.h>
const char* ssid = "your_ssid";
const char* passwd = "your_passwd";

HardwareSerial serial_ext(2);

typedef struct {
  uint32_t length;
  uint8_t *buf;
} jpeg_data_t;

jpeg_data_t jpeg_data;
static const int RX_BUF_SIZE = 20000;
static const uint8_t packet_begin[3] = { 0xFF, 0xD8, 0xEA };

/*
sendLineNotify参考链接。
https://github.com/anoken/purin_wo_mimamoru_gijutsu/blob/master/2_6_M5Camera_Send_LineNotify/2_6_M5Camera_Send_LineNotify.ino
*/
void sendLineNotify(uint8_t* image_data, size_t image_sz);

void setup() {
  M5.begin();
  M5.Lcd.setRotation(3);
  M5.Lcd.setCursor(0, 30, 4);
  M5.Lcd.println("m5stick_uart_wifi_converter");

  setup_wifi();

  jpeg_data.buf = (uint8_t *) malloc(sizeof(uint8_t) * RX_BUF_SIZE);
  jpeg_data.length = 0;
  if (jpeg_data.buf == NULL) {
    Serial.println("malloc jpeg buffer 1 error");
  }

  serial_ext.begin(115200, SERIAL_8N1, 32, 33);
}

void loop() {
  M5.update();

  if (serial_ext.available()) {
    uint8_t rx_buffer[10];
    int rx_size = serial_ext.readBytes(rx_buffer, 10);
    if (rx_size == 10) {   //packet receive of packet_begin
      if ((rx_buffer[0] == packet_begin[0]) && (rx_buffer[1] == packet_begin[1]) && (rx_buffer[2] == packet_begin[2])) {
        //image size receive of packet_begin
        jpeg_data.length = (uint32_t)(rx_buffer[4] << 16) | (rx_buffer[5] << 8) | rx_buffer[6];
        int rx_size = serial_ext.readBytes(jpeg_data.buf, jpeg_data.length);

        //image processing, for example, line notify send image
        sendLineNotify(jpeg_data.buf, jpeg_data.length);
        //image processing end
      }
    }
  }
  vTaskDelay(10 / portTICK_RATE_MS);
}

void setup_wifi() {
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, passwd);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}
[/mw_shl_code]


您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|Archiver|手机版|Arduino中文社区

GMT+8, 2024-12-29 01:00 , Processed in 0.073125 second(s), 15 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表