#include <Arduino.h> #include <U8g2lib.h> #include <Wire.h> #include "DHT.h" #include <ArduinoJson.h> #define DHTPIN A0 #define DHTTYPE DHT11 const int motorIn1 = 9; const int motorIn2 = 10; #define rank1 150 #define rank2 0 //iic驱动方式 U8G2_SSD1306_128X64_NONAME_1_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE); DHT dht(DHTPIN, DHTTYPE); StaticJsonDocument<200> sendJson; // 创建JSON对象,用来存放发送数据 StaticJsonDocument<200> readJson; // 创建JSON对象,用来存放接收到的数据 unsigned long lastUpdateTime = 0; //记录上次上传数据时间 const unsigned long updateInterval = 2000; // 在这里设置数据发送至云平台的时间间隔,单位为毫秒 long ID = 100416; // 定义设备ID号,替换成云平台生成的ID号 void setup() { u8g2.begin(); dht.begin(); pinMode(motorIn1, OUTPUT); pinMode(motorIn2, OUTPUT); Serial.begin(9600); } char h_str[3]; char t_str[3]; float h; float t; void loop() { // 该函数段可完成数据定时上报的功能,并且不会阻塞loop函数的运行 if (millis() - lastUpdateTime > updateInterval) { sendJsonData(); lastUpdateTime = millis(); } h = dht.readHumidity();//读湿度 t = dht.readTemperature();//读温度(摄氏度) strcpy(h_str, u8x8_u8toa(h, 2)); /* convert m to a string with two digits */ strcpy(t_str, u8x8_u8toa(t, 2)); /* convert m to a string with two digits */ if (t>26) { clockwise(rank1); } else { clockwise(rank2); } delay(1000); u8g2.firstPage(); do { u8g2.setFont(u8g2_font_fur20_tf); u8g2.drawStr(0, 23, "T"); u8g2.drawStr(20, 23, ":"); u8g2.drawStr(40, 23, t_str); u8g2.drawStr(90, 23, "C"); u8g2.drawStr(0, 63, "H"); u8g2.drawStr(20, 63, ":"); u8g2.drawStr(40, 63, h_str); u8g2.drawStr(90, 63, "%"); } while ( u8g2.nextPage() ); delay(1000); } //上传数值 void sendJsonData() { // 将数据添加到JSON对象中,左边为在云平台中定义的标识符,右边为变量 sendJson["ID"] = ID; sendJson["temperature"] = t; //将对象转换成字符串,并向ese8266发送消息 serializeJson(sendJson, Serial); Serial.print("\n"); } void clockwise(int Speed) { analogWrite(motorIn1, 0); analogWrite(motorIn2, Speed); }
时间: 2024-01-01 11:06:03 浏览: 232
这是一个Arduino的代码,使用了DHT11传感器来读取温湿度数据,同时使用了U8g2库来驱动OLED屏幕显示数据。还使用了一个步进电机,根据温度值的大小来控制电机的转速。代码中还使用了ArduinoJson库来处理JSON数据格式,并将数据上传到云平台。其中的sendJsonData函数可以定时上传数据到云平台,可以根据需要修改上传时间间隔。
相关问题
下面的代码是干什么用的,请生成说明注释,#include <Adafruit_I2CDevice.h> #include <splash.h> #include <U8g2lib.h> #include <Wire.h> #include <DHT.h>
```cpp
// 包含必要的库文件
#include <Adafruit_I2CDevice.h> // Adafruit I2C设备库,用于I2C通信
#include <splash.h> // splash库,可能用于处理图形或显示相关的操作
#include <U8g2lib.h> // U8g2库,一个用于驱动基于SAMD或Arduino的微控制器的图形用户界面
#include <Wire.h> // Wire库,用于Arduino的I2C通信
#include <DHT.h> // DHT库,用于读取DHT11或DHT22温湿度传感器的数据
// 这段代码可能在一个项目中用于控制一个带有I2C接口的传感器(如DHT11/22)和一个U8g2图形显示屏,通过I2C通信传输数据,并在显示屏上显示温度和湿度读数。
```
请写出下面库里所有的函数 #include <stdint.h> #include <Arduino.h> #include <TimeLib.h> #include "CommonMacro.h" #include "Configs/Config.h" #include "HAL/HAL.h" #include "ESPAsyncWebServer.h" // 包含异步Web服务器库文件 #include "ArduinoJson.h" #include <EEPROM.h> #include <WiFi.h> #include <HTTPClient.h> #include <zlib.h> #include <cmath> #include <WiFiUdp.h> #include "BH1750.h" #include <Wire.h> #include "DHT.h"
下面是这些库中的函数:
1. <stdint.h>:该库包含了整数类型的定义,如int8_t、uint8_t、int16_t、uint16_t等。
2. <Arduino.h>:该库是Arduino核心库,包含了很多基础函数,如digitalWrite()、analogRead()等。
3. <TimeLib.h>:该库包含了时间相关的函数,如now()、hour()、minute()等。
4. "CommonMacro.h":该库包含了一些常用的宏定义。
5. "Configs/Config.h":该库包含了一些配置参数的定义。
6. "HAL/HAL.h":该库包含了一些硬件抽象层相关的函数,如hal_init()等。
7. "ESPAsyncWebServer.h":该库包含了异步Web服务器相关的函数,如AsyncWebServer()、on()等。
8. "ArduinoJson.h":该库包含了JSON格式相关的函数,如JsonBuffer()、parseObject()等。
9. <EEPROM.h>:该库包含了EEPROM相关的函数,如EEPROM.write()、EEPROM.read()等。
10. <WiFi.h>:该库包含了WiFi相关的函数,如WiFi.begin()、WiFi.status()等。
11. <HTTPClient.h>:该库包含了HTTP客户端相关的函数,如HTTPClient()、begin()等。
12. <zlib.h>:该库包含了数据压缩相关的函数,如compress()、uncompress()等。
13. <cmath>:该库包含了数学函数相关的函数,如sin()、cos()等。
14. <WiFiUdp.h>:该库包含了UDP相关的函数,如WiFiUDP()、begin()等。
15. "BH1750.h":该库包含了BH1750光照度传感器相关的函数,如begin()、readLightLevel()等。
16. <Wire.h>:该库包含了I2C总线相关的函数,如Wire.begin()、Wire.requestFrom()等。
17. "DHT.h":该库包含了DHT温湿度传感器相关的函数,如DHT()、readTemperature()、readHumidity()等。
阅读全文
相关推荐
















