eew_Eu6WaC 发表于 2024-9-14 21:46

【Follow me第二季第2期】作品汇总

本帖最后由 eew_Eu6WaC 于 2024-10-15 18:18 编辑

<p><strong><span style="font-size:18px;">前言</span></strong></p>

<p><span style="font-size:16px;">很高兴能够参加本次活动(Follow me第二季第2期),后面我将一一的对板卡以及完成的活动进行介绍!</span></p>

<p>&nbsp;</p>

<p><strong><span style="font-size:18px;">板卡展示</span></strong></p>

<p><span style="font-size:16px;">图片如下,使用的板卡是Arduino UNO R4 WiFi,上面主要的芯片是瑞萨的RA4和乐鑫的ESP32-S3,配置的十分豪华。</span></p>

<p><span style="font-size:16px;">这块板子的内存比以前的更大,具有256kB闪存,32kB SRAM和8kB EEPROM。</span></p>

<p><span style="font-size:16px;">需要注意的是:RA4的工作电压固定为5V,而ESP32-S3模块的工作电压为3.3V。因此两个mcu之间的通信是通过逻辑电平转换器进行的。</span></p>

<p>我够买的东西都是活动必备物料:一个开发板、一个传感器以及一根连接线</p>

<p> &nbsp;</p>

<div style="text-align: left;"></div>

<div style="text-align: left;">另外还用到了其他的板子进行辅助,一块是树莓派4(用作与作HA中枢),一块STM32F4(用作与示波器)</div>

<div style="text-align: left;">&nbsp;</div>

<div style="text-align: left;"><span style="font-size:18px;"><strong>视频演示&nbsp;</strong></span></div>

<div style="text-align: left;"><iframe __idm_id__="5431297" allowfullscreen="true" frameborder="0" height="450" src="https://training.eeworld.com.cn/shareOpenCourseAPI?isauto=true&amp;lessonid=41235" style="background:#eee;margin-bottom:10px;" width="700"></iframe></div>

<div style="text-align: left;">
<p><span style="font-size:18px;"><strong>视频快捷索引:</strong></span></p>

<p><span style="font-size:12px;"><strong>入门任务:91:35</strong></span></p>

<p><span style="font-size:12px;"><strong>基础任务一:01:51</strong></span></p>

<p><span style="font-size:12px;"><strong>基础任务二:02:12</strong></span></p>

<p><span style="font-size:12px;"><strong>基础任务三:02:25</strong></span></p>

<p><span style="font-size:12px;"><strong>基础任务四:02:40</strong></span></p>

<p><span style="font-size:12px;"><strong>进阶任务+创意任务二:02:47</strong></span></p>
</div>

<div style="text-align: left;">&nbsp;</div>

<div style="text-align: left;"><strong><span style="font-size:18px;">任务总览&nbsp;</span></strong></div>

<div style="text-align: left;">
<ol>
        <li><span style="font-size:16px;">搭建环境并开启第一步Blink / 串口打印Hello EEWorld!<a href="https://bbs.eeworld.com.cn/thread-1294008-1-1.html" target="_blank">链接</a></span></li>
        <li><span style="font-size:16px;">驱动12x8点阵LED!<a href="https://bbs.eeworld.com.cn/thread-1294019-1-1.html" target="_blank">链接</a></span></li>
        <li><span style="font-size:16px;">用DAC生成正弦波!<a href="https://bbs.eeworld.com.cn/thread-1294020-1-1.html" target="_blank">链接</a></span></li>
        <li><span style="font-size:16px;">用OPAMP放大DAC信号!<a href="https://bbs.eeworld.com.cn/thread-1294021-1-1.html" target="_blank">链接</a></span></li>
        <li><span style="font-size:16px;">用ADC采集并且打印数据到串口等其他接口可上传到上位机显示曲线!<a href="https://bbs.eeworld.com.cn/thread-1294022-1-1.html" target="_blank">链接</a></span></li>
        <li><span style="font-size:16px;">通过Wi-Fi,利用MQTT协议接入到开源的智能家居平台HA(HomeAssistant)<a href="https://bbs.eeworld.com.cn/thread-1294023-1-1.html" target="_blank">链接</a></span></li>
        <li><span style="font-size:16px;">通过外部SHT40温湿度传感器,上传温湿度到HA,通过HA面板显示数据!</span><a href="https://bbs.eeworld.com.cn/thread-1294023-1-1.html" target="_blank">链接</a></li>
