0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示

如何使用ESP32创建一个气象站

科技观察员 来源:八色木 作者:八色木 2022-04-12 15:56 次阅读

在这个项目中,我们将使用ESP32创建一个气象站。基本原理是通过读取DHT22和BMP180传感器的数据,然后使用ESP32传输创建的网页上,在网页上显示气象数据。

电路图

poYBAGJVMCaAOFi4AAJF76tzN-U325.png

首先,将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的引脚图

pYYBAGJVMCuAF3VnAASJjiG1i4M778.png

气象站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

}

}

}

}

}

气象站项目代码释义

首先,确保项目所需的所有库均 include 了,然后定义连接DHT22温度和湿度传感器的引脚,再创建实例:

#include

#include

#include

#include

#define DHTPIN 15

#define DHTTYPE DHT22 DHT dht(DHTPIN, DHTTYPE);

Adafruit_BMP085 bmp;

接着存储Wi-Fi名称和密码,同时定义并创建服务器的端口

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函数中,会使用上面的Wi-Fi信数据将ESP32连接到的Wi-Fi网络。如果连接到网络成功,那么“connection successful”将显示在串口监视器上。否则,程序将继续尝试,直到连接到Wi-Fi网络。

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地址显示在串口监视器上。

Serial.println(WiFi.localIP());

然后程序将启动服务器,以便程序能够接收和发送数据到浏览器上。

server.begin();

在loop函数中,程序能够从传感器读取数据并存储在变量中,这样就可以在网页上显示数据了。

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);

然后检查客户端是否有发送HTTP请求,如果有客户端请求可用,那么程序将存储并显示结果在串行监视器上。在请求结束时,程序将发送HTML命令,在网页上显示传感器的数据。

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名称和密码信息替换为你自己的。然后上传代码并打开串口监视器。串口监视器将显示如下图所示的IP地址。

poYBAGJVMD6AevTtAAEX2X1WMBw112.png

在浏览器中输入这个IP地址。输入IP地址后,网页会显示如下图所示。

poYBAGJVMEKASlNGAAB5kV5QdpM722.png

现在传感器数据就从气象站上传到网页上了。

声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
  • 气象站
    +关注

    关注

    1

    文章

    692

    浏览量

    15339
  • DHT22
    +关注

    关注

    2

    文章

    49

    浏览量

    7067
  • ESP32
    +关注

    关注

    13

    文章

    896

    浏览量

    15804
收藏 人收藏

    评论

    相关推荐

    LabVIEW自动气象站演示

    基于LabVIEW自动气象站演示,包括可执行文件及源代码
    发表于 04-01 21:59

    如何使用miniprog3设置气象站

    你好,可以请人帮我,我想计划的cy3271-exp1 PSoC气象站板采用miniprog3这样我可以把周围的光强读数然后发送阅读通过串行命令我写的应用程序并存储的数据画成了GR促性腺激素释放
    发表于 04-15 08:32

    采用LabVIEW实现虚拟自动气象站设计

    设计的虚拟自动气象站系统主要由数据采集、数据处理、数据显示、数据保存四模块构成。如果数据采集周期没有特别要求,可以将这四模块通过数据流连接起来,即每采集次数据都需要经过数据处理、
    发表于 04-16 09:40

    如何使用树莓派制作小气象站

    自动气象站可以实时探测气温、湿度、气压、风速、风向、降雨量、紫外线辐射等气象信息,通过不同的传感器采集地面气象数据,数据采集完成后通过网络统传输到
    发表于 05-19 07:59

    基于机智云gokit4.0(G)和MDM9206的 小型智能气象站

    和关闭,以便根据土壤湿度进行灌溉控制。硬件说明本气象站以MDM9206模块作为SOC控制器,使用机智云平台提供的软硬件平台开发工具进行开发本项目。也可以使用esp8266/ESP32或具有GPRS功能
    发表于 07-19 16:18

    基于51单片机的气象站系统功能描述

    基于51单片机的气象站系统功能描述,使用51单片机实现气象站的全部功能,如温湿度、大气压强,光照度、风向、风强的检测,并有LCD12864、GSM、整流稳压等电路的部分。气象站系统电路原理图和PCB
    发表于 11-19 08:59

    怎样去设计基于ESP32的家庭气象站系统

    或 毕业设计技术解答毕设帮助:7468760412 主要器件本项目学长将使用ESP32创建气象站。基本原理是通过读取DHT22和BMP
    发表于 12-13 07:38

    分享太阳能WiFi气象站的设计方案

    计算器。第 12 步:ESP32 - Thingspeak-Deep-Sleep我们气象站的核心是 ESP8266 SOC,它是
    发表于 06-20 07:58

    DIY基于ESP8266的wifi气象站

    描述气象站ESP8266 E12带 ESPHome 的 DIY WiFi 停止处理不断变化的天气软件和 API,自己获取传感器数据
    发表于 06-24 07:26

    分享气象站项目

    描述气象站 | 风向
    发表于 07-11 07:16

    Wemos Mini Di继电器和气象站

    描述Wemos Mini Di继电器和气象站Wemos D1 迷你转接板,用于 3 个中继或 2 个中继+气象站
    发表于 08-17 06:26

    使用ESP8266和ST7735 TFT显示屏设计气象站

    描述基于 ESP8266 Nodemcu 和 ST7735 TFT 显示屏的气象站什么是气象站气象站种使用不同传感器收集与天气和环境相
    发表于 09-01 06:52

    如何使用Openweathermap和0.96英寸oled来制作气象站

    我是 ESP8266 的新手。我想通过使用 Openweathermap 和 0.96 英寸 oled 来尝试气象站。当我尝试编译代码时,出现此错误。怎么了? 调用使用属性错误声
    发表于 06-02 10:16

    ESP32气象站接口PCB屏蔽

    电子发烧友网站提供《ESP32气象站接口PCB屏蔽.zip》资料免费下载
    发表于 07-18 10:36 3次下载
    <b class='flag-5'>ESP32</b><b class='flag-5'>气象站</b>接口PCB屏蔽

    WIoT2气象站之Nextion TFT with ESP8266/ESP32

    电子发烧友网站提供《WIoT2气象站之Nextion TFT with ESP8266/ESP32.zip》资料免费下载
    发表于 01-30 11:58 1次下载
    WIoT2<b class='flag-5'>气象站</b>之Nextion TFT with <b class='flag-5'>ESP</b>8266/<b class='flag-5'>ESP32</b>