ESP32基于Arduino环境读取SHT20温湿度,显示在OLED上

ESP32基于Arduino环境读取SHT20温湿度,显示在OLED上


#include <Stdio.h>
#include <String.h>
#include <Wire.h>
#include "SSD1306.h"
#include "uFire_SHT20.h"

uFire_SHT20 sht20;



SSD1306 display(0x3c, 32, 33);
int counter = 0;
void setup() {
  Serial.begin(115200);
  Wire.setPins(32,33);
    Wire.begin();        // join i2c bus (address optional for master)
   display.init();
   sht20.begin();

}

 
void loop() {
     sht20.measure_all();
     Serial.println((String)sht20.tempC + "°C");
     Serial.println((String)sht20.RH + " %RH");
     Serial.println((String)sht20.vpd() + " kPa VPD");
    Serial.println();
    
    display.clear();
    display.drawString(0,0, "TEMP: " + (String)sht20.tempC + "°C");
    display.drawString(0,20, "HUMI: " + (String)sht20.RH + " %RH");
    display.display();
    counter ++;
    delay(1000);

}

加入RTC


#include <Stdio.h>
#include <String.h>
#include <Wire.h>
#include "SSD1306.h"
#include "uFire_SHT20.h"
#include "ESP32Time.h"


ESP32Time rtc;
uFire_SHT20 sht20;



SSD1306 display(0x3c, 32, 33);
int counter = 0;

void setup() {
  rtc.setTime(30, 24, 15, 17, 1, 2021);  // 17th Jan 2021 15:24:30
  Serial.begin(115200);
  Wire.setPins(32,33);
  
    Wire.begin();        // join i2c bus (address optional for master)
   display.init();
   sht20.begin();

}

 
void loop() {
     sht20.measure_all();
     Serial.println((String)sht20.tempC + "°C");
     Serial.println((String)sht20.RH + " %RH");
     Serial.println((String)sht20.vpd() + " kPa VPD");
     Serial.println(rtc.getTime("%A, %B %d %Y %H:%M:%S"));   // (String) returns time with specified format
    Serial.println();
    
    display.clear();
    display.drawString(0,0, "TEMP: " + (String)sht20.tempC + "°C");
    display.drawString(0,20, "HUMI: " + (String)sht20.RH + " %RH");
    display.display();
    counter ++;
    delay(1000);

}


加入SmartConfig一键配网和EEPROM保存wifi信息


#include <Stdio.h>
#include <String.h>
#include <WiFi.h>
#include <Wire.h>
#include "SSD1306.h"
#include "uFire_SHT20.h"
#include "ESP32Time.h"
#include "EEPROM.h"

String smart_data = "";
int size_data = 0; //WIFI数据长度
String wifiname;
String wifipsw;
bool flag; //读取EEPROM联网,是否成功标志位
int wifiname_len; // wifi名的长度

ESP32Time rtc;
uFire_SHT20 sht20;



SSD1306 display(0x3c, 32, 33);
int counter = 0;


//函数用于直接读取eeprom中的数据,直接完成后直接联网
void Config_wifi(const char *wifiname, const char *psw) 
{
  int i = 0;
  WiFi.begin(wifiname, psw);
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    if (i == 0)
    {
      Serial.print("正在配置网络");
    }
    Serial.print(".");
    i++;
    if (i == 15)
    {
      Serial.println("连接失败请使用APP一键配网");
      flag = true;
      break; 
    }
  }
  if (WiFi.isConnected())
  {

    Serial.println("网络连接成功");
    Serial.print("Local IP:");
    Serial.println(WiFi.localIP());
  }
}