</ol>

<p><span style="font-size:18px;"><strong>入门任务</strong></span></p>

<p><strong><span style="font-size:16px;">环</span><span style="font-size:18px;">境搭建:</span></strong><span style="font-size:18px;">搭建环境并开启第一步Blink / 串口打印Hello EEWorld!</span></p>

<p><span style="font-size:16px;">本次活动使用Arduino进行开发,虽然也可也使用瑞萨官方的IDE,但是Arduino比较方便!</span></p>

<p><span style="font-size:16px;">1.打开<a href="https://www.arduino.cc/en/software" target="_blank">Arduino IDE</a>官网,选择对应的系统和系统位数下载即可</span></p>

<p><span style="font-size:16px;">2.在开发板管理器中,搜索Arduino UNO R4 Boards,并且安装一下(安装完成后就可以看到对应的例程了)</span></p>

<p><span style="font-size:16px;">3,插上板卡,这IDE的设备选择器中就可以看到一样名字的板卡了</span></p>

<p><img src="https://bbs.eeworld.com.cn/data/attachment/forum/202409/15/073228n05nn3fmd77n9qrd.png" /></p>

<p><span style="font-size:18px;"><strong>串口打印和闪烁LED:</strong></span></p>

<p><span style="font-size:16px;">因为安装了这块开发板的板子,所以可以直接使用LED_BUILTIN这个引脚来使用,只需要初始化引脚为输出,并且设置串口的波特率,就可以正常工作了,下面贴出代码和视频</span></p>

<pre>
<code class="language-cpp">void setup() {
// open the serial port:
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
Serial.print("Hello Arduino! LED ON\r\n");
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
Serial.print("Hello Arduino! LED OFF\r\n");
delay(1000);
}</code></pre>

<p>b1dbb980813692317e7586298ec087f6</p>

<p><span style="font-size:18px;"><strong>基础任务一:</strong>驱动12x8点阵LED!</span></p>

<p><span style="font-size:16px;">设计思路:和控制单个LED相似,是需要给LED通上正向电压,设置GPIO的输出电压为1或者0即可控制灯的亮或者灭。但是正常使用的时候用一个I/O控制一个LED还是合理的,但是如果有七七四九个LED,总不能用49个IO来分别控制LED吧,那也太奢华了。所以采用矩阵的方式来控制,可以参考板卡对应的例程:需要使用到的是&quot;Arduino_LED_Matrix.h&quot;和&quot;Arduino_LED_Matrix.h&quot;这两个库,只需要事先将要显示的内容保存到Buff里面,然后再传进去就可以显示了</span></p>

<p><img src="https://bbs.eeworld.com.cn/data/attachment/forum/202409/15/074003ktzitnnstshd4scc.png.thumb.jpg" /></p>

<p> &nbsp;</p>

<p><span style="font-size:16px;">下面贴出代码和视频</span></p>

<pre>
<code class="language-cpp">#include "ArduinoGraphics.h"
#include "Arduino_LED_Matrix.h"

ArduinoLEDMatrix matrix;

void setup() {
Serial.begin(115200);
matrix.begin();
}

char SerialRcvBuf;

void loop() {
if(Serial.available() &gt; 0)
{
    memset(SerialRcvBuf, 0, sizeof(SerialRcvBuf));
   
    // 添加5个空格到缓冲区的开头
    for (int i = 0; i &lt; 6; i++)
    {
      SerialRcvBuf = ' ';
    }

    byte rcvMark = 6;
    while (Serial.available() &gt; 0 &amp;&amp; rcvMark &lt; 100)
    {
      SerialRcvBuf = char(Serial.read());
      delay(2); // 短暂延迟以防止过快读取
      rcvMark++;
    }
   
    // Serial.println(SerialRcvBuf);
}

matrix.beginDraw();

matrix.stroke(0xFFFFFFFF);
matrix.textScrollSpeed(80);

matrix.textFont(Font_5x7);
matrix.beginText(0, 1, 0xFFFFFF);
matrix.println(SerialRcvBuf);
matrix.endText(SCROLL_LEFT);

matrix.endDraw();
}</code></pre>

