在這個項(xiàng)目中,我們將使用ESP32創(chuàng)建一個氣象站。基本原理是通過讀取DHT22和BMP180傳感器的數(shù)據(jù),然后使用ESP32傳輸創(chuàng)建的網(wǎng)頁上,在網(wǎng)頁上顯示氣象數(shù)據(jù)。
電路圖
首先,將DHT22和ESP32連接起來。DHT22與ESP32的連接如下:
DHT22 引腳1 VCC —–>ESP32 / 3.3V;
DHT22 引腳2 DATA—–>ESP32 / D15;
DHT22引腳4 GND —–>ESP32 /GND.
然后將BMP180壓力傳感器連接到ESP32上。連接如下:
BMP180 Vin —–> ESP32 / 3.3V;
BMP180 GND —–> ESP32 /GND;
BMP180SCL —–> ESP32 / pin 22;(ESP32的22號引腳是SCL.)
BMP180SDA —–> ESP32 / pin 21;(ESP32的21號引腳是SDA.)
ESP32的22和21號引腳是I2C通信接口。詳見下圖ESP32的引腳圖
氣象站C代碼
#include
#include
#include
#include
#define DHTPIN 15
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
Adafruit_BMP085 bmp;
char pressure_value[4];
const char* wifi_name = "Asus_2.4G"; //Your Wifi name
const char* wifi_pass = "basemu.com"; //Your Wifi password
WiFiServer server(80); //Port 80
void setup()
{
Serial.begin(115200);
dht.begin();
bmp.begin();
// Let's connect to wifi network
Serial.print("Connecting to ");
Serial.print(wifi_name);
WiFi.begin(wifi_name, wifi_pass); //Connecting to wifi network
while (WiFi.status() != WL_CONNECTED) //Waiting for the responce of wifi network
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("Connection Successful");
Serial.print("IP address: ");
Serial.println(WiFi.localIP()); //Getting the IP address at which our webserver will be created
Serial.println("Type the above IP address into a browser search bar");
server.begin(); //Starting the server
}
void loop()
{
String pressure = String(bmp.readPressure());
// convert the reading to a char array
pressure.toCharArray(pressure_value, 4);
float hum = dht.readHumidity();
float temp = dht.readTemperature();
float fah = dht.readTemperature(true);
float heat_index = dht.computeHeatIndex(fah, hum);
float heat_indexC = dht.convertFtoC(heat_index);
WiFiClient client = server.available(); //Checking for incoming clients
if (client)
{
Serial.println("new client");
String currentLine = ""; //Storing the incoming data in the string
while (client.connected())
{
if (client.available()) //if there is some client data available
{
char c = client.read(); // read a byte
if (c == '\n') // check for newline character,
{
if (currentLine.length() == 0) //if line is blank it means its the end of the client HTTP request
{
client.print("
");
client.print("
);
client.print("
);
client.print(temp);
client.print("
Temperature in fah: ");
client.print(fah);
client.print("
Humidity is: ");
client.print(hum);
client.print("
Heat Index in C: ");
client.print(heat_indexC);
client.print("
Heat Index in fah: ");
client.print(heat_index);
client.print("
Pressure is: ");
client.print(pressure_value);
client.print("hpa");
client.print("
");
break; // break out of the while loop:
}
else
{ // if you got a newline, then clear currentLine:
currentLine = "";
}
}
else if (c != '\r')
{ // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
}
}
}
}
氣象站項(xiàng)目代碼釋義
首先,確保項(xiàng)目所需的所有庫均 include 了,然后定義連接DHT22溫度和濕度傳感器的引腳,再創(chuàng)建實(shí)例:
#include
#include
#include
#include
#define DHTPIN 15
#define DHTTYPE DHT22 DHT dht(DHTPIN, DHTTYPE);
Adafruit_BMP085 bmp;
接著存儲Wi-Fi名稱和密碼,同時定義并創(chuàng)建服務(wù)器的端口。
const char* wifi_name = "Asus_2.4G"; //Your Wifi name const char*
wifi_pass = "basemu.com"; //Your Wifi password
WiFiServer server(80); //Port 80
在setup函數(shù)中,會使用上面的Wi-Fi信數(shù)據(jù)將ESP32連接到的Wi-Fi網(wǎng)絡(luò)。如果連接到網(wǎng)絡(luò)成功,那么“connection successful”將顯示在串口監(jiān)視器上。否則,程序?qū)⒗^續(xù)嘗試,直到連接到Wi-Fi網(wǎng)絡(luò)。
Serial.print("Connecting to ");
Serial.print(wifi_name);
WiFi.begin(wifi_name, wifi_pass); //Connecting to wifi network
while (WiFi.status() != WL_CONNECTED) { //Waiting for the response of wifi network
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("Connection Successful");
下面的命令會將IP地址顯示在串口監(jiān)視器上。
Serial.println(WiFi.localIP());
然后程序?qū)臃?wù)器,以便程序能夠接收和發(fā)送數(shù)據(jù)到瀏覽器上。
server.begin();
在loop函數(shù)中,程序能夠從傳感器讀取數(shù)據(jù)并存儲在變量中,這樣就可以在網(wǎng)頁上顯示數(shù)據(jù)了。
String pressure = String(bmp.readPressure());
pressure.toCharArray(pressure_value, 4);
float hum = dht.readHumidity();
float temp = dht.readTemperature();
float fah = dht.readTemperature(true);
float heat_index = dht.computeHeatIndex(fah, hum);
float heat_indexC = dht.convertFtoC(heat_index);
然后檢查客戶端是否有發(fā)送HTTP請求,如果有客戶端請求可用,那么程序?qū)⒋鎯Σ@示結(jié)果在串行監(jiān)視器上。在請求結(jié)束時,程序?qū)l(fā)送HTML命令,在網(wǎng)頁上顯示傳感器的數(shù)據(jù)。
WiFiClient client = server.available(); //Checking for incoming clients
if (client){
Serial.println("new client");
String currentLine = ""; //Storing the incoming data in the string
while (client.connected()){
if (client.available()) //if there is some client data available
{
char c = client.read(); // read a byte
if (c == '\n') // check for newline character,
{
if (currentLine.length() == 0) //if line is blank it means it’s the end of the client HTTP
request { client.print("");
client.print("
ESP32 Weather Station
");
client.print("Temperature in C: ");
client.print(temp);
client.print(" Temperature in fah: ");
client.print(fah);
client.print(" Humidity is: ");
client.print(hum);
氣象站如何使用
首先,將代碼中的Wi-Fi名稱和密碼信息替換為你自己的。然后上傳代碼并打開串口監(jiān)視器。串口監(jiān)視器將顯示如下圖所示的IP地址。
在瀏覽器中輸入這個IP地址。輸入IP地址后,網(wǎng)頁會顯示如下圖所示。
現(xiàn)在傳感器數(shù)據(jù)就從氣象站上傳到網(wǎng)頁上了。
-
氣象站
+關(guān)注
關(guān)注
1文章
766瀏覽量
15964 -
DHT22
+關(guān)注
關(guān)注
2文章
51瀏覽量
7313 -
ESP32
+關(guān)注
關(guān)注
20文章
992瀏覽量
18470
發(fā)布評論請先 登錄
相關(guān)推薦
如何使用miniprog3設(shè)置氣象站
基于機(jī)智云gokit4.0(G)和MDM9206的 小型智能氣象站
怎樣去設(shè)計(jì)一個基于ESP32的家庭氣象站系統(tǒng)
DIY一個基于ESP8266的wifi氣象站
使用ESP8266和ST7735 TFT顯示屏設(shè)計(jì)氣象站
帶有esp8266和Python Flask的桌面氣象站

基于ESP8266的自動氣象站
基于ESP8266的自動氣象站
WIoT2氣象站之Nextion TFT with ESP8266/ESP32

使用Wio Terminal和Tensorflow Lite創(chuàng)建智能氣象站

評論