#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 15:06:03 浏览: 39
这是一个Arduino的代码,使用了DHT11传感器来读取温湿度数据,同时使用了U8g2库来驱动OLED屏幕显示数据。还使用了一个步进电机,根据温度值的大小来控制电机的转速。代码中还使用了ArduinoJson库来处理JSON数据格式,并将数据上传到云平台。其中的sendJsonData函数可以定时上传数据到云平台,可以根据需要修改上传时间间隔。
相关问题
#include <WiFi.h>
#include <WiFi.h> 是一个C++库,用于在Arduino开发板上实现与WiFi网络的通信。它提供了一组函数和类,使得连接到WiFi网络、发送和接收数据变得更加简单。
该库提供了以下功能:
1. 连接到WiFi网络:可以使用WiFi.begin()函数连接到指定的WiFi网络。需要提供WiFi网络的名称(SSID)和密码。
2. 获取连接状态:可以使用WiFi.status()函数获取当前WiFi连接的状态,例如已连接、正在连接或未连接。
3. 获取IP地址:可以使用WiFi.localIP()函数获取分配给设备的本地IP地址。
4. 发送和接收数据:可以使用WiFiClient类或WiFiUDP类来创建TCP或UDP连接,并通过它们发送和接收数据。
使用#include <WiFi.h>时,你可以在Arduino代码中使用这些功能来实现与WiFi网络的通信。
#include <Wire.h> 什么意思
`#include <Wire.h>` 是一个预处理指令,用于包含Arduino库中的Wire库。这个库提供了I2C通信的功能,可以用于与其他I2C设备进行通信。
在Arduino中,你可以使用Wire库来控制I2C总线,并通过I2C与其他设备进行通信,比如传感器、显示器、扩展模块等。通过包含`<Wire.h>`头文件,你可以在你的程序中使用Wire库提供的函数和类。
例如,你可以使用Wire库的函数来发送和接收数据,设置I2C总线的速度,以及控制I2C设备的连接和通信。
总之,`#include <Wire.h>` 是将Wire库包含到你的程序中,以便在代码中使用I2C通信功能。
希望这解释清楚了你的疑问!如果还有其他问题,请随时提问。