2022年2月18日
Jerry
3676
2023年11月4日
D1 MINI + 2路继电器 制作可测温、土壤湿度的智能浇水器,可设置浇水的土壤湿度阈值、浇水的时间周期,当检测到土壤湿度低于阈值时自动浇水。
成品:
需要的材料:
D1MIINI芯片、2路 继电器、DS18B20测温度、220V-5V稳压、还有土壤湿度传感器
自己用洞洞板焊接了一下,技术很捉急,惨不忍睹,背面就不展示了。
组装起来就可以了:
设计了一下点灯的APP界面:
界面可以获取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);
//把电压值按照[0,1023]映射到[100,0]
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);
}
原创文章,转载请注明出处:
https://jerrycoding.com/article/smart-water
《物联网开发》
0
微信
支付宝