我正在尝试打开电机驱动器,这将使水泵能够使用我使用 Blynk 创建的简单应用程序打开。但是,互联网连接仅保持大约 5 秒钟,然后断开连接。它连续执行此操作,因此整个系统无法正常工作。我曾尝试在它连接到互联网的短时间内测试该按钮,但它也无法正常工作。该按钮是我想用来打开或关闭电机驱动器的按钮。
这是我的代码:
#define BLYNK_PRINT Serial
#include <SoftwareSerial.h>
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
int sensor_raw_data;
int mapped_data;
int pump;
SoftwareSerial esdata(3,4);
#define auth "XXXXXXX"
#define ssid "XXXXXXX"
#define pass "XXXXXXX"
ESP8266 wifi(&esp8266);
// Send AT commands
String sendAT(String cmd, const int tmeout) {
String rsp = "";
esdata.print(cmd);
long int time = millis();
while((time + tmeout) > millis()) {
while(esdata.available()) {
char c = esdata.read();
rsp += c;
}
}
Serial.print(rsp);
return rsp;
}
void setup() {
Serial.begin(9600);
esdata.begin(9600);
Blynk.begin(auth, wifi, ssid, pass);
sendAT("AT+RST\r\n", 2000);
sendAT("AT\r\n", 1000);
sendAT("AT+CWMODE=1\r\n", 1000);
sendAT("AT+CWJAP=\""said"\",\""pass"\"\r\n", 2000);
while(!esdata.find("OK")) {
}
sendAT("AT+CIFSR\r\n", 1000);
sendAT("AT+CIPMUX=0\r\n", 1000);
pinMode(A0, INPUT);
pinMode(8, OUTPUT);
}
void loop() {
Blynk.run();
sensor_raw_data = analogRead(A0);
mapped_data=map(sensor_raw_data, 0, 1023, 100, 0);
String sensor_value = String(mapped_data);
Serial.print("Sensor Data: ");
Serial.println(mapped_data);
// Display the soil moisture on a gauge. // This is working properly
Blynk.virtualWrite(V5, prdata);
// Send notification if soil moisture is below 80%
if(mapped_data < 80) {
Blynk.notify("Soil Moisture Is Below 80%. Turn On Water Pump.");
} else {
Blynk.notify("Soil Moisture Is Above 80%. Turn Off Water Pump.");
}
String pmp = String(pump);
updateTS(sensor_value, pmp);
delay(2000);
}
这是按钮的功能。在我的应用程序中,按钮是虚拟引脚 8。电机驱动器连接到 Arduino 上的数字引脚 8。功能有问题吗?或者也许实际上还有另一种方法可以让按钮打开或关闭泵。
BLYNK_WRITE(V8) {
int pinValue = param.asInt();
if(pinValue == 1) {
digitalWrite(8, HIGH);
// This is the value that will be displayed for the pump status in thingspeak API graph.
pump = 100;
} else {
digitalWrite(8, LOW);
pump = 0;
}
}