在这个项目中,我们将使用esp32创建一个气象站。基本原理是通过读取dht22和bmp180传感器的数据,然后使用esp32传输创建的网页上,在网页上显示气象数据。
电路图
首先,将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;
bmp180 scl —–> esp32 / pin 22;(esp32的22号引脚是scl.)
bmp180 sda —–> 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
}
}
}
}
}
气象站项目代码释义
首先,确保项目所需的所有库均 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地址。
在浏览器中输入这个ip地址。输入ip地址后,网页会显示如下图所示。
现在传感器数据就从气象站上传到网页上了。
硅谷激光雷达Cepton:11个关于激光雷达的“谣言”
奥地利微电子推出业界首款具有嵌入式比特整形功能的FlexRa
苹果ios15系统什么时候出 ios15正式版什么时候推送
华为:3亿台设备即将升级鸿蒙系统
电路的响应
如何使用ESP32创建一个气象站
努比亚Z17发布会7点正式开始:看努比亚Z17发布会参与抽奖活动,努比亚Z17发布会视频直播、官方在线视频观看
电瓶修复—电动车电池虚标可怕的信号2
泰凌微电子丨TLSR827x系列低功耗多协议物联网SoC
光电液位开关与液位传感器有什么区别
拓普微的智能显示模块的优势介绍
中科院丁汉院士:智能制造两大核心技术
关于2GR-FKS/FXS 3.5L V6直喷汽油机的性能开发分析
LCD1602驱动为什么把字符代码写入DDRAM?
传祺GS4改装芬朗汽车音响“打造传奇之声”
普渡机器人走进泰国曼谷 迅速成为异国“新宠儿”
2K屏+双曲面,逆天的iPhone8终于来了!
BOSHIDA AC-DC电源模块元器件的损耗
深入分析eBPF加速容器网络转发的原理
浪潮信息提出基于配置模板的一键自动部署运维方式