<p>ff545fda72c2447e5acb2e9c32b355d9<br />
&nbsp;</p>

<p><span style="font-size:18px;"><strong>基础任务二:</strong>用DAC生成正弦波!</span></p>

<p><span style="font-size:16px;">连接示意图:为了防止STM32示波器烧掉,所以将输出的波形电压进行分压</span><br />
&nbsp;&nbsp;</p>

<p><span style="font-size:16px;">代码部分十分简单,只需要一行设置频率即可</span></p>

<p><span style="font-size:16px;">下面贴出代码和视频</span></p>

<pre>
<code class="language-cpp">#include "analogWave.h"

analogWave wave(DAC);

int freq = 1000;

void setup() {
wave.sine(freq);
}

void loop() {
}</code></pre>

<p>2e846dc94788ad9a361360edffbcdd0d<br />
&nbsp;</p>

<p>&nbsp;</p>

<p><span style="font-size:18px;"><strong>基础任务三:</strong>用OPAMP放大DAC信号!</span></p>

<p>连接示意图:为了防止STM32示波器烧掉,所以将输出的波形电压进行分压,并且将运放连接成同相比例运放,V0 = (1 + Rf / R3) * vi</p>

<p> &nbsp;</p>

<p> &nbsp;</p>

<p><span style="font-size:16px;">代码部分十分简单,相比于输出DAC波形,多了一行启动运放</span></p>

<p><span style="font-size:16px;">下面贴出代码和视频</span></p>

<pre>
<code class="language-cpp">#include "analogWave.h"
#include &lt;OPAMP.h&gt;
analogWave wave(DAC);

int freq = 1000;

void setup() {
OPAMP.begin(OPAMP_SPEED_HIGHSPEED);
wave.sine(freq);
}

void loop() {
}</code></pre>

<p>60a249e8ea9c443b2e0a91cbb567e5ad<br />
&nbsp;</p>

<p><span style="font-size:18px;"><strong>基础任务四:</strong>用ADC采集并且打印数据到串口等其他接口可上传到上位机显示曲线!</span></p>

<p><span style="font-size:16px;">通过Arduino显示波形很方便,IDE不仅自带了串口调试器还支持波形显示,只需要传入数据就可以产生波形,需要注意的是最高将串口的波特率调高一点可以稍微是波形更加平滑一丢丢</span></p>

<p><span style="font-size:16px;">下面贴出代码和视频</span></p>

<pre>
<code class="language-cpp">#include "analogWave.h"

analogWave wave(DAC);

int freq = 1;

void setup() {
Serial.begin(460800);
wave.sine(freq);
}

void loop() {
int ReadValue = analogRead(A5);
Serial.println(ReadValue);

delay(50);
}


</code></pre>

<p>79214eb638fcd4f5abc720fbaa56650b<br />
&nbsp;</p>

<p><span style="font-size:18px;"><strong>进阶任务+创意任务二:</strong>搭建HA平台,并且将传感器数据上传</span></p>

<p><span style="font-size:16px;">这个进阶任务的难点在于HA平台搭建和设备的接入,下面将介绍如何搭建平台和接入板卡</span></p>

<p>&nbsp;</p>

<p><span style="font-size:16px;">配置树莓派HA平台</span></p>

<ol cid="n54" mdtype="list">
        <li cid="n56" mdtype="list_item">
        <p cid="n57" mdtype="paragraph">插入烧写好的镜像SD卡到树莓派</p>
        </li>
        <li cid="n70" mdtype="list_item">
        <p cid="n68" mdtype="paragraph">接网线到路由器</p>
        </li>
        <li cid="n77" mdtype="list_item">
        <p cid="n75" mdtype="paragraph">将电脑通过WiFi或者网线接入到上面树莓派接入的路由器</p>
        </li>
        <li cid="n60" mdtype="list_item">
        <p cid="n58" mdtype="paragraph">接HDMI到显示屏等能够显示的设备(有条件的最后接上,没有问题也不是很大)</p>
        </li>
        <li cid="n63" mdtype="list_item">
        <p cid="n61" mdtype="paragraph">上电!</p>
        </li>
