1、任务描述
通过网络实现灯的远程控制灯,同时使用 LED 矩阵进行点亮与反馈。搭配温湿传感器,并通过网络连接到智能云端,可以从远程获取传感器的信息,并显示在MQTT与串口助手上,可以直观的看到目前的相关值与温湿度,进行实话监控,目标是通过网络上传传感器数据,让用户远程查看环境信息。
功能实现:
使用 UNO R4 WiFi 的内置 LED 矩阵,熟矩阵的连接图,与引脚具体的连接方式,确保 Wi-Fi 模块已配置好网络连接。
智能云端,通过使用 Wi-Fi 功能连接到云平台自建服务器(消息发布界面)。
通过接收指令,从云端获取远程指令,包括灯的显示值。
数值反馈,通过发布消息进行LED 矩阵显示,使用矩阵点亮对应的区域。
发送的值超过 90% 时,矩阵显示666图标。
选择传感器,温湿度传感器通过实时监控当前的温湿度值,并把当前的实际值反馈给PC端,传感器与 UNO R4 WiFi 连接通过 Qwiic 连接器简化连接HY1.0的连接器进行与SHT传感器板进行连接。读取传感器数据,实时采集传感器数值。
通过 Wi-Fi 将数据发送到云平台 MQTT ,实时显示数据。
2、代码框架:
框架是以ARduino uno r4 wifi进行扩展,实现远程端,手机与PC通过网络之间进行通信,把目前接收的与发送的信息反馈给UNO R4,进行数据传输,数据采集等。
3、工作代码:
温湿度采集代码:
void loop() {
// put your main code here, to run repeatedly:
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);
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 &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 &message) {
mqttClient.beginMessage(publish_topic_sht40_humidity);
mqttClient.print(message);
mqttClient.endMessage();
}
任务一远程点灯与消息发布代码:
void connectToWiFi() {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
Serial.print(".");
delay(5000);
}
Serial.println("\nYou're connected to the network");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
}
// Function to connect to MQTT broker and subscribe to a topic
void connectToMQTT() {
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)
; // Stay here if the connection fails
}
Serial.println("You're connected to the MQTT broker!");
// Now that we're connected, subscribe to the topic
subscribeToTopic();
}
// Function to subscribe to a topic and handle messages
void subscribeToTopic() {
Serial.print("Subscribing to topic: ");
Serial.println(subscribe_topic);
mqttClient.subscribe(subscribe_topic); // Subscribe to the topic
Serial.print("Waiting for messages on topic: ");
Serial.println(subscribe_topic);
// Set the callback function to handle incoming messages
mqttClient.onMessage(messageReceived); // Call onMessage() when a message arrives
}
// Function to handle incoming MQTT messages
void messageReceived(int messageSize) {
String topic = mqttClient.messageTopic(); // Get the topic of the message
String payload = mqttClient.readString(); // Get the message payload
Serial.print("Message received on topic: ");
Serial.println(topic);
Serial.print("Payload: ");
Serial.println(payload);
// Handle the received message if it's a number (assuming it's a string representation of a number)
if (topic == subscribe_topic) {
int receivedNumber = payload.toInt(); // Convert payload to an integer
if (receivedNumber > 90) {
displayNumber(666);
} else {
displayNumber(receivedNumber);
}
// Display the number on the LED matrix
}
}
// Function to display the received number on the LED matrix
void displayNumber(int number) {
matrix.clear(); // Clear the previous display
matrix.beginDraw();
matrix.stroke(0xFFFFFFFF); // Set the text color (white)
matrix.textFont(Font_4x6); // Choose the font size
matrix.beginText(0, 1, 0xFFFFFF); // Position for the text
matrix.println(String(number)); // Print the received number
matrix.endText();
matrix.endDraw();
}
4、工作效果:
使用MQTT与串口监视器显示的数值图:
使用MQTT与串口助手显示的数据信息图:
使用MQTT与串口监视器显示的温湿度数据信息图:
消息发布成功日志图:
消息发布滑块条:
总结:
通过Arduino Uno R4 WiFi开发板,实现了远程灯控与温湿度监控的智能系统。系统利用Wi-Fi模块连接到自建的智能云端服务器,实现远程指令的接收与传感器数据的上传。通过LED矩阵,系统能够直观地显示远程指令与传感器数据的反馈,当特定数值超过90%时,矩阵会显示“666”图标。
在硬件连接方面,采用Qwiic连接器简化了UNO R4 WiFi与SHT温湿度传感器的连接。在软件实现上,系统通过MQTT协议与云端进行通信,确保了数据的实时性和可靠性。
通过MQTT与串口监视器的显示,用户可以清晰地看到当前的温湿度数据、指令反馈等信息。系统还具备消息发布功能,能够实时记录并显示消息发布的成功日志。