大家好,我开始学习编码和第二语言英语。我有一个小项目,我使用Arduino uno、esp8266、dht11、lcd I2C并在我的项目中使用blynk应用程序我有代码,但我有疑问如何在不打开 WiFi 的情况下从dht获取信息数据到lcd 。现在我只在 WiFi 打开时在 LCD 上获得信息(温度和湿度)。如何在有和没有 WiFi 的情况下在 LCD 上显示温度和湿度。
#define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <DHT.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4);
char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
char ssid[] = "xxxxxxxxxxxx";
char pass[] = "xxxxxxxxxxxxxx";
#include <SoftwareSerial.h>
SoftwareSerial EspSerial(2, 3); // TX, RX
#define ESP8266_BAUD 9600
ESP8266 wifi(&EspSerial);
#define DHTPIN 8
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;
void setup()
{
Serial.begin(115200);
lcd.init();
lcd.backlight();
lcd.clear();
dht.begin();
EspSerial.begin(ESP8266_BAUD);
delay(10);
Blynk.begin(auth, wifi, ssid, pass);
dht.begin();
timer.setInterval(1000L, sendSensor);
}
void loop()
{
Blynk.run();
timer.run();
}
void sendSensor()
{
float h = dht.readHumidity();
float t = dht.readTemperature();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Humidity: ");
lcd.print(h);
lcd.print(" %");
lcd.setCursor(0,2);
lcd.print("Temperature: ");
lcd.print(t);
lcd.print(" C ");
Serial.print("Humidity: ");
Serial.print(h);
Serial.println (" % ");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println (" C ");
if (isnan(h) || isnan(t)) {
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Temperature: ");
lcd.print(t);
lcd.print(" C ");
lcd.setCursor(0,2);
lcd.print("Humidity: ");
lcd.print(h);
lcd.print(" % ");
Serial.print("Humidity: ");
Serial.print(h);
Serial.println (" % ");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println (" C ");
}
Blynk.virtualWrite(V6, h);
Blynk.virtualWrite(V2, t);
Serial.print("Humidity: ");
Serial.print(h);
Serial.println (" % ");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println (" C ");
}