Advertisement
DraKuLa21

Untitled

Jun 10th, 2019
475
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 15.79 KB | None | 0 0
  1. #include <Ethernet2.h>
  2. #include <SPI.h>
  3.  
  4.  
  5. // задаем константы
  6. const int ledPin =  A0;     // номер выхода светодиода
  7. const int led2Pin =  A1;
  8. const int led3Pin =  A2;
  9. const int led4Pin =  A3;
  10. const int led5Pin =  A4;
  11. const int led6Pin =  A5;
  12. const int buttonPin = 2;    // номер входа, подключенный к кнопке
  13. const int button2Pin = 3;
  14. const int button3Pin = 4;
  15. const int button4Pin = 5;
  16. const int button5Pin = 6;
  17. const int button6Pin = 7;
  18. const int led7Pin = 8;
  19.  
  20.  
  21. // переменные
  22. boolean lastButton = LOW;      // Переменная для сохранения предыдущего состояния кнопки
  23. boolean last2Button = LOW;
  24. boolean last3Button = LOW;
  25. boolean last4Button = LOW;
  26. boolean last5Button = LOW;
  27. boolean last6Button = LOW;
  28. boolean currentButton = LOW;   // Переменная для сохранения текущего состояния кнопки
  29. boolean current2Button = LOW;
  30. boolean current3Button = LOW;
  31. boolean current4Button = LOW;
  32. boolean current5Button = LOW;
  33. boolean current6Button = LOW;
  34. boolean ledOn = false;         // Текущее состояние светодиода (включен/выключен)
  35. boolean led2On = false;  
  36. boolean led3On = false;  
  37. boolean led4On = false;  
  38. boolean led5On = false;  
  39. boolean led6On = false;  
  40. boolean led7On = false;
  41.  
  42.  
  43. String readString = String(30);
  44.  
  45.  
  46. // объявляем переменную MY_IP со значением 10.36.1.52
  47. #define MY_IP "192.168.1.206"
  48. // указываем настройки сетевого интерфейса и порт веб-сервера
  49. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  50. IPAddress ip(192, 168, 1, 206);
  51. EthernetServer server(80);
  52.  
  53.  
  54. void setup() {
  55.   delay(500);
  56.  
  57. /*
  58.  * без строки "digitalWrite(Relay1, HIGH);" перед "pinMode" при включении
  59.  * ардуины будет происходить кратковременное переключение реле,
  60.  * а значит кратковременно подастся питание, что нам категорически
  61.  * не надо!
  62.  */
  63.  
  64.  
  65.   // инициализируем пин, подключенный к светодиоду, как выход
  66.   pinMode(ledPin, OUTPUT);
  67.   pinMode(led2Pin, OUTPUT);
  68.   pinMode(led3Pin, OUTPUT);
  69.   pinMode(led4Pin, OUTPUT);
  70.   pinMode(led5Pin, OUTPUT);
  71.   pinMode(led6Pin, OUTPUT);
  72.   pinMode(led7Pin, OUTPUT);
  73.   // инициализируем пин, подключенный к кнопке, как вход
  74.   pinMode(buttonPin, INPUT);
  75.   pinMode(button2Pin, INPUT);
  76.   pinMode(button3Pin, INPUT);
  77.   pinMode(button4Pin, INPUT);
  78.   pinMode(button5Pin, INPUT);
  79.   pinMode(button6Pin, INPUT);
  80.  
  81.  
  82.  
  83.  
  84. digitalWrite(ledPin, LOW);
  85. digitalWrite(led2Pin, LOW);
  86. digitalWrite(led3Pin, LOW);
  87. digitalWrite(led4Pin, LOW);
  88. digitalWrite(led5Pin, LOW);
  89. digitalWrite(led6Pin, LOW);
  90.  
  91. // стартуем сетевой интерфейс
  92. Ethernet.begin(mac, ip);
  93. server.begin();
  94.  
  95.  
  96. Serial.begin(9600);
  97. Serial.print("server is at ");
  98. Serial.println(Ethernet.localIP());
  99. }
  100.  
  101. /*
  102.  * функция сглаживания дребезга
  103.  * принимает в качестве аргумента предыдущее состояние кнопки
  104.  * и выдаёт фактическое.
  105.  */
  106. boolean debounce(boolean last){
  107.   boolean current = digitalRead(buttonPin);    // Считать состояние кнопки
  108.   if (last != current)                         // Если изменилось ...
  109.   {
  110.   delay(5);                                    // ждём 5 мс
  111.   current = digitalRead(buttonPin);            // Считываем состояние кнопки
  112.   return current;                              // Возвращаем состояние кнопки
  113.   }
  114. }
  115.  
  116. boolean debounce2(boolean last){
  117.   boolean current = digitalRead(button2Pin);    // Считать состояние кнопки
  118.   if (last != current)                         // Если изменилось ...
  119.   {
  120.   delay(5);                                    // ждём 5 мс
  121.   current = digitalRead(button2Pin);            // Считываем состояние кнопки
  122.   return current;                              // Возвращаем состояние кнопки
  123.   }
  124. }
  125.  
  126. boolean debounce3(boolean last){
  127.   boolean current = digitalRead(button3Pin);    // Считать состояние кнопки
  128.   if (last != current)                         // Если изменилось ...
  129.   {
  130.   delay(100);                                    // ждём 5 мс
  131.   current = digitalRead(button3Pin);            // Считываем состояние кнопки
  132.   return current;                              // Возвращаем состояние кнопки
  133.   }
  134. }
  135.  
  136. boolean debounce4(boolean last){
  137.   boolean current = digitalRead(button4Pin);    // Считать состояние кнопки
  138.   if (last != current)                         // Если изменилось ...
  139.   {
  140.   delay(5);                                    // ждём 5 мс
  141.   current = digitalRead(button4Pin);            // Считываем состояние кнопки
  142.   return current;                              // Возвращаем состояние кнопки
  143.   }
  144. }
  145.  
  146. boolean debounce5(boolean last){
  147.   boolean current = digitalRead(button5Pin);    // Считать состояние кнопки
  148.   if (last != current)                         // Если изменилось ...
  149.   {
  150.   delay(5);                                    // ждём 5 мс
  151.   current = digitalRead(button5Pin);            // Считываем состояние кнопки
  152.   return current;                              // Возвращаем состояние кнопки
  153.   }
  154. }
  155.  
  156. boolean debounce6(boolean last){
  157.   boolean current = digitalRead(button6Pin);    // Считать состояние кнопки
  158.   if (last != current)                         // Если изменилось ...
  159.   {
  160.   delay(5);                                    // ждём 5 мс
  161.   current = digitalRead(button6Pin);            // Считываем состояние кнопки
  162.   return current;                              // Возвращаем состояние кнопки
  163.   }
  164. }
  165.  
  166. void loop() {
  167.   EthernetClient client = server.available();
  168.   if (client) {
  169.     Serial.println("new client");
  170. //    boolean currentLineIsBlank = true;
  171.     while (client.connected()) {
  172.       if (client.available()) {
  173.         char c = client.read();
  174. //        Serial.write(c);
  175.  
  176. if (readString.length() < 30) {
  177. readString.concat( c); }
  178. Serial.print( c);
  179.  
  180.  
  181.        
  182. //if (c == '\n' && currentLineIsBlank) {
  183. if (c == '\n') {
  184. //Проверяем включили ли реле и светодиод?
  185. //Level=1 - включен
  186. //Level=0 - выключен
  187. if(readString.indexOf("Level=1") >=0) {
  188. //Включаем светодиод
  189. digitalWrite(ledPin, HIGH);
  190. ledOn = true;
  191.  
  192. }else if(readString.indexOf("Level=0") >=0){
  193. //Выключаем светодиод
  194. digitalWrite(ledPin, LOW);
  195. ledOn = false;
  196. }
  197.  
  198. if(readString.indexOf("Level2=1") >=0) {
  199. //Включаем светодиод
  200. digitalWrite(led2Pin, HIGH);
  201. led2On = true;
  202.  
  203. }else if(readString.indexOf("Level2=0") >=0){
  204. //Выключаем светодиод
  205. digitalWrite(led2Pin, LOW);
  206. led2On = false;
  207. }
  208.  
  209.  
  210. if(readString.indexOf("Level3=1") >=0) {
  211. //Включаем светодиод
  212. digitalWrite(led3Pin, HIGH);
  213. led3On = true;
  214.  
  215. }else if(readString.indexOf("Level3=0") >=0){
  216. //Выключаем светодиод
  217. digitalWrite(led3Pin, LOW);
  218. led3On = false;
  219. }
  220.  
  221.  
  222. if(readString.indexOf("Level4=1") >=0) {
  223. //Включаем светодиод
  224. digitalWrite(led4Pin, HIGH);
  225. led4On = true;
  226.  
  227. }else if(readString.indexOf("Level4=0") >=0){
  228. //Выключаем светодиод
  229. digitalWrite(led4Pin, LOW);
  230. led4On = false;
  231. }
  232.  
  233.  
  234. if(readString.indexOf("Level5=1") >=0) {
  235. //Включаем светодиод
  236. digitalWrite(led5Pin, HIGH);
  237. led5On = true;
  238.  
  239. }else if(readString.indexOf("Level5=0") >=0){
  240. //Выключаем светодиод
  241. digitalWrite(led5Pin, LOW);
  242. led5On = false;
  243. }
  244.  
  245.  
  246. if(readString.indexOf("Level6=1") >=0) {
  247. //Включаем светодиод
  248. digitalWrite(led6Pin, HIGH);
  249. led6On = true;
  250.  
  251. }else if(readString.indexOf("Level6=0") >=0){
  252. //Выключаем светодиод
  253. digitalWrite(led6Pin, LOW);
  254. led6On = false;
  255. }
  256.  
  257. if(readString.indexOf("Level10=1") >=0) {
  258. //Включаем светодиод
  259. digitalWrite(ledPin, HIGH);
  260. digitalWrite(led2Pin, HIGH);
  261. digitalWrite(led3Pin, HIGH);
  262. digitalWrite(led4Pin, HIGH);
  263. digitalWrite(led5Pin, HIGH);
  264. digitalWrite(led6Pin, HIGH);
  265.  
  266. ledOn = true;
  267. led2On = true;
  268. led3On = true;
  269. led4On = true;
  270. led5On = true;
  271. led6On = true;
  272.  
  273. }else if(readString.indexOf("Level10=0") >=0){
  274. //Выключаем светодиод
  275. digitalWrite(ledPin, LOW);
  276. digitalWrite(led2Pin, LOW);
  277. digitalWrite(led3Pin, LOW);
  278. digitalWrite(led4Pin, LOW);
  279. digitalWrite(led5Pin, LOW);
  280. digitalWrite(led6Pin, LOW);
  281.  
  282. ledOn = false;
  283. led2On = false;
  284. led3On = false;
  285. led4On = false;
  286. led5On = false;
  287. led6On = false;
  288. }
  289.  
  290. client.println("HTTP/1.1 200 OK");
  291. client.println("Content-Type: text/html");
  292. client.println("Connection: close");  // the connection will be closed after completion of the response
  293. //client.println("Refresh: 5");  // refresh the page automatically every 5 sec
  294. client.println();
  295. client.println("<!DOCTYPE HTML>");
  296. client.println("<html>");
  297. client.println("<meta http-equiv='Content-Type' content='text/html; charset=utf-8' /> ");
  298. client.println("<head><meta http-equiv=\"refresh\" content=\"5;URL=http://"MY_IP"/\"></head>");
  299. client.println(F("<body>"));
  300. client.println(F("<center><head><title>Управление светом</title></head>"));
  301.  
  302. client.print(F("<input type=button value='Моя комната | ON' onmousedown=location.href='/?Level=1'>"));
  303. client.println(F("<input type=button value='Моя комната | OFF' onmousedown=location.href='/?Level=0'><br/><br/>"));
  304. client.print(F("<input type=button value='Ванна | ON' onmousedown=location.href='/?Level2=1'>"));
  305. client.println(F("<input type=button value='Ванна | OFF' onmousedown=location.href='/?Level2=0'><br/><br/>"));
  306. client.print(F("<input type=button value='Туалет | ON' onmousedown=location.href='/?Level3=1'>"));
  307. client.println(F("<input type=button value='Туалет | OFF' onmousedown=location.href='/?Level3=0'><br/><br/>"));
  308. client.print(F("<input type=button value='Кухня | ON' onmousedown=location.href='/?Level4=1'>"));
  309. client.println(F("<input type=button value='Кухня | OFF' onmousedown=location.href='/?Level4=0'><br/><br/>"));
  310. client.print(F("<input type=button value='Спальня | ON' onmousedown=location.href='/?Level5=1'>"));
  311. client.println(F("<input type=button value='Спальня | OFF' onmousedown=location.href='/?Level5=0'><br/><br/>"));
  312. client.print(F("<input type=button value='Коридор | ON' onmousedown=location.href='/?Level6=1'>"));
  313. client.println(F("<input type=button value='Коридор | OFF' onmousedown=location.href='/?Level6=0'><br/><br/>"));
  314. client.print(F("<input type=button value='ON ALL' onmousedown=location.href='/?Level10=1'>"));
  315. client.println(F("<input type=button value='OFF ALL' onmousedown=location.href='/?Level10=0'><br/><br/>"));
  316. //client.println("<a href=\"/?Level=1\"\">Turn On Light</a><br />");
  317. //client.println("<a href=\"/?Level=0\"\">Turn Off Light</a><br />");
  318. //client.println("<a href=\"/?Level2=1\"\">Turn On 2 Light</a><br />");
  319. //client.println("<a href=\"/?Level2=0\"\">Turn Off 2 Light</a><br />");
  320.  
  321.  
  322.  
  323. if (ledOn){
  324. client.println("<font size=’5′>Моя комната - ON.");
  325. client.println("<br></br>");
  326. }else{
  327. client.println("<font size=’5′>Моя комната - OFF");
  328. client.println("<br></br>");
  329. }
  330.  
  331. if (led2On){
  332. client.println("<font size=’5′>Ванная - ON.");
  333. client.println("<br></br>");
  334. }else{
  335. client.println("<font size=’5′>Ванная - OFF");
  336. client.println("<br></br>");
  337. }
  338.  
  339. if (led3On){
  340. client.println("<font size=’5′>Туалет - ON.");
  341. client.println("<br></br>");
  342. }else{
  343. client.println("<font size=’5′>Туалет - OFF");
  344. client.println("<br></br>");
  345. }
  346.  
  347. if (led4On){
  348. client.println("<font size=’5′>Кухня - ON.");
  349. client.println("<br></br>");
  350. }else{
  351. client.println("<font size=’5′>Кухня - OFF");
  352. client.println("<br></br>");
  353. }
  354.  
  355. if (led5On){
  356. client.println("<font size=’5′>Спальня - ON.");
  357. client.println("<br></br>");
  358. }else{
  359. client.println("<font size=’5′>Спальня - OFF");
  360. client.println("<br></br>");
  361. }
  362.  
  363. if (led6On){
  364. client.println("<font size=’5′>Коридор - ON.");
  365. client.println("<br></br>");
  366. }else{
  367. client.println("<font size=’5′>Коридор - OFF");
  368. client.println("<br></br>");
  369. }
  370.  
  371. if (led7On){
  372. client.println("<font size=’5′>Преобразователь - ON.");
  373. client.println("<br></br>");
  374. }else{
  375. client.println("<font size=’5′>Преобразователь - OFF");
  376. client.println("<br></br>");
  377. }
  378.  
  379. client.println(F("</center></body>"));
  380. client.println("</html>");
  381. readString="";
  382. //останавливаем web-client
  383. client.stop();
  384.           }
  385.         }
  386.       }
  387.     }
  388.  
  389. // часть кода для кнопки, реле и светодиода
  390. currentButton = debounce(lastButton);
  391. if (lastButton == LOW && currentButton == HIGH)   // Если нажатие (условия для светодиода на пине 6)
  392. {
  393.   ledOn = !ledOn;
  394. }
  395.  
  396.  
  397. lastButton = currentButton;
  398. digitalWrite(ledPin, ledOn);                         // Изменить статус состояния светодиода
  399.  
  400.  
  401. current2Button = debounce2(last2Button);
  402. if (last2Button == LOW && current2Button == HIGH)   // Если нажатие (условия для светодиода на пине 6)
  403. {
  404.   led2On = !led2On;
  405. }
  406.  
  407.  
  408. last2Button = current2Button;
  409. digitalWrite(led2Pin, led2On);                         // Изменить статус состояния светодиода
  410.  
  411.  
  412. current3Button = debounce3(last3Button);
  413. if (last3Button == LOW && current3Button == HIGH)   // Если нажатие (условия для светодиода на пине 6)
  414. {
  415.   led3On = !led3On;
  416. }
  417.  
  418.  
  419. last3Button = current3Button;
  420. digitalWrite(led3Pin, led3On);                         // Изменить статус состояния светодиода
  421.  
  422.  
  423. current4Button = debounce4(last4Button);
  424. if (last4Button == LOW && current4Button == HIGH)   // Если нажатие (условия для светодиода на пине 6)
  425. {
  426.   led4On = !led4On;
  427. }
  428.  
  429.  
  430. last4Button = current4Button;
  431. digitalWrite(led4Pin, led4On);                         // Изменить статус состояния светодиода
  432.  
  433. current5Button = debounce5(last5Button);
  434. if (last5Button == LOW && current5Button == HIGH)   // Если нажатие (условия для светодиода на пине 6)
  435. {
  436.   led5On = !led5On;
  437. }
  438.  
  439.  
  440. last5Button = current5Button;
  441. digitalWrite(led5Pin, led5On);                         // Изменить статус состояния светодиода
  442.  
  443. current6Button = debounce6(last6Button);
  444. if (last6Button == LOW && current6Button == HIGH)   // Если нажатие (условия для светодиода на пине 6)
  445. {
  446.   led6On = !led6On;
  447. }
  448.  
  449.  
  450. last6Button = current6Button;
  451. digitalWrite(led6Pin, led6On);                         // Изменить статус состояния светодиода
  452.  
  453. if (ledOn == true || led2On == true || led3On == true || led4On == true || led5On == true || led6On == true) {
  454.   digitalWrite(led7Pin, HIGH);
  455.   led7On = true;
  456. }
  457. else
  458. {
  459.   //delay(500);
  460.   digitalWrite(led7Pin, LOW);
  461.   led7On = false;
  462. }
  463.  
  464. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement