Advertisement
RuiViana

Automação Erro Horst

Feb 27th, 2016
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 28.82 KB | None | 0 0
  1. #include <Wire.h>
  2.  
  3. #include <DHT.h>
  4.  
  5. #include <LiquidCrystal_I2C.h>
  6.  
  7.  
  8.  
  9.  
  10.  
  11. /*
  12. Sergio Mokshin
  13. Automação Live - Abril/2015
  14.  
  15. */
  16.  
  17. #include <SPI.h>
  18. #include <Ethernet.h>
  19. #include <EEPROM.h>
  20. #include "Wire.h"
  21. #include "DHT.h"
  22. #define DHTPIN 8
  23. #define DHTTYPE DHT11
  24.  
  25. //Endereços de LCDs 1602
  26. //LiquidCrystal_I2C lcd(0x3F, 16, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display
  27. LiquidCrystal_I2C lcd(0x27, 16,2);// set the LCD address to 0x27 for a 16 chars and 2 line display
  28. //LiquidCrystal_I2C lcd(32,16,2); // set the LCD address to 0x20 for a 16 chars and 2 line display
  29.  
  30. //Endereços de LCDs 2004
  31. //LiquidCrystal_I2C lcd(0x27, 20, 4); // set the LCD address to 0x27 for a 20 chars and 4 line display
  32.  
  33. //Se necessário utilizar um I2C Scanner http://playground.arduino.cc/Main/I2cScanner
  34.  
  35.  
  36. #define DS1307_I2C_ADDRESS 0x68
  37.  
  38. byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0xA6, 0x09 };
  39. byte ip[] = { 192, 168, 0, 202 };
  40. byte gateway[] = { 192, 168, 0, 1 };
  41. byte subnet[] = { 255, 255, 255, 0 };
  42. EthernetServer server(80);
  43.  
  44. #define PIN_S1 2
  45. #define PIN_S2 4
  46. #define PIN_S3 7
  47. #define PIN_S4 9
  48. #define PIN_S5 A0
  49. #define PIN_S6 A1
  50. #define PIN_S7 A2
  51. #define PIN_S8 A3
  52.  
  53. #define PIN_RED 3
  54. #define PIN_GREEN 6
  55. #define PIN_BLUE 5
  56.  
  57. int MemSaida1 = 1; //Endereço de memoria com conteudo ultimo comando enviado S1
  58. int MemSaida2 = 2; //Endereço de memoria com conteudo ultimo comando enviado S2
  59. int MemSaida3 = 3; //Endereço de memoria com conteudo ultimo comando enviado S3
  60. int MemSaida4 = 4; //Endereço de memoria com conteudo ultimo comando enviado S4
  61. int MemSaida5 = 5; //Endereço de memoria com conteudo ultimo comando enviado S5
  62. int MemSaida6 = 6; //Endereço de memoria com conteudo ultimo comando enviado S6
  63. int MemSaida7 = 7; //Endereço de memoria com conteudo ultimo comando enviado S7
  64. int MemSaida8 = 8; //Endereço de memoria com conteudo ultimo comando enviado S8
  65. int MemAuto = 9; //Endereço de memoria com conteudo ultimo comando enviado modo Automatico/Manual
  66.  
  67. int MemSaida1HrI = 6; //Endereço de memoria com conteudo inicio horario Saida 1
  68. int MemSaida1HrF = 7; //Endereço de memoria com conteudo fim horario Saida 1
  69. int MemSaida2HrI = 8; //Endereço de memoria com conteudo inicio horario Saida 2
  70. int MemSaida2HrF = 9; //Endereço de memoria com conteudo fim horario Saida 2
  71. int MemSaida3HrI = 10; //Endereço de memoria com conteudo inicio horario Saida 3
  72. int MemSaida3HrF = 11; //Endereço de memoria com conteudo fim horario Saida 3
  73. int MemSaida4HrI = 12; //Endereço de memoria com conteudo inicio horario Saida 4
  74. int MemSaida4HrF = 13; //Endereço de memoria com conteudo fim horario Saida 4
  75. int MemSaida5HrI = 13; //Endereço de memoria com conteudo inicio horario Saida 5
  76. int MemSaida5HrF = 14; //Endereço de memoria com conteudo fim horario Saida 5
  77. int MemSaida6HrI = 15; //Endereço de memoria com conteudo inicio horario Saida 6
  78. int MemSaida6HrF = 16; //Endereço de memoria com conteudo fim horario Saida 6
  79. int MemSaida7HrI = 17; //Endereço de memoria com conteudo inicio horario Saida 7
  80. int MemSaida7HrF = 18; //Endereço de memoria com conteudo fim horario Saida 7
  81. int MemSaida8HrI = 19; //Endereço de memoria com conteudo inicio horario Saida 8
  82. int MemSaida8HrF = 20; //Endereço de memoria com conteudo fim horario Saida 8
  83.  
  84. int MemRGBHrI = 21; //Endereço de memoria com conteudo inicio horario RGB
  85. int MemRGBHrF = 22; //Endereço de memoria com conteudo fim horario RGB
  86. int MemRGBType = 23; //Endereço de memoria com conteudo inicio horario RGB com cor Azul
  87. int MemRGBBLUEHrF = 24; //Endereço de memoria com conteudo fim horario RGB com cor Azul
  88.  
  89. int MemRed = 25; //Endereço de memoria com ultimo comando enviado Red
  90. int MemGreen = 26; //Endereço de memoria com ultimo comando enviado Green
  91. int MemBlue = 27; //Endereço de memoria com ultimo comando enviado Blue
  92.  
  93. int ValueSaveSaida1 = 0; //Conteudo da memoria com status Saida 1
  94. int ValueSaveSaida2 = 0; //Conteudo da memoria com status Saida 2
  95. int ValueSaveSaida3 = 0; //Conteudo da memoria com status Saida 3
  96. int ValueSaveSaida4 = 0; //Conteudo da memoria com status Saida 4
  97. int ValueSaveSaida5 = 0; //Conteudo da memoria com status Saida 5
  98. int ValueSaveSaida6 = 0; //Conteudo da memoria com status Saida 6
  99. int ValueSaveSaida7 = 0; //Conteudo da memoria com status Saida 7
  100. int ValueSaveSaida8 = 0; //Conteudo da memoria com status Saida 8
  101. int ValueSaveAuto = 0; //Conteudo da memoria com status Automatico ou Manual
  102.  
  103. int ValueSaida1HrI = 0; //Conteudo da memoria com inicio horario Saida 1
  104. int ValueSaida1HrF = 0; //Conteudo da memoria fim horario Saida 1
  105. int ValueSaida2HrI = 0; //Conteudo da memoria com inicio horario Saida 2
  106. int ValueSaida2HrF = 0; //Conteudo da memoria fim horario Saida 2
  107. int ValueSaida3HrI = 0; //Conteudo da memoria com inicio horario Saida 3
  108. int ValueSaida3HrF = 0; //Conteudo da memoria fim horario Saida 3
  109. int ValueSaida4HrI = 0; //Conteudo da memoria inicio horario Saida 4
  110. int ValueSaida4HrF = 0; //Conteudo da memoria fim horario Saida 4
  111. int ValueSaida5HrI = 0; //Conteudo da memoria inicio horario Saida 5
  112. int ValueSaida5HrF = 0; //Conteudo da memoria fim horario Saida 5
  113. int ValueSaida6HrI = 0; //Conteudo da memoria inicio horario Saida 6
  114. int ValueSaida6HrF = 0; //Conteudo da memoria fim horario Saida 6
  115. int ValueSaida7HrI = 0; //Conteudo da memoria inicio horario Saida 7
  116. int ValueSaida7HrF = 0; //Conteudo da memoria fim horario Saida 7
  117. int ValueSaida8HrI = 0; //Conteudo da memoria inicio horario Saida 8
  118. int ValueSaida8HrF = 0; //Conteudo da memoria fim horario Saida 8
  119. int ValueRGBHrI = 0; //Conteudo de memoria inicio horario RGB
  120. int ValueRGBHrF = 0; //Conteudo de memoria fim horario RGB
  121. int ValueRGBType = 0; //Conteudo de memoria com cor RGB para agendamento
  122. int ValueRed = 0; //Conteudo de memoria Red
  123. int ValueGreen = 0; //Conteudo de memoria Green
  124. int ValueBlue = 0; //Conteudo de memoria Blue
  125.  
  126.  
  127. boolean EEPROMComandos = false;
  128.  
  129. float humidity;
  130. float temp;
  131.  
  132.  
  133. int countLcd = 0;
  134.  
  135. String readString;
  136. byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  137. DHT dht(DHTPIN, DHTTYPE);
  138.  
  139. void setup() {
  140.  
  141.  
  142. //An EEPROM write takes 3.3 ms to complete. The EEPROM memory has a specified life of 100,000 write/erase cycles, so you may need to be careful about how often you write to it.
  143. //https://www.arduino.cc/en/Reference/EEPROMWrite
  144. //Manter em EPPROM acionamentos manuais do usuário
  145. //EEPROMComandos = true;
  146. EEPROMComandos = false;
  147.  
  148.  
  149. Wire.begin();
  150.  
  151. Ethernet.begin(mac, ip, gateway, subnet);
  152. server.begin();
  153. Serial.begin(9600); // for debug
  154.  
  155. pinMode(PIN_S1, OUTPUT);
  156. pinMode(PIN_S2, OUTPUT);
  157. pinMode(PIN_S3, OUTPUT);
  158. pinMode(PIN_S4, OUTPUT);
  159. pinMode(PIN_S5, OUTPUT);
  160. pinMode(PIN_S6, OUTPUT);
  161. pinMode(PIN_S7, OUTPUT);
  162. pinMode(PIN_S8, OUTPUT);
  163.  
  164. ValueSaveAuto = EEPROM.read(MemAuto);
  165.  
  166. ValueSaida1HrI = EEPROM.read(MemSaida1HrI);
  167. ValueSaida1HrF = EEPROM.read(MemSaida1HrF);
  168. ValueSaida2HrI = EEPROM.read(MemSaida2HrI);
  169. ValueSaida2HrF = EEPROM.read(MemSaida2HrF);
  170. ValueSaida3HrI = EEPROM.read(MemSaida3HrI);
  171. ValueSaida3HrF = EEPROM.read(MemSaida3HrF);
  172. ValueSaida4HrI = EEPROM.read(MemSaida4HrI);
  173. ValueSaida4HrF = EEPROM.read(MemSaida4HrF);
  174. ValueSaida5HrI = EEPROM.read(MemSaida5HrI);
  175. ValueSaida5HrF = EEPROM.read(MemSaida5HrF);
  176. ValueSaida6HrI = EEPROM.read(MemSaida6HrI);
  177. ValueSaida6HrF = EEPROM.read(MemSaida6HrF);
  178. ValueSaida7HrI = EEPROM.read(MemSaida7HrI);
  179. ValueSaida7HrF = EEPROM.read(MemSaida7HrF);
  180. ValueSaida8HrI = EEPROM.read(MemSaida8HrI);
  181. ValueSaida8HrF = EEPROM.read(MemSaida8HrF);
  182. ValueRGBHrI = EEPROM.read(MemRGBHrI);
  183. ValueRGBHrF = EEPROM.read(MemRGBHrF);
  184. ValueRGBType = EEPROM.read(MemRGBType);
  185.  
  186.  
  187. if (EEPROMComandos)
  188. {
  189. //Inicializando placa com valores armazenados na FLASH Memory
  190. digitalWrite(PIN_S1, EEPROM.read(MemSaida1));
  191. digitalWrite(PIN_S2, EEPROM.read(MemSaida2));
  192. digitalWrite(PIN_S3, EEPROM.read(MemSaida3));
  193. digitalWrite(PIN_S4, EEPROM.read(MemSaida4));
  194. digitalWrite(PIN_S5, EEPROM.read(MemSaida5));
  195. digitalWrite(PIN_S6, EEPROM.read(MemSaida6));
  196. digitalWrite(PIN_S7, EEPROM.read(MemSaida7));
  197. digitalWrite(PIN_S8, EEPROM.read(MemSaida8));
  198.  
  199. ValueRed = EEPROM.read(MemRed);
  200. ValueGreen = EEPROM.read(MemGreen);
  201. ValueBlue = EEPROM.read(MemBlue);
  202.  
  203. analogWrite(PIN_RED, ValueRed);
  204. analogWrite(PIN_GREEN, ValueGreen);
  205. analogWrite(PIN_BLUE, ValueBlue);
  206.  
  207. }
  208. lcd.begin();
  209. lcd.clear();
  210.  
  211. dht.begin();
  212. }
  213.  
  214. void loop() {
  215.  
  216. getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
  217.  
  218. humidity = dht.readHumidity();
  219. temp = dht.readTemperature();
  220.  
  221. if(isnan(temp))
  222. temp = 0;
  223.  
  224. if(isnan(humidity))
  225. humidity = 0;
  226.  
  227.  
  228. WebServer();
  229. ModoAuto();
  230. PrintLcd();
  231. }
  232.  
  233. void PrintLcd() {
  234.  
  235. lcd.backlight();
  236.  
  237. lcd.setCursor(0, 0);
  238. if (dayOfMonth < 10)
  239. lcd.print("0");
  240. lcd.print(dayOfMonth);
  241. lcd.print("/");
  242. if (month < 10)
  243. lcd.print("0");
  244. lcd.print(month);
  245. lcd.print("/");
  246. lcd.print(year);
  247. lcd.print(" ");
  248. if (hour < 10)
  249. lcd.print("0");
  250. lcd.print(hour);
  251. lcd.print(":");
  252. if (minute < 10)
  253. lcd.print("0");
  254. lcd.print(minute);
  255. lcd.print(":");
  256. if (second < 10)
  257. lcd.print("0");
  258. lcd.print(second);
  259.  
  260. countLcd++;
  261.  
  262.  
  263. if (countLcd <= 50)
  264. {
  265.  
  266. lcd.setCursor(0, 1);
  267. lcd.print("Temp:");
  268. lcd.print(temp);
  269. lcd.print("-");
  270. lcd.print(humidity);
  271. lcd.print("%");
  272. }
  273. else
  274. {
  275. lcd.setCursor(0, 1);
  276. lcd.print("Saidas:");
  277. lcd.print(ValueSaveSaida1);
  278. lcd.print(ValueSaveSaida2);
  279. lcd.print(ValueSaveSaida3);
  280. lcd.print(ValueSaveSaida4);
  281. lcd.print(ValueSaveSaida5);
  282. lcd.print(ValueSaveSaida6);
  283. lcd.print(ValueSaveSaida7);
  284. lcd.print(ValueSaveSaida8);
  285.  
  286.  
  287. if (ValueRed > 10 )
  288. lcd.print("R");
  289. else
  290. lcd.print(" ");
  291. if (ValueGreen > 10 )
  292. lcd.print("G");
  293. else
  294. lcd.print(" ");
  295.  
  296. if (ValueBlue > 10 )
  297. lcd.print("B ");
  298. else
  299. lcd.print(" ");
  300. }
  301.  
  302. if (countLcd >= 100)
  303. countLcd = 0;
  304.  
  305. }
  306.  
  307. void WebServer() {
  308.  
  309. EthernetClient client = server.available();
  310. if (client) {
  311. while (client.connected()) {
  312. if (client.available()) {
  313. char c = client.read();
  314. if (readString.length() < 100) {
  315. readString += c;
  316. //Serial.print(c);
  317. }
  318.  
  319. //if HTTP request has ended
  320. if (c == '\n') {
  321. //Serial.println(readString);
  322.  
  323.  
  324. Serial.println(readString);
  325. if (readString.indexOf("?AUTOL") > 0) {
  326. ValueSaveAuto = 1;
  327. EEPROM.write(MemAuto, 1);
  328. }
  329.  
  330. if (readString.indexOf("?AUTOD") > 0) {
  331. ValueSaveAuto = 0;
  332. EEPROM.write(MemAuto, 0);
  333. }
  334.  
  335. if (readString.indexOf("?DataHora") > 0) {
  336.  
  337. String Data = readString.substring(readString.indexOf("y") + 1, readString.lastIndexOf("y"));
  338. String Horario = readString.substring(readString.indexOf("z") + 1, readString.lastIndexOf("z"));
  339.  
  340. // Serial.println(Data);
  341. // Serial.println(Horario);
  342.  
  343. //Data no formato dd/mm/yyyy
  344. String Dia = Data.substring(0, Data.indexOf("/"));
  345. String temp = Data.substring(Data.indexOf("/"));
  346. String Mes = temp.substring(1, 3);
  347. String Ano = temp.substring(temp.lastIndexOf("/") + 3);
  348.  
  349. String Hora = Horario.substring(0, Horario.indexOf(":"));
  350. String temp2 = Horario.substring(Horario.indexOf(":"));
  351. String Minuto = temp2.substring(1, 3);
  352.  
  353. //Serial.println(Hora);
  354. //Serial.println(Minuto);
  355.  
  356. // Serial.println(Dia);
  357. // Serial.println(Mes);
  358. // Serial.println(Ano);
  359.  
  360.  
  361. second = 0;
  362. minute = Minuto.toInt();
  363. hour = Hora.toInt();
  364. dayOfWeek = 1;
  365. dayOfMonth = Dia.toInt();
  366. month = Mes.toInt();
  367. year = Ano.toInt();
  368.  
  369. // Serial.println(dayOfMonth);
  370. // Serial.println(month);
  371. // Serial.println(year);
  372.  
  373. setDateDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year);
  374. }
  375.  
  376. if (readString.indexOf("?S1L") > 0) {
  377. digitalWrite(PIN_S1, HIGH);
  378. if (EEPROMComandos)
  379. {
  380. EEPROM.write(MemSaida1, 1);
  381. }
  382. }
  383. if (readString.indexOf("?S1D") > 0) {
  384. digitalWrite(PIN_S1, LOW);
  385. if (EEPROMComandos)
  386. {
  387. EEPROM.write(MemSaida1, 0);
  388. }
  389. }
  390.  
  391. if (readString.indexOf("?S2L") > 0) {
  392. digitalWrite(PIN_S2, HIGH);
  393. if (EEPROMComandos)
  394. {
  395. EEPROM.write(MemSaida2, 2);
  396. }
  397. }
  398. if (readString.indexOf("?S2D") > 0) {
  399. digitalWrite(PIN_S2, LOW);
  400. if (EEPROMComandos)
  401. {
  402. EEPROM.write(MemSaida2, 0);
  403. }
  404. }
  405. if (readString.indexOf("?S3L") > 0) {
  406. digitalWrite(PIN_S3, HIGH);
  407. if (EEPROMComandos)
  408. {
  409. EEPROM.write(MemSaida3, 3);
  410. }
  411. }
  412. if (readString.indexOf("?S3D") > 0) {
  413. digitalWrite(PIN_S3, LOW);
  414. if (EEPROMComandos)
  415. {
  416. EEPROM.write(MemSaida3, 0);
  417. }
  418. }
  419. if (readString.indexOf("?S4L") > 0) {
  420. digitalWrite(PIN_S4, HIGH);
  421. if (EEPROMComandos)
  422. {
  423. EEPROM.write(MemSaida4, 4);
  424. }
  425. }
  426. if (readString.indexOf("?S4D") > 0) {
  427. digitalWrite(PIN_S4, LOW);
  428. if (EEPROMComandos)
  429. {
  430. EEPROM.write(MemSaida4, 0);
  431. }
  432. }
  433. if (readString.indexOf("?S5L") > 0) {
  434. digitalWrite(PIN_S5, HIGH);
  435. if (EEPROMComandos)
  436. {
  437. EEPROM.write(MemSaida5, 5);
  438. }
  439. }
  440. if (readString.indexOf("?S5D") > 0) {
  441. digitalWrite(PIN_S5, LOW);
  442. if (EEPROMComandos)
  443. {
  444. EEPROM.write(MemSaida5, 0);
  445. }
  446. }
  447. if (readString.indexOf("?S6L") > 0) {
  448. digitalWrite(PIN_S6, HIGH);
  449. if (EEPROMComandos)
  450. {
  451. EEPROM.write(MemSaida6, 6);
  452. }
  453. }
  454. if (readString.indexOf("?S6D") > 0) {
  455. digitalWrite(PIN_S6, LOW);
  456. if (EEPROMComandos)
  457. {
  458. EEPROM.write(MemSaida6, 0);
  459. }
  460. }
  461. if (readString.indexOf("?S7L") > 0) {
  462. digitalWrite(PIN_S7, HIGH);
  463. if (EEPROMComandos)
  464. {
  465. EEPROM.write(MemSaida7, 7);
  466. }
  467. }
  468. if (readString.indexOf("?S7D") > 0) {
  469. digitalWrite(PIN_S7, LOW);
  470. if (EEPROMComandos)
  471. {
  472. EEPROM.write(MemSaida7, 0);
  473. }
  474. }
  475. if (readString.indexOf("?S8L") > 0) {
  476. digitalWrite(PIN_S8, HIGH);
  477. if (EEPROMComandos)
  478. {
  479. EEPROM.write(MemSaida8, 8);
  480. }
  481. }
  482. if (readString.indexOf("?S8D") > 0) {
  483. digitalWrite(PIN_S8, LOW);
  484. if (EEPROMComandos)
  485. {
  486. EEPROM.write(MemSaida8, 0);
  487. }
  488. }
  489.  
  490.  
  491. if (readString.indexOf("?RED") > 0) {
  492. ValueRed = 255;
  493. ValueGreen = 0;
  494. ValueBlue = 0;
  495. analogWrite(PIN_RED, ValueRed);
  496. analogWrite(PIN_GREEN, ValueGreen);
  497. analogWrite(PIN_BLUE, ValueBlue);
  498. if (EEPROMComandos)
  499. {
  500. EEPROM.write(MemRed, ValueRed);
  501. EEPROM.write(MemGreen, ValueGreen);
  502. EEPROM.write(MemBlue, ValueBlue);
  503. }
  504. }
  505.  
  506. if (readString.indexOf("?GRE") > 0) {
  507. ValueRed = 0;
  508. ValueGreen = 255;
  509. ValueBlue = 0;
  510. analogWrite(PIN_RED, ValueRed);
  511. analogWrite(PIN_GREEN, ValueGreen);
  512. analogWrite(PIN_BLUE, ValueBlue);
  513. if (EEPROMComandos)
  514. {
  515. EEPROM.write(MemRed, ValueRed);
  516. EEPROM.write(MemGreen, ValueGreen);
  517. EEPROM.write(MemBlue, ValueBlue);
  518. }
  519. }
  520.  
  521. if (readString.indexOf("?BLU") > 0) {
  522. ValueRed = 0;
  523. ValueGreen = 0;
  524. ValueBlue = 255;
  525. analogWrite(PIN_RED, ValueRed);
  526. analogWrite(PIN_GREEN, ValueGreen);
  527. analogWrite(PIN_BLUE, ValueBlue);
  528. if (EEPROMComandos)
  529. {
  530. EEPROM.write(MemRed, ValueRed);
  531. EEPROM.write(MemGreen, ValueGreen);
  532. EEPROM.write(MemBlue, ValueBlue);
  533. }
  534. }
  535.  
  536. if (readString.indexOf("?WHI") > 0) {
  537. ValueRed = 255;
  538. ValueGreen = 255;
  539. ValueBlue = 255;
  540. analogWrite(PIN_RED, ValueRed);
  541. analogWrite(PIN_GREEN, ValueGreen);
  542. analogWrite(PIN_BLUE, ValueBlue);
  543. if (EEPROMComandos)
  544. {
  545. EEPROM.write(MemRed, ValueRed);
  546. EEPROM.write(MemGreen, ValueGreen);
  547. EEPROM.write(MemBlue, ValueBlue);
  548. }
  549. }
  550.  
  551. if (readString.indexOf("?RGBOFF") > 0) {
  552. ValueRed = 0;
  553. ValueGreen = 0;
  554. ValueBlue = 0;
  555. analogWrite(PIN_RED, ValueRed);
  556. analogWrite(PIN_GREEN, ValueGreen);
  557. analogWrite(PIN_BLUE, ValueBlue);
  558. if (EEPROMComandos)
  559. {
  560. EEPROM.write(MemRed, ValueRed);
  561. EEPROM.write(MemGreen, ValueGreen);
  562. EEPROM.write(MemBlue, ValueBlue);
  563. }
  564. }
  565.  
  566. if (readString.indexOf("?AgeS1HrI") > 0) {
  567.  
  568. int cmd = readString.substring(readString.indexOf("y") + 1, readString.lastIndexOf("y")).toInt();
  569. EEPROM.write(MemSaida1HrI, cmd);
  570. ValueSaida1HrI = cmd;
  571.  
  572. cmd = readString.substring(readString.indexOf("z") + 1, readString.lastIndexOf("z")).toInt();
  573. EEPROM.write(MemSaida1HrF, cmd);
  574. ValueSaida1HrF = cmd;
  575.  
  576. }
  577.  
  578. if (readString.indexOf("?AgeS2HrI") > 0) {
  579.  
  580. int cmd = readString.substring(readString.indexOf("y") + 1, readString.lastIndexOf("y")).toInt();
  581. EEPROM.write(MemSaida2HrI, cmd);
  582. ValueSaida2HrI = cmd;
  583. cmd = readString.substring(readString.indexOf("z") + 1, readString.lastIndexOf("z")).toInt();
  584. EEPROM.write(MemSaida2HrF, cmd);
  585. ValueSaida2HrF = cmd;
  586. }
  587.  
  588. if (readString.indexOf("?AgeS3HrI") > 0) {
  589.  
  590. int cmd = readString.substring(readString.indexOf("y") + 1, readString.lastIndexOf("y")).toInt();
  591. EEPROM.write(MemSaida3HrI, cmd);
  592. ValueSaida3HrI = cmd;
  593.  
  594. cmd = readString.substring(readString.indexOf("z") + 1, readString.lastIndexOf("z")).toInt();
  595. EEPROM.write(MemSaida3HrF, cmd);
  596. ValueSaida3HrF = cmd;
  597. }
  598.  
  599. if (readString.indexOf("?AgeS4HrI") > 0) {
  600. int cmd = readString.substring(readString.indexOf("y") + 1, readString.lastIndexOf("y")).toInt();
  601. EEPROM.write(MemSaida4HrI, cmd);
  602. ValueSaida4HrI = cmd;
  603.  
  604. cmd = readString.substring(readString.indexOf("z") + 1, readString.lastIndexOf("z")).toInt();
  605. EEPROM.write(MemSaida4HrF, cmd);
  606. ValueSaida4HrF = cmd;
  607. }
  608.  
  609. if (readString.indexOf("?AgeS5HrI") > 0) {
  610. int cmd = readString.substring(readString.indexOf("y") + 1, readString.lastIndexOf("y")).toInt();
  611. EEPROM.write(MemSaida5HrI, cmd);
  612. ValueSaida5HrI = cmd;
  613.  
  614. cmd = readString.substring(readString.indexOf("z") + 1, readString.lastIndexOf("z")).toInt();
  615. EEPROM.write(MemSaida5HrF, cmd);
  616. ValueSaida5HrF = cmd;
  617. }
  618.  
  619. if (readString.indexOf("?AgeS6HrI") > 0) {
  620. int cmd = readString.substring(readString.indexOf("y") + 1, readString.lastIndexOf("y")).toInt();
  621. EEPROM.write(MemSaida6HrI, cmd);
  622. ValueSaida6HrI = cmd;
  623.  
  624. cmd = readString.substring(readString.indexOf("z") + 1, readString.lastIndexOf("z")).toInt();
  625. EEPROM.write(MemSaida6HrF, cmd);
  626. ValueSaida6HrF = cmd;
  627. }
  628.  
  629. if (readString.indexOf("?AgeS7HrI") > 0) {
  630. int cmd = readString.substring(readString.indexOf("y") + 1, readString.lastIndexOf("y")).toInt();
  631. EEPROM.write(MemSaida7HrI, cmd);
  632. ValueSaida7HrI = cmd;
  633.  
  634. cmd = readString.substring(readString.indexOf("z") + 1, readString.lastIndexOf("z")).toInt();
  635. EEPROM.write(MemSaida7HrF, cmd);
  636. ValueSaida7HrF = cmd;
  637. }
  638.  
  639. if (readString.indexOf("?AgeS8HrI") > 0) {
  640. int cmd = readString.substring(readString.indexOf("y") + 1, readString.lastIndexOf("y")).toInt();
  641. EEPROM.write(MemSaida8HrI, cmd);
  642. ValueSaida8HrI = cmd;
  643.  
  644. cmd = readString.substring(readString.indexOf("z") + 1, readString.lastIndexOf("z")).toInt();
  645. EEPROM.write(MemSaida8HrF, cmd);
  646. ValueSaida8HrF = cmd;
  647. }
  648.  
  649.  
  650. if (readString.indexOf("?AgeRGBHrI") > 0) {
  651. int cmd = readString.substring(readString.indexOf("y") + 1, readString.lastIndexOf("y")).toInt();
  652. EEPROM.write(MemRGBHrI, cmd);
  653. ValueRGBHrI = cmd;
  654.  
  655. cmd = readString.substring(readString.indexOf("z") + 1, readString.lastIndexOf("z")).toInt();
  656. EEPROM.write(MemRGBHrF, cmd);
  657. ValueRGBHrF = cmd;
  658. }
  659.  
  660. SendResponse(client);
  661. delay(1);
  662. client.stop();
  663. readString = "";
  664.  
  665. }
  666. }
  667. }
  668. }
  669.  
  670. }
  671.  
  672.  
  673. void SendResponse(EthernetClient client) {
  674.  
  675. ModoAuto(); // Atualizar variaveis
  676.  
  677.  
  678. ValueSaveSaida1 = digitalRead(PIN_S1);
  679. ValueSaveSaida2 = digitalRead(PIN_S2);
  680. ValueSaveSaida3 = digitalRead(PIN_S3);
  681. ValueSaveSaida4 = digitalRead(PIN_S4);
  682. ValueSaveSaida5 = digitalRead(PIN_S5);
  683. ValueSaveSaida6 = digitalRead(PIN_S6);
  684. ValueSaveSaida7 = digitalRead(PIN_S7);
  685. ValueSaveSaida8 = digitalRead(PIN_S8);
  686.  
  687. client.println(F("HTTP/1.1 200 OK")); //send new page
  688. client.println(F("Content-Type: application/json"));
  689. client.println();
  690. client.print("dataCB");
  691. client.println(F("({"));
  692.  
  693. client.print("\"Auto\":\"");
  694. client.print(ValueSaveAuto);
  695. client.println("\"");
  696.  
  697.  
  698. client.print(",\"Day\":");
  699. client.println(dayOfMonth, DEC);
  700. client.print(",\"Mounth\":");
  701. client.println(month, DEC);
  702. client.print(",\"Year\":");
  703. client.println(year, DEC);
  704. client.print(",\"Hour\":");
  705. client.println(hour, DEC);
  706. client.print(",\"Minute\":");
  707. client.println(minute, DEC);
  708. client.print(",\"Second\":");
  709. client.println(second, DEC);
  710.  
  711.  
  712. client.print(",\"temp\":");
  713. client.println(temp);
  714. client.print(",\"humidity\":");
  715. client.println(humidity);
  716.  
  717.  
  718. client.print(",\"S1\":");
  719. client.println(ValueSaveSaida1);
  720. client.print(",\"S2\":");
  721. client.println(ValueSaveSaida2);
  722. client.print(",\"S3\":");
  723. client.println(ValueSaveSaida3);
  724. client.print(",\"S4\":");
  725. client.println(ValueSaveSaida4);
  726. client.print(",\"S5\":");
  727. client.println(ValueSaveSaida5);
  728. client.print(",\"S6\":");
  729. client.println(ValueSaveSaida6);
  730. client.print(",\"S7\":");
  731. client.println(ValueSaveSaida7);
  732. client.print(",\"S8\":");
  733. client.println(ValueSaveSaida8);
  734.  
  735. client.print(",\"AgeS1HrI\":");
  736. client.println(ValueSaida1HrI);
  737. client.print(",\"AgeS1HrF\":");
  738. client.println(ValueSaida1HrF);
  739. client.print(",\"AgeS2HrI\":");
  740. client.println(ValueSaida2HrI);
  741. client.print(",\"AgeS2HrF\":");
  742. client.println(ValueSaida2HrF);
  743. client.print(",\"AgeS3HrI\":");
  744. client.println(ValueSaida3HrI);
  745. client.print(",\"AgeS3HrF\":");
  746. client.println(ValueSaida3HrF);
  747. client.print(",\"AgeS4HrI\":");
  748. client.println(ValueSaida4HrI);
  749. client.print(",\"AgeS4HrF\":");
  750. client.println(ValueSaida4HrF);
  751. client.print(",\"AgeS5HrI\":");
  752. client.println(ValueSaida5HrI);
  753. client.print(",\"AgeS5HrF\":");
  754. client.println(ValueSaida5HrF);
  755. client.print(",\"AgeS6HrI\":");
  756. client.println(ValueSaida6HrI);
  757. client.print(",\"AgeS6HrF\":");
  758. client.println(ValueSaida6HrF);
  759. client.print(",\"AgeS7HrI\":");
  760. client.println(ValueSaida7HrI);
  761. client.print(",\"AgeS7HrF\":");
  762. client.println(ValueSaida7HrF);
  763. client.print(",\"AgeS8HrI\":");
  764. client.println(ValueSaida8HrI);
  765. client.print(",\"AgeS8HrF\":");
  766. client.println(ValueSaida8HrF);
  767.  
  768. client.print(",\"AgeRGBHrI\":");
  769. client.println(ValueRGBHrI);
  770. client.print(",\"AgeRGBHrF\":");
  771. client.println(ValueRGBHrF);
  772.  
  773. client.print(",\"Red\":");
  774. client.println(ValueRed);
  775. client.print(",\"Green\":");
  776. client.println(ValueGreen);
  777. client.print(",\"Blue\":");
  778. client.println(ValueBlue);
  779.  
  780. client.print(",\"A6\":");
  781. client.println(analogRead(A6));
  782. client.print(",\"A7\":");
  783. client.println(analogRead(A7));
  784.  
  785. client.println(F("})"));
  786. client.println();
  787.  
  788.  
  789. }
  790.  
  791. void ModoAuto() {
  792.  
  793. //Verifica se modo Automático está ativado
  794. if (ValueSaveAuto == 1)
  795. {
  796. //Saida 1
  797. if (ValueSaida1HrI <= hour && ValueSaida1HrF >= hour)
  798. {
  799. digitalWrite(PIN_S1, HIGH);
  800. }
  801. else
  802. {
  803. digitalWrite(PIN_S1, LOW);
  804. }
  805.  
  806. //Saida 2
  807. if (ValueSaida2HrI <= hour && ValueSaida2HrF >= hour)
  808. {
  809. digitalWrite(PIN_S2, HIGH);
  810. }
  811. else
  812. {
  813. digitalWrite(PIN_S2, LOW);
  814. }
  815.  
  816. //Saida 3
  817. if (ValueSaida3HrI <= hour && ValueSaida3HrF >= hour)
  818. {
  819. digitalWrite(PIN_S3, HIGH);
  820. }
  821. else
  822. {
  823. digitalWrite(PIN_S3, LOW);
  824. }
  825.  
  826. //Saida 4
  827. if (ValueSaida4HrI <= hour && ValueSaida4HrF >= hour)
  828. {
  829. digitalWrite(PIN_S4, HIGH);
  830. }
  831. else
  832. {
  833. digitalWrite(PIN_S4, LOW);
  834. }
  835. //Saida 5
  836. if (ValueSaida5HrI <= hour && ValueSaida5HrF >= hour)
  837. {
  838. digitalWrite(PIN_S5, HIGH);
  839. }
  840. else
  841. {
  842. digitalWrite(PIN_S5, LOW);
  843. }
  844.  
  845. //Saida 6
  846. if (ValueSaida6HrI <= hour && ValueSaida6HrF >= hour)
  847. {
  848. digitalWrite(PIN_S6, HIGH);
  849. }
  850. else
  851. {
  852. digitalWrite(PIN_S6, LOW);
  853. }
  854.  
  855. //Saida 7
  856. if (ValueSaida7HrI <= hour && ValueSaida7HrF >= hour)
  857. {
  858. digitalWrite(PIN_S7, HIGH);
  859. }
  860. else
  861. {
  862. digitalWrite(PIN_S7, LOW);
  863. }
  864.  
  865. //Saida 8
  866. if (ValueSaida8HrI <= hour && ValueSaida8HrF >= hour)
  867. {
  868. digitalWrite(PIN_S8, HIGH);
  869. }
  870. else
  871. {
  872. digitalWrite(PIN_S8, LOW);
  873. }
  874.  
  875. //RGB
  876. if (ValueRGBHrI <= hour && ValueRGBHrF >= hour)
  877. {
  878. analogWrite(PIN_RED, ValueRed);
  879. analogWrite(PIN_GREEN, ValueGreen);
  880. analogWrite(PIN_BLUE, ValueBlue);
  881. }
  882. else
  883. {
  884. analogWrite(PIN_RED, 0);
  885. analogWrite(PIN_GREEN, 0);
  886. analogWrite(PIN_BLUE, 0);
  887. }
  888. }
  889. }
  890.  
  891. void getDateDs1307(byte * second,
  892. byte * minute,
  893. byte * hour,
  894. byte * dayOfWeek,
  895. byte * dayOfMonth,
  896. byte * month,
  897. byte * year)
  898. {
  899.  
  900. Wire.beginTransmission(DS1307_I2C_ADDRESS);
  901. Wire.write(0);
  902. Wire.endTransmission();
  903. Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
  904.  
  905. *second = bcdToDec(Wire.read() & 0x7f);
  906. *minute = bcdToDec(Wire.read());
  907. *hour = bcdToDec(Wire.read() & 0x3f); // Need to change this if 12 hour am/pm
  908. *dayOfWeek = bcdToDec(Wire.read());
  909. *dayOfMonth = bcdToDec(Wire.read());
  910. *month = bcdToDec(Wire.read());
  911. *year = bcdToDec(Wire.read());
  912.  
  913. }
  914.  
  915. byte decToBcd(byte val)
  916. {
  917. return ( (val / 10 * 16) + (val % 10) );
  918. }
  919.  
  920. byte bcdToDec(byte val)
  921. {
  922. return ( (val / 16 * 10) + (val % 16) );
  923. }
  924.  
  925. void setDateDs1307(byte second, // 0-59
  926. byte minute, // 0-59
  927. byte hour, // 1-23
  928. byte dayOfWeek, // 1-7
  929. byte dayOfMonth, // 1-28/29/30/31
  930. byte month, // 1-12
  931. byte year) // 0-99
  932. {
  933. Wire.beginTransmission(DS1307_I2C_ADDRESS);
  934. Wire.write(0);
  935. Wire.write(decToBcd(second)); // 0 to bit 7 starts the clock
  936. Wire.write(decToBcd(minute));
  937. Wire.write(decToBcd(hour)); // If you want 12 hour am/pm you need to set
  938. // bit 6 (also need to change readDateDs1307)
  939. Wire.write(decToBcd(dayOfWeek));
  940. Wire.write(decToBcd(dayOfMonth));
  941. Wire.write(decToBcd(month));
  942. Wire.write(decToBcd(year));
  943. Wire.endTransmission();
  944. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement