//#define DEBUGWeb //#define DEBUGBT #include <SPI.h> //#include <Ethernet.h> //#include <WebServer.h> #include <Ethernet2.h> #include <WebServer2.h> byte relays[] = {2, 3, 4, 5, 6, 7, 8, 9}; byte relaysStatus[sizeof(relays)]; byte relaysCount = sizeof(relays); #define relayOpen 0 byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; IPAddress ip(192, 168, 2, 14); #define PREFIX "/" WebServer webserver(PREFIX, 80); template<class T> inline Print &operator <<(Print &obj, T arg) { obj.print(arg); return obj; } void web(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete) { if (type == WebServer:OST) { bool repeat; char name[16], value[16]; do { repeat = server.readPOSTparam(name, 16, value, 16); if (strcmp(name, "r1") == 0) { if (strcmp(value, "1") == 0) relayControl(1, 1); if (strcmp(value, "0") == 0) relayControl(1, 0); } else if (strcmp(name, "r2") == 0) { if (strcmp(value, "1") == 0) relayControl(2, 1); if (strcmp(value, "0") == 0) relayControl(2, 0); } else if (strcmp(name, "r3") == 0) { if (strcmp(value, "1") == 0) relayControl(3, 1); if (strcmp(value, "0") == 0) relayControl(3, 0); } else if (strcmp(name, "r4") == 0) { if (strcmp(value, "1") == 0) relayControl(4, 1); if (strcmp(value, "0") == 0) relayControl(4, 0); } else if (strcmp(name, "r5") == 0) { if (strcmp(value, "1") == 0) relayControl(5, 1); if (strcmp(value, "0") == 0) relayControl(5, 0); } else if (strcmp(name, "r6") == 0) { if (strcmp(value, "1") == 0) relayControl(6, 1); if (strcmp(value, "0") == 0) relayControl(6, 0); } else if (strcmp(name, "r7") == 0) { if (strcmp(value, "1") == 0) relayControl(7, 1); if (strcmp(value, "0") == 0) relayControl(7, 0); } else if (strcmp(name, "r8") == 0) { if (strcmp(value, "1") == 0) relayControl(8, 1); if (strcmp(value, "0") == 0) relayControl(8, 0); } } while (repeat); server.httpSeeOther(PREFIX); return; } server.httpSuccess(); if (type == WebServer::GET) { P(message) = "<html><head><title>Relay control</title>" "<meta http-equiv='refresh' content='2'></head>" "<body>" "<h1>Relay control</h1>" "<form action='/' method='POST'>"; server.printP(message); for (byte i = 0; i < relaysCount; i++) { server << "Relay " << 1 + i << " <button name=r" << 1 + i; if (relaysStatus == 0) server << " value=1 type=submit > OFF </button><br>"; else server << " value=0 type=submit > ON </button><br>"; } server << "</form></body></html>"; } } void setup() { Serial.begin(9600); Serial1.begin(38400); for (byte i = 0; i < relaysCount; i++) { pinMode(relays, OUTPUT); digitalWrite(relays, !relayOpen); } Ethernet.begin(mac, ip); webserver.begin(); webserver.setDefaultCommand(&web); Serial.println(Ethernet.localIP()); } void loop() { webserver.processConnection(); BT(); serial(); } String inString; void BT() { char buffer[100]; int i = 0; //если есть данные - читаем if (Serial1.available()) { delay(50); //загоняем прочитанное в буфер while (Serial1.available() && i < 99) { buffer[i++] = Serial1.read(); // Если символ конца сообщения //if (buffer[i - 1] == '*') break; } //закрываем массив buffer[i++] = '\0'; #ifdef DEBUGBT Serial.println(buffer); #endif } //если буфер наполнен if (i > 0) serial_read(buffer); } void serial() { char buffer[100]; int i = 0; //если есть данные - читаем if (Serial.available()) { delay(50); //загоняем прочитанное в буфер while (Serial.available() && i < 99) { buffer[i++] = Serial.read(); // Если символ конца сообщения //if (buffer[i - 1] == '*') break; } //закрываем массив buffer[i++] = '\0'; #ifdef DEBUGBT Serial.println(buffer); #endif } //если буфер наполнен if (i > 0) serial_read(buffer); } void serial_read(char array[100]) { char *varsArray[100]; char *str; int i = 0; while ((str = strtok_r(array, "_", &array)) != NULL) { varsArray = str; i++; #ifdef DEBUGBT Serial.print("str "); Serial.println(str); #endif } command_processing(varsArray); } // Пример комманды: 1_1 - вкл 1-е реле, 7_0 - выкл 7-е реле void command_processing (char *varsArray[10]) { byte relay = String(varsArray[0]).toInt(); byte status = String(varsArray[1]).toInt(); if (relay != 0 && status <= 1) relayControl(relay, status); #ifdef DEBUGBT Serial.print("relay "); Serial.println(relay); Serial.print("status "); Serial.println(status); #endif } void relayControl(byte relay, bool relayStatus) { if (relay <= relaysCount && relayStatus <= 1) if (relaysStatus[relay - 1] != relayStatus) { byte pin = relays[relay - 1]; relaysStatus[relay - 1] = relayStatus; // Изменить состояние реле digitalWrite(pin, !relayStatus); // Debug //#ifdef DEBUGWeb Serial.print("relay n: "); Serial.println(relay); Serial.print("relay pin: "); Serial.println(pin); Serial.print("status: "); Serial.println(relayStatus); //#endif } } вот код работает где то 60 секунд и отказ
Код (C++): //#define DEBUGWeb //#define DEBUGBT #include <SPI.h> //#include <Ethernet.h> //#include <WebServer.h> #include <Ethernet2.h> #include <WebServer2.h> byte relays[] = {2, 3, 4, 5, 6, 7, 8, 9}; byte relaysStatus[sizeof(relays)]; byte relaysCount = sizeof(relays); #define relayOpen 0 byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; IPAddress ip(192, 168, 2, 14); #define PREFIX "/" WebServer webserver(PREFIX, 80); template<class T> inline Print &operator <<(Print &obj, T arg) { obj.print(arg); return obj; } void web(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete) { if (type == WebServer::POST) { bool repeat; char name[16], value[16]; do { repeat = server.readPOSTparam(name, 16, value, 16); if (strcmp(name, "r1") == 0) { if (strcmp(value, "1") == 0) relayControl(1, 1); if (strcmp(value, "0") == 0) relayControl(1, 0); } else if (strcmp(name, "r2") == 0) { if (strcmp(value, "1") == 0) relayControl(2, 1); if (strcmp(value, "0") == 0) relayControl(2, 0); } else if (strcmp(name, "r3") == 0) { if (strcmp(value, "1") == 0) relayControl(3, 1); if (strcmp(value, "0") == 0) relayControl(3, 0); } else if (strcmp(name, "r4") == 0) { if (strcmp(value, "1") == 0) relayControl(4, 1); if (strcmp(value, "0") == 0) relayControl(4, 0); } else if (strcmp(name, "r5") == 0) { if (strcmp(value, "1") == 0) relayControl(5, 1); if (strcmp(value, "0") == 0) relayControl(5, 0); } else if (strcmp(name, "r6") == 0) { if (strcmp(value, "1") == 0) relayControl(6, 1); if (strcmp(value, "0") == 0) relayControl(6, 0); } else if (strcmp(name, "r7") == 0) { if (strcmp(value, "1") == 0) relayControl(7, 1); if (strcmp(value, "0") == 0) relayControl(7, 0); } else if (strcmp(name, "r8") == 0) { if (strcmp(value, "1") == 0) relayControl(8, 1); if (strcmp(value, "0") == 0) relayControl(8, 0); } } while (repeat); server.httpSeeOther(PREFIX); return; } server.httpSuccess(); if (type == WebServer::GET) { P(message) = "<html><head><title>Relay control</title>" "<meta http-equiv='refresh' content='2'></head>" "<body>" "<h1>Relay control</h1>" "<form action='/' method='POST'>"; server.printP(message); for (byte i = 0; i < relaysCount; i++) { server << "Relay " << 1 + i << " <button name=r" << 1 + i; if (relaysStatus[i] == 0) server << " value=1 type=submit > OFF </button><br>"; else server << " value=0 type=submit > ON </button><br>"; } server << "</form></body></html>"; } } void setup() { Serial.begin(9600); Serial1.begin(38400); for (byte i = 0; i < relaysCount; i++) { pinMode(relays[i], OUTPUT); digitalWrite(relays[i], !relayOpen); } Ethernet.begin(mac, ip); webserver.begin(); webserver.setDefaultCommand(&web); Serial.println(Ethernet.localIP()); } void loop() { webserver.processConnection(); BT(); serial(); } String inString; void BT() { char buffer[100]; int i = 0; //если есть данные - читаем if (Serial1.available()) { delay(50); //загоняем прочитанное в буфер while (Serial1.available() && i < 99) { buffer[i++] = Serial1.read(); // Если символ конца сообщения //if (buffer[i - 1] == '*') break; } //закрываем массив buffer[i++] = '\0'; #ifdef DEBUGBT Serial.println(buffer); #endif } //если буфер наполнен if (i > 0) serial_read(buffer); } void serial() { char buffer[100]; int i = 0; //если есть данные - читаем if (Serial.available()) { delay(50); //загоняем прочитанное в буфер while (Serial.available() && i < 99) { buffer[i++] = Serial.read(); // Если символ конца сообщения //if (buffer[i - 1] == '*') break; } //закрываем массив buffer[i++] = '\0'; #ifdef DEBUGBT Serial.println(buffer); #endif } //если буфер наполнен if (i > 0) serial_read(buffer); } void serial_read(char array[100]) { char *varsArray[100]; char *str; int i = 0; while ((str = strtok_r(array, "_", &array)) != NULL) { varsArray[i] = str; i++; #ifdef DEBUGBT Serial.print("str "); Serial.println(str); #endif } command_processing(varsArray); } // Пример комманды: 1_1 - вкл 1-е реле, 7_0 - выкл 7-е реле void command_processing (char *varsArray[10]) { byte relay = String(varsArray[0]).toInt(); byte status = String(varsArray[1]).toInt(); if (relay != 0 && status <= 1) relayControl(relay, status); #ifdef DEBUGBT Serial.print("relay "); Serial.println(relay); Serial.print("status "); Serial.println(status); #endif } void relayControl(byte relay, bool relayStatus) { if (relay <= relaysCount && relayStatus <= 1) if (relaysStatus[relay - 1] != relayStatus) { byte pin = relays[relay - 1]; relaysStatus[relay - 1] = relayStatus; // Изменить состояние реле digitalWrite(pin, !relayStatus); // Debug //#ifdef DEBUGWeb Serial.print("relay n: "); Serial.println(relay); Serial.print("relay pin: "); Serial.println(pin); Serial.print("status: "); Serial.println(relayStatus); //#endif } }