现学现卖,diy了一个远程浇花的原型-Arduino中文社区 - Powered by Discuz! Archiver

半支红梅 发表于 2022-10-2 13:11

现学现卖,diy了一个远程浇花的原型

本帖最后由 半支红梅 于 2022-10-2 13:21 编辑

假期第一天,热得出奇,呆家里折腾出这个”半成品“,期望用它来监控阳台的环境,并可以实现远程浇花。至于今年夏天,我家阳台干死了许多花花,那就是另一个故事了。


APP界面:


使用到的材料:





esp32-c3-12f-kit


5v继电器


bh1750亮度传感器


dht11温湿度传感器

接线:
开发板<====>外设
io19                DHT11 out脚
io18                BH1750 SDA脚
io9                  BH1750 SCL脚
io8                  继电器 in脚



需要注意的点:
板子上的丝印不一定是正确的!
这个BH1750的sda,scl脚印反了,导致我一度怀疑这个模块坏掉了。
我的这块esp32-c3-12f-kit开发板的丝印居然也是错的!花了不少时间才跳出这个坑。


以下是代码:


/* *****************************************************************
*
* Download latest Blinker library here:
* https://github.com/blinker-iot/blinker-library/archive/master.zip
*
*
* Blinker is a cross-hardware, cross-platform solution for the IoT.
* It provides APP, device and server support,
* and uses public cloud services for data transmission and storage.
* It can be used in smart home, data monitoring and other fields
* to help users build Internet of Things projects better and faster.
*
* Make sure installed 2.7.4 or later ESP8266/Arduino package,
* if use ESP8266 with Blinker.
* https://github.com/esp8266/Arduino/releases
*
* Make sure installed 1.0.5 or later ESP32/Arduino package,
* if use ESP32 with Blinker.
* https://github.com/espressif/arduino-esp32/releases
*
* Docs: https://diandeng.tech/doc
*      
*
* *****************************************************************
*
* Blinker 库下载地址:
* https://github.com/blinker-iot/blinker-library/archive/master.zip
*
* Blinker 是一套跨硬件、跨平台的物联网解决方案,提供APP端、设备端、
* 服务器端支持,使用公有云服务进行数据传输存储。可用于智能家居、
* 数据监测等领域,可以帮助用户更好更快地搭建物联网项目。
*
* 如果使用 ESP8266 接入 Blinker,
* 请确保安装了 2.7.4 或更新的 ESP8266/Arduino 支持包。
* https://github.com/esp8266/Arduino/releases
*
* 如果使用 ESP32 接入 Blinker,
* 请确保安装了 1.0.5 或更新的 ESP32/Arduino 支持包。
* https://github.com/espressif/arduino-esp32/releases
*
* 文档: https://diandeng.tech/doc
*      
*
* *****************************************************************/

#define BLINKER_WIFI

#include <Blinker.h>

char auth[] = "Your Device Secret Key";
char ssid[] = "Your WiFi network SSID or name";
char pswd[] = "Your WiFi network WPA password or WEP key";

BlinkerNumber HUMI("humi");
BlinkerNumber TEMP("temp");
BlinkerNumber LUX("lux");
BlinkerButton PUMP("pump");

// Download Adafruit DHT-sensor-library library here:
// https://github.com/adafruit/DHT-sensor-library
#include <DHT.h>

#define DHTPIN 19

#define DHTTYPE DHT11// DHT 11
// #define DHTTYPE DHT22   // DHT 22(AM2302), AM2321
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

DHT dht(DHTPIN, DHTTYPE);

#include <BH1750.h>
#include <Wire.h>

#define BH1750SCL 18
#define BH1750SDA 9

BH1750 lightMeter;

#define PUMPPIN 8

uint32_t read_time = 0;

float humi_read, temp_read, lux_read;

void dataRead(const String& data) {
BLINKER_LOG("Blinker readString: ", data);

Blinker.vibrate();

uint32_t BlinkerTime = millis();

Blinker.print("millis", BlinkerTime);
}

