c++ 显示DHT11温度数据时,如何正确刷新Adafruit 1306 OLED?

cnh2zyt3  于 2023-02-01  发布在  其他
关注(0)|答案(1)|浏览(136)

温度数据在我的OLED上没有更新。无论发生什么,温度数字都保持不变。
我使用的是带有DHT 11温度/湿度传感器、Adafruit SSD 1306 OLED和2个按钮的ESP8266。
我的代码显示了一个网页服务器上的温度,它在页面刷新时工作和更新都很好,现在,如果你按下一个按钮,字符串“测试!”将出现在oled上,如果你按下另一个按钮,温度将显示出来。
但是如果我再次按下按钮或者同时按下“测试!”按钮和温度按钮,温度就不会更新,所以它不会更新当前的温度数据。我不确定这是因为我的oled没有正确清除,还是因为我的代码不允许buttonpress()函数更新新的温度数据。或者两者都有?
有什么建议,我可以做些什么,让oled刷新与当前的温度数据一样,网络服务器呢?

// Including the ESP8266 WiFi library
#include <ESP8266WiFi.h>
#include <DHT.h> // temp and humid sensor

// We need to include Wire.h for I2C communication
#include <Wire.h>

#include <SPI.h>

#include <Adafruit_SSD1306.h>

#define OLED_RESET LED_BUILTIN
Adafruit_SSD1306 display(OLED_RESET);

// Uncomment one of the lines below for whatever DHT sensor type you're using!
#define DHTTYPE DHT11   // DHT 11
//#define DHTTYPE DHT21   // DHT 21 (AM2301)
//#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321


int pushButton1 = 16;
int pushButton2 = 10;

int buttonState1 = 0;
int buttonState2 = 0;

// Replace with your network details
const char* ssid = "MyUsername";
const char* password = "MyPassword";

// Web Server on port 80
WiFiServer server(80);

// DHT Sensor
const int DHTPin = 2;
// Initialize DHT sensor.
DHT dht(DHTPin, DHTTYPE);

// Temporary variables
static char celsiusTemp[7];
static char fahrenheitTemp[7];
static char humidityTemp[7];

void setup() {

  pinMode(pushButton1, INPUT);
  pinMode(pushButton2, INPUT);
  // Initializing serial port for debugging purposes
  Serial.begin(115200);
  delay(10);


  // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
  display.begin(SSD1306_SWITCHCAPVCC, 0x3D);  // initialize with the I2C addr 0x3D (for the 128x64)
  // init done

  // Show image buffer on the display hardware.
  // Since the buffer is intialized with an Adafruit splashscreen
  // internally, this will display the splashscreen.
  display.display();
  delay(2000);

  // Clear the buffer.
  display.clearDisplay();

  dht.begin();

  // Connecting to WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");

  // Starting the web server
  server.begin();
  Serial.println("Web server running. Waiting for the ESP IP...");
  delay(10000);

  // Printing the ESP IP address
  Serial.println(WiFi.localIP());

}