</ol>

<p cid="n71" mdtype="paragraph">若插上HDMI,稍稍稍微的登上那么一段时间,等待系统启动完成就可以看到这个界面了,若没有显示屏,不要捉急,在网站上输入</p>

<ul>
        <li><code class="hljs"><a href="http://homeassistant.local:8123/" target="_blank">http://homeassistant.local:8123</a>/</code></li>
</ul>
等待网页显示。若成功启动了,恭喜你完成了第一步!!!

<div></div>

<p cid="n86" mdtype="paragraph">能成功进入网站上,会显示这个界面准备,我一开始信了这个鬼(这最多可能需要20分钟),结果等了一个多小时都没有进去&hellip;&hellip;</p>

<p cid="n36" mdtype="paragraph">&nbsp;</p>

<div></div>

<p cid="n82" mdtype="paragraph">第二天起来看到已经到了<strong>Home Assistant</strong>&nbsp;的欢迎界面,按照步骤一步步下去就可以了</p>

<div></div>

<div></div>

<div></div>

<p>&nbsp;</p>

<div></div>

<div></div>

<p cid="n43" mdtype="paragraph">点击FINISH之后,恭喜你现在已经启动并运行了 Home Assistant!!!</p>

<h1 cid="n222" mdtype="heading">HA配置</h1>

<h2 cid="n288" mdtype="heading">SSH安装</h2>

<ol cid="n303" mdtype="list">
        <li cid="n304" mdtype="list_item">
        <p cid="n305" mdtype="paragraph">设置</p>
        </li>
        <li cid="n306" mdtype="list_item">
        <p cid="n307" mdtype="paragraph">加载项</p>
        </li>
        <li cid="n308" mdtype="list_item">
        <p cid="n309" mdtype="paragraph">加载项商店</p>
        </li>
        <li cid="n310" mdtype="list_item">
        <p cid="n311" mdtype="paragraph">搜索<strong>ssh</strong></p>
        </li>
</ol>

<div></div>

<p>&nbsp;</p>

<ol cid="n318" mdtype="list" start="5">
        <li cid="n320" mdtype="list_item">
        <p cid="n321" mdtype="paragraph">安装后</p>

        <p cid="n325" mdtype="paragraph"></p>

        <p>&nbsp;</p>
        </li>
        <li cid="n324" mdtype="list_item">
        <p cid="n322" mdtype="paragraph">成功安装的界面:</p>
        </li>
</ol>

<div></div>

<h2 cid="n227" mdtype="heading">Samba 安装</h2>

<ol cid="n223" mdtype="list">
        <li cid="n230" mdtype="list_item">
        <p cid="n231" mdtype="paragraph">设置</p>
        </li>
        <li cid="n234" mdtype="list_item">
        <p cid="n232" mdtype="paragraph">加载项</p>
        </li>
        <li cid="n237" mdtype="list_item">
        <p cid="n235" mdtype="paragraph">加载项商店</p>
        </li>
        <li cid="n240" mdtype="list_item">
        <p cid="n238" mdtype="paragraph">搜索<strong>Samba</strong></p>
        </li>
        <li cid="n270" mdtype="list_item">
        <p cid="n271" mdtype="paragraph">安装</p>
        </li>
        <li cid="n272" mdtype="list_item">
        <p cid="n273" mdtype="paragraph">配置:用户名和密码</p>
        </li>
        <li cid="n280" mdtype="list_item">
        <p cid="n278" mdtype="paragraph">使用:</p>

        <ol cid="n284" mdtype="list">
                <li cid="n283" mdtype="list_item">
                <p cid="n281" mdtype="paragraph">Win+R</p>
                </li>
                <li cid="n287" mdtype="list_item">
                <p cid="n285" mdtype="paragraph">输出<strong>\ip</strong>,例如</p>

                <ul>
                        <li><code class="hljs">\\192.168.1.43</code></li>
                </ul>
                ,这个ip就是上面ssh中查到的ip地址</li>
                <li cid="n330" mdtype="list_item">
                <p cid="n328" mdtype="paragraph">就可以跟使用文件夹一样,对树莓派里面的文件进行操作了</p>
                </li>
        </ol>
        </li>
