Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Ethernet2.h>
- #include <SPI.h>
- // задаем константы
- const int ledPin = A0; // номер выхода светодиода
- const int led2Pin = A1;
- const int led3Pin = A2;
- const int led4Pin = A3;
- const int led5Pin = A4;
- const int led6Pin = A5;
- const int buttonPin = 2; // номер входа, подключенный к кнопке
- const int button2Pin = 3;
- const int button3Pin = 4;
- const int button4Pin = 5;
- const int button5Pin = 6;
- const int button6Pin = 7;
- const int led7Pin = 8;
- // переменные
- boolean lastButton = LOW; // Переменная для сохранения предыдущего состояния кнопки
- boolean last2Button = LOW;
- boolean last3Button = LOW;
- boolean last4Button = LOW;
- boolean last5Button = LOW;
- boolean last6Button = LOW;
- boolean currentButton = LOW; // Переменная для сохранения текущего состояния кнопки
- boolean current2Button = LOW;
- boolean current3Button = LOW;
- boolean current4Button = LOW;
- boolean current5Button = LOW;
- boolean current6Button = LOW;
- boolean ledOn = false; // Текущее состояние светодиода (включен/выключен)
- boolean led2On = false;
- boolean led3On = false;
- boolean led4On = false;
- boolean led5On = false;
- boolean led6On = false;
- boolean led7On = false;
- String readString = String(30);
- // объявляем переменную MY_IP со значением 10.36.1.52
- #define MY_IP "192.168.1.206"
- // указываем настройки сетевого интерфейса и порт веб-сервера
- byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
- IPAddress ip(192, 168, 1, 206);
- EthernetServer server(80);
- void setup() {
- delay(500);
- /*
- * без строки "digitalWrite(Relay1, HIGH);" перед "pinMode" при включении
- * ардуины будет происходить кратковременное переключение реле,
- * а значит кратковременно подастся питание, что нам категорически
- * не надо!
- */
- // инициализируем пин, подключенный к светодиоду, как выход
- pinMode(ledPin, OUTPUT);
- pinMode(led2Pin, OUTPUT);
- pinMode(led3Pin, OUTPUT);
- pinMode(led4Pin, OUTPUT);
- pinMode(led5Pin, OUTPUT);
- pinMode(led6Pin, OUTPUT);
- pinMode(led7Pin, OUTPUT);
- // инициализируем пин, подключенный к кнопке, как вход
- pinMode(buttonPin, INPUT);
- pinMode(button2Pin, INPUT);
- pinMode(button3Pin, INPUT);
- pinMode(button4Pin, INPUT);
- pinMode(button5Pin, INPUT);
- pinMode(button6Pin, INPUT);
- digitalWrite(ledPin, LOW);
- digitalWrite(led2Pin, LOW);
- digitalWrite(led3Pin, LOW);
- digitalWrite(led4Pin, LOW);
- digitalWrite(led5Pin, LOW);
- digitalWrite(led6Pin, LOW);
- // стартуем сетевой интерфейс
- Ethernet.begin(mac, ip);
- server.begin();
- Serial.begin(9600);
- Serial.print("server is at ");
- Serial.println(Ethernet.localIP());
- }
- /*
- * функция сглаживания дребезга
- * принимает в качестве аргумента предыдущее состояние кнопки
- * и выдаёт фактическое.
- */
- boolean debounce(boolean last){
- boolean current = digitalRead(buttonPin); // Считать состояние кнопки
- if (last != current) // Если изменилось ...
- {
- delay(5); // ждём 5 мс
- current = digitalRead(buttonPin); // Считываем состояние кнопки
- return current; // Возвращаем состояние кнопки
- }
- }
- boolean debounce2(boolean last){
- boolean current = digitalRead(button2Pin); // Считать состояние кнопки
- if (last != current) // Если изменилось ...
- {
- delay(5); // ждём 5 мс
- current = digitalRead(button2Pin); // Считываем состояние кнопки
- return current; // Возвращаем состояние кнопки
- }
- }
- boolean debounce3(boolean last){
- boolean current = digitalRead(button3Pin); // Считать состояние кнопки
- if (last != current) // Если изменилось ...
- {
- delay(100); // ждём 5 мс
- current = digitalRead(button3Pin); // Считываем состояние кнопки
- return current; // Возвращаем состояние кнопки
- }
- }
- boolean debounce4(boolean last){
- boolean current = digitalRead(button4Pin); // Считать состояние кнопки
- if (last != current) // Если изменилось ...
- {
- delay(5); // ждём 5 мс
- current = digitalRead(button4Pin); // Считываем состояние кнопки
- return current; // Возвращаем состояние кнопки
- }
- }
- boolean debounce5(boolean last){
- boolean current = digitalRead(button5Pin); // Считать состояние кнопки
- if (last != current) // Если изменилось ...
- {
- delay(5); // ждём 5 мс
- current = digitalRead(button5Pin); // Считываем состояние кнопки
- return current; // Возвращаем состояние кнопки
- }
- }
- boolean debounce6(boolean last){
- boolean current = digitalRead(button6Pin); // Считать состояние кнопки
- if (last != current) // Если изменилось ...
- {
- delay(5); // ждём 5 мс
- current = digitalRead(button6Pin); // Считываем состояние кнопки
- return current; // Возвращаем состояние кнопки
- }
- }
- void loop() {
- EthernetClient client = server.available();
- if (client) {
- Serial.println("new client");
- // boolean currentLineIsBlank = true;
- while (client.connected()) {
- if (client.available()) {
- char c = client.read();
- // Serial.write(c);
- if (readString.length() < 30) {
- readString.concat( c); }
- Serial.print( c);
- //if (c == '\n' && currentLineIsBlank) {
- if (c == '\n') {
- //Проверяем включили ли реле и светодиод?
- //Level=1 - включен
- //Level=0 - выключен
- if(readString.indexOf("Level=1") >=0) {
- //Включаем светодиод
- digitalWrite(ledPin, HIGH);
- ledOn = true;
- }else if(readString.indexOf("Level=0") >=0){
- //Выключаем светодиод
- digitalWrite(ledPin, LOW);
- ledOn = false;
- }
- if(readString.indexOf("Level2=1") >=0) {
- //Включаем светодиод
- digitalWrite(led2Pin, HIGH);
- led2On = true;
- }else if(readString.indexOf("Level2=0") >=0){
- //Выключаем светодиод
- digitalWrite(led2Pin, LOW);
- led2On = false;
- }
- if(readString.indexOf("Level3=1") >=0) {
- //Включаем светодиод
- digitalWrite(led3Pin, HIGH);
- led3On = true;
- }else if(readString.indexOf("Level3=0") >=0){
- //Выключаем светодиод
- digitalWrite(led3Pin, LOW);
- led3On = false;
- }
- if(readString.indexOf("Level4=1") >=0) {
- //Включаем светодиод
- digitalWrite(led4Pin, HIGH);
- led4On = true;
- }else if(readString.indexOf("Level4=0") >=0){
- //Выключаем светодиод
- digitalWrite(led4Pin, LOW);
- led4On = false;
- }
- if(readString.indexOf("Level5=1") >=0) {
- //Включаем светодиод
- digitalWrite(led5Pin, HIGH);
- led5On = true;
- }else if(readString.indexOf("Level5=0") >=0){
- //Выключаем светодиод
- digitalWrite(led5Pin, LOW);
- led5On = false;
- }
- if(readString.indexOf("Level6=1") >=0) {
- //Включаем светодиод
- digitalWrite(led6Pin, HIGH);
- led6On = true;
- }else if(readString.indexOf("Level6=0") >=0){
- //Выключаем светодиод
- digitalWrite(led6Pin, LOW);
- led6On = false;
- }
- if(readString.indexOf("Level10=1") >=0) {
- //Включаем светодиод
- digitalWrite(ledPin, HIGH);
- digitalWrite(led2Pin, HIGH);
- digitalWrite(led3Pin, HIGH);
- digitalWrite(led4Pin, HIGH);
- digitalWrite(led5Pin, HIGH);
- digitalWrite(led6Pin, HIGH);
- ledOn = true;
- led2On = true;
- led3On = true;
- led4On = true;
- led5On = true;
- led6On = true;
- }else if(readString.indexOf("Level10=0") >=0){
- //Выключаем светодиод
- digitalWrite(ledPin, LOW);
- digitalWrite(led2Pin, LOW);
- digitalWrite(led3Pin, LOW);
- digitalWrite(led4Pin, LOW);
- digitalWrite(led5Pin, LOW);
- digitalWrite(led6Pin, LOW);
- ledOn = false;
- led2On = false;
- led3On = false;
- led4On = false;
- led5On = false;
- led6On = false;
- }
- client.println("HTTP/1.1 200 OK");
- client.println("Content-Type: text/html");
- client.println("Connection: close"); // the connection will be closed after completion of the response
- //client.println("Refresh: 5"); // refresh the page automatically every 5 sec
- client.println();
- client.println("<!DOCTYPE HTML>");
- client.println("<html>");
- client.println("<meta http-equiv='Content-Type' content='text/html; charset=utf-8' /> ");
- client.println("<head><meta http-equiv=\"refresh\" content=\"5;URL=http://"MY_IP"/\"></head>");
- client.println(F("<body>"));
- client.println(F("<center><head><title>Управление светом</title></head>"));
- client.print(F("<input type=button value='Моя комната | ON' onmousedown=location.href='/?Level=1'>"));
- client.println(F("<input type=button value='Моя комната | OFF' onmousedown=location.href='/?Level=0'><br/><br/>"));
- client.print(F("<input type=button value='Ванна | ON' onmousedown=location.href='/?Level2=1'>"));
- client.println(F("<input type=button value='Ванна | OFF' onmousedown=location.href='/?Level2=0'><br/><br/>"));
- client.print(F("<input type=button value='Туалет | ON' onmousedown=location.href='/?Level3=1'>"));
- client.println(F("<input type=button value='Туалет | OFF' onmousedown=location.href='/?Level3=0'><br/><br/>"));
- client.print(F("<input type=button value='Кухня | ON' onmousedown=location.href='/?Level4=1'>"));
- client.println(F("<input type=button value='Кухня | OFF' onmousedown=location.href='/?Level4=0'><br/><br/>"));
- client.print(F("<input type=button value='Спальня | ON' onmousedown=location.href='/?Level5=1'>"));
- client.println(F("<input type=button value='Спальня | OFF' onmousedown=location.href='/?Level5=0'><br/><br/>"));
- client.print(F("<input type=button value='Коридор | ON' onmousedown=location.href='/?Level6=1'>"));
- client.println(F("<input type=button value='Коридор | OFF' onmousedown=location.href='/?Level6=0'><br/><br/>"));
- client.print(F("<input type=button value='ON ALL' onmousedown=location.href='/?Level10=1'>"));
- client.println(F("<input type=button value='OFF ALL' onmousedown=location.href='/?Level10=0'><br/><br/>"));
- //client.println("<a href=\"/?Level=1\"\">Turn On Light</a><br />");
- //client.println("<a href=\"/?Level=0\"\">Turn Off Light</a><br />");
- //client.println("<a href=\"/?Level2=1\"\">Turn On 2 Light</a><br />");
- //client.println("<a href=\"/?Level2=0\"\">Turn Off 2 Light</a><br />");
- if (ledOn){
- client.println("<font size=’5′>Моя комната - ON.");
- client.println("<br></br>");
- }else{
- client.println("<font size=’5′>Моя комната - OFF");
- client.println("<br></br>");
- }
- if (led2On){
- client.println("<font size=’5′>Ванная - ON.");
- client.println("<br></br>");
- }else{
- client.println("<font size=’5′>Ванная - OFF");
- client.println("<br></br>");
- }
- if (led3On){
- client.println("<font size=’5′>Туалет - ON.");
- client.println("<br></br>");
- }else{
- client.println("<font size=’5′>Туалет - OFF");
- client.println("<br></br>");
- }
- if (led4On){
- client.println("<font size=’5′>Кухня - ON.");
- client.println("<br></br>");
- }else{
- client.println("<font size=’5′>Кухня - OFF");
- client.println("<br></br>");
- }
- if (led5On){
- client.println("<font size=’5′>Спальня - ON.");
- client.println("<br></br>");
- }else{
- client.println("<font size=’5′>Спальня - OFF");
- client.println("<br></br>");
- }
- if (led6On){
- client.println("<font size=’5′>Коридор - ON.");
- client.println("<br></br>");
- }else{
- client.println("<font size=’5′>Коридор - OFF");
- client.println("<br></br>");
- }
- if (led7On){
- client.println("<font size=’5′>Преобразователь - ON.");
- client.println("<br></br>");
- }else{
- client.println("<font size=’5′>Преобразователь - OFF");
- client.println("<br></br>");
- }
- client.println(F("</center></body>"));
- client.println("</html>");
- readString="";
- //останавливаем web-client
- client.stop();
- }
- }
- }
- }
- // часть кода для кнопки, реле и светодиода
- currentButton = debounce(lastButton);
- if (lastButton == LOW && currentButton == HIGH) // Если нажатие (условия для светодиода на пине 6)
- {
- ledOn = !ledOn;
- }
- lastButton = currentButton;
- digitalWrite(ledPin, ledOn); // Изменить статус состояния светодиода
- current2Button = debounce2(last2Button);
- if (last2Button == LOW && current2Button == HIGH) // Если нажатие (условия для светодиода на пине 6)
- {
- led2On = !led2On;
- }
- last2Button = current2Button;
- digitalWrite(led2Pin, led2On); // Изменить статус состояния светодиода
- current3Button = debounce3(last3Button);
- if (last3Button == LOW && current3Button == HIGH) // Если нажатие (условия для светодиода на пине 6)
- {
- led3On = !led3On;
- }
- last3Button = current3Button;
- digitalWrite(led3Pin, led3On); // Изменить статус состояния светодиода
- current4Button = debounce4(last4Button);
- if (last4Button == LOW && current4Button == HIGH) // Если нажатие (условия для светодиода на пине 6)
- {
- led4On = !led4On;
- }
- last4Button = current4Button;
- digitalWrite(led4Pin, led4On); // Изменить статус состояния светодиода
- current5Button = debounce5(last5Button);
- if (last5Button == LOW && current5Button == HIGH) // Если нажатие (условия для светодиода на пине 6)
- {
- led5On = !led5On;
- }
- last5Button = current5Button;
- digitalWrite(led5Pin, led5On); // Изменить статус состояния светодиода
- current6Button = debounce6(last6Button);
- if (last6Button == LOW && current6Button == HIGH) // Если нажатие (условия для светодиода на пине 6)
- {
- led6On = !led6On;
- }
- last6Button = current6Button;
- digitalWrite(led6Pin, led6On); // Изменить статус состояния светодиода
- if (ledOn == true || led2On == true || led3On == true || led4On == true || led5On == true || led6On == true) {
- digitalWrite(led7Pin, HIGH);
- led7On = true;
- }
- else
- {
- //delay(500);
- digitalWrite(led7Pin, LOW);
- led7On = false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement