前言
這里就是介紹一下我使用ESP32-CAN的使用心得,并且附帶使用流程,環(huán)境搭建以及代碼供大家使用和查看,希望對(duì)大家有所幫助咯~~
一、環(huán)境配置
我們開發(fā)的每款芯片都離不開環(huán)境的配置,工欲善其事,必先利其器啦,這里我介紹使用兩種方式去開發(fā)我們的ESP32系列
Arduino IDE
Arduino的環(huán)境我之前有提到和配置過,這里我就直接附上連接,安裝教程配置即可
Arduino IDE 使用安裝以及ESP32庫的導(dǎo)入(離線)https://blog.csdn.net/herui_2/article/details/135296814?spm=1001.2014.3001.5501
PlatformIO IDE
PlatformIO IDE是使用到了 VScode 的插件功能,首先我們需要去下載 VScode的編譯器,然后使用他的插件功能去下載
可以參考下面教程
VSCode 使用 Keil5 插件推薦 附帶Keil5安裝教程https://blog.csdn.net/herui_2/article/details/135296814?spm=1001.2014.3001.5501
如圖,搜索 PlatformIO 點(diǎn)擊VScode的插件部分即可
?安裝完成界面
二、資料獲取
關(guān)注微信公眾號(hào)--星之援工作室 發(fā)送關(guān)鍵字(ESP32-CAN)
????
?資料說明
?Arduino和PlatformIO使用到的代碼是一致的,只是打開的方式不同,命名及為項(xiàng)目
三、代碼展示
接下來以Arduino為例演示,使用到了一些外部配置文件,都放到了我們的項(xiàng)目文件夾下了
選擇開發(fā)芯片
編譯通過
上傳代碼
首先需要進(jìn)入下載模式
?選擇好連接的端口以后直接上傳即可
?本代碼涵蓋了視頻流數(shù)據(jù)以及串口轉(zhuǎn)發(fā),可以直接與STM32進(jìn)行通信
#include "esp_camera.h"
#include <WiFi.h>
// Select camera model
// #define CAMERA_MODEL_WROVER_KIT
// #define CAMERA_MODEL_ESP_EYE
// #define CAMERA_MODEL_M5STACK_PSRAM
// #define CAMERA_MODEL_M5STACK_WIDE
#define CAMERA_MODEL_AI_THINKER
#include "camera_pins.h"
const char *ssid = "WIFI_For";
const char *password = "12345678";
const IPAddress ip(192, 168, 4, 1);
const IPAddress gateway(192, 168, 4, 1);
const IPAddress subnet(255, 255, 255, 0);
void cameraBegin(); // 相機(jī)初始化
int startCameraServer(); // 啟動(dòng)相機(jī)服務(wù)
void setWifi();
WiFiServer server(8000); // 申明AP對(duì)象
void setup()
{
Serial.begin(115200);
Serial.setDebugOutput(true);
Serial.println("開機(jī)");
pinMode(4, OUTPUT);
digitalWrite(4, HIGH); // 閃光燈
cameraBegin();
setWifi(); // 設(shè)置wifi
int prot = startCameraServer(); // 開啟相機(jī)服務(wù)并得到服務(wù)端口
IPAddress myAddress = WiFi.localIP();
Serial.println(myAddress);
Serial.print("請(qǐng)?jiān)跒g覽器中輸入:");
Serial.print(WiFi.softAPIP());
delay(1000);
digitalWrite(4, LOW); // 閃光燈
}
void loop()
{
String msgs = "", onDeng = "";
WiFiClient client = server.available();
boolean receiveFlag = false; // 是否接到消息標(biāo)志位
if (client)
{
while (client.connected())
{
while (Serial.available() > 0)
{
char c = Serial.read();
receiveFlag = true; // 接到來自小車的串口消息
msgs += c;
}
while (client.available() > 0)
{
char c = client.read();
Serial.write(c); // 向串口發(fā)送消息
onDeng += c;
}
if (onDeng == "DENG_ON")
{
digitalWrite(4, HIGH); // 閃光燈
}
else if (onDeng == "DENG_OFF")
{
digitalWrite(4, LOW); // 閃光燈
}
if (receiveFlag)
{
receiveFlag = false;
client.write(&msgs[0]); // 向客戶端發(fā)送消息
msgs = "";
}
onDeng = "";
}
client.stop();
}
else
delay(500);
}
/**
* @brief 配置wifi
*/
void setWifi()
{
WiFi.mode(WIFI_AP); // 設(shè)置wifi模
WiFi.softAPConfig(ip, gateway, subnet); // 配置網(wǎng)絡(luò)信息
WiFi.softAP(ssid, password); // 配置wifi名稱及密碼
Serial.println("開啟網(wǎng)絡(luò)....等待連接.....");
delay(500);
server.begin();
delay(500);
}
/**
* @brief 相機(jī)初始化
*
*/
void cameraBegin()
{
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;
// 用高規(guī)格初始化來預(yù)分配更大的緩沖區(qū)
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;
}
#if defined(CAMERA_MODEL_ESP_EYE)
pinMode(13, INPUT_PULLUP);
pinMode(14, INPUT_PULLUP);
#endif
// 相機(jī)初始化
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();
// 最初的傳感器垂直翻轉(zhuǎn),顏色有點(diǎn)飽和
if (s->id.PID == OV3660_PID)
{
s->set_vflip(s, 0); // 反轉(zhuǎn)
s->set_brightness(s, 1); // 將暗處調(diào)高
s->set_saturation(s, -2); // 降低飽和度
}
// drop down frame size for higher initial frame rate
s->set_framesize(s, FRAMESIZE_QVGA);
#if defined(CAMERA_MODEL_M5STACK_WIDE)
s->set_vflip(s, 1);
s->set_hmirror(s, 1);
#endif
}
四、相關(guān)項(xiàng)目與資料
一篇文章足夠你學(xué)習(xí)ESP32,提供史上最全的ESP32教程https://blog.csdn.net/XiaoXiaoPengBo/article/details/123952643?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522170718720216800182123332%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=170718720216800182123332&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~top_positive~default-2-123952643-null-null.nonecase&utm_term=ESP32&spm=1018.2226.3001.4450
物聯(lián)網(wǎng)畢設(shè) -- 圖像小車(局域網(wǎng)+環(huán)境檢測(cè)+GPS)https://blog.csdn.net/herui_2/article/details/135073514物聯(lián)網(wǎng)畢設(shè) -- 養(yǎng)殖場(chǎng)的智能環(huán)境監(jiān)測(cè)(STM32+WiFi+局域圖傳)https://blog.csdn.net/herui_2/article/details/135671916?spm=1001.2014.3001.5501
聯(lián)系方式 微信號(hào):13648103287