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

vany5921 发表于 2019-10-30 14:03

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

本帖最后由 vany5921 于 2019-10-30 21:32 编辑

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

      M5StickV的Grove口为34,35,之前的案例多次提到端口配置,不再赘述
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)

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


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

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

2.数据收发


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

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

static const uint8_t packet_begin = { 0xFF, 0xD8, 0xEA };

if (serial_ext.available()) {
    uint8_t rx_buffer;
    int rx_size = serial_ext.readBytes(rx_buffer, 10);
    if (rx_size == 10) {   //packet receive of packet_begin
      if ((rx_buffer == packet_begin) && (rx_buffer == packet_begin) && (rx_buffer == packet_begin)) {
      //image size receive of packet_begin
      jpeg_data.length = (uint32_t)(rx_buffer << 16) | (rx_buffer << 8) | rx_buffer;
      int rx_size = serial_ext.readBytes(jpeg_data.buf, jpeg_data.length);
      }
    }
}


3.完整代码


=================M5StickV代码==============
## 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()
      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")

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

#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 = { 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;
    int rx_size = serial_ext.readBytes(rx_buffer, 10);
    if (rx_size == 10) {   //packet receive of packet_begin
      if ((rx_buffer == packet_begin) && (rx_buffer == packet_begin) && (rx_buffer == packet_begin)) {
      //image size receive of packet_begin
      jpeg_data.length = (uint32_t)(rx_buffer << 16) | (rx_buffer << 8) | rx_buffer;
      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());
}



页: [1]
查看完整版本: 【搬运】M5StickV串口通讯——图像发送