D1 MINI 制作可测温、土壤湿度的智能浇水器
本帖最后由 jerryiot 于 2022-2-18 15:26 编辑D1 MINI + 2路继电器 制作可测温、土壤湿度的智能浇水器,可设置浇水的土壤湿度阈值、浇水的时间周期,当检测到土壤湿度低于阈值时自动浇水。
成品:https://www.bilibili.com/video/BV1A34y1y7s7?share_source=copy_web
需要的材料:D1MIINI芯片、2路 继电器、DS18B20测温度、220V-5V稳压、还有土壤湿度传感器https://cdn.jerrycoding.com/media/ckeditor_upload/2022/02/18/_20220218144533.jpghttps://cdn.jerrycoding.com/media/ckeditor_upload/2022/02/18/_20220218150032_dfUAazD.jpg自己用洞洞板焊接了一下,技术很捉急,惨不忍睹,背面就不展示了。组装起来就可以了:https://cdn.jerrycoding.com/media/ckeditor_upload/2022/02/18/_20220218144539_YQ8gLUo.jpg设计了一下点灯的APP界面:https://cdn.jerrycoding.com/media/ckeditor_upload/2022/02/18/_20220218144542.jpg界面可以获取DS18B20测量的室温,土壤湿度,WIFI信号强度以及两个开关:浇水泵和循环水泵。浇水启动一次持续10s,循环水泵是用来沼泽过滤的,冬天沼泽过滤撤掉了。最后贴一下代码:#define BLINKER_WIFI
#include <Blinker.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 2 // DS18B20 on NodeMCU pin D4
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);
char auth[] = "";
char ssid[] = "";
char pswd[] = "";
// 新建组件对象
BlinkerButton Button1("btn-w");
BlinkerButton Button2("btn-s");
BlinkerNumber TEMP("temp");
BlinkerNumber JSCOUNT("jscount");
BlinkerNumber SYSRSSI("rssi");
BlinkerNumber SOILVALUE("soil");
BlinkerText SYSTIME("systime");
BlinkerSlider SliderWP("wp");
BlinkerSlider SliderWY("wy");
//定义土壤湿度
#define soilPin A0
//Relay
#define RELAY1 D5
#define RELAY2 D6
float temp_read = 0;
//浇水计数器
int TOTALCOUNT=0;
//浇水阈值(土壤湿度低于该值浇水)
int WY = 20;
//浇水周期(单位小时)
int WP = 4;
void sliderWYcallback(int32_t value)
{
BLINKER_LOG("get slider value: ", value);
WY = value;
}
void sliderWPcallback(int32_t value)
{
BLINKER_LOG("get slider value: ", value);
WP = value;
}
void heartbeat()
{
TEMP.print(temp_read);
String tstr = getTime();
int tmpsoil = getSoilValue();
SYSRSSI.print(WiFi.RSSI());
SYSTIME.print(tstr);
SOILVALUE.print(tmpsoil);
JSCOUNT.print(TOTALCOUNT);
SliderWP.print(WP);
SliderWY.print(WY);
if (digitalRead(RELAY2) == HIGH)
{
Button2.color("#708090");
Button2.print("off");
}
else if(digitalRead(RELAY2) == LOW)
{
Button2.color("#6666ff");
Button2.print("on");
}
}
// 按下按键即会执行该函数
void button1_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
startRainOneTime();
}
void button2_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
if (state == BLINKER_CMD_ON) {
digitalWrite(RELAY2, LOW);
Button2.color("#6666ff");
Button2.print("on");
}
else if (state == BLINKER_CMD_OFF) {
digitalWrite(RELAY2, HIGH);
Button2.color("#708090");
Button2.print("off");
}
}
String getTime()
{
uint32_t time=millis();
int day=time / 86400000;
int hour=(time-(86400000*day)) / 3600000;
int min=(time-(86400000*day)-(3600000*hour)) / 60000;
String timeStr=String(day)+"天"+String(hour)+"时"+String(min)+"分";
if (hour > 0 and hour % WP == 0 and getSoilValue() < WY)
{
Blinker.print(timeStr);
Blinker.print("自动浇水一次!");
startRainOneTime();
}
return timeStr;
}
int getSoilValue()
{
int soilValue;
int soilMoisture;
//读取土壤传感器上的电压值
soilValue = analogRead(soilPin);
//把电压值按照映射到
soilMoisture = map(soilValue,0,1023,100,0);
return soilMoisture;
}
void startRainOneTime()
{
//继电器开10s 用于浇水
digitalWrite(RELAY1, !digitalRead(RELAY1));
Blinker.delay(10000);
digitalWrite(RELAY1, !digitalRead(RELAY1));
TOTALCOUNT++;
}
void setup()
{
// 初始化串口
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
BLINKER_DEBUG.debugAll();
// 初始化有LED的IO
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
//Relay
pinMode(RELAY1, OUTPUT);
digitalWrite(RELAY1, HIGH);
pinMode(RELAY2, OUTPUT);
digitalWrite(RELAY2, LOW);
// 初始化blinker
Blinker.begin(auth, ssid, pswd);
Button1.attach(button1_callback);
Button2.attach(button2_callback);
SliderWP.attach(sliderWPcallback);
SliderWY.attach(sliderWYcallback);
Blinker.attachHeartbeat(heartbeat);
DS18B20.begin();
}
void loop() {
Blinker.run();
DS18B20.requestTemperatures();
temp_read = DS18B20.getTempCByIndex(0);
Serial.print(temp_read);
BLINKER_LOG("temp: ", temp_read);
Blinker.delay(2000);
}
能发下电路图学习一下吗 510506404 发表于 2022-4-9 01:30
能发下电路图学习一下吗
没画过电路图 就根据代码连就好了
Blinker的库文件怎么下载呀
暮光大盗 发表于 2022-5-4 19:23
Blinker的库文件怎么下载呀
会了会了 学习了,谢谢楼主 楼主,能发一下点灯app软件各组件对应的代码名称吗?:D 暮光大盗 发表于 2022-5-5 19:00
楼主,能发一下点灯app软件各组件对应的代码名称吗?
// 新建组件对象
BlinkerButton Button1("btn-w");
BlinkerButton Button2("btn-s");
BlinkerNumber TEMP("temp");
BlinkerNumber JSCOUNT("jscount");
BlinkerNumber SYSRSSI("rssi");
BlinkerNumber SOILVALUE("soil");
BlinkerText SYSTIME("systime");
BlinkerSlider SliderWP("wp");
BlinkerSlider SliderWY("wy");
jerryiot 发表于 2022-5-5 19:02
// 新建组件对象
BlinkerButton Button1("btn-w");
BlinkerButton Button2("btn-s");
可以发一下手机app上的截图吗? 暮光大盗 发表于 2022-5-5 19:07
可以发一下手机app上的截图吗?
时间太久了 已经 改了很多版了
页:
[1]
2