Advertisement
Anwnimos

Smart Home2

Oct 15th, 2016
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.33 KB | None | 0 0
  1. #include <Ethernet.h>
  2. #include <EthernetClient.h>
  3. #include <EthernetServer.h>
  4. #include <EthernetUdp.h>
  5.  
  6. /*--------------------------------------------------------------
  7. Program: eth_websrv_SD_Ajax_in_out
  8.  
  9. Description: Arduino web server that displays 4 analog inputs,
  10. the state of 3 switches and controls 4 outputs,
  11. 2 using checkboxes and 2 using buttons.
  12. The web page is stored on the micro SD card.
  13.  
  14. Hardware: Arduino Uno and official Arduino Ethernet
  15. shield. Should work with other Arduinos and
  16. compatible Ethernet shields.
  17. 2Gb micro SD card formatted FAT16.
  18. A2 to A4 analog inputs, pins 2, 3 and 5 for
  19. the switches, pins 6 to 9 as outputs (LEDs).
  20.  
  21. Software: Developed using Arduino 1.0.5 software
  22. Should be compatible with Arduino 1.0 +
  23. SD card contains web page called index.htm
  24.  
  25. References: - WebServer example by David A. Mellis and
  26. modified by Tom Igoe
  27. - SD card examples by David A. Mellis and
  28. Tom Igoe
  29. - Ethernet library documentation:
  30. http://arduino.cc/en/Reference/Ethernet
  31. - SD Card library documentation:
  32. http://arduino.cc/en/Reference/SD
  33.  
  34. Date: 4 April 2013
  35. Modified: 19 June 2013
  36. - removed use of the String class
  37.  
  38. Author: W.A. Smith, http://startingelectronics.com
  39. --------------------------------------------------------------*/
  40.  
  41. #include <SPI.h>
  42. #include <Ethernet.h>
  43. #include <SD.h>
  44. // size of buffer used to capture HTTP requests
  45. #define REQ_BUF_SZ 60
  46.  
  47. // MAC address from Ethernet shield sticker under board
  48. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  49. IPAddress ip(192, 168, 1, 20); // IP address, may need to change depending on network
  50. EthernetServer server(80); // create a server at port 80
  51. File webFile; // the web page file on the SD card
  52. char HTTP_req[REQ_BUF_SZ] = {0}; // buffered HTTP request stored as null terminated string
  53. char req_index = 0; // index into HTTP_req buffer
  54. boolean LED_state[20] = {0}; // stores the states of the LEDs
  55.  
  56. void setup()
  57. {
  58. // disable Ethernet chip
  59. pinMode(10, OUTPUT);
  60. digitalWrite(10, HIGH);
  61.  
  62. Serial.begin(9600); // for debugging
  63.  
  64. // initialize SD card
  65. Serial.println("Initializing SD card...");
  66. if (!SD.begin(4)) {
  67. Serial.println("ERROR - SD card initialization failed!");
  68. return; // init failed
  69. }
  70. Serial.println("SUCCESS - SD card initialized.");
  71. // check for index.htm file
  72. if (!SD.exists("index.htm")) {
  73. Serial.println("ERROR - Can't find index.htm file!");
  74. return; // can't find index file
  75. }
  76. Serial.println("SUCCESS - Found index.htm file.");
  77. // switches on pins 2, 3 and 5
  78. pinMode(2, INPUT);
  79. pinMode(3, INPUT);
  80. pinMode(5, INPUT);
  81. // LEDs
  82. pinMode(6, OUTPUT);
  83. pinMode(7, OUTPUT);
  84. pinMode(8, OUTPUT);
  85. pinMode(9, OUTPUT);
  86. pinMode(22, OUTPUT);
  87. pinMode(23, OUTPUT);
  88. pinMode(24, OUTPUT);
  89. pinMode(25, OUTPUT);
  90. pinMode(26, OUTPUT);
  91. pinMode(27, OUTPUT);
  92. pinMode(28, OUTPUT);
  93. pinMode(29, OUTPUT);
  94. pinMode(30, OUTPUT);
  95. pinMode(31, OUTPUT);
  96. pinMode(32, OUTPUT);
  97. pinMode(33, OUTPUT);
  98. pinMode(34, OUTPUT);
  99. pinMode(35, OUTPUT);
  100. pinMode(36, OUTPUT);
  101. pinMode(37, OUTPUT);
  102.  
  103. Ethernet.begin(mac, ip); // initialize Ethernet device
  104. server.begin(); // start to listen for clients
  105. }
  106.  
  107. void loop()
  108. {
  109. EthernetClient client = server.available(); // try to get client
  110.  
  111. if (client) { // got client?
  112. boolean currentLineIsBlank = true;
  113. while (client.connected()) {
  114. if (client.available()) { // client data available to read
  115. char c = client.read(); // read 1 byte (character) from client
  116. // limit the size of the stored received HTTP request
  117. // buffer first part of HTTP request in HTTP_req array (string)
  118. // leave last element in array as 0 to null terminate string (REQ_BUF_SZ - 1)
  119. if (req_index < (REQ_BUF_SZ - 1)) {
  120. HTTP_req[req_index] = c; // save HTTP request character
  121. req_index++;
  122. }
  123. // last line of client request is blank and ends with \n
  124. // respond to client only after last line received
  125. if (c == '\n' && currentLineIsBlank) {
  126. // send a standard http response header
  127. client.println("HTTP/1.1 200 OK");
  128. // remainder of header follows below, depending on if
  129. // web page or XML page is requested
  130. // Ajax request - send XML file
  131. if (StrContains(HTTP_req, "ajax_inputs")) {
  132. // send rest of HTTP header
  133. client.println("Content-Type: text/xml");
  134. client.println("Connection: keep-alive");
  135. client.println();
  136. SetLEDs();
  137. // send XML file containing input states
  138. XML_response(client);
  139. }
  140. else { // web page request
  141. // send rest of HTTP header
  142. client.println("Content-Type: text/html");
  143. client.println("Connection: keep-alive");
  144. client.println();
  145. // send web page
  146. webFile = SD.open("index.htm"); // open web page file
  147. if (webFile) {
  148. while(webFile.available()) {
  149. client.write(webFile.read()); // send web page to client
  150. }
  151. webFile.close();
  152. }
  153. }
  154. // display received HTTP request on serial port
  155. Serial.print(HTTP_req);
  156. // reset buffer index and all buffer elements to 0
  157. req_index = 0;
  158. StrClear(HTTP_req, REQ_BUF_SZ);
  159. break;
  160. }
  161. // every line of text received from the client ends with \r\n
  162. if (c == '\n') {
  163. // last character on line of received text
  164. // starting new line with next character read
  165. currentLineIsBlank = true;
  166. }
  167. else if (c != '\r') {
  168. // a text character was received from client
  169. currentLineIsBlank = false;
  170. }
  171. } // end if (client.available())
  172. } // end while (client.connected())
  173. delay(1); // give the web browser time to receive the data
  174. client.stop(); // close the connection
  175. } // end if (client)
  176. }
  177.  
  178. // checks if received HTTP request is switching on/off LEDs
  179. // also saves the state of the LEDs
  180. void SetLEDs(void)
  181. {
  182. // LED 1 (pin 6)
  183. if (StrContains(HTTP_req, "LED1=1")) {
  184. LED_state[0] = 1; // save LED state
  185. digitalWrite(6, HIGH);
  186. }
  187. else if (StrContains(HTTP_req, "LED1=0")) {
  188. LED_state[0] = 0; // save LED state
  189. digitalWrite(6, LOW);
  190. }
  191. // LED 2 (pin 7)
  192. if (StrContains(HTTP_req, "LED2=1")) {
  193. LED_state[1] = 1; // save LED state
  194. digitalWrite(7, HIGH);
  195. }
  196. else if (StrContains(HTTP_req, "LED2=0")) {
  197. LED_state[1] = 0; // save LED state
  198. digitalWrite(7, LOW);
  199. }
  200. // LED 3 (pin 8)
  201. if (StrContains(HTTP_req, "LED3=1")) {
  202. LED_state[2] = 1; // save LED state
  203. digitalWrite(8, HIGH);
  204. }
  205. else if (StrContains(HTTP_req, "LED3=0")) {
  206. LED_state[2] = 0; // save LED state
  207. digitalWrite(8, LOW);
  208. }
  209. // LED 4 (pin 9)
  210. if (StrContains(HTTP_req, "LED4=1")) {
  211. LED_state[3] = 1; // save LED state
  212. digitalWrite(9, HIGH);
  213. }
  214. else if (StrContains(HTTP_req, "LED4=0")) {
  215. LED_state[3] = 0; // save LED state
  216. digitalWrite(9, LOW);
  217. }
  218. // LED 5 (pin 22)
  219. if (StrContains(HTTP_req, "LED5=1")) {
  220. LED_state[4] = 1; // save LED state
  221. digitalWrite(22, HIGH);
  222. }
  223. else if (StrContains(HTTP_req, "LED5=0")) {
  224. LED_state[4] = 0; // save LED state
  225. digitalWrite(22, LOW);
  226. }
  227. // LED 6 (pin 23)
  228. if (StrContains(HTTP_req, "LED6=1")) {
  229. LED_state[5] = 1; // save LED state
  230. digitalWrite(23, HIGH);
  231. }
  232. else if (StrContains(HTTP_req, "LED6=0")) {
  233. LED_state[5] = 0; // save LED state
  234. digitalWrite(23, LOW);
  235. }
  236. // LED 7 (pin 24)
  237. if (StrContains(HTTP_req, "LED7=1")) {
  238. LED_state[6] = 1; // save LED state
  239. digitalWrite(24, HIGH);
  240. }
  241. else if (StrContains(HTTP_req, "LED7=0")) {
  242. LED_state[6] = 0; // save LED state
  243. digitalWrite(24, LOW);
  244. }
  245. // LED 8 (pin 25)
  246. if (StrContains(HTTP_req, "LED8=1")) {
  247. LED_state[7] = 1; // save LED state
  248. digitalWrite(25, HIGH);
  249. }
  250. else if (StrContains(HTTP_req, "LED8=0")) {
  251. LED_state[7] = 0; // save LED state
  252. digitalWrite(25, LOW);
  253. }
  254. // LED 9 (pin 26)
  255. if (StrContains(HTTP_req, "LED9=1")) {
  256. LED_state[8] = 1; // save LED state
  257. digitalWrite(26, HIGH);
  258. }
  259. else if (StrContains(HTTP_req, "LED9=0")) {
  260. LED_state[8] = 0; // save LED state
  261. digitalWrite(26, LOW);
  262. }
  263. // Porta anoigma (pin 27)
  264. if (StrContains(HTTP_req, "LED10=1")) {
  265. LED_state[9] = 1; // save LED state
  266. digitalWrite(27, HIGH);
  267. }
  268. else if (StrContains(HTTP_req, "LED10=0")) {
  269. LED_state[9] = 0; // save LED state
  270. digitalWrite(27, LOW);
  271. }
  272. // Porta kleisimo (pin 28)
  273. if (StrContains(HTTP_req, "LED11=1")) {
  274. LED_state[10] = 1; // save LED state
  275. digitalWrite(28, HIGH);
  276. }
  277. else if (StrContains(HTTP_req, "LED11=0")) {
  278. LED_state[10] = 0; // save LED state
  279. digitalWrite(28, LOW);
  280. }
  281. // Parathiro Mpaniou anoigma (pin 29)
  282. if (StrContains(HTTP_req, "LED12=1")) {
  283. LED_state[11] = 1; // save LED state
  284. digitalWrite(29, HIGH);
  285. }
  286. else if (StrContains(HTTP_req, "LED12=0")) {
  287. LED_state[11] = 0; // save LED state
  288. digitalWrite(29, LOW);
  289. }
  290. // Parathiro mpaniou kleisimo (pin 30)
  291. if (StrContains(HTTP_req, "LED13=1")) {
  292. LED_state[12] = 1; // save LED state
  293. digitalWrite(30, HIGH);
  294. }
  295. else if (StrContains(HTTP_req, "LED13=0")) {
  296. LED_state[12] = 0; // save LED state
  297. digitalWrite(30, LOW);
  298. }
  299. // Parathiro krevatokamaras anoigma (pin 31)
  300. if (StrContains(HTTP_req, "LED14=1")) {
  301. LED_state[13] = 1; // save LED state
  302. digitalWrite(31, HIGH);
  303. }
  304. else if (StrContains(HTTP_req, "LED14=0")) {
  305. LED_state[13] = 0; // save LED state
  306. digitalWrite(31, LOW);
  307. }
  308. // Parathiro krevatokamaras kleisimo (pin 32)
  309. if (StrContains(HTTP_req, "LED15=1")) {
  310. LED_state[14] = 1; // save LED state
  311. digitalWrite(32, HIGH);
  312. }
  313. else if (StrContains(HTTP_req, "LED15=0")) {
  314. LED_state[14] = 0; // save LED state
  315. digitalWrite(32, LOW);
  316. }
  317. // Parathiro kouzinas anoigma (pin 33)
  318. if (StrContains(HTTP_req, "LED16=1")) {
  319. LED_state[15] = 1; // save LED state
  320. digitalWrite(33, HIGH);
  321. }
  322. else if (StrContains(HTTP_req, "LED16=0")) {
  323. LED_state[15] = 0; // save LED state
  324. digitalWrite(33, LOW);
  325. }
  326. // Parathiro kouzinas kleisimo (pin 34)
  327. if (StrContains(HTTP_req, "LED17=1")) {
  328. LED_state[16] = 1; // save LED state
  329. digitalWrite(34, HIGH);
  330. }
  331. else if (StrContains(HTTP_req, "LED17=0")) {
  332. LED_state[16] = 0; // save LED state
  333. digitalWrite(34, LOW);
  334. }
  335. // Parathiro domatiou anoigma (pin 35)
  336. if (StrContains(HTTP_req, "LED18=1")) {
  337. LED_state[17] = 1; // save LED state
  338. digitalWrite(35, HIGH);
  339. }
  340. else if (StrContains(HTTP_req, "LED18=0")) {
  341. LED_state[17] = 0; // save LED state
  342. digitalWrite(35, LOW);
  343. }
  344. // Parathiro domatiou kleisimo (pin 36)
  345. if (StrContains(HTTP_req, "LED19=1")) {
  346. LED_state[18] = 1; // save LED state
  347. digitalWrite(36, HIGH);
  348. }
  349. else if (StrContains(HTTP_req, "LED19=0")) {
  350. LED_state[18] = 0; // save LED state
  351. digitalWrite(36, LOW);
  352. }
  353. // Anemistiras (pin 37)
  354. if (StrContains(HTTP_req, "LED20=1")) {
  355. LED_state[19] = 1; // save LED state
  356. digitalWrite(37, HIGH);
  357. }
  358. else if (StrContains(HTTP_req, "LED20=0")) {
  359. LED_state[19] = 0; // save LED state
  360. digitalWrite(37, LOW);
  361. }
  362.  
  363.  
  364. }
  365.  
  366. // send the XML file with analog values, switch status
  367. // and LED status
  368. void XML_response(EthernetClient cl)
  369. {
  370. int analog_val; // stores value read from analog inputs
  371. int count; // used by 'for' loops
  372. int sw_arr[] = {2, 3, 5}; // pins interfaced to switches
  373.  
  374. cl.print("<?xml version = \"1.0\" ?>");
  375. cl.print("<inputs>");
  376. // read analog inputs
  377. for (count = 2; count <= 5; count++) { // A2 to A5
  378. analog_val = analogRead(count);
  379. cl.print("<analog>");
  380. cl.print(analog_val);
  381. cl.println("</analog>");
  382. }
  383. // read switches
  384. for (count = 0; count < 3; count++) {
  385. cl.print("<switch>");
  386. if (digitalRead(sw_arr[count])) {
  387. cl.print("ON");
  388. }
  389. else {
  390. cl.print("OFF");
  391. }
  392. cl.println("</switch>");
  393. }
  394. // checkbox LED states
  395. // LED1
  396. cl.print("<LED>");
  397. if (LED_state[0]) {
  398. cl.print("checked");
  399. }
  400. else {
  401. cl.print("unchecked");
  402. }
  403. cl.println("</LED>");
  404. // LED2
  405. cl.print("<LED>");
  406. if (LED_state[1]) {
  407. cl.print("checked");
  408. }
  409. else {
  410. cl.print("unchecked");
  411. }
  412. cl.println("</LED>");
  413. // button LED states
  414. // LED3
  415. cl.print("<LED>");
  416. if (LED_state[2]) {
  417. cl.print("on");
  418. }
  419. else {
  420. cl.print("off");
  421. }
  422. cl.println("</LED>");
  423. // LED4
  424. cl.print("<LED>");
  425. if (LED_state[3]) {
  426. cl.print("on");
  427. }
  428. else {
  429. cl.print("off");
  430. }
  431. cl.print("<LED>");
  432. // LED5
  433. cl.print("<LED>");
  434. if (LED_state[4]) {
  435. cl.print("on");
  436. }
  437. else {
  438. cl.print("off");
  439. }
  440. cl.println("</LED>");
  441. // LED6
  442. cl.print("<LED>");
  443. if (LED_state[5]) {
  444. cl.print("on");
  445. }
  446. else {
  447. cl.print("off");
  448. }
  449. cl.println("</LED>");
  450. // LED7
  451. cl.print("<LED>");
  452. if (LED_state[6]) {
  453. cl.print("on");
  454. }
  455. else {
  456. cl.print("off");
  457. }
  458. cl.println("</LED>");
  459. // LED8
  460. cl.print("<LED>");
  461. if (LED_state[7]) {
  462. cl.print("on");
  463. }
  464. else {
  465. cl.print("off");
  466. }
  467. cl.println("</LED>");
  468. // LED9
  469. cl.print("<LED>");
  470. if (LED_state[8]) {
  471. cl.print("on");
  472. }
  473. else {
  474. cl.print("off");
  475. }
  476. cl.println("</LED>");
  477. // Porta anoigma
  478. cl.print("<LED>");
  479. if (LED_state[9]) {
  480. cl.print("on");
  481. }
  482. else {
  483. cl.print("off");
  484. }
  485. cl.println("</LED>");
  486. // Porta kleisimo
  487. cl.print("<LED>");
  488. if (LED_state[10]) {
  489. cl.print("on");
  490. }
  491. else {
  492. cl.print("off");
  493. }
  494. cl.println("</LED>");
  495. // Parathiro mpaniou anoigma
  496. cl.print("<LED>");
  497. if (LED_state[11]) {
  498. cl.print("on");
  499. }
  500. else {
  501. cl.print("off");
  502. }
  503. cl.println("</LED>");
  504. // Parathiro mpaniou kleisimo
  505. cl.print("<LED>");
  506. if (LED_state[12]) {
  507. cl.print("on");
  508. }
  509. else {
  510. cl.print("off");
  511. }
  512. cl.println("</LED>");
  513. // Parathiro krevatokamaras anoigma
  514. cl.print("<LED>");
  515. if (LED_state[13]) {
  516. cl.print("on");
  517. }
  518. else {
  519. cl.print("off");
  520. }
  521. cl.println("</LED>");
  522. // Parathiro krevatokamara kleisimo
  523. cl.print("<LED>");
  524. if (LED_state[14]) {
  525. cl.print("on");
  526. }
  527. else {
  528. cl.print("off");
  529. }
  530. cl.println("</LED>");
  531. // Parathiro kouzinas anoigma
  532. cl.print("<LED>");
  533. if (LED_state[15]) {
  534. cl.print("on");
  535. }
  536. else {
  537. cl.print("off");
  538. }
  539. cl.println("</LED>");
  540. // Parathiro kouzinas kleisimo
  541. cl.print("<LED>");
  542. if (LED_state[16]) {
  543. cl.print("on");
  544. }
  545. else {
  546. cl.print("off");
  547. }
  548. cl.println("</LED>");
  549. // Parathiro saloniou anoigma
  550. cl.print("<LED>");
  551. if (LED_state[17]) {
  552. cl.print("on");
  553. }
  554. else {
  555. cl.print("off");
  556. }
  557. cl.println("</LED>");
  558. // Parathiro saloniou kleisimo
  559. cl.print("<LED>");
  560. if (LED_state[18]) {
  561. cl.print("on");
  562. }
  563. else {
  564. cl.print("off");
  565. }
  566. cl.println("</LED>");
  567. // Anemistiras
  568. cl.print("<LED>");
  569. if (LED_state[19]) {
  570. cl.print("on");
  571. }
  572. else {
  573. cl.print("off");
  574. }
  575. cl.println("</LED>");
  576.  
  577. cl.print("</inputs>");
  578. }
  579.  
  580. // sets every element of str to 0 (clears array)
  581. void StrClear(char *str, char length)
  582. {
  583. for (int i = 0; i < length; i++) {
  584. str[i] = 0;
  585. }
  586. }
  587.  
  588. // searches for the string sfind in the string str
  589. // returns 1 if string found
  590. // returns 0 if string not found
  591. char StrContains(char *str, char *sfind)
  592. {
  593. char found = 0;
  594. char index = 0;
  595. char len;
  596.  
  597. len = strlen(str);
  598.  
  599. if (strlen(sfind) > len) {
  600. return 0;
  601. }
  602. while (index < len) {
  603. if (str[index] == sfind[found]) {
  604. found++;
  605. if (strlen(sfind) == found) {
  606. return 1;
  607. }
  608. }
  609. else {
  610. found = 0;
  611. }
  612. index++;
  613. }
  614.  
  615. return 0;
  616. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement