Jay
Jay
ESP8266配合sever酱实现下雨报警
ESP8266配合sever酱实现下雨报警

作为老工具人,难免会被当做监视器。整天叫我看着有没下雨,这谁顶得住。于是乎想着用ESP8266做一个下雨自动报警,省的去看天气咯。

ESP8266在淘宝上购得,加上一个雨滴传感器和DHT11,一共25块包邮。

DOITING公司生产的ESP-12F

参数如下:

  • Operating voltage: 3.3V (3.0 ~ 3.6V)
  • Working environment temperature: -40 – 85 °C
  • CPU Tensilica L106
  1. RAM 50KB (available)说是512k的内存
  2. Flash 32Mbit
  • System
  1. 802.11 b/g/n
  2. Frequency range 2.4 GHz ~ 2.5 GHz (2400 MHz ~ 2483.5 MHz)
  3. Built-in Tensilica L106 ultra-low power 32-bit micro MCU with 16-bit thin mode, supporting 80 MHz and 160 frequency
  4. MHz, support for RTOS
  5. WIFI @2.4 GHz, support WPA/WPA2 security mode
  6. Support UART, I2C, GPIO, PWM, SDIO, SPI, ADC, PWM, IR
  7. Built-in 10 bit high precision ADC
  8. Support TCP, UDP, HTTP, FTP
  9. Built-in TR switch, balun, LNA, power amplifier and matching network
  10. Built-in PLL, voltage regulator, and power management components + 20 dBm output power in 802.11b mode
  11. The average operating current is 80mA, the deep sleep holding current is 20uA, and the shutdown current is less than 5uA.
  12. Can double as application processor SDIO 2.0, SPI, UART
  13. Wake up, connect and pass packets within 2ms
  14. Standby power consumption is less than 1.0mW (DTIM3)
  15. Support local serial port burning, cloud upgrade, host download and burning
  16. Support Station / SoftAP / SoftAP + Station wireless network mode

这里我们选择Arduino IDE进行开发

配置Arduino IDE

使用Arduino IDE 进行开发,需要对其进行配置

进入IDE ,点击文件-首选项

Arduino 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将会灭掉,此时将水滴在传感器上你的微信就会收到消息。

进阶

通过调用天气API以及DHT11输出天气信息

使用VSCode编写代码并烧录

将ESP8266接入Home Assistant

发表评论

textsms
account_circle
email

Jay

ESP8266配合sever酱实现下雨报警
作为老工具人,难免会被当做监视器。整天叫我看着有没下雨,这谁顶得住。于是乎想着用ESP8266做一个下雨自动报警,省的去看天气咯。 ESP8266在淘宝上购得,加上一个雨滴传感器和DHT11…
扫描二维码继续阅读
2020-06-10