我有一些代码可以执行多项操作,我想了解一些有关各种中断的信息。我的大多数代码都是基于事件的。所以我使用计时器来调用某些事件。但是,当某些事件发生时,我为不同事件配置的中继最终会在短时间内打开和关闭。
例如,一段代码进入互联网并读取我服务器中文件夹中文本文件的状态。如果值为0,则继电器关闭,如果值为1,则继电器打开。目前,我每30秒轮询一次,以便读取文件的更改。我有php脚本,允许我写入该文本文件。与此同时,我还有另一个每小时向网络发送数据的事件。当我发送数据时,短时间内继电器的状态会根据发送数据之前的初始状态而开启或关闭。
它有点烦人,因为我知道当我知道文本文件没有改变它的状态时继电器应该保持打开或关闭,因为我没有写入它。所以另一个事件以某种方式导致继电器打开或关闭,而我没有告诉它。这是怎么回事?中断会起作用吗?内部外部?我仍然希望发送数据,并且仍然希望中继的状态说明相同,因此它们都具有优先权。您如何在代码中对此进行说明?
下面张贴了一段代码。正如你每隔6分钟或360秒看到的那样,我发出请求并发送数据,每20秒我发一次请求读取文件。因此,当6分钟事件的时间警报响起时,有一个简短的5-10状态,其他事件的继电器开启或关闭。
#include "Wire.h"
#include "DS1307RTC.h"
#include "DHT.h"
#include "PID_v1.h"
#include "SPI.h"
#include "Time.h"
#include "TimeAlarms.h"
#include "WiFi.h"
#define SensorPin 2
#define Offset 0
#define DHTPIN 2
#define DHTTYPE DHT11
#define RELAY_ON 1
#define RELAY_OFF 0
#define Relay1 9
#define Relay2 8
#define Relay3 6
#define Relay4 5
#define Relay5 4
#define Relay6 3
#define Relay7 38
#define Relay8 39
char server[] = "xx"; //IPAddress server(54,200,69,220);
char ssid[] = "xx"; //SSID Home Network Name
char pass[] = "xx"; //Key or Network Password
int status = WL_IDLE_STATUS;
int analogChannel0 = 0; //Soil Moisture Sensor
int flag = 0;
unsigned long int avgValue;
WiFiClient client;
int xx = 300;
int zz = 20;
int ww = 360;
double Setpoint, Input, Output;
//onTime(18,20,25)
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Setpoint = 7;
dht.begin();
Wire.begin();
//myPID.SetSampleTime(30000);
digitalWrite(Relay1, RELAY_ON);
digitalWrite(Relay2, RELAY_ON);
digitalWrite(Relay3, RELAY_ON);
digitalWrite(Relay4, RELAY_ON);
digitalWrite(Relay5, RELAY_ON);
digitalWrite(Relay6, RELAY_ON);
digitalWrite(Relay7, RELAY_ON);
digitalWrite(Relay8, RELAY_ON);
pinMode(Relay1, OUTPUT);
pinMode(Relay2, OUTPUT);
pinMode(Relay3, OUTPUT);
pinMode(Relay4, OUTPUT);
pinMode(Relay5, OUTPUT);
pinMode(Relay6, OUTPUT);
pinMode(Relay7, OUTPUT);
pinMode(Relay8, OUTPUT);
Serial.begin(9600);
setSyncProvider(RTC.get);
Alarm.timerRepeat(ww, SendData);
Alarm.timerRepeat(zz, ReadData);
while ( status != WL_CONNECTED) {
status = WiFi.begin(ssid, pass);
}
} //End of Void Setup
void loop() {
Alarm.delay(0);
} //End of Loop
void SendData(){
if (client.connect(server, 80)) {
tmElements_t tm;
int SoilMoisture = analogRead(analogChannel0);
client.print("GET xx");
client.print("&SoilMoisture=");
client.print(SoilMoisture);
client.print("&Setpoint=");
client.println(Setpoint);
}
}
void ReadData()
{
if (client.connect(server, 80)) {
client.println("GET xx.txt HTTP/1.1");
client.println("Host: xx");
client.println("Connection: close"); //tell the server that we expect it to close the connection. if we dont do this it should keep the connection open.
client.println();
}
flag = 0;
while (client.available()) {
char c = client.read();
Serial.write(c);
if (c == '0'){
flag = 0;
}
if (c == '1'){
flag = 1;
}
}
if (flag == 0) {
digitalWrite(Relay3, LOW);
}
if (flag == 1) {
digitalWrite(Relay3, HIGH);
}
}