【Follow me Season 2 Episode 2】Task Summary Post
[Copy link]
This post was last edited by eew_gz8e7C on 2024-10-7 15:30
1. Video Introduction
2. Task Implementation
2.1 Getting Started Task (Required): Build the environment and start the first step Blink / Serial port print Hello EEWorld!
Software Process:
Code:
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);
}
Effect display:
blink_print
Implementation process:
【Follow me Season 2 Issue 2】Unboxing + Environment Building + Blink & Serial Printing - DigiKey Technology Zone - Electronic Engineering World - Forum (eeworld.com.cn)
2.2 Basic tasks (must be done): drive 12x8 dot matrix LED; use DAC to generate sine wave; use OPAMP to amplify DAC signal; use ADC to collect and print data to serial port and other interfaces to upload to host computer to display curve
Software Process:
Code:
// 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);
}
Effect display:
LED_MX
DAC
OPAMP
Implementation process:
【Follow me Season 2 Issue 2】Basic Task Dot Matrix + DAC Sine Wave + OPAMP Amplification + Serial Port Printing and Curve Output - DigiKey Technology Zone - Electronic Engineering World - Forum (eeworld.com.cn)
2.3 Advanced Task (Must Do): Connect to the open source smart home platform HA (HomeAssistant) via Wi-Fi using the MQTT protocol
Software Process:
Code:
#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++;
}
}
Effect display:
MQTT
Implementation process:
【Follow me Season 2 Episode 2】Advanced Task: MQTT protocol access to open source smart home HomeAssistant - DigiKey Technology Zone - Electronic Engineering World Forum (eeworld.com.cn)
2.4 Extended Tasks Upload the light intensity to HA through an external ambient light sensor and display the data on the HA panel + upload the temperature and humidity to HA through an external temperature and humidity sensor and display the data on the HA panel
Software Process:
Code:
//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();
}
}
Effect display:
Implementation
process :
【Follow me Season 2, Issue 2】Extended Task HA Panel Displays Temperature, Humidity, and Illuminance Data - DigiKey Technology Zone - Electronic Engineering World - Forum (eeworld.com.cn)
3. Experience
After work, I was able to learn and master the use of uno r4 wifi through forum activities, and I gained a deep understanding of the use of the open source smart home Home Assistant system. Although I encountered many problems in the process, I solved them all by communicating with forum friends. Thanks to eeworld and Digi-Key for providing the event opportunity. I wish similar events will continue to be better and better.
4. Task Code Summary
https://download.eeworld.com.cn/detail/eew_gz8e7C/634546
|