Advertisement
Guest User

Arduino Code

a guest
Apr 7th, 2018
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.61 KB | None | 0 0
  1. /*
  2. Web Server
  3.  
  4. A simple web server that shows the value of the analog input pins.
  5. using an Arduino Wiznet Ethernet shield.
  6.  
  7. Circuit:
  8. * Ethernet shield attached to pins 10, 11, 12, 13
  9. * Analog inputs attached to pins A0 through A5 (optional)
  10.  
  11. created 18 Dec 2009
  12. by David A. Mellis
  13. modified 9 Apr 2012
  14. by Tom Igoe
  15.  
  16. */
  17.  
  18. #include <SPI.h>
  19. #include <Ethernet.h>
  20.  
  21. // Enter a MAC address and IP address for your controller below.
  22. // The IP address will be dependent on your local network:
  23. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x2D };
  24.  
  25.  
  26. byte gateway[] = { 192, 168, 1, 1 }; // <------- PUT YOUR ROUTERS IP Address to which your shield is connected Here
  27. byte subnet[] = { 255, 255, 255, 0 }; // <------- It will be as it is in most of the cases
  28. IPAddress ip(192,168,1,200);
  29.  
  30. // Initialize the Ethernet server library
  31. // with the IP address and port you want to use
  32. // (port 80 is default for HTTP):
  33. EthernetServer server(80);
  34.  
  35.  
  36. #define RELAY_CH1 2
  37. #define RELAY_CH2 3
  38. #define RELAY_CH3 4
  39. #define RELAY_CH4 5
  40. #define RELAY_CH5 6
  41. #define RELAY_CH6 7
  42. #define RELAY_CH7 8
  43. #define RELAY_CH8 9
  44.  
  45. String readString;
  46.  
  47. void setup() {
  48.  
  49.  
  50. pinMode(RELAY_CH1, OUTPUT);
  51. digitalWrite(RELAY_CH1, HIGH); // switch on LED1
  52.  
  53. pinMode(RELAY_CH2, OUTPUT);
  54. digitalWrite(RELAY_CH2, HIGH); // switch on LED2
  55.  
  56. pinMode(RELAY_CH3, OUTPUT);
  57. digitalWrite(RELAY_CH3, HIGH); // switch on LED3
  58.  
  59. pinMode(RELAY_CH4, OUTPUT);
  60. digitalWrite(RELAY_CH4, HIGH); // switch on LED4
  61.  
  62. pinMode(RELAY_CH5, OUTPUT);
  63. digitalWrite(RELAY_CH5, HIGH); // switch on LED5
  64.  
  65. pinMode(RELAY_CH6, OUTPUT);
  66. digitalWrite(RELAY_CH6, HIGH); // switch on LED6
  67.  
  68. pinMode(RELAY_CH7, OUTPUT);
  69. digitalWrite(RELAY_CH7, HIGH); // switch on LED7
  70.  
  71. pinMode(RELAY_CH8, OUTPUT);
  72. digitalWrite(RELAY_CH8, HIGH); // switch on LED8
  73.  
  74.  
  75.  
  76. // Open serial communications and wait for port to open:
  77. Serial.begin(9600);
  78. while (!Serial) {
  79. ; // wait for serial port to connect. Needed for Leonardo only
  80. }
  81.  
  82.  
  83. // start the Ethernet connection and the server:
  84. Ethernet.begin(mac, ip);
  85.  
  86. server.begin();
  87. Serial.print("server is at ");
  88. Serial.println(Ethernet.localIP());
  89.  
  90. }
  91.  
  92.  
  93. void loop() {
  94. // listen for incoming clients
  95. EthernetClient client = server.available();
  96.  
  97. if (client) {
  98. Serial.println("new client");
  99. // an http request ends with a blank line
  100. boolean currentLineIsBlank = true;
  101. while (client.connected()) {
  102.  
  103. if (client.available()) {
  104.  
  105. char c = client.read();
  106.  
  107. if (readString.length() < 100) {
  108. //store characters to string
  109. readString += c; //Serial.print(c);
  110. }
  111.  
  112. Serial.write(c);
  113. // if you've gotten to the end of the line (received a newline
  114. // character) and the line is blank, the http request has ended,
  115. // so you can send a reply
  116.  
  117. if (c == '\n' && currentLineIsBlank) {
  118. // send a standard http response header
  119. client.println("HTTP/1.1 200 OK");
  120. client.println("Content-Type: text/html");
  121. client.println("Connection: close"); // the connection will be closed after completion of the response
  122. client.println("Refresh: 5"); // refresh the page automatically every 5 sec
  123. client.println();
  124. client.println("<!DOCTYPE HTML>");
  125. client.println("<html>");
  126.  
  127. client.println("<HEAD>");
  128. client.println("<meta name='apple-mobile-web-app-capable' content='yes' />");
  129. client.println("<meta name='apple-mobile-web-app-status-bar-style' content='black-translucent' />");
  130. //client.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"http://www.digital2u.net/autohome.css\" />");
  131. client.println("<meta http-equiv=\"refresh\" content=\"5;url=http://192.168.1.200/\"/>" );
  132. client.println("</HEAD>");
  133.  
  134. client.println("<body bgcolor=\"#D0D0D0\">");
  135. //client.println("<hr/>");
  136. //client.println("<hr/>");
  137.  
  138. //client.println("<h4><center><img border=\"2\" src=\"https://lh3.googleusercontent.com/-C6BoJrRUFko/UEUFeCwkvdI/AAAAAAAAAOc/E7gcYvPV6r4/s960/Logo.jpg\" /></center></h4>");
  139. //client.println("<hr/>");
  140. //client.println("<hr/>");
  141.  
  142. client.print("<center> <p> <h1>Welcome to Arduino Home Control </h1></p> ");
  143. //client.println("<br />");
  144. //client.println("<br />");
  145.  
  146.  
  147. // Relay Status Display
  148. client.println("<center>");
  149.  
  150. client.println("<table border=\"5\">");
  151.  
  152. client.println("<tr>");
  153. if (!digitalRead(RELAY_CH1))
  154. {
  155. client.print("<td> <p style=\"font-family:arial;color:black;font-size:26px;\">Device 1.</p><p style=\"font-family:arial;color:green;font-size:35px;\">ON</p> </td>");
  156. }
  157. else
  158. {
  159. client.print("<td> <p style=\"font-family:arial;color:black;font-size:26px;\">Device 1.</p><p style=\"font-family:arial;color:red;font-size:35px;\">OFF</p></td>");
  160. }
  161.  
  162.  
  163. if (!digitalRead(RELAY_CH2))
  164. {
  165. client.print("<td> <p style=\"font-family:arial;color:black;font-size:26px;\">Device 2.</p><p style=\"font-family:arial;color:green;font-size:35px;\">ON</p></td>");
  166. }
  167. else
  168. {
  169. client.print("<td> <p style=\"font-family:arial;color:black;font-size:26px;\">Device 2.</p><p style=\"font-family:arial;color:red;font-size:35px;\">OFF</p></td>");
  170. }
  171.  
  172.  
  173. if (!digitalRead(RELAY_CH3))
  174. {
  175. client.print("<td><p style=\"font-family:arial;color:black;font-size:26px;\">Device 3.</p><p style=\"font-family:arial;color:green;font-size:35px;\">ON</p></td>");
  176. }
  177. else
  178. {
  179. client.print("<td><p style=\"font-family:arial;color:black;font-size:26px;\">Device 3.</p><p style=\"font-family:arial;color:red;font-size:35px;\">OFF</p></td>");
  180. }
  181.  
  182.  
  183. if (!digitalRead(RELAY_CH4))
  184. {
  185. client.print("<td><p style=\"font-family:arial;color:black;font-size:26px;\">Device 4.</p><p style=\"font-family:arial;color:green;font-size:35px;\">ON</p></td>");
  186. }
  187. else
  188. {
  189. client.print("<td><p style=\"font-family:arial;color:black;font-size:26px;\">Device 4.</p><p style=\"font-family:arial;color:red;font-size:35px;\">OFF</p></td>");
  190. }
  191.  
  192.  
  193. if (!digitalRead(RELAY_CH5))
  194. {
  195. client.print("<td><p style=\"font-family:arial;color:black;font-size:26px;\">Device 5.</p><p style=\"font-family:arial;color:green;font-size:35px;\">ON</p></td>");
  196. }
  197. else
  198. {
  199. client.print("<td><p style=\"font-family:arial;color:black;font-size:26px;\">Device 5.</p><p style=\"font-family:arial;color:red;font-size:35px;\">OFF</p></td>");
  200. }
  201.  
  202.  
  203. if (!digitalRead(RELAY_CH6))
  204. {
  205. client.print("<td><p style=\"font-family:arial;color:black;font-size:26px;\">Device 6.</p><p style=\"font-family:arial;color:green;font-size:35px;\">ON</p></td>");
  206. }
  207. else
  208. {
  209. client.print("<td><p style=\"font-family:arial;color:black;font-size:26px;\">Device 6.</p><p style=\"font-family:arial;color:red;font-size:35px;\">OFF</p></td>");
  210. }
  211.  
  212.  
  213. if (!digitalRead(RELAY_CH7))
  214. {
  215. client.print("<td><p style=\"font-family:arial;color:black;font-size:26px;\">Device 7.</p><p style=\"font-family:arial;color:green;font-size:35px;\">ON</p></td>");
  216. }
  217. else
  218. {
  219. client.print("<td><p style=\"font-family:arial;color:black;font-size:26px;\">Device 7.</p><p style=\"font-family:arial;color:red;font-size:35px;\">OFF</p></td>");
  220. }
  221.  
  222.  
  223. if (!digitalRead(RELAY_CH8))
  224. {
  225. client.print("<td><p style=\"font-family:arial;color:black;font-size:26px;\">Device 8.</p><p style=\"font-family:arial;color:green;font-size:35px;\">ON</p></td>");
  226. }
  227. else
  228. {
  229. client.print("<td><p style=\"font-family:arial;color:black;font-size:26px;\">Device 8.</p><p style=\"font-family:arial;color:red;font-size:35px;\">OFF</p></td>");
  230. }
  231.  
  232. client.println("</tr>");
  233. client.println("</table>");
  234.  
  235. client.println("</center>");
  236. client.println("<br />");
  237.  
  238.  
  239.  
  240. /* Relay Control Code */
  241.  
  242. client.println("<a href=\"/?relay1on\"\"> <button style=\"width:360px;height:120px\"> <font size=\"7\"; color:red ;>Device 1 ON </font> </button> </a> ");
  243. client.println("<a href=\"/?relay1off\"\"> <button style=\"width:360px;height:120px\"> <font size=\"7\">Device 1 OFF </font> </button> </a> <br />");
  244. client.println("<br />");
  245.  
  246.  
  247. client.println("<a href=\"/?relay2on\"\"> <button style=\"width:360px;height:120px\"> <font size=\"7\">Device 2 ON </font> </button> </a> ");
  248. client.println("<a href=\"/?relay2off\"\"> <button style=\"width:360px;height:120px\"> <font size=\"7\">Device 2 OFF </font> </button> </a> <br />");
  249. client.println("<br />");
  250.  
  251.  
  252. client.println("<a href=\"/?relay3on\"\"> <button style=\"width:360px;height:120px\"> <font size=\"7\">Device 3 ON </font> </button> </a> ");
  253. client.println("<a href=\"/?relay3off\"\"> <button style=\"width:360px;height:120px\"> <font size=\"7\">Device 3 OFF </font> </button> </a> <br />");
  254. client.println("<br />");
  255.  
  256.  
  257. client.println("<a href=\"/?relay4on\"\"> <button style=\"width:360px;height:120px\"> <font size=\"7\">Device 4 ON </font> </button> </a> ");
  258. client.println("<a href=\"/?relay4off\"\"> <button style=\"width:360px;height:120px\"> <font size=\"7\">Device 4 OFF </font> </button> </a> <br />");
  259. client.println("<br />");
  260.  
  261.  
  262. client.println("<a href=\"/?relay5on\"\"> <button style=\"width:360px;height:120px\"> <font size=\"7\">Device 5 ON </font> </button> </a> ");
  263. client.println("<a href=\"/?relay5off\"\"><button style=\"width:360px;height:120px\"> <font size=\"7\">Device 5 OFF </font> </button> </a> <br />");
  264. client.println("<br />");
  265.  
  266.  
  267. client.println("<a href=\"/?relay6on\"\"><button style=\"width:360px;height:120px\"> <font size=\"7\">Device 6 ON </font> </button> </a> ");
  268. client.println("<a href=\"/?relay6off\"\"><button style=\"width:360px;height:120px\"> <font size=\"7\">Device 6 OFF </font> </button> </a> <br />");
  269. client.println("<br />");
  270.  
  271.  
  272. client.println("<a href=\"/?relay7on\"\"><button style=\"width:360px;height:120px\"> <font size=\"7\">Device 7 ON </font> </button> </a> ");
  273. client.println("<a href=\"/?relay7off\"\"><button style=\"width:360px;height:120px\"> <font size=\"7\">Device 7 OFF </font> </button> </a> <br />");
  274. client.println("<br />");
  275.  
  276.  
  277. client.println("<a href=\"/?relay8on\"\"><button style=\"width:360px;height:120px\"> <font size=\"7\">Device 8 ON </font> </button> </a> ");
  278. client.println("<a href=\"/?relay8off\"\"><button style=\"width:360px;height:120px\"> <font size=\"7\">Device 8 OFF </font> </button> </a> <br />");
  279. client.println("<br />");
  280.  
  281.  
  282. // control arduino pin via ethernet Start //
  283.  
  284. if(readString.indexOf("?relay1on") >0)//checks for on
  285. {
  286. digitalWrite(RELAY_CH1, LOW); // set pin 4 high
  287. //Serial.println("Led On");
  288.  
  289. client.println("<link rel='apple-touch-icon' href='http://chriscosma.co.cc/on.png' />");
  290. //client.println("Light 1 Is On");
  291. client.println("<br />");
  292. }
  293. else{
  294. if(readString.indexOf("?relay1off") >0)//checks for off
  295. {
  296. digitalWrite(RELAY_CH1, HIGH); // set pin 4 low
  297. //Serial.println("Led Off");
  298. client.println("<link rel='apple-touch-icon' href='http://chriscosma.co.cc/off.png' />");
  299.  
  300. //client.println("Light 1 Is Off");
  301. client.println("<br />");
  302. }
  303. }
  304.  
  305.  
  306. if(readString.indexOf("?relay2on") >0)//checks for on
  307. {
  308. digitalWrite(RELAY_CH2, LOW); // set pin 4 high
  309. //Serial.println("Led On");
  310.  
  311. client.println("<link rel='apple-touch-icon' href='http://chriscosma.co.cc/on.png' />");
  312. //client.println("Light 1 Is On");
  313. client.println("<br />");
  314. }
  315. else{
  316. if(readString.indexOf("?relay2off") >0)//checks for off
  317. {
  318. digitalWrite(RELAY_CH2, HIGH); // set pin 4 low
  319. //Serial.println("Led Off");
  320. client.println("<link rel='apple-touch-icon' href='http://chriscosma.co.cc/off.png' />");
  321.  
  322. //client.println("Light 1 Is Off");
  323. client.println("<br />");
  324. }
  325. }
  326.  
  327.  
  328. if(readString.indexOf("?relay3on") >0)//checks for on
  329. {
  330. digitalWrite(RELAY_CH3, LOW); // set pin 4 high
  331. //Serial.println("Led On");
  332.  
  333. client.println("<link rel='apple-touch-icon' href='http://chriscosma.co.cc/on.png' />");
  334. //client.println("Light 1 Is On");
  335. client.println("<br />");
  336. }
  337. else{
  338. if(readString.indexOf("?relay3off") >0)//checks for off
  339. {
  340. digitalWrite(RELAY_CH3, HIGH); // set pin 4 low
  341. //Serial.println("Led Off");
  342. client.println("<link rel='apple-touch-icon' href='http://chriscosma.co.cc/off.png' />");
  343.  
  344. //client.println("Light 1 Is Off");
  345. client.println("<br />");
  346. }
  347. }
  348.  
  349. if(readString.indexOf("?relay4on") >0)//checks for on
  350. {
  351. digitalWrite(RELAY_CH4, LOW); // set pin 4 high
  352. //Serial.println("Led On");
  353.  
  354. client.println("<link rel='apple-touch-icon' href='http://chriscosma.co.cc/on.png' />");
  355. //client.println("Light 1 Is On");
  356. client.println("<br />");
  357. }
  358. else{
  359. if(readString.indexOf("?relay4off") >0)//checks for off
  360. {
  361. digitalWrite(RELAY_CH4, HIGH); // set pin 4 low
  362. //Serial.println("Led Off");
  363. client.println("<link rel='apple-touch-icon' href='http://chriscosma.co.cc/off.png' />");
  364.  
  365. //client.println("Light 1 Is Off");
  366. client.println("<br />");
  367. }
  368. }
  369.  
  370. if(readString.indexOf("?relay5on") >0)//checks for on
  371. {
  372. digitalWrite(RELAY_CH5, LOW); // set pin 4 high
  373. //Serial.println("Led On");
  374.  
  375. client.println("<link rel='apple-touch-icon' href='http://chriscosma.co.cc/on.png' />");
  376. //client.println("Light 1 Is On");
  377. client.println("<br />");
  378. }
  379. else{
  380. if(readString.indexOf("?relay5off") >0)//checks for off
  381. {
  382. digitalWrite(RELAY_CH5, HIGH); // set pin 4 low
  383. //Serial.println("Led Off");
  384. client.println("<link rel='apple-touch-icon' href='http://chriscosma.co.cc/off.png' />");
  385.  
  386. //client.println("Light 1 Is Off");
  387. client.println("<br />");
  388. }
  389. }
  390.  
  391.  
  392. if(readString.indexOf("?relay6on") >0)//checks for on
  393. {
  394. digitalWrite(RELAY_CH6, LOW); // set pin 4 high
  395. //Serial.println("Led On");
  396.  
  397. client.println("<link rel='apple-touch-icon' href='http://chriscosma.co.cc/on.png' />");
  398. //client.println("Light 1 Is On");
  399. client.println("<br />");
  400. }
  401. else{
  402. if(readString.indexOf("?relay6off") >0)//checks for off
  403. {
  404. digitalWrite(RELAY_CH6, HIGH); // set pin 4 low
  405. //Serial.println("Led Off");
  406. client.println("<link rel='apple-touch-icon' href='http://chriscosma.co.cc/off.png' />");
  407.  
  408. //client.println("Light 1 Is Off");
  409. client.println("<br />");
  410. }
  411. }
  412.  
  413.  
  414. if(readString.indexOf("?relay7on") >0)//checks for on
  415. {
  416. digitalWrite(RELAY_CH7, LOW); // set pin 4 high
  417. //Serial.println("Led On");
  418.  
  419. client.println("<link rel='apple-touch-icon' href='http://chriscosma.co.cc/on.png' />");
  420. //client.println("Light 1 Is On");
  421. client.println("<br />");
  422. }
  423. else{
  424. if(readString.indexOf("?relay7off") >0)//checks for off
  425. {
  426. digitalWrite(RELAY_CH7, HIGH); // set pin 4 low
  427. //Serial.println("Led Off");
  428. client.println("<link rel='apple-touch-icon' href='http://chriscosma.co.cc/off.png' />");
  429.  
  430. //client.println("Light 1 Is Off");
  431. client.println("<br />");
  432. }
  433. }
  434.  
  435. if(readString.indexOf("?relay8on") >0)//checks for on
  436. {
  437. digitalWrite(RELAY_CH8, LOW); // set pin 4 high
  438. //Serial.println("Led On");
  439.  
  440. client.println("<link rel='apple-touch-icon' href='http://chriscosma.co.cc/on.png' />");
  441. //client.println("Light 1 Is On");
  442. client.println("<br />");
  443. }
  444. else{
  445. if(readString.indexOf("?relay8off") >0)//checks for off
  446. {
  447. digitalWrite(RELAY_CH8, HIGH); // set pin 4 low
  448. //Serial.println("Led Off");
  449. client.println("<link rel='apple-touch-icon' href='http://chriscosma.co.cc/off.png' />");
  450.  
  451. //client.println("Light 1 Is Off");
  452. client.println("<br />");
  453. }
  454. }
  455.  
  456. // INPUT
  457.  
  458.  
  459. // output the value of each analog input pin
  460. //for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
  461. // int sensorReading = analogRead(analogChannel);
  462. // client.print("analog input ");
  463. // client.print(analogChannel);
  464. // client.print(" is ");
  465. // client.print(sensorReading);
  466. // client.println("<br />");
  467. //}
  468.  
  469. client.println("<hr> <p> By <a href=\"http://mrmodder.com\"></p><p style=\"font-family:arial;color:blue;font-size:20px;\">Mrmodder.com</p></a>");
  470.  
  471. readString="";
  472. client.println("</body>");
  473. client.println("</html>");
  474.  
  475. break;
  476. }// if ( c == \n )
  477.  
  478.  
  479. if (c == '\n') {
  480. // you're starting a new line
  481. currentLineIsBlank = true;
  482. }
  483. else if (c != '\r') {
  484. // you've gotten a character on the current line
  485. currentLineIsBlank = false;
  486. }
  487.  
  488. }
  489. }
  490.  
  491. // give the web browser time to receive the data
  492. delay(1);
  493. //client.println("<meta http-equiv=\"refresh\" content=\"10;url=http://192.168.1.200/\"/>" );
  494.  
  495. // close the connection:
  496. client.stop();
  497. Serial.println("client disonnected");
  498.  
  499. }//if(Client)
  500.  
  501. }//Loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement