Модуль реле
- Войдите на сайт для отправки комментариев
Вс, 24/11/2019 - 12:03
Здравствуйте, после включения Arduino сразу появляется напряжение на пинах управления реле и реле естественно оказываются во включённом состоянии.
Как сделать, чтобы после включения/загрузки Arduino реле не включались, а включались только после подачи команды?
#include <dht11.h>
#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
#define DHT11_PIN 7
#define REQ_BUF_SZ 40
dht11 DHT;
File webFile;
char HTTP_req[REQ_BUF_SZ] = {0}; // buffered HTTP request stored as null terminated string
char req_index = 0; // index into HTTP_req buffer
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(192, 168, 3, 181);
EthernetServer server(80);
bool pin1;
bool pin2;
bool pin3;
bool pin4;
int pin5;
void setup() {
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(9, OUTPUT);
SD.begin(4);
Ethernet.begin(mac, ip);
server.begin();
pin1 = pin2 = pin3 = pin4 = 0;
}
void loop() {
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
if (req_index < (REQ_BUF_SZ - 1)) {
HTTP_req[req_index] = c; // save HTTP request character
req_index++;
}
if (c == '\n' && currentLineIsBlank) {
if (StrContains(HTTP_req, "GET /")) {
if (StrContains(HTTP_req, "/ ")
|| StrContains(HTTP_req, "/index.htm")) {
sendHtmlFile(client, "index.htm");
} else if (StrContains(HTTP_req, "/favicon.ico")) {
sendFile(client, "favicon.ico");
} else if (StrContains(HTTP_req, "/temp.png")) {
sendFile(client, "temp.png");
} else if (StrContains(HTTP_req, "/humid.png")) {
sendFile(client, "humid.png");
} else if (StrContains(HTTP_req, "/flame.png")) {
sendFile(client, "flame.png");
} else if (StrContains(HTTP_req, "/my.css")) {
sendFile(client, "my.css");
} else if (StrContains(HTTP_req, "ajax_flame")) {
sendBaseAnswer(client);
int smoke_gas = 0; //пин на котором подключен MQ-2
int sensorReading = analogRead(smoke_gas);
int chk;
chk = DHT.read(DHT11_PIN);
char buff[32];
sprintf(buff, "%d:%d:%d:%d:%d:%d:%d:%d:",
sensorReading, DHT.temperature, DHT.humidity,
digitalRead(2), digitalRead(3), digitalRead(5), digitalRead(6),
pin5);
client.println(buff);
} else if (StrContains(HTTP_req, "setpin?")) {
if (StrContains(HTTP_req, "pin=1")) {
pin1 = !pin1;
digitalWrite(2, pin1);
} else if (StrContains(HTTP_req, "pin=2")) {
pin2 = !pin2;
digitalWrite(3, pin2);
} else if (StrContains(HTTP_req, "pin=3")) {
pin3 = !pin3;
digitalWrite(5, pin3);
} else if (StrContains(HTTP_req, "pin=4")) {
pin4 = !pin4;
digitalWrite(6, pin4);
} else if (StrContains(HTTP_req, "pin=5")) {
String input = HTTP_req;
int posStart = input.indexOf("value=");
int posEnd = input.indexOf(' ', posStart);
String param = input.substring(posStart + 6, posEnd + 1);
pin5 = param.toInt();
analogWrite(9, pin5);
}
sendBaseAnswer(client);
}
}
req_index = 0;
StrClear(HTTP_req, REQ_BUF_SZ);
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
} else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
}
}
bool sendHtmlFile(EthernetClient client, char *fileName) {
webFile = SD.open(fileName);
sendBaseAnswer(client);
return sendFile(client, webFile);
}
bool sendFile(EthernetClient client, char *fileName) {
webFile = SD.open(fileName);
sendHttpOkAnswer(client);
client.println();
return sendFile(client, webFile);
}
bool sendFile(EthernetClient client, File &webFile) {
if (webFile) {
while (webFile.available())
client.write(webFile.read()); // send web page to client
webFile.close();
return 1;
}
return 0;
}
void sendBaseAnswer(EthernetClient client) {
sendHttpOkAnswer(client);
client.println(F("Content-Type: text/html"));
client.println(F("Connection: close"));
client.println();
}
void sendHttpOkAnswer(EthernetClient client) {
client.println(F("HTTP/1.1 200 OK"));
}
void StrClear(char *str, char length) {
for (int i = 0; i < length; i++) {
str[i] = 0;
}
}
char StrContains(char *str, char *sfind) {
char found = 0;
char index = 0;
char len;
len = strlen(str);
if (strlen(sfind) > len) {
return 0;
}
while (index < len) {
if (str[index] == sfind[found]) {
found++;
if (strlen(sfind) == found) {
return 1;
}
}
else {
found = 0;
}
index++;
}
return 0;
}
В сетап , перед пинмодой установит нужный уровень на выходном пине.
В сетап , перед пинмодой установит нужный уровень на выходном пине.
Можно поподробнее пожалуйста, это первый код Arduino с которым я столкнулся.
nik182 спасибо, разобрался, заработало!
В процедуре setup ,перед операторами pinMode, вставить оператор digitalWrite c параметром LOW или НIGH так , чтобы реле не срабатывало при старте. Как ещё подробнее я не знаю. Читайте раздел программирование на сайте по названным операторам. Там есть примеры применения.