</ol>

<h2 cid="n340" mdtype="heading">安装MQTT</h2>

<ol cid="n354" mdtype="list">
        <li cid="n355" mdtype="list_item">
        <p cid="n356" mdtype="paragraph">设置</p>
        </li>
        <li cid="n357" mdtype="list_item">
        <p cid="n358" mdtype="paragraph">加载项</p>
        </li>
        <li cid="n359" mdtype="list_item">
        <p cid="n360" mdtype="paragraph">加载项商店</p>
        </li>
        <li cid="n361" mdtype="list_item">
        <p cid="n362" mdtype="paragraph">搜索<strong>MQTT</strong></p>
        </li>
</ol>

<div></div>

<ol cid="n248" mdtype="list" start="5">
        <li cid="n366" mdtype="list_item">
        <p cid="n367" mdtype="paragraph">设置-》设备于服务-》打开MQTT-》配置,就可以了以下界面,进行配置ip,用户名、密码等信息,记住这些信息哟</p>
        </li>
</ol>

<div></div>

<ol cid="n250" mdtype="list" start="6">
        <li cid="n368" mdtype="list_item">
        <p cid="n369" mdtype="paragraph">添加设备,使用的软件是MQTTx,发送下面数据,湿度同理</p>

        <pre cid="n377" lang="c" mdtype="fences" spellcheck="false">
{
&quot;name&quot;: &quot;SHT40 Humidity&quot;,
&quot;state_topic&quot;: &quot;homeassistant/sensor/sht40_humidity/state&quot;,
&quot;unit_of_measurement&quot;: &quot;%rH&quot;,
&quot;device_class&quot;: &quot;temperature&quot;,
&quot;json_attributes_topic&quot;: &quot;homeassistant/sensor/sht40_humidity/attributes&quot;,
&quot;unique_id&quot;: &quot;sht40_humidity&quot;,
&quot;device&quot;: {
&nbsp;&quot;identifiers&quot;: [&quot;sht40&quot;],
&nbsp;&quot;name&quot;: &quot;SHT40 Sensor&quot;,
&nbsp;&quot;model&quot;: &quot;SHT40&quot;,
&nbsp;&quot;manufacturer&quot;: &quot;SHT&quot;
}
}</pre>

        <ol cid="n380" mdtype="list" start="7">
                <li cid="n385" mdtype="list_item">
                <p cid="n386" mdtype="paragraph">就可以看到添加的设备了</p>
                </li>
        </ol>

        <p cid="n382" mdtype="paragraph">.</p>

        <div></div>
        </li>
</ol>

<p>下面贴出代码和视频</p>

<pre>
<code class="language-cpp">#include "Adafruit_SHT4x.h"

#define SDA_PIN 27
#define SCL_PIN 26

Adafruit_SHT4x sht4 = Adafruit_SHT4x();

#include &lt;WiFi.h&gt;
#include &lt;ArduinoMqttClient.h&gt;

char ssid[] = "wifi";// your network SSID (name)
char pass[] = "passpword";// your network password

char mqtt_user[] = "master";
char mqtt_pass[] = "1";

WiFiClient wifiClient;
MqttClient mqttClient(wifiClient);

const char broker[] = "192.168.1.22"; // MQTT broker address
int port = 1883;
const char subscribe_topic[] = "/status";
const char publish_topic_sht40_temperature[] = "homeassistant/sensor/sht40_temperature/state";
const char publish_topic_sht40_humidity[] = "homeassistant/sensor/sht40_humidity/state";

sensors_event_t humidity, temp;

