Guest User

Untitled

a guest
Sep 26th, 2012
111
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <DHCPLite.h>
  2. #include <RedFly.h>
  3. #include <EEPROM.h>
  4. #include <OneWire.h>
  5. #include <DallasTemperature.h>
  6.  
  7. byte serverIP[] = { 192, 168, 1, 150 };
  8. byte netmask[] = { 255,255,255, 0 };
  9. byte gateway[] = { 192, 168, 1, 1 }; // ip from gateway/router (not needed)
  10. byte broadcast[] = { 255,255,255, 255 };
  11. byte *currentIP = serverIP;
  12. char domainName[] = "mshome.net";
  13. char serverName[] = "arduino\x06mshome\x03net";
  14.  
  15. uint8_t hDHCP, hDNSTCP, hDNSUDP, hHTTPUDP, hHTTPTCP = 0xFF; // socket handles; 0xFF means closed/not used; only needed here for HTTP
  16.  
  17. #define LEDPIN 12
  18. #define HEATPIN 13 // LED simulating heating element
  19. #define DRUMPIN 14 // LED simulating motor turning drum
  20. #define REVERSEPIN 15 // LED simulating indicating drum turning reverse (used to distribute clothes)
  21.  
  22. #define ONE_WIRE_BUS 16 // Data wire from dallas chips
  23. #define humidpin 17 // humidity sensor
  24. #define waterpin 18 // watertank full
  25. #define currentpin 19 // analog voltage input from current clamp
  26. #define airflowpin 20 // analog voltage from airflow sensor
  27.  
  28. #define HTTP_SERVER_PORT 80 //What port should the webpage be served on? default is 80 but could be anything up to 65535. You might want to check this link for ports allready in use, but in most cases you can pick any number as long the service is not in use on you network: https://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.txt
  29. #define SERVER_PORT 4444
  30.  
  31. #define CONFIG_VERSION "ar1"
  32. #define CONFIG_START 0
  33.  
  34. // NetworkConnectTo
  35. struct WiFiStorageStruct {
  36. char version[4];
  37. char ssid[24];
  38. char pwd[16];
  39. byte addr[4];
  40. unsigned int id;
  41. } WiFiConfig = {
  42. CONFIG_VERSION,
  43. "sandkaeret1",
  44. "6161616161",
  45. {192, 168, 1, 150},
  46. 0
  47. };
  48.  
  49. void loadConfig() {
  50. if (EEPROM.read(CONFIG_START + 0) == CONFIG_VERSION[0] &&
  51. EEPROM.read(CONFIG_START + 1) == CONFIG_VERSION[1] &&
  52. EEPROM.read(CONFIG_START + 2) == CONFIG_VERSION[2])
  53. for (unsigned int t=0; t<sizeof(WiFiConfig); t++)
  54. *((char*)&WiFiConfig + t) = EEPROM.read(CONFIG_START + t);
  55. }
  56.  
  57. void saveConfig() {
  58. for (unsigned int t=0; t<sizeof(WiFiConfig); t++)
  59. EEPROM.write(CONFIG_START + t, *((char*)&WiFiConfig + t));
  60. }
  61.  
  62. void blink(int pin, int n) {
  63. for (int i = 0; i < n; i++) {
  64. digitalWrite(pin, HIGH); delay(200);
  65. digitalWrite(pin, LOW); delay(200);
  66. }
  67. }
  68.  
  69. byte adhoc = 0; // 1 - adhoc connection or 0 - connected to AP
  70.  
  71.  
  72. OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
  73.  
  74. DallasTemperature sensors(&oneWire); // Pass our oneWire reference to Dallas Temperature.
  75.  
  76. DeviceAddress RoomThermometer = { 0x28, 0xA0, 0x34, 0xC3, 0x03, 0x00, 0x00, 0x8C }; // Array to hold address for Room thermometer
  77. DeviceAddress DrumThermometer = { 0x28, 0xF3, 0x19, 0x33, 0x03, 0x00, 0x00, 0x9E }; // Array to hold address for Drum thermometer
  78. float tempC=0;
  79. int temperature1;
  80. int temperature2;
  81.  
  82. void setup() {
  83. uint8_t ret;
  84.  
  85. loadConfig();
  86.  
  87. blink(LEDPIN, 1);
  88.  
  89. //init the WiFi module on the shield
  90. ret = RedFly.init(115200, HIGH_POWER); //LOW_POWER MED_POWER HIGH_POWER
  91. if (!ret) {
  92. RedFly.scan();
  93. adhoc = ret = RedFly.join(WiFiConfig.ssid, WiFiConfig.pwd, INFRASTRUCTURE);
  94. if (ret) {
  95. char network[16];
  96. sprintf(network, "TestNetwork%d", WiFiConfig.id);
  97. ret = RedFly.join(network, IBSS_CREATOR, 10);
  98. }
  99. if (!ret) {
  100. currentIP = adhoc ? serverIP : WiFiConfig.addr;
  101. ret = RedFly.begin(currentIP, gateway, netmask);
  102. }
  103. }
  104.  
  105. if (adhoc) { // only open DHCP/DNS ports in adhoc config
  106. // listen for DHCP messages on DHCP_SERVER_PORT (UDP)
  107. hDHCP = RedFly.socketListen(PROTO_UDP, DHCP_SERVER_PORT);
  108. // listen for DNS messages on DNS_SERVER_PORT (both UDP and TCP on the same port)
  109. hDNSTCP = RedFly.socketListen(PROTO_TCP, DNS_SERVER_PORT);
  110. hDNSUDP = RedFly.socketListen(PROTO_UDP, DNS_SERVER_PORT);
  111. }
  112.  
  113. // listen for UDP messages on port 80 (to test echo)
  114. hHTTPUDP = RedFly.socketListen(PROTO_UDP, HTTP_SERVER_PORT);
  115.  
  116. // 10 blinks on error, 5 blinks on AP connection and 3 blinks on adhoc connection
  117. blink(LEDPIN, ret ? 10 : (adhoc ? 3 : 5));
  118.  
  119. pinMode(12, OUTPUT);
  120. pinMode(13, OUTPUT); // heating element
  121. pinMode(14, OUTPUT); // drum motor
  122. pinMode(15, OUTPUT); // drum reverse
  123.  
  124. pinMode(16, INPUT); // DS18B20 one wire bus
  125. pinMode(17, INPUT); // humidity sensor
  126. pinMode(18, INPUT); // watertank full
  127. pinMode(19, INPUT); // current clamp
  128. pinMode(20, INPUT); // airflow
  129.  
  130.  
  131. sensors.setResolution(RoomThermometer, 12); // set the resolution to 12 bit
  132. sensors.setResolution(DrumThermometer, 12);
  133. }
  134.  
  135.  
  136. void loop()
  137. {
  138. uint8_t sock, *ptr, buf[DHCP_MESSAGE_SIZE];
  139. uint16_t buf_len, rd, len;
  140. uint16_t port; //incoming UDP port
  141. uint8_t ip[4]; //incoming UDP ip
  142.  
  143. //check if socket is closed and start listening
  144. if (hHTTPTCP == 0xFF || RedFly.socketClosed(hHTTPTCP)) hHTTPTCP = RedFly.socketListen(PROTO_TCP, HTTP_SERVER_PORT); // start listening on port 80
  145.  
  146. // get data
  147. sock = 0xFF; // 0xFF = return data from all open sockets
  148. ptr = buf;
  149. buf_len = 0;
  150. do {
  151. rd = RedFly.socketRead(&sock, &len, ip, &port, ptr, sizeof(buf)-buf_len);
  152. if((rd != 0) && (rd != 0xFFFF)) { // 0xFFFF = connection closed
  153. ptr += rd;
  154. buf_len += rd;
  155. }
  156. } while(len != 0);
  157.  
  158. // process and send back data
  159. if (buf_len && (sock != 0xFF)) {
  160. if (sock == hDHCP) {
  161. buf_len = DHCPreply((RIP_MSG*)buf, buf_len, currentIP, domainName); // zero returned means the message was not recognized
  162. if (buf_len) RedFly.socketSend(sock, buf, buf_len, broadcast, port);
  163. }
  164. else if (sock == hDNSTCP || sock == hDNSUDP) {
  165. buf_len = DNSreply((DNS_MSG*)buf, buf_len, currentIP, serverName); // zero returned means the message was not recognized
  166. if (sock == hDNSTCP) {
  167. if (buf_len) RedFly.socketSend(sock, buf, buf_len);
  168. }
  169. else {
  170. if (buf_len) RedFly.socketSend(sock, buf, buf_len, ip, port); // send back to the same ip/port the message came from
  171. }
  172. }
  173. else if (sock == hHTTPUDP) {
  174. sprintf((char*)buf+buf_len, " %d", millis());
  175. RedFly.socketSend(hHTTPUDP, buf, strlen((char*)buf), ip, port);
  176. }
  177. else if (sock == hHTTPTCP) {
  178. const char *OK = PSTR("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n");
  179. const char *ContentLengthHeader = PSTR("Content-Length: %d\r\n\r\n");
  180. const char *STYLE = PSTR("\r\n<html style='font-family:Verdana,Geneva,Georgia,Chicago,Arial,Sans-serif;color:#002d80'>");
  181. int pin, value;
  182. char mode, ignore;
  183. char *out = (char *)buf;
  184.  
  185.  
  186. if (strncmp_P(out, PSTR("GET / HTTP"), 10) == 0) {
  187. RedFly.socketSendPGM(sock, OK);
  188. RedFly.socketSendPGM(sock, STYLE);
  189. RedFly.socketSendPGM(sock, PSTR("<h1><center>T&oslash;rretumbler</center><br/></font></h1>"));
  190. RedFly.socketSendPGM(sock, PSTR("<h6><center><a href='http://192.168.1.151/'>Vaskemaskine</a> -- <a href='http://192.168.1.152'>Opvasker</a></center></h6><br/></font><br/>"));
  191. RedFly.socketSendPGM(sock, PSTR("<center><HR WIDTH='25%' SIZE='3' NOSHADE></center><br/></font>"));
  192. RedFly.socketSendPGM(sock, PSTR("<center><table border='1'><tr><th>Bomuld</th><th>Syntetisk</th></tr><tr><td><center><a href='/cycle1'>Skabst&oslash;rt Plus</a></td><td><center><a href='/cycle2'>Skabst&oslash;rt Plus</a></td></tr><tr><td><center><a href='/cycle3'>Skabst&oslash;rt</a></td><td><center><a href='/cycle4'>Skabst&oslash;rt</a></td></tr><tr><td><center><a href='/cycle5'>Stryget&oslash;rt</a></td><td><center><a href='/cycle6'>Stryget&oslash;rt</a></td></tr></table></center><br>"));
  193. RedFly.socketSendPGM(sock, PSTR("<center><HR WIDTH='25%' SIZE='3' NOSHADE></center><br/></font>"));
  194. RedFly.socketSendPGM(sock, PSTR("<center><table border='1'><tr><th>Tid</th></tr><tr><td><center><a href='/cycle7'>20 Min</center></td></tr><tr><td><center><a href='/cycle8'>40 Min</center></td></tr><tr><td><center><a href='/cycle9'>60 Min</center></td></tr></table></center><br>"));
  195. RedFly.socketSendPGM(sock, PSTR("<center><HR WIDTH='25%' SIZE='3' NOSHADE></center><br/></font>"));
  196. RedFly.socketSendPGM(sock, PSTR("<center><table border='1'><tr><th>Diverse</th></tr><tr><td><a href='/cycle0'>Kold Luft (Stopper ikke Automatisk)</td></tr><tr><td><a href='/wifi'>Wi-fi ops&aelig;tning</a></td></tr><tr><td><a href='/Val'>Prog. Indst.</a></td></tr></table></center><br>"));
  197. RedFly.socketClose(sock);
  198. }
  199. else if (strncmp_P(out, PSTR("GET /cycle1 HTTP"), 11) == 0) {
  200.  
  201.  
  202.  
  203.  
  204. RedFly.socketSendPGM(sock, OK);
  205. RedFly.socketSendPGM(sock, STYLE);
  206. RedFly.socketSendPGM(sock, PSTR("<h1><center>Bomuld - Skabst&oslash;rt Plus</center><br/></font></h1>"));
  207. RedFly.socketSendPGM(sock, PSTR("<center><HR WIDTH='25%' SIZE='3' NOSHADE></center><br/></font>"));
  208. RedFly.socketSendPGM(sock, PSTR("<center><input type='submit' style='height: 25px; width: 100px' value='Start'/></center>"));
  209. RedFly.socketSendPGM(sock, PSTR("</br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br>"));
  210. RedFly.socketSendPGM(sock, PSTR("<center><input type='submit' style='height: 25px; width: 100px' value='Stop'/></center>"));
  211. RedFly.socketSendPGM(sock, PSTR("</br>"));
  212. RedFly.socketSendPGM(sock, PSTR("<center><HR WIDTH='25%' SIZE='3' NOSHADE></center><br/></font>"));
  213. RedFly.socketSendPGM(sock, PSTR("<center><a href='/'>Hovedmenu</a></center>"));
  214. RedFly.socketClose(sock);
  215.  
  216.  
  217. }
  218.  
  219.  
  220. else if (strncmp_P(out, PSTR("GET /cycle2 HTTP"), 11) == 0) {
  221.  
  222.  
  223.  
  224.  
  225. RedFly.socketSendPGM(sock, OK);
  226. RedFly.socketSendPGM(sock, STYLE);
  227. RedFly.socketSendPGM(sock, PSTR("<h1><center>Syntetisk - Skabst&oslash;rt Plus</center><br/></font></h1>"));
  228. RedFly.socketSendPGM(sock, PSTR("<center><HR WIDTH='25%' SIZE='3' NOSHADE></center><br/></font>"));
  229. RedFly.socketSendPGM(sock, PSTR("<center><input type='submit' style='height: 25px; width: 100px' value='Start'/></center>"));
  230. RedFly.socketSendPGM(sock, PSTR("</br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br>"));
  231. RedFly.socketSendPGM(sock, PSTR("<center><input type='submit' style='height: 25px; width: 100px' value='Stop'/></center>"));
  232. RedFly.socketSendPGM(sock, PSTR("</br>"));
  233. RedFly.socketSendPGM(sock, PSTR("<center><HR WIDTH='25%' SIZE='3' NOSHADE></center><br/></font>"));
  234. RedFly.socketSendPGM(sock, PSTR("<center><a href='/'>Hovedmenu</a></center>"));
  235. RedFly.socketClose(sock);
  236.  
  237.  
  238. }
  239.  
  240.  
  241. else if (strncmp_P(out, PSTR("GET /cycle3 HTTP"), 11) == 0) {
  242.  
  243.  
  244.  
  245.  
  246. RedFly.socketSendPGM(sock, OK);
  247. RedFly.socketSendPGM(sock, STYLE);
  248. RedFly.socketSendPGM(sock, PSTR("<h1><center>Bomuld - Skabst&oslash;rt</center><br/></font></h1>"));
  249. RedFly.socketSendPGM(sock, PSTR("<center><HR WIDTH='25%' SIZE='3' NOSHADE></center><br/></font>"));
  250. RedFly.socketSendPGM(sock, PSTR("<center><input type='submit' style='height: 25px; width: 100px' value='Start'/></center>"));
  251. RedFly.socketSendPGM(sock, PSTR("</br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br>"));
  252. RedFly.socketSendPGM(sock, PSTR("<center><input type='submit' style='height: 25px; width: 100px' value='Stop'/></center>"));
  253. RedFly.socketSendPGM(sock, PSTR("</br>"));
  254. RedFly.socketSendPGM(sock, PSTR("<center><HR WIDTH='25%' SIZE='3' NOSHADE></center><br/></font>"));
  255. RedFly.socketSendPGM(sock, PSTR("<center><a href='/'>Hovedmenu</a></center>"));
  256. RedFly.socketClose(sock);
  257.  
  258.  
  259. }
  260.  
  261.  
  262. else if (strncmp_P(out, PSTR("GET /cycle4 HTTP"), 11) == 0) {
  263.  
  264.  
  265.  
  266.  
  267. RedFly.socketSendPGM(sock, OK);
  268. RedFly.socketSendPGM(sock, STYLE);
  269. RedFly.socketSendPGM(sock, PSTR("<h1><center>Syntetisk - Skabst&oslash;rt</center><br/></font></h1>"));
  270. RedFly.socketSendPGM(sock, PSTR("<center><HR WIDTH='25%' SIZE='3' NOSHADE></center><br/></font>"));
  271. RedFly.socketSendPGM(sock, PSTR("<center><input type='submit' style='height: 25px; width: 100px' value='Start'/></center>"));
  272. RedFly.socketSendPGM(sock, PSTR("</br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br>"));
  273. RedFly.socketSendPGM(sock, PSTR("<center><input type='submit' style='height: 25px; width: 100px' value='Stop'/></center>"));
  274. RedFly.socketSendPGM(sock, PSTR("</br>"));
  275. RedFly.socketSendPGM(sock, PSTR("<center><HR WIDTH='25%' SIZE='3' NOSHADE></center><br/></font>"));
  276. RedFly.socketSendPGM(sock, PSTR("<center><a href='/'>Hovedmenu</a></center>"));
  277. RedFly.socketClose(sock);
  278.  
  279.  
  280. }
  281.  
  282.  
  283.  
  284. else if (strncmp_P(out, PSTR("GET /cycle5 HTTP"), 11) == 0) {
  285.  
  286.  
  287.  
  288.  
  289. RedFly.socketSendPGM(sock, OK);
  290. RedFly.socketSendPGM(sock, STYLE);
  291. RedFly.socketSendPGM(sock, PSTR("<h1><center>Bomuld - Stryget&oslash;rt</center><br/></font></h1>"));
  292. RedFly.socketSendPGM(sock, PSTR("<center><HR WIDTH='25%' SIZE='3' NOSHADE></center><br/></font>"));
  293. RedFly.socketSendPGM(sock, PSTR("<center><input type='submit' style='height: 25px; width: 100px' value='Start'/></center>"));
  294. RedFly.socketSendPGM(sock, PSTR("</br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br>"));
  295. RedFly.socketSendPGM(sock, PSTR("<center><input type='submit' style='height: 25px; width: 100px' value='Stop'/></center>"));
  296. RedFly.socketSendPGM(sock, PSTR("</br>"));
  297. RedFly.socketSendPGM(sock, PSTR("<center><HR WIDTH='25%' SIZE='3' NOSHADE></center><br/></font>"));
  298. RedFly.socketSendPGM(sock, PSTR("<center><a href='/'>Hovedmenu</a></center>"));
  299. RedFly.socketClose(sock);
  300.  
  301.  
  302. }
  303.  
  304.  
  305.  
  306. else if (strncmp_P(out, PSTR("GET /cycle6 HTTP"), 11) == 0) {
  307.  
  308.  
  309.  
  310.  
  311. RedFly.socketSendPGM(sock, OK);
  312. RedFly.socketSendPGM(sock, STYLE);
  313. RedFly.socketSendPGM(sock, PSTR("<h1><center>Syntetisk - Stryget&oslash;rt</center><br/></font></h1>"));
  314. RedFly.socketSendPGM(sock, PSTR("<center><HR WIDTH='25%' SIZE='3' NOSHADE></center><br/></font>"));
  315. RedFly.socketSendPGM(sock, PSTR("<center><input type='submit' style='height: 25px; width: 100px' value='Start'/></center>"));
  316. RedFly.socketSendPGM(sock, PSTR("</br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br>"));
  317. RedFly.socketSendPGM(sock, PSTR("<center><input type='submit' style='height: 25px; width: 100px' value='Stop'/></center>"));
  318. RedFly.socketSendPGM(sock, PSTR("</br>"));
  319. RedFly.socketSendPGM(sock, PSTR("<center><HR WIDTH='25%' SIZE='3' NOSHADE></center><br/></font>"));
  320. RedFly.socketSendPGM(sock, PSTR("<center><a href='/'>Hovedmenu</a></center>"));
  321. RedFly.socketClose(sock);
  322.  
  323.  
  324. }
  325.  
  326. else if (strncmp_P(out, PSTR("GET /cycle7 HTTP"), 11) == 0) {
  327.  
  328.  
  329.  
  330.  
  331. RedFly.socketSendPGM(sock, OK);
  332. RedFly.socketSendPGM(sock, STYLE);
  333. RedFly.socketSendPGM(sock, PSTR("<h1><center>20 Min</center><br/></font></h1>"));
  334. RedFly.socketSendPGM(sock, PSTR("<center><HR WIDTH='25%' SIZE='3' NOSHADE></center><br/></font>"));
  335. RedFly.socketSendPGM(sock, PSTR("<center><input type='submit' style='height: 25px; width: 100px' value='Start'/></center>"));
  336. RedFly.socketSendPGM(sock, PSTR("</br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br>"));
  337. RedFly.socketSendPGM(sock, PSTR("<center><input type='submit' style='height: 25px; width: 100px' value='Stop'/></center>"));
  338. RedFly.socketSendPGM(sock, PSTR("</br>"));
  339. RedFly.socketSendPGM(sock, PSTR("<center><HR WIDTH='25%' SIZE='3' NOSHADE></center><br/></font>"));
  340. RedFly.socketSendPGM(sock, PSTR("<center><a href='/'>Hovedmenu</a></center>"));
  341. RedFly.socketClose(sock);
  342.  
  343.  
  344. }
  345.  
  346.  
  347.  
  348. else if (strncmp_P(out, PSTR("GET /cycle8 HTTP"), 11) == 0) {
  349.  
  350.  
  351.  
  352.  
  353. RedFly.socketSendPGM(sock, OK);
  354. RedFly.socketSendPGM(sock, STYLE);
  355. RedFly.socketSendPGM(sock, PSTR("<h1><center>40 Min</center><br/></font></h1>"));
  356. RedFly.socketSendPGM(sock, PSTR("<center><HR WIDTH='25%' SIZE='3' NOSHADE></center><br/></font>"));
  357. RedFly.socketSendPGM(sock, PSTR("<center><input type='submit' style='height: 25px; width: 100px' value='Start'/></center>"));
  358. RedFly.socketSendPGM(sock, PSTR("</br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br>"));
  359. RedFly.socketSendPGM(sock, PSTR("<center><input type='submit' style='height: 25px; width: 100px' value='Stop'/></center>"));
  360. RedFly.socketSendPGM(sock, PSTR("</br>"));
  361. RedFly.socketSendPGM(sock, PSTR("<center><HR WIDTH='25%' SIZE='3' NOSHADE></center><br/></font>"));
  362. RedFly.socketSendPGM(sock, PSTR("<center><a href='/'>Hovedmenu</a></center>"));
  363. RedFly.socketClose(sock);
  364.  
  365.  
  366. }
  367.  
  368.  
  369. else if (strncmp_P(out, PSTR("GET /cycle9 HTTP"), 11) == 0) {
  370.  
  371.  
  372.  
  373.  
  374. RedFly.socketSendPGM(sock, OK);
  375. RedFly.socketSendPGM(sock, STYLE);
  376. RedFly.socketSendPGM(sock, PSTR("<h1><center>60 Min</center><br/></font></h1>"));
  377. RedFly.socketSendPGM(sock, PSTR("<center><HR WIDTH='25%' SIZE='3' NOSHADE></center><br/></font>"));
  378. RedFly.socketSendPGM(sock, PSTR("<center><input type='submit' style='height: 25px; width: 100px' value='Start'/></center>"));
  379. RedFly.socketSendPGM(sock, PSTR("</br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br>"));
  380. RedFly.socketSendPGM(sock, PSTR("<center><input type='submit' style='height: 25px; width: 100px' value='Stop'/></center>"));
  381. RedFly.socketSendPGM(sock, PSTR("</br>"));
  382. RedFly.socketSendPGM(sock, PSTR("<center><HR WIDTH='25%' SIZE='3' NOSHADE></center><br/></font>"));
  383. RedFly.socketSendPGM(sock, PSTR("<center><a href='/'>Hovedmenu</a></center>"));
  384. RedFly.socketClose(sock);
  385.  
  386.  
  387. }
  388.  
  389.  
  390. else if (strncmp_P(out, PSTR("GET /cycle0 HTTP"), 12) == 0) {
  391.  
  392.  
  393.  
  394.  
  395. RedFly.socketSendPGM(sock, OK);
  396. RedFly.socketSendPGM(sock, STYLE);
  397. RedFly.socketSendPGM(sock, PSTR("<h1><center>Kold Luft</center><br/></font></h1>"));
  398. RedFly.socketSendPGM(sock, PSTR("<center><HR WIDTH='25%' SIZE='3' NOSHADE></center><br/></font>"));
  399. RedFly.socketSendPGM(sock, PSTR("<center><input type='submit' style='height: 25px; width: 100px' value='Start'/></center>"));
  400. RedFly.socketSendPGM(sock, PSTR("</br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br>"));
  401. RedFly.socketSendPGM(sock, PSTR("<center><input type='submit' style='height: 25px; width: 100px' value='Stop'/></center>"));
  402. RedFly.socketSendPGM(sock, PSTR("</br>"));
  403. RedFly.socketSendPGM(sock, PSTR("<center><HR WIDTH='25%' SIZE='3' NOSHADE></center><br/></font>"));
  404. RedFly.socketSendPGM(sock, PSTR("<center><a href='/'>Hovedmenu</a></center>"));
  405. RedFly.socketClose(sock);
  406.  
  407.  
  408. }
  409.  
  410. else if (strncmp_P(out, PSTR("GET /val HTTP"), 1) == 0) {
  411. RedFly.socketSendPGM(sock, OK);
  412. RedFly.socketSendPGM(sock, STYLE);
  413. RedFly.socketSendPGM(sock, PSTR("<h1><center><form method='post'>Program Indstillinger</font></h1>"));
  414. RedFly.socketSendPGM(sock, PSTR("<center><HR WIDTH='25%' SIZE='3' NOSHADE></center><br/></font>"));
  415. RedFly.socketSendPGM(sock, PSTR("<center><table border='1'>"));
  416. RedFly.socketSendPGM(sock, PSTR("<tr><td><left>Bomuld Skabst&oslash;rt Plus Temp:</td><td><left><input style='' value='' name='c1temp'/></td></tr><tr><td><left>Bomuld Skabst&oslash;rt Plus Fugt:</td><td><input style='' value='' name='c1humid'/></td></tr>"));
  417. RedFly.socketSendPGM(sock, PSTR("<tr><td><left>Syntetisk Skabst&oslash;rt Plus Temp:</td><td><left><input style='' value='' name='c2temp'/></td></tr><tr><td><left>Syntetisk Skabst&oslash;rt Fugt:</td><td><left><input style='' value='' name='c2humid'/></td></tr>"));
  418. RedFly.socketSendPGM(sock, PSTR("<tr><td><left>Bomuld Skabst&oslash;rt Temp:</td><td><left><input style='' value='' name='c4temp'/></td></tr><tr><td><left>Bomuld Skabst&oslash;rt Fugt:</td><td><left><input style='' value='' name='c3humid'/></td></tr>"));
  419. RedFly.socketSendPGM(sock, PSTR("<tr><td><left>Syntetisk Skabst&oslash;rt Temp:</td><td><left><input style='' value='' name='c4temp'/></td></tr><tr><td><left>Syntetisk Skabst&oslash;rt Fugt:</td><td><left><input style='' value='' name='c4humid'/></td></tr>"));
  420. RedFly.socketSendPGM(sock, PSTR("<tr><td><left>Bomuld Stryget&oslash;rt Temp:</td><td><left><input style='' value='' name='c5temp'/></td></tr><tr><td><left>Bomuld Stryget&oslash;rt Fugt:</td><td><left><input style='' value='' name='c5humid'/></td></tr>"));
  421. RedFly.socketSendPGM(sock, PSTR("<tr><td><left>Syntetisk Stryget&oslash;rt Temp:</td><td><left><input style='' value='' name='c6temp'/></td></tr><tr><td><left>Syntetisk Stryget&oslash;rt Fugt:</td><td><left><input style='' value='' name='c6humid'/></td></tr>"));
  422. RedFly.socketSendPGM(sock, PSTR("</table></center><br>"));
  423. RedFly.socketSendPGM(sock, PSTR("<center><HR WIDTH='25%' SIZE='3' NOSHADE></center><br/></font>"));
  424. RedFly.socketSendPGM(sock, PSTR("<center><table border='1'>"));
  425. RedFly.socketSendPGM(sock, PSTR("<tr><td><left>20 Min Temp:</td><td><left><input style='' value='' name='c7temp'/></td></tr>"));
  426. RedFly.socketSendPGM(sock, PSTR("<tr><td><left>40 Min Temp:</td><td><left><input style='' value='' name='c8temp'/></td></tr>"));
  427. RedFly.socketSendPGM(sock, PSTR("<tr><td><left>60 Min Temp:</td><td><left><input style='' value='' name='c9temp'/></td></tr>"));
  428. RedFly.socketSendPGM(sock, PSTR("</table></center><br>"));
  429. RedFly.socketSendPGM(sock, PSTR("<center><HR WIDTH='25%' SIZE='3' NOSHADE></center></font>"));
  430. RedFly.socketSendPGM(sock, PSTR("<br><center>Klik p&aring; <input type='submit' value='Opdater'/> vil opdatere indstillingerne</form></html>"));
  431. RedFly.socketSendPGM(sock, PSTR("<br><br><a href='/'>Hovedmenu</a>"));
  432. RedFly.socketClose(sock);
  433. }
  434. else if (strncmp_P(out, PSTR("POST /val HTTP"), 2) == 0) {
  435. char *st, *fi;
  436. char *a = NULL, *p = NULL, *s = NULL;
  437. out[buf_len] = '\0';
  438. if (st = strstr(out, "a="))
  439. if (sscanf(st+2, "%d.%d.%d.%d", WiFiConfig.addr, WiFiConfig.addr+1, WiFiConfig.addr+2, WiFiConfig.addr+3) == 4) a = st+2;
  440. if (st = strstr(out, "p="))
  441. if (fi = strstr(st, "&")) { fi[0] = '\0'; p = st+2; }
  442. if (st = strstr(out, "s="))
  443. if (fi = strstr(st, "&")) { fi[0] = '\0'; s = st+2; }
  444.  
  445. RedFly.socketSendPGM(sock, OK);
  446. RedFly.socketSendPGM(sock, STYLE);
  447. if (a && p && s) {
  448. strcpy(WiFiConfig.pwd, p);
  449. strcpy(WiFiConfig.ssid, s);
  450. saveConfig();
  451. RedFly.socketSendPGM(sock, PSTR("Updated.</html>"));
  452. RedFly.socketClose(sock);
  453.  
  454. }
  455. else {
  456. RedFly.socketSendPGM(sock, PSTR("Error. Please return back and update the configuration.</html>"));
  457. RedFly.socketClose(sock);
  458. }
  459. }
  460.  
  461.  
  462.  
  463.  
  464.  
  465. else if (strncmp_P(out, PSTR("GET /wifi HTTP"), 14) == 0) {
  466. RedFly.socketSendPGM(sock, OK);
  467. RedFly.socketSendPGM(sock, STYLE);
  468. RedFly.socketSendPGM(sock, PSTR("<form method='post'>Wifi configuration<p>SSID: <input style='margin-left:48px' value='"));
  469. RedFly.socketSend(sock, (uint8_t*)WiFiConfig.ssid, strlen(WiFiConfig.ssid));
  470. RedFly.socketSendPGM(sock, PSTR("' name='s'/><br/>Password: <input type='password' name='p' style='margin-left:10px'/><br/>IP address: <input name='a' value='"));
  471. sprintf(out, "%d.%d.%d.%d", WiFiConfig.addr[0], WiFiConfig.addr[1], WiFiConfig.addr[2], WiFiConfig.addr[3]);
  472. RedFly.socketSend(sock, (uint8_t*)out, strlen(out));
  473. RedFly.socketSendPGM(sock, PSTR("'/> (xxx.xxx.xxx.xxx)<br/><br/>Clicking <input type='submit' value='Update'/> will update the configuration and restart the board.</p></form></html>"));
  474. RedFly.socketSendPGM(sock, PSTR("</br><a href='/'>Back</a>"));
  475. RedFly.socketClose(sock);
  476. }
  477. else if (strncmp_P(out, PSTR("POST /wifi HTTP"), 15) == 0) {
  478. char *st, *fi;
  479. char *a = NULL, *p = NULL, *s = NULL;
  480. out[buf_len] = '\0';
  481. if (st = strstr(out, "a="))
  482. if (sscanf(st+2, "%d.%d.%d.%d", WiFiConfig.addr, WiFiConfig.addr+1, WiFiConfig.addr+2, WiFiConfig.addr+3) == 4) a = st+2;
  483. if (st = strstr(out, "p="))
  484. if (fi = strstr(st, "&")) { fi[0] = '\0'; p = st+2; }
  485. if (st = strstr(out, "s="))
  486. if (fi = strstr(st, "&")) { fi[0] = '\0'; s = st+2; }
  487.  
  488. RedFly.socketSendPGM(sock, OK);
  489. RedFly.socketSendPGM(sock, STYLE);
  490. if (a && p && s) {
  491. strcpy(WiFiConfig.pwd, p);
  492. strcpy(WiFiConfig.ssid, s);
  493. saveConfig();
  494. RedFly.socketSendPGM(sock, PSTR("Updated. The board has been restarted with the new configuration.</html>"));
  495. RedFly.socketClose(sock);
  496.  
  497. // restart Arduino by calling a (pseudo)function at address 0
  498. void(* reset) (void) = 0; // declare reset function @ address 0
  499. reset();
  500. }
  501. else {
  502. RedFly.socketSendPGM(sock, PSTR("Error. Please return back and update the configuration.</html>"));
  503. RedFly.socketClose(sock);
  504. }
  505. }
  506. else if (sscanf_P(out, PSTR("GET /D%2d=%1d HTTP"), &pin, &value) == 2) {
  507. pinMode(pin, OUTPUT);
  508. digitalWrite(pin, value ? HIGH : LOW);
  509.  
  510. const char * SetPinMessage = PSTR("Set digital pin %d to %d; <a href='/D%d=%d'>toggle</a>");
  511. sprintf_P(out, SetPinMessage, pin, !!value, pin, !value);
  512.  
  513. RedFly.socketSendPGM(sock, OK);
  514. RedFly.socketSendPGM(sock, STYLE);
  515. RedFly.socketSend(sock, out);
  516. RedFly.socketSendPGM(sock, PSTR("</br><a href='/'>Back</a>"));
  517. RedFly.socketClose(sock);
  518. }
  519. else if (sscanf_P(out, PSTR("GET /%1[AD]%2d= %1[H]"), &mode, &pin, &ignore) == 3) {
  520.  
  521. // get the value
  522. pinMode(pin, INPUT);
  523. int value = (mode == 'A' ? analogRead(pin) : digitalRead(pin));
  524.  
  525. // calculate content length
  526. itoa(value, out, 10);
  527. int contentLength = strlen(out);
  528.  
  529. sprintf_P(out, OK);
  530. sprintf_P(out+strlen(out), ContentLengthHeader, contentLength);
  531. itoa(value, out + strlen(out), 10);
  532.  
  533. RedFly.socketSend(sock, out); // push the actual content out
  534.  
  535. }
  536. else {
  537. RedFly.socketSendPGM(sock, OK);
  538. RedFly.socketSendPGM(sock, PSTR("\r\n<html><head><title>Arduino</title><script type='text/javascript'>var SIDE=200;var DELAY=100;var request=new XMLHttpRequest();function getUrl(a,b){request.onreadystatechange=function(){if(request.readyState==4){b(request.responseText);request.onreadystatechange=function(){}}};request.open('GET',a,true);request.send(null)}function doit(){var d=document.getElementById('info');var b=document.getElementById('out');b.width=b.height=SIDE;var a=b.getContext('2d');function c(e){a.clearRect(0,0,SIDE,SIDE);a.beginPath();a.arc(SIDE/2,SIDE/2,e/10,0,Math.PI*2,true);a.fillStyle='#002D80';a.fill();d.innerHTML=e;getMore=function(){getUrl(window.location.pathname+'=',c)};setTimeout('getMore()',DELAY)}c(0)};</script>"));
  539. RedFly.socketSendPGM(sock, PSTR("</br></br><a href='/'>Back</a>"));
  540. RedFly.socketSendPGM(sock, PSTR("<style type='text/css'>html,body{width:100%;height:100%}html{overflow:hidden}body{margin:0;font-family:Verdana,Geneva,Georgia,Chicago,Arial,Sans-serif,'MS Sans Serif'}#info{position:absolute;padding:4px;left:12px;top:10px;background-color:#fff;border:1px solid #002d80;color:#002d80;opacity:.8;-moz-border-radius:5px;-webkit-border-radius:5px}</style></head><body onload='doit()'><canvas id='out'></canvas><div id='info'/></body></html>"));
  541. RedFly.socketClose(sock);
  542. }
  543. }
  544. }
  545.  
  546.  
  547. }
RAW Paste Data