- 2024-10-30
-
加入了学习《【Follow me第二季第2期】全任务教程》,观看 【Follow me第二季第2期】全任务教程
- 2024-10-04
-
发表了主题帖:
【Follow me第二季第2期】全任务提交 & 经验分享
本帖最后由 Vaintory 于 2024-11-1 19:20 编辑
# Follow me 第二季第2期
与得捷一起解锁开发板超能力!
> 使用到的元器件:
> - [Arduino UNO R4 WiFi](https://docs.arduino.cc/hardware/uno-r4-wifi/)
> - Adafruit SHT40
## 入门任务
> 1. 搭建环境
> 1. Blink
> 1. 串口打印Hello EEWorld!
1. 在VSCode中安装`PlatformIO`插件
2. 在`PIO Home`中创建新的工程
3. 参考[官方例程](https://docs.arduino.cc/tutorials/uno-r4-wifi/r4-wifi-getting-started/)编写代码
---
**流程图**
---
**完整代码**
```cpp
#include
// the setup routine runs once when you press reset:
void setup() {
Serial.begin(9600);
// initialize the digital pin as an output.
pinMode(LED_BUILTIN, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop() {
Serial.println("Hello EEWorld!");
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off
delay(1000); // wait for a second
}
```
## 基础任务
> 1. 驱动12x8点阵LED
> 1. 用DAC生成正弦波
> 1. 用OPAMP放大DAC信号
> 1. 用ADC采集并且打印数据到串口等其他接口可上传到上位机显示曲线
### LED Matrix
Arduino UNO R4 WiFi配有内置 12x8 LED 矩阵,可对其进行编程以显示图形、动画、充当界面,甚至玩游戏。
1. 引入头文件
```cpp
#include "Arduino_LED_Matrix.h"
```
1. 创建 LED 矩阵对象
```cpp
ArduinoLEDMatrix matrix;
```
1. 启动 LED 矩阵
```cpp
matrix.begin();
```
LED矩阵屏幕驱动应具有如下结构
```cpp
#include "Arduino_LED_Matrix.h"
ArduinoLEDMatrix matrix;
void setup() {
Serial.begin(115200);
matrix.begin();
}
```
---
下面介绍如何显示滚动文字。LED 矩阵现在支持通过`ArduinoGraphics`库打印字符,该库支持:
- 通过`matrix.beginText(x,y, 0xFFFFFF)`设置文本的起始位置, “0xFFFFFF”代表默认颜色(红色)。
- 通过`matrix.printText("This message is printed")`打印文本
- 结束打印并(可选)指定滚动方向`matrix.endText(direction)`
- 支持`SCROLL_LEFT`和`SCROLL_RIGHT`。如果不需要滚动,请留空。
---
**流程图**
---
**完整代码**
```cpp
// To use ArduinoGraphics APIs, please include BEFORE Arduino_LED_Matrix
#include "ArduinoGraphics.h"
#include "Arduino_LED_Matrix.h"
ArduinoLEDMatrix matrix;
void setup() {
Serial.begin(115200);
matrix.begin();
matrix.beginDraw();
matrix.stroke(0xFFFFFFFF);
// add some static text
// will only show "UNO" (not enough space on the display)
const char text[] = "UNO r4";
matrix.textFont(Font_4x6);
matrix.beginText(0, 1, 0xFFFFFF);
matrix.println(text);
matrix.endText();
matrix.endDraw();
delay(2000);
}
void loop() {
// Make it scroll!
matrix.beginDraw();
matrix.stroke(0xFFFFFFFF);
matrix.textScrollSpeed(100);
// add the text
const char text[] = " Hello EEWorld! ";
matrix.textFont(Font_5x7);
matrix.beginText(0, 1, 0xFFFFFF);
matrix.println(text);
matrix.endText(SCROLL_LEFT);
matrix.endDraw();
}
```
> 注意:`ArduinoGraphics`需要在PIO Home中安装!
### DAC & ADC
> 官方分别提供了`DAC`和`ADC`的官方文档:
> - [DAC参考文档](https://docs.arduino.cc/tutorials/> uno-r4-wifi/dac)
> - [ADC参考文档](https://docs.arduino.cc/tutorials/uno-r4-wifi/adc-resolution/)
Arduino UNO R4 WiFi 具有内置DAC (数模转换器),用于将数字信号转换为模拟信号。此功能可用于构建大量有趣的音频项目,但也可用作专业实验室设备,例如廉价的函数发生器。
对于许多需要模拟输出的用例,使用 PWM(脉冲宽度调制)而不是真正的模拟输出将产生基本相同的结果。数字输出引脚只能完全打开(高)或完全关闭(低),但通过精确定时快速打开和关闭,可以控制平均电压并模拟模拟输出。这种方法称为脉宽调制。
例如,在对 LED 进行调光时,您可以随意使用启用 PWM 的数字引脚作为模拟输出引脚,并且 LED 的调光效果与您使用 DAC 输出时的调光效果相同。
然而情况并非总是如此,对于许多用途,您将需要使用真正的模拟输出才能获得所需的结果。一个这样的情况是在音频用途中,其中 PWM 输出根本无法提供与真正的模拟输出相同的音质,并且首先需要一些调整才能工作。
---
模数转换器 (ADC) 将模拟信号转换为数字信号。 Arduino 板上的标准分辨率设置为 10 位 (0-1023)。 UNO R4 WiFi 支持高达 14 位分辨率,可以从模拟信号中提供更精确的值。
---
**流程图**
---
**完整代码**
```cpp
#include "analogWave.h" // Include the library for analog waveform generation
analogWave wave(DAC); // 使用DAC引脚, 创建analogWave类实例
int freq = 1; // in hertz, change accordingly
int reading; // 保存读取的ADC值
void setup() {
Serial.begin(115200); // 初始化串口, 波特率为115200
analogReadResolution(14); // change to 14-bit resolution
wave.sine(freq); // Generate a sine wave with the initial frequency
}
void loop() {
reading = analogRead(A3); // returns a value between 0-16383
Serial.print(">sin:");
Serial.println(reading);
delay(10); // Delay for one second before repeating
}
```
## 进阶任务
通过Wi-Fi,利用MQTT协议接入到开源的智能家居平台HA(HomeAssistant)
### 通过WiFi连接网络
[官方参考文档](https://docs.arduino.cc/tutorials/uno-r4-wifi/r4-wifi-getting-started/)
Arduino UNO R4 WiFi具有内置 ESP32-S3 模块,使您能够连接到 Wi-Fi® 网络并执行网络操作。包括 HTTPS、MQTT、UDP 在内的协议都经过测试和支持。
Wi-Fi® 支持通过内置启用WiFiS3 Arduino UNO R4 板包附带的库。安装板包会自动安装WiFiS3库。
> 将开发板连接到互联网的最简单方法是通过Arduino 云平台。在这里您可以配置、编程、监控和同步您的设备,而无需编写任何网络代码。
作为标准做法,保护隐私起见,我们将`SSID`和`PASSWORD`存储在一个单独的头文件中(称为`arduino_secrets.h`)。
```cpp
// arduino_secrets.h header file
#define SECRET_SSID "yournetwork"
#define SECRET_PASS "yourpassword"
```
> 将您的网络和密码存储在单独的文件中可以最大限度地降低您意外共享 Wi-Fi® 凭据的风险。
Arduino官方提供了使用WPA连接的例程:
```cpp
/*
This example connects to an unencrypted WiFi network.
Then it prints the MAC address of the WiFi module,
the IP address obtained, and other network details.
created 13 July 2010
by dlf (Metodo2 srl)
modified 31 May 2012
by Tom Igoe
Find the full UNO R4 WiFi Network documentation here:
https://docs.arduino.cc/tutorials/uno-r4-wifi/wifi-examples#connect-with-wpa
*/
#include
#include "arduino_secrets.h"
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
int status = WL_IDLE_STATUS; // the WiFi radio's status
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// check for the WiFi module:
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Communication with WiFi module failed!");
// don't continue
while (true);
}
String fv = WiFi.firmwareVersion();
if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
Serial.println("Please upgrade the firmware");
}
// attempt to connect to WiFi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
// you're connected now, so print out the data:
Serial.print("You're connected to the network");
printCurrentNet();
printWifiData();
}
void loop() {
// check the network connection once every 10 seconds:
delay(10000);
printCurrentNet();
}
void printWifiData() {
// print your board's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print your MAC address:
byte mac[6];
WiFi.macAddress(mac);
Serial.print("MAC address: ");
printMacAddress(mac);
}
void printCurrentNet() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print the MAC address of the router you're attached to:
byte bssid[6];
WiFi.BSSID(bssid);
Serial.print("BSSID: ");
printMacAddress(bssid);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.println(rssi);
// print the encryption type:
byte encryption = WiFi.encryptionType();
Serial.print("Encryption Type:");
Serial.println(encryption, HEX);
Serial.println();
}
void printMacAddress(byte mac[]) {
for (int i = 0; i < 6; i++) {
if (i > 0) {
Serial.print(":");
}
if (mac < 16) {
Serial.print("0");
}
Serial.print(mac, HEX);
}
Serial.println();
}
```
---
**流程图**
### Home Assistant
为了将Arduino设备接入Home Assistant平台,我们需要`
arduino-home-assistant`库([github🔗](https://github.com/dawidchyrzynski/arduino-home-assistant))。ArduinoHA 允许使用 MQTT 将基于 Arduino/ESP 的设备与 Home Assistant 集成。该库旨在使用尽可能少的资源(RAM/闪存)。
其具有如下特性:
- Two-way communication (state reporting and command - execution)
- MQTT discovery (device is added to the Home Assistant - panel automatically)
- MQTT Last Will and Testament
- Support for custom MQTT messages (publishing and - subscribing)
- Auto reconnect with MQTT broker
- Reporting availability (online/offline states) of a device
- Doxygen documentation for all classes
- Covered by unit tests (AUnit + EpoxyDuino + AUniter)
依赖库作者提供的`Button`示例代码如下:
```cpp
#include
#include
#define BROKER_ADDR IPAddress(192,168,0,17)
byte mac[] = {0x00, 0x10, 0xFA, 0x6E, 0x38, 0x4A};
EthernetClient client;
HADevice device(mac, sizeof(mac));
HAMqtt mqtt(client, device);
// "myButtonA" and "myButtonB" are unique IDs of buttons. You should define you own ID.
HAButton buttonA("myButtonA");
HAButton buttonB("myButtonB");
void onButtonCommand(HAButton* sender)
{
if (sender == &buttonA) {
// button A was clicked, do your logic here
} else if (sender == &buttonB) {
// button B was clicked, do your logic here
}
}
void setup() {
// you don't need to verify return status
Ethernet.begin(mac);
// optional device's details
device.setName("Arduino");
device.setSoftwareVersion("1.0.0");
// optional properties
buttonA.setIcon("mdi:fire");
buttonA.setName("Click me A");
buttonB.setIcon("mdi:home");
buttonB.setName("Click me B");
// press callbacks
buttonA.onCommand(onButtonCommand);
buttonB.onCommand(onButtonCommand);
mqtt.begin(BROKER_ADDR);
}
void loop() {
Ethernet.maintain();
mqtt.loop();
}
```
我们融合连接WiFi代码和HA按键代码,即可得到完整代码:
```cpp
#include "arduino_secrets.h"
#include
#include
#include
void printWifiData();
void printCurrentNet();
void printMacAddress(byte mac[]);
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] =
SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
int status = WL_IDLE_STATUS; // the WiFi radio's status
WiFiClient wifiClient;
HADevice device;
HAMqtt mqtt(wifiClient, device);
// "myButtonA" and "myButtonB" are unique IDs of buttons. You should define you
// own ID.
HAButton buttonA("myButtonA");
HAButton buttonB("myButtonB");
ArduinoLEDMatrix matrix;
void onButtonCommand(HAButton *sender) {
if (sender == &buttonA) {
// button A was clicked, do your logic here
matrix.loadFrame(LEDMATRIX_CLOUD_WIFI);
} else if (sender == &buttonB) {
// button B was clicked, do your logic here
matrix.loadFrame(LEDMATRIX_UNO);
}
}
void setup() {
// Initialize serial and wait for port to open:
Serial.begin(9600);
matrix.begin();
matrix.loadFrame(LEDMATRIX_EMOJI_BASIC);
byte mac[6];
WiFi.macAddress(mac);
device.setUniqueId(mac, sizeof(mac));
// attempt to connect to Wifi network:
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
// failed, retry
Serial.print(".");
delay(500);
}
Serial.println("You're connected to the network");
Serial.println();
printCurrentNet();
printWifiData();
// optional device's details
device.setName("Arduino");
device.setSoftwareVersion("1.0.0");
// optional properties
buttonA.setIcon("mdi:fire");
buttonA.setName("Click me A");
buttonB.setIcon("mdi:home");
buttonB.setName("Click me B");
// press callbacks
buttonA.onCommand(onButtonCommand);
buttonB.onCommand(onButtonCommand);
mqtt.begin(MQTT_SERVER, MQTT_USERNAME, MQTT_PASSWORD);
Serial.println("Connected to MQTT server.");
}
void loop() { mqtt.loop(); }
void printWifiData() {
// print your board's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print your MAC address:
byte mac[6];
WiFi.macAddress(mac);
Serial.print("MAC address: ");
printMacAddress(mac);
}
void printCurrentNet() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print the MAC address of the router you're attached to:
byte bssid[6];
WiFi.BSSID(bssid);
Serial.print("BSSID: ");
printMacAddress(bssid);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.println(rssi);
// print the encryption type:
byte encryption = WiFi.encryptionType();
Serial.print("Encryption Type:");
Serial.println(encryption, HEX);
Serial.println();
}
void printMacAddress(byte mac[]) {
for (int i = 0; i < 6; i++) {
if (i > 0) {
Serial.print(":");
}
if (mac < 16) {
Serial.print("0");
}
Serial.print(mac, HEX);
}
Serial.println();
}
```
---
**流程图**
## 扩展任务
通过外部 SHT40 温湿度传感器,上传温湿度到HA,通过HA面板显示数据。
### I2C 和 Qwiic
[官方文档🔗](https://docs.arduino.cc/tutorials/uno-r4-wifi/qwiic/)
I2C 是 Qwiic 中使用的通信协议,该协议允许您仅使用两个引脚将许多外围设备连接到单个控制器板。
Qwiic 是一个由分线模块和开发板组成的生态系统,带有所谓的 Qwiic 连接器。例如,Arduino UNO R4 WiFi 就有一个。 Qwiic 生态系统将 I2C 的灵活性与预捆绑电缆的易用性结合在一起,通过收集所有引脚以在单根电缆中启动和运行大型 I2C 设备网络,可以非常轻松地创建 I2C 设备系列。
实际上,这意味着 Qwiic 设备的接线就像将它们串联插入一样简单,然后就完成了。
官方提供示例代码如下:
```cpp
#include
#include
AHT20 humiditySensor;
void setup() {
Serial.begin(9600);
Wire1.begin(); //Join I2C bus
//Check if the AHT20 will acknowledge
if (humiditySensor.begin(Wire1) == false) {
Serial.println("AHT20 not detected. Please check wiring. Freezing.");
while (1)
;
}
Serial.println("AHT20 acknowledged.");
}
void loop() {
float temperature = humiditySensor.getTemperature();
Serial.print("Temperature: ");
Serial.print(temperature, 2);
Serial.println(" C");
}
```
### Adafruit SHT40
为了使用 Adafruit SHT40 温湿度传感器,我们需要安装 `Adafruit SHT4x` 库,该库为 SHT4x 系列温湿度传感器提供了驱动支持。
官方提供示例代码如下:
```cpp
/***************************************************
This is an example for the SHT4x Humidity & Temp Sensor
Designed specifically to work with the SHT4x sensor from Adafruit
----> https://www.adafruit.com/products/4885
These sensors use I2C to communicate, 2 pins are required to
interface
****************************************************/
#include "Adafruit_SHT4x.h"
Adafruit_SHT4x sht4 = Adafruit_SHT4x();
void setup() {
Serial.begin(115200);
while (!Serial)
delay(10); // will pause Zero, Leonardo, etc until serial console opens
Serial.println("Adafruit SHT4x test");
if (! sht4.begin()) {
Serial.println("Couldn't find SHT4x");
while (1) delay(1);
}
Serial.println("Found SHT4x sensor");
Serial.print("Serial number 0x");
Serial.println(sht4.readSerial(), HEX);
// You can have 3 different precisions, higher precision takes longer
sht4.setPrecision(SHT4X_HIGH_PRECISION);
switch (sht4.getPrecision()) {
case SHT4X_HIGH_PRECISION:
Serial.println("High precision");
break;
case SHT4X_MED_PRECISION:
Serial.println("Med precision");
break;
case SHT4X_LOW_PRECISION:
Serial.println("Low precision");
break;
}
// You can have 6 different heater settings
// higher heat and longer times uses more power
// and reads will take longer too!
sht4.setHeater(SHT4X_NO_HEATER);
switch (sht4.getHeater()) {
case SHT4X_NO_HEATER:
Serial.println("No heater");
break;
case SHT4X_HIGH_HEATER_1S:
Serial.println("High heat for 1 second");
break;
case SHT4X_HIGH_HEATER_100MS:
Serial.println("High heat for 0.1 second");
break;
case SHT4X_MED_HEATER_1S:
Serial.println("Medium heat for 1 second");
break;
case SHT4X_MED_HEATER_100MS:
Serial.println("Medium heat for 0.1 second");
break;
case SHT4X_LOW_HEATER_1S:
Serial.println("Low heat for 1 second");
break;
case SHT4X_LOW_HEATER_100MS:
Serial.println("Low heat for 0.1 second");
break;
}
}
void loop() {
sensors_event_t humidity, temp;
uint32_t timestamp = millis();
sht4.getEvent(&humidity, &temp);// populate temp and humidity objects with fresh data
timestamp = millis() - timestamp;
Serial.print("Temperature: "); Serial.print(temp.temperature); Serial.println(" degrees C");
Serial.print("Humidity: "); Serial.print(humidity.relative_humidity); Serial.println("% rH");
Serial.print("Read duration (ms): ");
Serial.println(timestamp);
delay(1000);
}
```
参考两段代码可以得到最终代码:
```cpp
#include "arduino_secrets.h"
#include
#include
#include
#include
#include
void printWifiData();
void printCurrentNet();
void printMacAddress(byte mac[]);
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID;
char pass[] = SECRET_PASS;
int status = WL_IDLE_STATUS; // the WiFi radio's status
WiFiClient wifiClient;
HADevice device;
HAMqtt mqtt(wifiClient, device);
unsigned long lastUpdateAt = 0;
HASensorNumber sht40_t("Temperature", HASensorNumber::PrecisionP2);
HASensorNumber sht40_h("Humidity", HASensorNumber::PrecisionP2);
Adafruit_SHT4x sht4 = Adafruit_SHT4x();
void setup() {
// Initialize serial and wait for port to open:
Serial.begin(115200);
while (!Serial)
delay(10); // will pause Zero, Leonardo, etc until serial console opens
byte mac[6];
WiFi.macAddress(mac);
device.setUniqueId(mac, sizeof(mac));
// attempt to connect to Wifi network:
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
// failed, retry
Serial.print(".");
delay(500);
}
Serial.println("You're connected to the network");
Serial.println();
printCurrentNet();
printWifiData();
// optional device's details
device.setName("Arduino");
device.setSoftwareVersion("1.0.0");
// configure sensor
sht40_t.setIcon("mdi:temperature-celsius");
sht40_t.setName("Temperature");
sht40_t.setUnitOfMeasurement("°C");
sht40_h.setIcon("mdi:water-percent");
sht40_h.setName("Humidity");
sht40_h.setUnitOfMeasurement("% rH");
mqtt.begin(MQTT_SERVER, MQTT_USERNAME, MQTT_PASSWORD);
Serial.println("Connected to MQTT server.");
Serial.println("Adafruit SHT4x test");
if (!sht4.begin(&Wire1)) {
Serial.println("Couldn't find SHT4x");
while (1)
delay(1);
}
Serial.println("Found SHT4x sensor");
Serial.print("Serial number 0x");
Serial.println(sht4.readSerial(), HEX);
sht4.setPrecision(SHT4X_HIGH_PRECISION);
sht4.setHeater(SHT4X_NO_HEATER);
}
void loop() {
mqtt.loop();
if ((millis() - lastUpdateAt) > 1000) { // 1000ms debounce time
sensors_event_t humidity, temp;
sht4.getEvent(&humidity, &temp); // udpate data
sht40_h.setValue(humidity.relative_humidity);
sht40_t.setValue(temp.temperature);
lastUpdateAt = millis();
}
}
void printWifiData() {
// print your board's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print your MAC address:
byte mac[6];
WiFi.macAddress(mac);
Serial.print("MAC address: ");
printMacAddress(mac);
}
void printCurrentNet() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print the MAC address of the router you're attached to:
byte bssid[6];
WiFi.BSSID(bssid);
Serial.print("BSSID: ");
printMacAddress(bssid);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.println(rssi);
// print the encryption type:
byte encryption = WiFi.encryptionType();
Serial.print("Encryption Type:");
Serial.println(encryption, HEX);
Serial.println();
}
void printMacAddress(byte mac[]) {
for (int i = 0; i < 6; i++) {
if (i > 0) {
Serial.print(":");
}
if (mac < 16) {
Serial.print("0");
}
Serial.print(mac, HEX);
}
Serial.println();
}
```
---
**流程图**
## 活动体验
参加得捷电子 FollowMe 第二季活动后,我对电子硬件设计和物联网应用的理解加深了许多。入门任务中的简单“Blink”测试让我快速熟悉了Arduino UNO R4 WiFi的基本操作,而驱动点阵LED和信号处理的基础任务,则让我在动手的过程中逐步建立了系统思维。特别是在Wi-Fi通信和MQTT协议方面,通过连接HA平台,我不仅理解了智能家居的基本实现方法,还体验到了物联网应用的实际效果。
扩展任务选择了SHT40温湿度传感器,将环境数据上传至HA平台的过程让我领悟到传感器数据的采集与上传并非单纯的数据输出,而是通过合理的软硬件协作才能实现稳定、可靠的效果。这让我深刻理解了在物联网设备中,硬件编程和网络通信环节的优化对于整体应用体验的重要性。
整个活动也培养了我独立解决问题的能力。例如,在调试传感器连接时,因接口兼容性和通信协议问题,我遇到过数据传输不稳定的情况。通过查阅文档、调整配置,并在论坛上获得了其他参与者的帮助,终于成功实现了数据传输。这种过程不仅加深了对传感器和HA平台的理解,也让我体会到了社区学习的氛围与价值。
## [视频🔗](https://training.eeworld.com.cn/course/68681)
## [代码🔗](https://download.eeworld.com.cn/detail/Vaintory/634536)
-
上传了资料:
【Follow me第二季第2期】全任务代码
-
加入了学习《FollowMe 第二季:2 - Arduino UNO R4 Wi-Fi 及任务讲解》,观看 Arduino UNO R4 Wi-Fi 及任务讲解
- 2024-10-01
-
加入了学习《【Follow me 第二季第2期任务】 各个任务实现的展示效果》,观看 【Follow me 第二季第2期任务】Blink / 串口打印Hello EEWorld!
- 2024-09-20
-
加入了学习《Verilog RTL编程实践》,观看 Verilog RTL编程实践 1
-
加入了学习《机器人学:建模、控制与视觉》,观看 第14章:视觉图像处理
-
加入了学习《机器人学:建模、控制与视觉》,观看 第4章:刚体速度和静力
-
加入了学习《机器人学:建模、控制与视觉》,观看 第1章:概述;第2章:机器人机构
-
加入了学习《C++零基础入门到精通》,观看 第四章-08-无返回值函数和void类型
-
加入了学习《深入理解计算机系统CMU 》,观看 Course Overview
-
加入了学习《5G边缘计算》,观看 Communications and Networks
-
加入了学习《嵌入式开发入门模电(模拟电路)基础》,观看 npn三极管仿真
-
加入了学习《ROS趣味入门教程》,观看 什么是ROS
-
加入了学习《新版RTOS教程:15天入门RT-Thread内核》,观看 初识RT-Thread
-
加入了学习《FreeRTOS应用开发(正点原子)》,观看 第1讲 RTOS背景知识简介
-
加入了学习《自己动手写操作系统》,观看 定期时间片轮转
- 2023-12-07
-
发表了主题帖:
【得捷电子Follow me第2期】任务汇总【修改调整版】
本帖最后由 Vaintory 于 2023-12-7 15:19 编辑
# 一、演示视频
在本次follow me第二期活动中,深入探讨如何使用ESP32-S3开发板实现网络连接、屏幕显示、LED灯控制、日历和时钟等功能。
视频地址: [https://training.eeworld.com.cn/course/68171](https://training.eeworld.com.cn/course/68171)
# 二、任务合集帖子
## 任务1:控制屏幕显示中文
任务简介:完成屏幕的控制,并且能显示中文
### 关键代码
```python
import board
import displayio
from adafruit_display_text import label
from adafruit_bitmap_font import bitmap_font
# 1. 屏幕基础配置
display = board.DISPLAY
# 2. 使用字体
font_file = "fonts/FZSKBXK-25.pcf"
font = bitmap_font.load_font(font_file)
text_group = displayio.Group(scale=1, x=0, y=0)
text = "你好, 我是V!"
text_area = label.Label(font=font, text=text,
color=0Xfefae0, x=32, y=65)
text_group.append(text_area)
display.show(text_group)
while True:
pass # 挂起任务
```
### 效果展示
### 分享贴
https://bbs.eeworld.com.cn/thread-1260660-1-1.html
## 任务2:网络功能使用
任务简介:完成网络功能的使用,能够创建热点和连接到WiFi
### 关键代码
```python
import os
import wifi
print(f"Connecting to {os.getenv('CIRCUITPY_WIFI_SSID')}")
wifi.radio.connect(os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD"))
print(f"Connected to {os.getenv('CIRCUITPY_WIFI_SSID')}")
print(f"My IP address: {wifi.radio.ipv4_address}")
wifi.radio.start_ap(ssid="ESP32-S3", password="12345678")
while True:
pass
```
### 效果展示
### 分享贴
https://bbs.eeworld.com.cn/thread-1261224-1-1.html
## 任务3:控制WS2812B
任务简介:使用按键控制板载Neopixel LED的显示和颜色切换
### 主要代码
```python
import board
import digitalio
import neopixel
# 1. 定义颜色列表
colors = [
(255, 0, 0), # red
(0, 255, 0), # green
(0, 0, 255), # blue
(0, 255, 255), # cyan
(255, 0, 255), # purple
(255, 255, 0), # yellow
(255, 255, 255), # white
(0, 0, 0) # black(off)
]
# 2. 初始化按钮
button = digitalio.DigitalInOut(board.BUTTON)
button.switch_to_input(pull=digitalio.Pull.UP)
# 3. 初始化NeoPixel
pixel = neopixel.NeoPixel(board.NEOPIXEL, 1)
pixel.brightness = 0.1
# 4. 主程序
color_idx = 0
pixel.fill(colors[color_idx])
while True:
if button.value == False:
time.sleep(0.02) # 软件消抖20ms
if button.value == False:
color_idx = (color_idx + 1) % len(colors)
pixel.fill(colors[color_idx])
while not button.value:
pass # 单次按下
```
### 效果展示
### 分享贴
https://bbs.eeworld.com.cn/thread-1261211-1-1.html
## 任务4:日历和时钟
任务简介:一个可通过互联网更新的万年历时钟,并显示当地的天气信息
### 主要代码
```python
# WiFi 相关头文件
import os
import wifi
# 定时与睡眠相关头文件
import time
import alarm
# 网络请求相关头文件
import adafruit_requests
import socketpool
import ssl
# OLED显示相关头文件
import board
import displayio
from adafruit_display_text import label
from adafruit_bitmap_font import bitmap_font
# 0. 定义 API 请求地址
TIME_URL = 'https://io.adafruit.com/api/v2/%s/integrations/time/strftime?x-aio-key=%s&fmt=%s&tz=%s' % \
(os.getenv('ADAFRUIT_IO_USERNAME'), os.getenv('ADAFRUIT_IO_KEY'), '%25b %25e, %25H:%25M', 'Asia/Hong_Kong')
WEATHER_URL = 'https://restapi.amap.com/v3/weather/weatherInfo?key=%s&city=%s&extensions=base' % \
(os.getenv('GAODE_KEY'), '440305')
# 1. 连接 WiFi
print(f"Connecting to {os.getenv('CIRCUITPY_WIFI_SSID')}")
wifi.radio.connect(os.getenv("CIRCUITPY_WIFI_SSID"),
os.getenv("CIRCUITPY_WIFI_PASSWORD"))
print(f"Connected to {os.getenv('CIRCUITPY_WIFI_SSID')}")
print(f"My IP address: {wifi.radio.ipv4_address}")
# 2. 创建套接字池
pool = socketpool.SocketPool(wifi.radio)
requests = adafruit_requests.Session(pool, ssl.create_default_context())
# 3. 获取时间
response = requests.get(TIME_URL)
local_time = response.text
# time = 'Oct 30, 19:35'
# 4. 获取天气
response = requests.get(WEATHER_URL)
data = response.json()
weather = f"{data['lives'][0]['weather']} {data['lives'][0]['temperature']}℃"
# weather = '多云 25℃'
# 5. 显示
display = board.DISPLAY # 初始化屏幕对象
font_file = "fonts/FZSKBXK-25.pcf" # 设置中文字体
font = bitmap_font.load_font(font_file)
text_group = displayio.Group(scale=1, x=0, y=0)
text_group.append(
label.Label(font=font, text=local_time, color=0xfefae0, x=50, y=60))
text_group.append(
label.Label(font=font, text=weather, color=0xfefae0, x=63, y=85))
display.show(text_group)
while True:
# 循环更新时间
response = requests.get(TIME_URL)
if(response.text != local_time):
local_time = response.text
# 更新显示内容
text_group[0] = label.Label(
font=font, text=local_time, color=0xfefae0, x=50, y=60)
display.show(text_group)
# 创建一个 60s 后的 Alarm
time_alarm = alarm.time.TimeAlarm(monotonic_time=time.monotonic() + 60)
# 进入轻度睡眠等待唤醒
alarm.light_sleep_until_alarms(time_alarm)
# 唤醒后会进入下一个 while True 循环
```
### 效果展示
### 分享贴
https://bbs.eeworld.com.cn/thread-1261366-1-1.html
---
总之,ESP32-S3开发板是一款功能强大、灵活多变的开发板,它可以快速实现各种应用场景,并且具有良好的扩展性和可定制性。
感谢EEWORLD社区举办的这次follow me活动, 虽然本职业是web开发者,发货时间有点紧, 但通过本次活动的学习和实践,在这里结识了一批志同道合的伙伴,相互交流,共同成长。我了解circuitpython环境的下python的使用和一些常用库的调用, 帮助我更好地操作开发板上的功能 期待下一次活动的到来。
# 三、可编译下载的代码
## 任务一
https://download.eeworld.com.cn/detail/Vaintory/629767
## 任务二
https://download.eeworld.com.cn/detail/Vaintory/629768
## 任务三
https://download.eeworld.com.cn/detail/Vaintory/629769
## 任务四
https://download.eeworld.com.cn/detail/Vaintory/629770