void setup() {

        //打开串口
        Serial.begin(115200);

        while (!Serial)
          delay(10); // will pause Zero, Leonardo, etc until serial console opens

        //连接WIFI
        // Connect to WiFi
        Serial.print("Attempting to connect to WPA SSID: ");
        Serial.println(ssid);
        while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
          // failed, retry
          Serial.print(".");
          delay(5000);
        }
        Serial.println("\nYou're connected to the network");
        Serial.print("IP Address: ");
        Serial.println(WiFi.localIP());
        Serial.print("Gateway IP: ");
        Serial.println(WiFi.gatewayIP());
        Serial.print("Subnet Mask: ");
        Serial.println(WiFi.subnetMask());
        Serial.print("MAC Address: ");
        //Serial.println(WiFi.macAddress());
        Serial.println();

        //连接MQTT
        // Set MQTT username and password
        mqttClient.setUsernamePassword(mqtt_user, mqtt_pass);

        Serial.print("Attempting to connect to the MQTT broker.");

        if (!mqttClient.connect(broker, port)) {
          Serial.print("MQTT connection failed! Error code = ");
          Serial.println(mqttClient.connectError());

          while (1);
        }

        Serial.println("You're connected to the MQTT broker!");

        Serial.print("Subscribing to topic: ");
        Serial.println(subscribe_topic);

        // Subscribe to a topic
        mqttClient.subscribe(subscribe_topic);

        Serial.print("Waiting for messages on topic: ");
        Serial.println(subscribe_topic);


        //连接SHT40
        TwoWire i2cWire = TwoWire(SCL_PIN, SDA_PIN);

        Serial.println("SHT40 测试");
        if (! sht4.begin(&amp;i2cWire)) {
          Serial.println("无法访问SHT40");
          while (1) delay(1);
        }
        Serial.println("开始读取SHT40序列号");
        Serial.print("SHT40的序列号是:");
        Serial.println(sht4.readSerial(), HEX);



        uint32_t timestamp = millis();
        sht4.getEvent(&amp;humidity, &amp;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);

}

void loop() {
        // put your main code here, to run repeatedly:

        uint32_t timestamp = millis();

        Serial.println("1");

        sht4.getEvent(&amp;humidity, &amp;temp);// populate temp and humidity objects with fresh data

        Serial.println("2");

        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);

        publishMessage_sht40_temperature(String(temp.temperature));
        publishMessage_sht40_humidity(String(humidity.relative_humidity));

}

// Function to publish a message to the MQTT broker
//发布温度信息
void publishMessage_sht40_temperature(const String &amp;message) {
        mqttClient.beginMessage(publish_topic_sht40_temperature);
        mqttClient.print(message);
        mqttClient.endMessage();
}

// Function to publish a message to the MQTT broker
//发布湿度信息
void publishMessage_sht40_humidity(const String &amp;message) {
        mqttClient.beginMessage(publish_topic_sht40_humidity);
        mqttClient.print(message);
        mqttClient.endMessage();
}</code></pre>

<p>22c31ffd26363531fbb9b978904aae78<br />
&nbsp;</p>

<p><span style="font-size:18px;"><strong>项目总结</strong></span></p>

<p><span style="font-size:18px;"><strong>&nbsp; &nbsp; </strong></span><span style="font-size:16px;">这块开发板是我第一块Arduino开发板,与之前学习使用的都是STM32这些板子。听说了Arduino使用起来非常的方便,现在在上面进行开发亲身体会到这便捷和乐趣。通过对上面任务的完成,不断的学习了一些库以及Arduino外设的使用,虽然有时候并不知道库里的引脚是哪个引脚,现在也能方便的跳进库文件里面查看了。最后感谢得捷和电子森林能够提供这行活动的机会,让我探索Arduino的世界!</span></p>

<p><br />
<span style="font-size:18px;"><strong>项目源码</strong></span></p>
</div>

<p><a href="https://download.eeworld.com.cn/detail/eew_Eu6WaC/634340">Follow me 第二季第2期 代码汇总-嵌入式开发相关资料下载-EEWORLD下载中心</a></p>

sunduoze 发表于 2024-11-11 21:49

“电子森林”??什么鬼?
页: [1]
查看完整版本: 【Follow me第二季第2期】作品汇总