// 按下按键即会执行该函数
void switch_callback(const String& state) {
BLINKER_LOG("get button state: ", state);
digitalWrite(PUMPPIN, !digitalRead(PUMPPIN));
Blinker.vibrate();

if (state == BLINKER_CMD_ON) {
    BLINKER_LOG("Toggle on!");
    PUMP.print("on");
} else if (state == BLINKER_CMD_OFF) {
    BLINKER_LOG("Toggle off!");
    PUMP.print("off");
} else {
    BLINKER_LOG("Get user setting: ", state);
    PUMP.print();
}
}

void heartbeat() {
HUMI.print(humi_read);
TEMP.print(temp_read);
LUX.print(lux_read);
}

void setup() {
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);

pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);

pinMode(PUMPPIN, OUTPUT);
digitalWrite(PUMPPIN, LOW);

Blinker.begin(auth, ssid, pswd);
Blinker.attachData(dataRead);
Blinker.attachHeartbeat(heartbeat);
PUMP.attach(switch_callback);

Wire.begin(BH1750SCL, BH1750SDA);
// On esp8266 you can select SCL and SDA pins using Wire.begin(D4, D3);

if (lightMeter.begin(BH1750::CONTINUOUS_HIGH_RES_MODE)) {
    Serial.println(F("BH1750 Advanced begin"));
} else {
    Serial.println(F("Error initialising BH1750"));
}

dht.begin();
}

void loop() {
Blinker.run();

if (read_time == 0 || (millis() - read_time) >= 2000) {
    read_time = millis();

    float h = dht.readHumidity();
    float t = dht.readTemperature();

    if (isnan(h) || isnan(t)) {
      BLINKER_LOG("Failed to read from DHT sensor!");
      return;
    }

    float hic = dht.computeHeatIndex(t, h, false);

    humi_read = h;
    temp_read = t;

    BLINKER_LOG("Humidity: ", h, " %");
    BLINKER_LOG("Temperature: ", t, " *C");
    BLINKER_LOG("Heat index: ", hic, " *C");

    if (lightMeter.measurementReady()) {
      float lux = lightMeter.readLightLevel();
      lux_read = lux;
      BLINKER_LOG("Light: ", lux, " lx");
    }
}
}


APP界面配置:

{¨version¨¨2.0.0¨¨config¨{¨headerColor¨¨transparent¨¨headerStyle¨¨dark¨¨background¨{¨img¨¨assets/img/bg/f4.jpg¨¨isFull¨»}}¨dashboard¨|{¨type¨¨deb¨¨mode¨É¨bg¨É¨cols¨Ñ¨rows¨Í¨key¨¨debug¨´x´É´y´¤C¨lstyle¨Ê}{ßC¨num¨¨t0¨¨湿度¨¨ico¨¨fad fa-humidity¨¨clr¨¨#076EEF¨¨min¨É¨max¨¢1c¨uni¨´%´ßFÉßGËßHËßI¨humi¨´x´É´y´Ñ¨rt¨«ßKÉ}{ßCßLßM¨温度¨ßO¨fad fa-thermometer-three-quarters¨ßQ¨#00A90C¨ßSÉßT¢2QßU´℃´ßFÉßGËßHËßI¨temp¨´x´Ë´y´ÑßW«ßKÉ}{ßCßLßM¨亮度¨ßO¨fad fa-sun¨ßQ¨#FBA613¨ßSÉßT¢H31ßU¨lx¨ßFÉßGÍßHËßI¨lux¨´x´Í´y´ÑßW«ßKÊ}{ßC¨btn¨ßO¨fad fa-power-off¨ßEÊßM¨水泵¨¨t1¨¨文本2¨ßFÌßGËßHËßI¨pump¨´x´Ì´y´ÍßKÊßQßZ}÷¨actions¨|÷¨triggers¨|÷ßW|÷}

Highnose 发表于 2022-10-2 15:59

你这浇了,花又淹死了
页: [1]
查看完整版本: 现学现卖,diy了一个远程浇花的原型