Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2012
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.27 KB | None | 0 0
  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 13
  18. #define ONE_WIRE_BUS 63 // Data wire is plugged into port 2 on the Arduino
  19.  
  20. #define HTTP_SERVER_PORT 80
  21. #define SERVER_PORT 4444
  22.  
  23. #define CONFIG_VERSION "ar1"
  24. #define CONFIG_START 0
  25. // NetworkConnectTo
  26. struct WiFiStorageStruct {
  27. char version[4];
  28. char ssid[24];
  29. char pwd[16];
  30. byte addr[4];
  31. unsigned int id;
  32. } WiFiConfig = {
  33. CONFIG_VERSION,
  34. "sandkaeret1",
  35. "6161616161",
  36. {192, 168, 1, 150},
  37. 0
  38. };
  39.  
  40. void loadConfig() {
  41. if (EEPROM.read(CONFIG_START + 0) == CONFIG_VERSION[0] &&
  42. EEPROM.read(CONFIG_START + 1) == CONFIG_VERSION[1] &&
  43. EEPROM.read(CONFIG_START + 2) == CONFIG_VERSION[2])
  44. for (unsigned int t=0; t<sizeof(WiFiConfig); t++)
  45. *((char*)&WiFiConfig + t) = EEPROM.read(CONFIG_START + t);
  46. }
  47.  
  48. void saveConfig() {
  49. for (unsigned int t=0; t<sizeof(WiFiConfig); t++)
  50. EEPROM.write(CONFIG_START + t, *((char*)&WiFiConfig + t));
  51. }
  52.  
  53. void blink(int pin, int n) {
  54. for (int i = 0; i < n; i++) {
  55. digitalWrite(pin, HIGH); delay(200);
  56. digitalWrite(pin, LOW); delay(200);
  57. }
  58. }
  59.  
  60. byte adhoc = 0; // 1 - adhoc connection or 0 - connected to AP
  61.  
  62.  
  63. OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
  64.  
  65. DallasTemperature sensors(&oneWire); // Pass our oneWire reference to Dallas Temperature.
  66.  
  67. DeviceAddress RoomThermometer = { 0x28, 0xA0, 0x34, 0xC3, 0x03, 0x00, 0x00, 0x8C }; // Array to hold address for inside thermometer
  68. DeviceAddress DrumThermometer = { 0x28, 0xF3, 0x19, 0x33, 0x03, 0x00, 0x00, 0x9E }; // Array to hold address for outside thermometer
  69. float tempC=0;
  70. int temperature1;
  71. int temperature2;
  72.  
  73. void setup() {
  74. uint8_t ret;
  75.  
  76. loadConfig();
  77.  
  78. blink(LEDPIN, 1);
  79.  
  80. //init the WiFi module on the shield
  81. ret = RedFly.init(115200, HIGH_POWER); //LOW_POWER MED_POWER HIGH_POWER
  82. if (!ret) {
  83. RedFly.scan();
  84. adhoc = ret = RedFly.join(WiFiConfig.ssid, WiFiConfig.pwd, INFRASTRUCTURE);
  85. if (ret) {
  86. char network[16];
  87. sprintf(network, "TestNetwork%d", WiFiConfig.id);
  88. ret = RedFly.join(network, IBSS_CREATOR, 10);
  89. }
  90. if (!ret) {
  91. currentIP = adhoc ? serverIP : WiFiConfig.addr;
  92. ret = RedFly.begin(currentIP, gateway, netmask);
  93. }
  94. }
  95.  
  96. if (adhoc) { // only open DHCP/DNS ports in adhoc config
  97. // listen for DHCP messages on DHCP_SERVER_PORT (UDP)
  98. hDHCP = RedFly.socketListen(PROTO_UDP, DHCP_SERVER_PORT);
  99. // listen for DNS messages on DNS_SERVER_PORT (both UDP and TCP on the same port)
  100. hDNSTCP = RedFly.socketListen(PROTO_TCP, DNS_SERVER_PORT);
  101. hDNSUDP = RedFly.socketListen(PROTO_UDP, DNS_SERVER_PORT);
  102. }
  103.  
  104. // listen for UDP messages on port 80 (to test echo)
  105. hHTTPUDP = RedFly.socketListen(PROTO_UDP, HTTP_SERVER_PORT);
  106.  
  107. // 10 blinks on error, 5 blinks on AP connection and 3 blinks on adhoc connection
  108. blink(LEDPIN, ret ? 10 : (adhoc ? 3 : 5));
  109.  
  110. pinMode(63, INPUT); // DS18B20 one wire bus
  111. sensors.setResolution(RoomThermometer, 12); // set the resolution to 12 bit
  112. sensors.setResolution(DrumThermometer, 12);
  113. }
  114.  
  115.  
  116. void loop()
  117. {
  118. uint8_t sock, *ptr, buf[DHCP_MESSAGE_SIZE];
  119. uint16_t buf_len, rd, len;
  120. uint16_t port; //incoming UDP port
  121. uint8_t ip[4]; //incoming UDP ip
  122.  
  123. //check if socket is closed and start listening
  124. if (hHTTPTCP == 0xFF || RedFly.socketClosed(hHTTPTCP)) hHTTPTCP = RedFly.socketListen(PROTO_TCP, HTTP_SERVER_PORT); // start listening on port 80
  125.  
  126. // get data
  127. sock = 0xFF; // 0xFF = return data from all open sockets
  128. ptr = buf;
  129. buf_len = 0;
  130. do {
  131. rd = RedFly.socketRead(&sock, &len, ip, &port, ptr, sizeof(buf)-buf_len);
  132. if((rd != 0) && (rd != 0xFFFF)) { // 0xFFFF = connection closed
  133. ptr += rd;
  134. buf_len += rd;
  135. }
  136. } while(len != 0);
  137.  
  138. // process and send back data
  139. if (buf_len && (sock != 0xFF)) {
  140. if (sock == hDHCP) {
  141. buf_len = DHCPreply((RIP_MSG*)buf, buf_len, currentIP, domainName); // zero returned means the message was not recognized
  142. if (buf_len) RedFly.socketSend(sock, buf, buf_len, broadcast, port);
  143. }
  144. else if (sock == hDNSTCP || sock == hDNSUDP) {
  145. buf_len = DNSreply((DNS_MSG*)buf, buf_len, currentIP, serverName); // zero returned means the message was not recognized
  146. if (sock == hDNSTCP) {
  147. if (buf_len) RedFly.socketSend(sock, buf, buf_len);
  148. }
  149. else {
  150. if (buf_len) RedFly.socketSend(sock, buf, buf_len, ip, port); // send back to the same ip/port the message came from
  151. }
  152. }
  153. else if (sock == hHTTPUDP) {
  154. sprintf((char*)buf+buf_len, " %d", millis());
  155. RedFly.socketSend(hHTTPUDP, buf, strlen((char*)buf), ip, port);
  156. }
  157. else if (sock == hHTTPTCP) {
  158. const char *OK = PSTR("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n");
  159. const char *ContentLengthHeader = PSTR("Content-Length: %d\r\n\r\n");
  160. const char *STYLE = PSTR("\r\n<html style='font-family:Verdana,Geneva,Georgia,Chicago,Arial,Sans-serif;color:#002d80'>");
  161. int pin, value;
  162. char mode, ignore;
  163. char *out = (char *)buf;
  164.  
  165.  
  166. if (strncmp_P(out, PSTR("GET / HTTP"), 10) == 0) {
  167. RedFly.socketSendPGM(sock, OK);
  168. RedFly.socketSendPGM(sock, STYLE);
  169. RedFly.socketSendPGM(sock, PSTR("Hello, World!<br/><br/>You can <a href='/wifi'>update wifi configuration</a>, <a href='/D13=1'>turn LED on (digital pin 13)</a>, or <a href='/A1'>display analog (A1) sensor values</a>"));
  170. RedFly.socketClose(sock);
  171. }
  172. else if (strncmp_P(out, PSTR("GET /wifi HTTP"), 14) == 0) {
  173. RedFly.socketSendPGM(sock, OK);
  174. RedFly.socketSendPGM(sock, STYLE);
  175. RedFly.socketSendPGM(sock, PSTR("<form method='post'>Wifi configuration<p>SSID: <input style='margin-left:48px' value='"));
  176. RedFly.socketSend(sock, (uint8_t*)WiFiConfig.ssid, strlen(WiFiConfig.ssid));
  177. RedFly.socketSendPGM(sock, PSTR("' name='s'/><br/>Password: <input type='password' name='p' style='margin-left:10px'/><br/>IP address: <input name='a' value='"));
  178. sprintf(out, "%d.%d.%d.%d", WiFiConfig.addr[0], WiFiConfig.addr[1], WiFiConfig.addr[2], WiFiConfig.addr[3]);
  179. RedFly.socketSend(sock, (uint8_t*)out, strlen(out));
  180. 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>"));
  181. RedFly.socketSendPGM(sock, PSTR("</br><a href='/'>Back</a>"));
  182. RedFly.socketClose(sock);
  183. }
  184. else if (strncmp_P(out, PSTR("POST /wifi HTTP"), 15) == 0) {
  185. char *st, *fi;
  186. char *a = NULL, *p = NULL, *s = NULL;
  187. out[buf_len] = '\0';
  188. if (st = strstr(out, "a="))
  189. if (sscanf(st+2, "%d.%d.%d.%d", WiFiConfig.addr, WiFiConfig.addr+1, WiFiConfig.addr+2, WiFiConfig.addr+3) == 4) a = st+2;
  190. if (st = strstr(out, "p="))
  191. if (fi = strstr(st, "&")) { fi[0] = '\0'; p = st+2; }
  192. if (st = strstr(out, "s="))
  193. if (fi = strstr(st, "&")) { fi[0] = '\0'; s = st+2; }
  194.  
  195. RedFly.socketSendPGM(sock, OK);
  196. RedFly.socketSendPGM(sock, STYLE);
  197. if (a && p && s) {
  198. strcpy(WiFiConfig.pwd, p);
  199. strcpy(WiFiConfig.ssid, s);
  200. saveConfig();
  201. RedFly.socketSendPGM(sock, PSTR("Updated. The board has been restarted with the new configuration.</html>"));
  202. RedFly.socketClose(sock);
  203.  
  204. // restart Arduino by calling a (pseudo)function at address 0
  205. void(* reset) (void) = 0; // declare reset function @ address 0
  206. reset();
  207. }
  208. else {
  209. RedFly.socketSendPGM(sock, PSTR("Error. Please return back and update the configuration.</html>"));
  210. RedFly.socketClose(sock);
  211. }
  212. }
  213. else if (sscanf_P(out, PSTR("GET /D%2d=%1d HTTP"), &pin, &value) == 2) {
  214. pinMode(pin, OUTPUT);
  215. digitalWrite(pin, value ? HIGH : LOW);
  216.  
  217. const char * SetPinMessage = PSTR("Set digital pin %d to %d; <a href='/D%d=%d'>toggle</a>");
  218. sprintf_P(out, SetPinMessage, pin, !!value, pin, !value);
  219.  
  220. RedFly.socketSendPGM(sock, OK);
  221. RedFly.socketSendPGM(sock, STYLE);
  222. RedFly.socketSend(sock, out);
  223. RedFly.socketSendPGM(sock, PSTR("</br><a href='/'>Back</a>"));
  224. RedFly.socketClose(sock);
  225. }
  226. else if (sscanf_P(out, PSTR("GET /%1[AD]%2d= %1[H]"), &mode, &pin, &ignore) == 3) {
  227.  
  228. // get the value
  229. pinMode(pin, INPUT);
  230. int value = (mode == 'A' ? analogRead(pin) : digitalRead(pin));
  231.  
  232. // calculate content length
  233. itoa(value, out, 10);
  234. int contentLength = strlen(out);
  235.  
  236. sprintf_P(out, OK);
  237. sprintf_P(out+strlen(out), ContentLengthHeader, contentLength);
  238. itoa(value, out + strlen(out), 10);
  239.  
  240. RedFly.socketSend(sock, out); // push the actual content out
  241.  
  242. }
  243. else {
  244. RedFly.socketSendPGM(sock, OK);
  245. 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>"));
  246. RedFly.socketSendPGM(sock, PSTR("</br></br><a href='/'>Back</a>"));
  247. 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:10px;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>"));
  248. RedFly.socketClose(sock);
  249. }
  250. }
  251. }
  252.  
  253.  
  254. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement