Advertisement
Guest User

Untitled

a guest
May 8th, 2013
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.20 KB | None | 0 0
  1. #include <SD.h>
  2.  
  3. #include <TimedAction.h>
  4.  
  5. #include <Dhcp.h>
  6. #include <Dns.h>
  7. #include <Ethernet.h>
  8. #include <EthernetClient.h>
  9. #include <EthernetServer.h>
  10. #include <EthernetUdp.h>
  11. #include <util.h>
  12. #include <SPI.h>
  13.  
  14. #include <SoftwareSerial.h>
  15. //Attach the serial display's RX line to digital pin 2
  16. SoftwareSerial LCD(3,5); // pin 2 = TX, pin 3 = RX (unused)
  17.  
  18. TimedAction resetToActive = TimedAction (1000, resetMsg);
  19.  
  20.  
  21. int curMin = 0;
  22. int curHour = 0;
  23. int curSec = 0;
  24.  
  25. TimedAction t_updateSec = TimedAction (998, updateSec);
  26.  
  27. // network configuration. gateway and subnet are optional.
  28.  
  29. // the media access control (ethernet hardware) address for the shield:
  30. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEB };
  31. //the IP address for the shield:
  32. byte ip[] = { 192, 168, 178, 42 };
  33. // the router's gateway address:
  34. byte gateway[] = { 192, 168, 178, 1 };
  35. // the subnet:
  36. byte subnet[] = { 255, 255, 0, 0 };
  37.  
  38. String curLine;
  39. String curGet;
  40. String completeRequest;
  41.  
  42. // telnet defaults to port 80
  43. EthernetServer server = EthernetServer(80);
  44.  
  45. void setup()
  46. {
  47. pinMode (8, OUTPUT);
  48. pinMode (4, OUTPUT);
  49. if(!SD.begin (4))
  50. {
  51. digitalWrite (8, HIGH);
  52. delay (500);
  53. digitalWrite (8, LOW);
  54. delay (500);
  55. digitalWrite (8, HIGH);
  56. delay (500);
  57. digitalWrite (8, LOW);
  58. delay (500);
  59. }
  60. // initialize the ethernet device
  61. Ethernet.begin(mac, ip, gateway, subnet);
  62.  
  63. // start listening for clients
  64. server.begin();
  65.  
  66. LCD.begin(9600); //set up serial port for 9600 baud
  67. delay(600); //wait for LCD to boot up
  68.  
  69. LCD.write (0x7C);
  70. LCD.write (157);
  71. clearLCD();
  72. gotoPos (1);
  73. LCD.write ("ACTIVE");
  74.  
  75. Serial.begin (9600);
  76. }
  77. void loop()
  78. {
  79. resetToActive.check();
  80. t_updateSec.check();
  81. // listen for incoming clients
  82. EthernetClient client = server.available();
  83. if (client) {
  84. resetToActive.disable();
  85. // an http request ends with a blank line
  86. boolean currentLineIsBlank = true;
  87. while (client.connected()) {
  88. if (client.available()) {
  89. char c = client.read();
  90. // if you've gotten to the end of the line (received a newline
  91. // character) and the line is blank, the http request has ended,
  92. // so you can send a reply
  93. // Serial.print (c);
  94. curLine += c;
  95. completeRequest += c;
  96. if (c == '\n' && currentLineIsBlank) {
  97. // Let's parse
  98. // send a standard http response header
  99. // REDIRECTIION
  100. // client.println("HTTP/1.1 301 Moved Permanently");
  101. // client.println("Location: http://jan.apple-iv.net/vt");
  102. // client.stop();
  103. Serial.println (completeRequest.indexOf('Cache'));
  104. if (completeRequest.indexOf ('Cache'))
  105. {
  106. client.println ("HTTP/1.1 200 OK");
  107. client.println("Content-Type: text/html");
  108. client.println("Connection: close");
  109. client.println();
  110. client.println("<!DOCTYPE HTML>");
  111. client.println("<html>");
  112. // add a meta refresh tag, so the browser pulls again every 5 seconds:
  113. //client.println("<meta http-equiv=\"refresh\" content=\"5\">");
  114. // output the value of each analog input pin
  115.  
  116. if (curGet == "/")
  117. {
  118. client.print ("<h1>Index</h1>");
  119. }
  120. else if (curGet.substring (0, 6) == "/lamp/")
  121. {
  122. client.print ("Aktion: lamp -- Param: ");
  123. client.print (curGet.substring (6));
  124. String param = curGet.substring (6);
  125. if (param.charAt (0) == '1')
  126. {
  127. digitalWrite (8, HIGH);
  128. clearLCD();
  129. gotoPos (2);
  130. LCD.write ("ACTION: LAMP");
  131. gotoPos (4, 2);
  132. LCD.write ("PARAM: 1");
  133. }
  134. else
  135. {
  136. digitalWrite (8, LOW);
  137. clearLCD();
  138. gotoPos (2);
  139. LCD.write ("ACTION: LAMP");
  140. gotoPos (4, 2);
  141. LCD.write ("PARAM: 0");
  142. }
  143.  
  144. }
  145. else if (curGet.substring (0, 6) == "/time/")
  146. {
  147. // /time/A/B/C
  148. // --4---67891111
  149. // 0123
  150.  
  151. int h;
  152. int m;
  153. int s;
  154.  
  155. h = (int)curGet.substring (6,7).charAt (0) - 65;
  156. m = (int)curGet.substring (8,9).charAt (0) - 65;
  157. s = (int)curGet.charAt (10) - 65;
  158.  
  159.  
  160. clearAndReset ();
  161. LCD.write ("UPDATE TIME: ");
  162. LCD.print ((int)h);
  163. LCD.write ("-");
  164. LCD.print ((int)m);
  165. LCD.write ("-");
  166. LCD.print ((int)s);
  167.  
  168. curHour = h;
  169. curMin = m;
  170. curSec = s;
  171.  
  172. client.print ((int)curGet.charAt (10) - 65);
  173. client.write (" ");
  174. client.print (curGet.charAt (10));
  175. }
  176. else if (curGet.substring (0, 8) == "/curtime")
  177. {
  178. clearLCD();
  179. gotoPos (2);
  180. LCD.write ("CURERENT TIME:");
  181. gotoPos (4,2);
  182. if (curHour < 10)
  183. LCD.write ("0");
  184. LCD.print (curHour);
  185. LCD.write (":");
  186. if (curMin < 10)
  187. LCD.write ("0");
  188. LCD.print (curMin);
  189. LCD.write (":");
  190. if (curSec < 10)
  191. LCD.write ("0");
  192. LCD.print (curSec);
  193.  
  194. client.print ("OK");
  195. }
  196. else if (curGet.substring (0, 6) == "/test/")
  197. {
  198. File fh = SD.open ("/test.txt");
  199. client.print (fh);
  200. client.print (fh.size());
  201. while (fh.available())
  202. client.print (fh.read());
  203.  
  204. fh.close();
  205. client.print ("WHATSUP");
  206. }
  207. else
  208. client.print ("<h1>Undefined</h1>");
  209. client.print("<br />");
  210. client.println("</html>");
  211.  
  212. curLine = "";
  213. completeRequest = "";
  214. }
  215.  
  216. break;
  217. }
  218. if (c == '\n') {
  219. if (curLine.substring (0, 3) == "GET")
  220. {
  221. curGet = curLine.substring (4, curLine.length() - 11);
  222. }
  223. curLine = "";
  224. // you're starting a new line
  225. currentLineIsBlank = true;
  226. }
  227. else if (c != '\r') {
  228. // you've gotten a character on the current line
  229. currentLineIsBlank = false;
  230. }
  231. }
  232. }
  233. // give the web browser time to receive the data
  234. delay(1);
  235. // close the connection:
  236. client.stop();
  237. gotoPos (1, 2);
  238.  
  239. curLine = "";
  240. completeRequest = "";
  241. resetToActive.reset();
  242. resetToActive.enable();
  243. }
  244. }
  245.  
  246. void clearLCD ()
  247. {
  248. LCD.write (254);
  249. LCD.write (0x01);
  250. delay (10);
  251. }
  252.  
  253. void resetPos ()
  254. {
  255. LCD.write (254);
  256. LCD.write (128);
  257. }
  258.  
  259. void clearAndReset ()
  260. {
  261. clearLCD();
  262. resetPos();
  263. }
  264.  
  265. void gotoPos (int num)
  266. {
  267. gotoPos (num, 1);
  268. }
  269.  
  270. void gotoPos (int num, int line)
  271. {
  272. int base = 127;
  273. if (line == 2) base = 191;
  274.  
  275. if (num < 1) num = 1;
  276. if (num > 16) num = 16;
  277.  
  278. LCD.write (254);
  279. LCD.write (base + num);
  280. delay (10); // fix wrong chars
  281. }
  282.  
  283. void resetMsg ()
  284. {
  285. clearLCD();
  286. gotoPos (2);
  287. LCD.write ("LISTENING (80)");
  288. gotoPos (2, 2);
  289. LCD.write ("192.168.178.42");
  290. resetToActive.disable();
  291. }
  292.  
  293. void updateSec ()
  294. {
  295. curSec ++;
  296.  
  297. if (curSec == 60)
  298. {
  299. curMin ++;
  300. curSec = 0;
  301. if (curMin == 60)
  302. {
  303. curHour ++;
  304. curMin = 0;
  305. if (curHour == 24)
  306. curHour = 0;
  307. }
  308. }
  309.  
  310. if (curHour == 5 && curMin == 16)
  311. {
  312. digitalWrite (8, HIGH);
  313. }
  314. /*
  315. clearAndReset();
  316.  
  317. LCD.print (curHour);
  318. LCD.write (":");
  319. LCD.print (curMin);
  320. LCD.write (":");
  321. LCD.print (curSec);
  322. */
  323. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement