本帖最后由 eew_gz8e7C 于 2024-10-7 15:30 编辑
一 、视频介绍
二、任务实现
2.1 入门任务(必做):搭建环境并开启第一步Blink / 串口打印Hello EEWorld!
软件流程:
代码:
void setup() {
// put your setup code here, to run once:
//配置波特率
Serial.begin(115200);
while (!Serial) { }
//配置LED引脚为输出模式
//pinMode(102, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
//演示控制板载LED亮灭,并且打印LED状态
digitalWrite(LED_BUILTIN, LOW);
printf("LED ON\n");
delay(1000);
digitalWrite(LED_BUILTIN, HIGH);
printf("LED OFF\n");
delay(1000);
}
效果展示:
blink_print
实现过程:
【Follow me第二季第2期】开箱+环境搭建+Blink&串口打印 - DigiKey得捷技术专区 - 电子工程世界-论坛 (eeworld.com.cn)
2.2 基础任务(必做):驱动12x8点阵LED;用DAC生成正弦波;用OPAMP放大DAC信号;用ADC采集并且打印数据到串口等其他接口可上传到上位机显示曲线
软件流程:
代码:
// LED_MX
#include "Arduino_LED_Matrix.h" //调用LED_Matrix库
ArduinoLEDMatrix matrix; //实例化点阵对象
void setup() {
// put your setup code here, to run once:
Serial.begin(115200); //设置波特率
matrix.begin(); //点阵使能
byte frame[8][12] = {
{ 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0 },
{ 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0 },
{ 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0 },
{ 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0 },
{ 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
}; //点阵爱心数据
matrix.renderBitmap(frame, 8, 12); //点阵渲染显示
}
void loop() {
// put your main code here, to run repeatedly:
}
//DAC
#include "analogWave.h"
analogWave wave(DAC); // 使用DAC引脚实例化模拟曲线对象wave
float freq = 0.5; // 设置曲线初始频率
void setup() {
Serial.begin(115200); // 串口波特率
wave.sine(freq); // 使用模拟曲线对象wave按照初始频率生成正弦波
wave.amplitude(0.5);
}
void loop() {
printf("%d\n",analogRead(A4)); // 读取正弦值
delay(100);
}
//OPAMP
#include "analogWave.h"
#include <OPAMP.h>
analogWave wave(DAC); // 使用DAC引脚实例化模拟曲线对象wave
float freq = 1; // 设置曲线初始频率
void setup() {
Serial.begin(250000); // 串口波特率
OPAMP.begin(OPAMP_SPEED_HIGHSPEED); //设置OPAMP
wave.sine(freq); // 使用模拟曲线对象wave按照初始频率生成正弦波
wave.amplitude(0.4); //设置正弦曲线幅值为0.4
}
void loop() {
printf("%d\n",analogRead(A4)); // 读取DAC输出正弦值
Serial.print(" ");
printf("%d\n",analogRead(A5)); // 读取OPAMP输出正弦值
delay(100);
}
效果展示:
LED_MX
DAC
OPAMP
实现过程:
【Follow me第二季第2期】基础任务 点阵+DAC正弦波+OPAMP放大+串口打印与曲线输出 - DigiKey得捷技术专区 - 电子工程世界-论坛 (eeworld.com.cn)
2.3 进阶任务(必做):通过Wi-Fi,利用MQTT协议接入到开源的智能家居平台HA(HomeAssistant)
软件流程:
代码:
#include <ArduinoMqttClient.h>
#include <WiFiS3.h>
#include "secrets.h"
//wifi信息
char ssid[] = WIFISSID;
char pass[] = WIFIPWD;
//mqtt服务器信息
const char broker[] = BROKER;
const int port = BROKER_PORT;
//mqtt客户端
WiFiClient wifiClient;
MqttClient mqttClient(wifiClient);
//最大重连次数
const int maxTryTimes = 10;
//测试用发送订阅主题
const char sendTopic[] = "HA/echo";
//测试用接收订阅主题
const char backTopic[] = "HA/back";
//发送间隔
const long interval = 1000;
unsigned long previousMillis = 0;
int count = 0;
void setup() {
//初始化串口
Serial.begin(115200);
while (!Serial) {
;
}
// 连接WIFI
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
Serial.print(".");
delay(1000);
}
Serial.println("You're connected to the network");
Serial.println(WiFi.localIP());
Serial.print("Attempting to connect to the MQTT broker: ");
Serial.println(broker);
int tryTimes=0;
//连接MQTT服务器
while (!mqttClient.connect(broker, port)) {
Serial.print("MQTT connection failed! Error code = ");
Serial.println(mqttClient.connectError());
if (tryTimes>maxTryTimes-1)
{
printf("MQTT connection failed over %d times",maxTryTimes);
while(1){};
}
delay(500);
printf("Try more %d times\n",++tryTimes);
}
Serial.println("You're connected to the MQTT broker!");
Serial.println();
Serial.print("Subscribing to topic: ");
Serial.println(backTopic);
Serial.println();
//订阅主题
mqttClient.subscribe(backTopic);
Serial.print("Waiting for messages on topic: ");
Serial.println(backTopic);
Serial.println();
}
void loop() {
// 查看主题消息
int messageSize = mqttClient.parseMessage();
if (messageSize) {
// we received a message, print out the topic and contents
Serial.print("Received a message with topic '");
Serial.print(mqttClient.messageTopic());
Serial.print("', length ");
Serial.print(messageSize);
Serial.println(" bytes:");
// 打印输出主题消息
while (mqttClient.available()) {
Serial.print((char)mqttClient.read());
}
Serial.println();
Serial.println();
}
unsigned long currentMillis = millis();
//发送测试消息到主题
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
Serial.print("Sending message to topic: ");
Serial.println(sendTopic);
Serial.print("echo ");
Serial.println(count);
mqttClient.beginMessage(sendTopic);
mqttClient.print("echo ");
mqttClient.print(count);
mqttClient.endMessage();
Serial.println();
count++;
}
}
效果展示:
MQTT
实现过程:
【Follow me第二季第2期】进阶任务 MQTT协议接入到开源的智能家居HomeAssistant - DigiKey得捷技术专区 - 电子工程世界-论坛 (eeworld.com.cn)
2.4 扩展任务 通过外部 环境光传感器,上传光照度到HA,通过HA面板显示数据+通过外部温湿度传感器,上传温湿度到HA,通过HA面板显示数据
软件流程:
代码:
//LIGHT
#include "secrets.h"
#include <WiFiS3.h>
#include <ArduinoHA.h>
#include <Wire.h>
#define LIGHT_SENSOR A4
//wifi信息
char ssid[] = WIFISSID;
char pass[] = WIFIPWD;
//mqtt服务器信息
const char broker[] = BROKER;
const int port = BROKER_PORT;
byte mac[] = { 0x00, 0x10, 0xFA, 0x6E, 0x38, 0x4A };
//tcp客户端
WiFiClient client;
//HA虚拟设备配置
HADevice device(mac, sizeof(mac));
//mqtt客户端实例化
HAMqtt mqtt(client, device);
//HA传感器实例化
HASensorNumber lightSensor("LightSensor", HABaseDeviceType::PrecisionP2);
//参考上次更新时间
unsigned long lastUpdateAt = 0;
//光线传感器
void getLightValue(float* const pval) {
int sensorValue = analogRead(LIGHT_SENSOR);
*pval = sensorValue/1023.0;
}
void setup() {
//初始化串口
Serial.begin(115200);
while (!Serial) {
;
}
// 连接WIFI
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
Serial.print(".");
delay(1000);
}
Serial.println("You're connected to the network");
Serial.println(WiFi.localIP());
Serial.print("Attempting to connect to the MQTT broker: ");
Serial.println(broker);
device.setName("亮度");
device.setSoftwareVersion("1.0.0");
lightSensor.setIcon("mdi:home");
lightSensor.setName("亮度");
lightSensor.setUnitOfMeasurement("%");
mqtt.begin(BROKER, BROKER_PORT);
}
void loop() {
mqtt.loop();
float light;
if ((millis() - lastUpdateAt) > 1000) {
lightSensor.setValue(light*100);
Serial.print("light:");
Serial.print(light*100);
Serial.println("%");
lastUpdateAt = millis();
}
}
//TEMP&HUMI
#include "secrets.h"
#include <WiFiS3.h>
#include <ArduinoHA.h>
#include <Wire.h>
#include "AHT20.h"
//wifi信息
char ssid[] = WIFISSID;
char pass[] = WIFIPWD;
//mqtt服务器信息
const char broker[] = BROKER;
const int port = BROKER_PORT;
byte mac[] = { 0x00, 0x10, 0xFA, 0x6E, 0x38, 0x4A };
//tcp客户端
WiFiClient client;
//HA虚拟设备配置
HADevice device(mac, sizeof(mac));
//mqtt客户端实例化
HAMqtt mqtt(client, device);
//HA传感器实例化
HASensorNumber tempSensor("tempSensor", HABaseDeviceType::PrecisionP2);
HASensorNumber humiSensor("humiSensor", HABaseDeviceType::PrecisionP2);
//参考上次更新时间
unsigned long lastUpdateAt = 0;
//温湿度传感器AHT20
AHT20 AHT;
void setup() {
//初始化串口
Serial.begin(115200);
while (!Serial) {
;
}
AHT.begin();
// 连接WIFI
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
Serial.print(".");
delay(1000);
}
Serial.println("You're connected to the network");
Serial.println(WiFi.localIP());
//设置MQTT服务器
Serial.print("Attempting to connect to the MQTT broker: ");
Serial.println(broker);
//设置HA虚拟设备信息
device.setName("温湿度");
device.setSoftwareVersion("1.0.0");
tempSensor.setIcon("mdi:home");
tempSensor.setName("温度");
tempSensor.setUnitOfMeasurement("°C");
humiSensor.setIcon("mdi:home");
humiSensor.setName("湿度");
humiSensor.setUnitOfMeasurement("%");
mqtt.begin(BROKER, BROKER_PORT);
}
void loop() {
mqtt.loop();
float humi, temp, light;
if ((millis() - lastUpdateAt) > 1000) {
int ret = AHT.getSensor(&humi, &temp);
if (ret) {
tempSensor.setValue(temp);
humiSensor.setValue(humi * 100);
Serial.print("humidity: ");
Serial.print(humi * 100);
Serial.print("%\t temerature: ");
Serial.println(temp);
}
lastUpdateAt = millis();
}
}
效果展示:
light
TEMP_HUMI
实现过程:
【Follow me第二季第2期】扩展任务 HA面板显示温湿度、光照度数据 - DigiKey得捷技术专区 - 电子工程世界-论坛 (eeworld.com.cn)
三、心得体会
工作之余能够通过论坛活动学习并掌握uno r4 wifi的使用,并且深入了解了开源智能家居Home Assistant系统的使用,收获满满。虽然过程中遇到了许多问题,但是通过和坛友们沟通询问也都全部一一解决。感谢eeworld和得捷提供的活动机会。祝类似的活动越办越好。
四、任务代码汇总
https://download.eeworld.com.cn/detail/eew_gz8e7C/634546
|