作为老工具人,难免会被当做监视器。整天叫我看着有没下雨,这谁顶得住。于是乎想着用ESP8266做一个下雨自动报警,省的去看天气咯。
ESP8266在淘宝上购得,加上一个雨滴传感器和DHT11,一共25块包邮。
参数如下:
- Operating voltage: 3.3V (3.0 ~ 3.6V)
- Working environment temperature: -40 - 85 °C
- CPU Tensilica L106
- RAM 50KB (available)说是512k的内存
- Flash 32Mbit
- System
- 802.11 b/g/n
- Frequency range 2.4 GHz ~ 2.5 GHz (2400 MHz ~ 2483.5 MHz)
- Built-in Tensilica L106 ultra-low power 32-bit micro MCU with 16-bit thin mode, supporting 80 MHz and 160 frequency
- MHz, support for RTOS
- WIFI @2.4 GHz, support WPA/WPA2 security mode
- Support UART, I2C, GPIO, PWM, SDIO, SPI, ADC, PWM, IR
- Built-in 10 bit high precision ADC
- Support TCP, UDP, HTTP, FTP
- Built-in TR switch, balun, LNA, power amplifier and matching network
- Built-in PLL, voltage regulator, and power management components + 20 dBm output power in 802.11b mode
- The average operating current is 80mA, the deep sleep holding current is 20uA, and the shutdown current is less than 5uA.
- Can double as application processor SDIO 2.0, SPI, UART
- Wake up, connect and pass packets within 2ms
- Standby power consumption is less than 1.0mW (DTIM3)
- Support local serial port burning, cloud upgrade, host download and burning
- Support Station / SoftAP / SoftAP + Station wireless network mode
这里我们选择Arduino IDE进行开发
配置Arduino IDE
使用Arduino IDE 进行开发,需要对其进行配置
进入IDE ,点击文件-首选项
在URL处添加http://arduino.esp8266.com/stable/package_esp8266com_index.json
点击保存,再进入工具-开发板-开发板管理器,搜索ESP8266进行安装(最好挂梯子)
安装完成后进入工具-开发板就能看到ESP8266的数据,这里选择WeMos D1 R1
并将Ports改为你的端口。至此,IDE配置完成。
代码编写
将雨滴传感器的VCC接到3.3V引脚,GND接到GND引脚,A0接到A0引脚
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#define LED 2
#ifndef STASSID
#define STASSID "你的WIFI名"
#define STAPSK "密码"
#endif
//-----WiFi settings------------
const char *ssid = STASSID;
const char *password = STAPSK;
//------------------------------
String key = "server酱key";
void setup()
{
Serial.begin(115200);
pinMode(LED, OUTPUT);
digitalWrite(LED, 0);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void wechat(String title, String message)
{
HTTPClient http;
//发送post请求,把消息发出去
http.begin("http://sc.ftqq.com/" + key + ".send?text=" + title + "&desp=" + message);
int httpCode = http.GET();
http.end();
//结束
}
void loop()
{
digitalWrite(LED, 1);
int sensorValue = analogRead(A0);
if (sensorValue < 888)
{
wechat("可能下雨了", "传感器表面可能有水或者下雨了,请检查");
}
delay(2000);
}
点击上传至ESP8266,当(wifi)配置正确,LED将会灭掉,此时将水滴在传感器上你的微信就会收到消息。
进阶
使用VSCode编写代码并烧录
发表评论