void read_eeprom()
{
  wifiname = "";
  wifipsw = "";
  // EEPROM.begin(20); //申请操作到地址4095(比如你只需要读写地址为100上的一个字节,该处也需输入参数101)
  int a = int(EEPROM.read(0)) - int('0'); //wifi名的长度
  int b = (int(EEPROM.read(1)) - int('0')) * 10 + (int(EEPROM.read(2)) - int('0')) ; //读数据;
  // EEPROM.begin(b); //申请操作到地址4095(比如你只需要读写地址为100上的一个字节,该处也需输入参数101)
  Serial.println(a);
  Serial.println(b);
  for (int addr = 3; addr < b + 1; addr++)
  {
    int data = EEPROM.read(addr); //读数据
    // Serial.print(char(data));
    if (addr < a + 3)
    {
      wifiname = wifiname + char(data);
    }
    else
    {
      wifipsw = wifipsw + char(data);
    }
    delay(2);
  }
  Serial.print("SSID:");
  Serial.println(wifiname);
  Serial.print("PASSWORD:");
  Serial.println(wifipsw);

}
void smartConfig()
{
  WiFi.mode(WIFI_STA);
  Serial.println("\r\nWait for Smartconfig");
  delay(2000);
  // 等待配网
  WiFi.beginSmartConfig();

  while (1)
  {
    Serial.print(".");
    delay(500);
    if (WiFi.smartConfigDone())
    {
      Serial.println("SmartConfig Success");
      Serial.printf("SSID:%s\r\n", WiFi.SSID().c_str());
      Serial.printf("PSW:%s\r\n", WiFi.psk().c_str());
      WiFi.setAutoConnect(true);  // 设置自动连接


      wifiname_len = String(WiFi.SSID().c_str()).length(); //获取wifi名的长度
      //  EEPROM.write(0, char(wifiname_len)); //写在地址0的数据为存入的wifi名的长度


      // EEPROM.write(1, char(size_data)); //写在地址1的数据为存入的数据长度

      smart_data = String(wifiname_len) + String(size_data) + String (WiFi.SSID().c_str()) + String(WiFi.psk().c_str());
      size_data = String(smart_data).length();
      smart_data = String(wifiname_len) + String(size_data) + String (WiFi.SSID().c_str()) + String(WiFi.psk().c_str());

      Serial.println(size_data);
      Serial.println(smart_data); //打印出保存在eeprom数据

      Serial.println(String(smart_data).length());//打印出保存在eeprom中的数据长度

      for (int addr = 0; addr < size_data + 1; addr++)
      {
        //int data = addr%256; //在该代码中等同于int data = addr;因为下面write方法是以字节为存储单位的.
        //String(smart_data).charAt(addr);
        EEPROM.write(addr, toascii(String(smart_data).charAt(addr))); //写数据
      }
      EEPROM.commit(); //保存更改的数据
      // EEPROM.end(); //同EEPROM.commit();

      Serial.println("End write");
      break;
    }
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}


void setup() {
  rtc.setTime(30, 24, 15, 28, 3, 2021);  // 17th Jan 2021 15:24:30
  Serial.begin(115200);
  Wire.setPins(32,33);
  Wire.begin();        // join i2c bus (address optional for master)
  display.init();
  sht20.begin();
  
  EEPROM.begin(50); //申请操作到地址4095(比如你只需要读写地址为100上的一个字节,该处也需输入参数101)
  flag = false;
  read_eeprom();
  Config_wifi(wifiname.c_str(), wifipsw.c_str());
  if (flag)
  {
    smartConfig();
  }


}

 
void loop() {
     sht20.measure_all();
     Serial.println((String)sht20.tempC + "°C");
     Serial.println((String)sht20.RH + " %RH");
     Serial.println((String)sht20.vpd() + " kPa VPD");
     Serial.println(rtc.getTime("%A, %B %d %Y %H:%M:%S"));   // (String) returns time with specified format
    Serial.println();
    
    display.clear();
    display.drawString(0,0, "T:" + (String)sht20.tempC + "°C"+"  "+"H:" + (String)sht20.RH + "%");
    display.drawString(0,20, (String)rtc.getHour(true)+":"+(String)rtc.getMinute()+":"+(String)rtc.getSecond());
    display.drawString(0,32, (String)rtc.getYear()+":"+(String)(rtc.getMonth()+1)+":"+(String)rtc.getDay()+"   "+(String)rtc.getDayofWeek());
    Serial.println((String)rtc.getYear()+":"+(String)(rtc.getMonth()+1)+":"+(String)rtc.getDay()+"   "+(String)rtc.getDayofWeek());
    display.display();
    counter ++;
    delay(1000);

}


已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 游动-白 设计师:白松林 返回首页