
你觉得这篇文章怎么样? 帮助我们为您提供更好的内容。

Thank you! Your feedback has been received.

There was a problem submitting your feedback, please try again later.

你觉得这篇文章怎么样?
过去一个月,英国宣布全国进入极端高温红色警报紧急状态,这是首次针对高温天气发布国家紧急状态。与此同时,数百人在欧洲的热浪中丧生。此外,香港正面临高温天气影响,香港天文台亦公布有关「长时间高温警报」的特别天气提示,这是天文台首次发出长时间高温警报的特别提醒。
这种极端天气的主要原因之一是全球变暖。燃烧化石燃料和砍伐森林是全球变暖的原因。因此,了解我们周围的空气质量可以帮助我们改善全球变暖。在这里,我不会说我们不使用电力作为解决全球变暖问题的最有效方法。因为我认为这是不可能的,尤其是在这种极端天气下。正如我们在有关欧洲热浪的坏消息中看到的那样。因此,我认为正确用电是解决全球变暖的更有效方法。在这个项目中,我将使用空气质量套件制作移动空调,通过空气质量套件(ESDK)测量的温度来确定我们是否需要打开移动空调。
在本章中,我将分享我如何从 Arduino Nano 33 IoT 中的 Air Quality Kit 接收数据。
众所周知,有一个温度传感器可以检查环境温度。此外,我们可以轻松检查仪表板上的所有测量值。但是,在这个项目中,我将通过 MQTT 将数据发送到 MCU(Arduino Nano 33 IoT)进行空调。
MQTT
空气质量套件已安装在 MQTT 中。在开始之前,我们可以先好好测试一下 MQTT 功能。
https://docs.designspark.io/projects/aq-device/en/latest/use.html#mqtt
我们可以通过 SSH 连接并输入“ mosquitto_sub -h localhost -t '#' ”来检查 MQTT。
在我们检查它是否正常工作后,我们可以进入 MCU 部分。
在这个项目中,我将通过 localhost 与 Air Quality Kit(发布)和 Arduino Nano 33 IoT(订阅)进行通信。因此,我们需要检查 Air Quality Kit 的 IP 地址并创建我们首先订阅的主题的名称。
const char broker[] = "";
//IP address of the Air Quality Kit
int port = 1883;
const char topic[] = "airquality/#";
然后,我们可以通过“mqttClient.onMessage()”来接收MQTT消息。
void onMqttMessage(int messageSize) {
char input[384] = {0};
// we received a message, print out the topic and contents
Serial.println("Received a message with topic '");
Serial.print(mqttClient.messageTopic());
Serial.print("', length ");
Serial.print(messageSize);
Serial.println(" bytes:");
}
接收到数据后,我们可以通过 JSON 对接收到的数据进行反序列化。通过 JSON,我们可以在 JsonDocument 上分配接收到的数据,并获取值。
//JSON
StaticJsonDocument<384> doc;
DeserializationError error = deserializeJson(doc, input);
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.f_str());
return;
}
if(!error){
const char* hardwareId = doc["hardwareId"];
// "RPI0000000008abc681"
JsonObject thv = doc["thv"];
thv_vocIndex = thv["vocIndex"];
thv_temperature = thv["temperature"];
thv_humidity = thv["humidity"];
const char* thv_sensor = thv["sensor"];
JsonObject pm = doc["pm"];
const char* pm_sensor = pm["sensor"];
pm_pm1_0 = pm["pm1.0"];
pm_pm2_5 = pm["pm2.5"];
pm_pm4_0 = pm["pm4.0"];
pm_pm10 = pm["pm10"];
const char* geohash = doc["geohash"];
const char* co2_sensor = doc["co2"]["sensor"];
co2_co2 = doc["co2"]["co2"];
Serial.println("---------------------------------------------------");
Serial.print("VocIndex: "); Serial.println(thv_vocIndex);
Serial.print("Temperature: "); Serial.println(thv_temperature);
Serial.print("Humidity: "); Serial.println(thv_humidity);
Serial.print("co2: "); Serial.println(co2_co2);
Serial.print("pm_pm1_0: "); Serial.println(pm_pm1_0);
Serial.print("pm_pm2_5: "); Serial.println(pm_pm2_5);
Serial.print("pm_pm4_0: "); Serial.println(pm_pm4_0);
Serial.print("pm_pm10: "); Serial.println(pm_pm10);
}
最后,我们可以在 MCU 中接收数据,有了这些数据,我们可以在 MCU 上做任何我们想要的项目。
下一章,我将进入项目的核心部分——空调,敬请期待,希望你对这个项目感兴趣。