// runs over and over again
void loop() {

    buttonpress();
    WiFiClient client = server.available();
    if (client) {
      Serial.println("New client");
      // bolean to locate when the http request ends
      boolean blank_line = true;
      while (client.connected()) {
        if (client.available()) {
          char c = client.read();

          if (c == '\n' && blank_line) {
              // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
              float h = dht.readHumidity();
              // Read temperature as Celsius (the default)
              float t = dht.readTemperature();
              // Read temperature as Fahrenheit (isFahrenheit = true)
              float f = dht.readTemperature(true);
              // Check if any reads failed and exit early (to try again).
              if (isnan(h) || isnan(t) || isnan(f)) {
                Serial.println("Failed to read from DHT sensor!");
                strcpy(celsiusTemp,"Failed");
                strcpy(fahrenheitTemp, "Failed");
                strcpy(humidityTemp, "Failed");         
              }
              else{
                // Computes temperature values in Celsius + Fahrenheit and Humidity
                float hic = dht.computeHeatIndex(t, h, false);       
                dtostrf(hic, 6, 2, celsiusTemp);             
                float hif = dht.computeHeatIndex(f, h);
                dtostrf(hif, 6, 2, fahrenheitTemp);         
                dtostrf(h, 6, 2, humidityTemp);
                // You can delete the following Serial.print's, it's just for debugging purposes
                Serial.print("Humidity: ");
                Serial.print(h);
                Serial.print(" %\t Temperature: ");
                Serial.print(t);
                Serial.print(" *C ");
                Serial.print(f);
                Serial.print(" *F\t Heat index: ");
                Serial.print(hic);
                Serial.print(" *C ");
                Serial.print(hif);
                Serial.print(" *F");
                Serial.print("Humidity: ");
                Serial.print(h);
                Serial.print(" %\t Temperature: ");
                Serial.print(t);
                Serial.print(" *C ");
                Serial.print(f);
                Serial.print(" *F\t Heat index: ");
                Serial.print(hic);
                Serial.print(" *C ");
                Serial.print(hif);
                Serial.println(" *F");
              }
              client.println("HTTP/1.1 200 OK");
              client.println("Content-Type: text/html");
              client.println("Connection: close");
              client.println();
              // your actual web page that displays temperature and humidity
              client.println("<!DOCTYPE HTML>");
              client.println("<html>");
              client.println("<head></head><body><h1>ESP8266 - Temperature and Humidity</h1><h3>Temperature in Celsius: ");
              client.println(celsiusTemp);
              client.println("*C</h3><h3>Temperature in Fahrenheit: ");
              client.println(fahrenheitTemp);
              client.println("*F</h3><h3>Humidity: ");
              client.println(humidityTemp);
              client.println("%</h3><h3>");
              client.println("</body></html>");     
              break;
          }
          if (c == '\n') {
            // when starts reading a new line
            blank_line = true;
          }
          else if (c != '\r') {
            // when finds a character on the current line
            blank_line = false;
          }
        }
      }  
      // closing the client connection
      delay(1);
      client.stop();
      Serial.println("Client disconnected.");
    }


}   

void buttonpress() {

  // read the state of the pushbutton value:
  buttonState1 = digitalRead(pushButton1);
  buttonState2 = digitalRead(pushButton2);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState1 == LOW) {     
    // turn LED on:    
    Serial.println("Button 1 Pushed");

  // text display tests
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0,0);
  display.println("Test!");

  display.display();
  delay(400);
  display.clearDisplay();

  }
  if (buttonState2 == LOW) {     
    // turn LED on:    
    Serial.println("Button 2 Pushed");

  // text display tests
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0,0);
  float f = dht.readTemperature(true);
  display.println(f);
  display.display();
  delay(400);
  display.clearDisplay();

  }

}
3b6akqbq

3b6akqbq1#

试着把buttonpress()放到while(client.connected())循环中

while (client.connected()) {
    buttonpress();
    if (client.available()) {
      char c = client.read();
      etc...

并删除行

delay(400);
  display.clearDisplay();

从按钮按下,移动显示。clearDisplay();在按钮测试之前:

void buttonpress() {

  // read the state of the pushbutton value:
  buttonState1 = digitalRead(pushButton1);
  buttonState2 = digitalRead(pushButton2);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState1 == LOW) {     
    // turn LED on:    
    Serial.println("Button 1 Pushed");

  // text display tests
 
  display.clearDisplay();

  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0,0);
  display.println("Test!");

  display.display();
          
  }
  if (buttonState2 == LOW) {     
    // turn LED on:    
    Serial.println("Button 2 Pushed");

  // text display tests

  display.clearDisplay();

  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0,0);
  float f = dht.readTemperature(true);
  display.println(f);
  display.display();
     
  }

}

注意:dht.readTemperature可能需要1秒,因此按下第二个按钮可能会影响服务器响应

相关问题