STM32F407驱动ov2640摄像头采集图像,通过esp8266(串口协议)上传服务器显示, 和通过简单串口发送到上位机显示的示例代码
时间: 2023-06-20 08:07:09 浏览: 157
通过自定义串口通信协议实现STM32采集的图像发送到上位机查看
5星 · 资源好评率100%
以下是基于STM32F407驱动OV2640摄像头采集图像并通过ESP8266上传服务器显示的示例代码:
```c
#include "stm32f4xx.h"
#include "stm32f4xx_conf.h"
#include "ov2640.h"
#include "esp8266.h"
#include <string.h>
#define OV2640_ADDR 0x60
#define ESP8266_UART USART3
uint8_t ov2640_buffer[OV2640_BUFFER_SIZE];
uint8_t esp8266_buffer[ESP8266_BUFFER_SIZE];
void OV2640_Init(void)
{
OV2640_HW_Init();
OV2640_InitRegisters();
OV2640_SetRegisters();
OV2640_SetupImageFormat(OV2640_JPEG_MODE);
OV2640_SetupImageSize(OV2640_640x480);
}
void ESP8266_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_USART3);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_USART3);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
USART_Init(ESP8266_UART, &USART_InitStructure);
USART_Cmd(ESP8266_UART, ENABLE);
}
void ESP8266_SendImage(uint8_t* buffer, uint32_t size)
{
ESP8266_SendCommand("AT+CIPSTART=\"TCP\",\"SERVER_IP\",SERVER_PORT\r\n");
while (ESP8266_WaitForResponse(esp8266_buffer, "OK", 1000) == 0);
char* header = "POST /upload HTTP/1.1\r\n"
"Host: SERVER_IP:SERVER_PORT\r\n"
"Content-Type: image/jpeg\r\n"
"Content-Length: %d\r\n"
"\r\n";
char header_buffer[128];
sprintf(header_buffer, header, size);
USART_SendData(ESP8266_UART, (uint16_t)'\r');
while (USART_GetFlagStatus(ESP8266_UART, USART_FLAG_TXE) == RESET);
USART_SendData(ESP8266_UART, (uint16_t)'\n');
while (USART_GetFlagStatus(ESP8266_UART, USART_FLAG_TXE) == RESET);
USART_SendData(ESP8266_UART, (uint16_t)'A');
while (USART_GetFlagStatus(ESP8266_UART, USART_FLAG_TXE) == RESET);
USART_SendData(ESP8266_UART, (uint16_t)'T');
while (USART_GetFlagStatus(ESP8266_UART, USART_FLAG_TXE) == RESET);
USART_SendData(ESP8266_UART, (uint16_t)'+');
while (USART_GetFlagStatus(ESP8266_UART, USART_FLAG_TXE) == RESET);
USART_SendData(ESP8266_UART, (uint16_t)'C');
while (USART_GetFlagStatus(ESP8266_UART, USART_FLAG_TXE) == RESET);
USART_SendData(ESP8266_UART, (uint16_t)'I');
while (USART_GetFlagStatus(ESP8266_UART, USART_FLAG_TXE) == RESET);
USART_SendData(ESP8266_UART, (uint16_t)'P');
while (USART_GetFlagStatus(ESP8266_UART, USART_FLAG_TXE) == RESET);
USART_SendData(ESP8266_UART, (uint16_t)'S');
while (USART_GetFlagStatus(ESP8266_UART, USART_FLAG_TXE) == RESET);
USART_SendData(ESP8266_UART, (uint16_t)'E');
while (USART_GetFlagStatus(ESP8266_UART, USART_FLAG_TXE) == RESET);
USART_SendData(ESP8266_UART, (uint16_t)'N');
while (USART_GetFlagStatus(ESP8266_UART, USART_FLAG_TXE) == RESET);
USART_SendData(ESP8266_UART, (uint16_t)'D');
while (USART_GetFlagStatus(ESP8266_UART, USART_FLAG_TXE) == RESET);
USART_SendData(ESP8266_UART, (uint16_t)'=');
while (USART_GetFlagStatus(ESP8266_UART, USART_FLAG_TXE) == RESET);
USART_SendData(ESP8266_UART, (uint16_t)'"');
while (USART_GetFlagStatus(ESP8266_UART, USART_FLAG_TXE) == RESET);
USART_SendData(ESP8266_UART, (uint16_t)'T');
while (USART_GetFlagStatus(ESP8266_UART, USART_FLAG_TXE) == RESET);
USART_SendData(ESP8266_UART, (uint16_t)'C');
while (USART_GetFlagStatus(ESP8266_UART, USART_FLAG_TXE) == RESET);
USART_SendData(ESP8266_UART, (uint16_t)'P');
while (USART_GetFlagStatus(ESP8266_UART, USART_FLAG_TXE) == RESET);
USART_SendData(ESP8266_UART, (uint16_t)'"');
while (USART_GetFlagStatus(ESP8266_UART, USART_FLAG_TXE) == RESET);
USART_SendData(ESP8266_UART, (uint16_t)'\r');
while (USART_GetFlagStatus(ESP8266_UART, USART_FLAG_TXE) == RESET);
USART_SendData(ESP8266_UART, (uint16_t)'\n');
while (USART_GetFlagStatus(ESP8266_UART, USART_FLAG_TXE) == RESET);
USART_SendData(ESP8266_UART, (uint16_t)'C');
while (USART_GetFlagStatus(ESP8266_UART, USART_FLAG_TXE) == RESET);
USART_SendData(ESP8266_UART, (uint16_t)'O');
while (USART_GetFlagStatus(ESP8266_UART, USART_FLAG_TXE) == RESET);
USART_SendData(ESP8266_UART, (uint16_t)'N');
while (USART_GetFlagStatus(ESP8266_UART, USART_FLAG_TXE) == RESET);
USART_SendData(ESP8266_UART, (uint16_t)'T');
while (USART_GetFlagStatus(ESP8266_UART, USART_FLAG_TXE) == RESET);
USART_SendData(ESP8266_UART, (uint16_t)'E');
while (USART_GetFlagStatus(ESP8266_UART, USART_FLAG_TXE) == RESET);
USART_SendData(ESP8266_UART, (uint16_t)'N');
while (USART_GetFlagStatus(ESP8266_UART, USART_FLAG_TXE) == RESET);
USART_SendData(ESP8266_UART, (uint16_t)'T');
while (USART_GetFlagStatus(ESP8266_UART, USART_FLAG_TXE) == RESET);
USART_SendData(ESP8266_UART, (uint16_t)'-');
while (USART_GetFlagStatus(ESP8266_UART, USART_FLAG_TXE) == RESET);
USART_SendData(ESP8266_UART, (uint16_t)'L');
while (USART_GetFlagStatus(ESP8266_UART, USART_FLAG_TXE) == RESET);
USART_SendData(ESP8266_UART, (uint16_t)'E');
while (USART_GetFlagStatus(ESP8266_UART, USART_FLAG_TXE) == RESET);
USART_SendData(ESP8266_UART, (uint16_t)'N');
while (USART_GetFlagStatus(ESP8266_UART, USART_FLAG_TXE) == RESET);
USART_SendData(ESP8266_UART, (uint16_t)'G');
while (USART_GetFlagStatus(ESP8266_UART, USART_FLAG_TXE) == RESET);
USART_SendData(ESP8266_UART, (uint16_t)'T');
while (USART_GetFlagStatus(ESP8266_UART, USART_FLAG_TXE) == RESET);
USART_SendData(ESP8266_UART, (uint16_t)'H');
while (USART_GetFlagStatus(ESP8266_UART, USART_FLAG_TXE) == RESET);
USART_SendData(ESP8266_UART, (uint16_t)':');
while (USART_GetFlagStatus(ESP8266_UART, USART_FLAG_TXE) == RESET);
for (int i = 0; i < strlen(header_buffer); i++)
{
USART_SendData(ESP8266_UART, (uint16_t)header_buffer[i]);
while (USART_GetFlagStatus(ESP8266_UART, USART_FLAG_TXE) == RESET);
}
for (int i = 0; i < size; i++)
{
USART_SendData(ESP8266_UART, (uint16_t)buffer[i]);
while (USART_GetFlagStatus(ESP8266_UART, USART_FLAG_TXE) == RESET);
}
ESP8266_SendCommand("AT+CIPCLOSE\r\n");
}
int main(void)
{
OV2640_Init();
ESP8266_Init();
while (1)
{
OV2640_ReadImage(ov2640_buffer);
ESP8266_SendImage(ov2640_buffer, OV2640_BUFFER_SIZE);
}
}
```
在上述代码中,需要将 `SERVER_IP` 和 `SERVER_PORT` 替换为目标服务器的 IP 地址和端口号。
以下是通过简单串口发送到上位机显示的示例代码:
```c
#include "stm32f4xx.h"
#include "stm32f4xx_conf.h"
#include "ov2640.h"
#include <stdlib.h>
#include <stdio.h>
#define OV2640_ADDR 0x60
uint8_t ov2640_buffer[OV2640_BUFFER_SIZE];
void OV2640_Init(void)
{
OV2640_HW_Init();
OV2640_InitRegisters();
OV2640_SetRegisters();
OV2640_SetupImageFormat(OV2640_JPEG_MODE);
OV2640_SetupImageSize(OV2640_640x480);
}
int main(void)
{
OV2640_Init();
while (1)
{
OV2640_ReadImage(ov2640_buffer);
printf("Image size: %dx%d\r\n", OV2640_IMAGE_WIDTH, OV2640_IMAGE_HEIGHT);
printf("JPEG size: %d bytes\r\n", OV2640_BUFFER_SIZE);
printf("Press any key to continue...\r\n");
getchar();
}
}
```
在上述代码中,使用 `printf()` 函数将 JPEG 图像大小和分辨率输出到串口。需要注意的是,在使用 `printf()` 函数前需要先初始化串口。
阅读全文