Advertisement
bangnaga

Arduino Ajax

Jun 12th, 2012
804
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.80 KB | None | 0 0
  1. /*
  2.   Ajax 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 4 Sep 2010
  14.  by Tom Igoe
  15.  modified 12 Jun 2012
  16.  by Bang Naga
  17.  
  18.  - - - - - - - - - - - - - - - - - - - - - - - -
  19.  modified 16 Sep 2011 // Ajax version.
  20.  by TETRASTYLE
  21.  for Arduino IDE 0022
  22.  
  23.  get Digital & Analog port status ( JSON(P) formatted data)
  24.   <-- http://192.168.1.177/?=
  25.  
  26.   return value
  27.   --> cb({"D":[D0,D1,D2,D3,D4,D5,D6,D7],"A":[A0,A1,A2,A3,A4,A5]});
  28.  
  29.  toggle Digital Port (D0 - D7)
  30.   <-- http://192.168.1.177/?btn=0
  31.   <-- http://192.168.1.177/?btn=1
  32.       ...
  33.   <-- http://192.168.1.177/?btn=7
  34.  
  35. */
  36.  
  37. #include <SPI.h>
  38. #include <Ethernet.h>
  39.  
  40. boolean gflag=false;
  41. String parm;
  42. int dout[8]={0,0,0,0,0,0,0,0};
  43.  
  44. // Enter a MAC address and IP address for your controller below.
  45. // The IP address will be dependent on your local network:
  46. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  47. byte ip[] = { 192, 168, 10, 73 };
  48. byte subnet[] = { 255,255,255,0};
  49. byte gateway[] = { 192,168,0,254};
  50.  
  51. // Initialize the Ethernet server library
  52. // with the IP address and port you want to use
  53. // (port 80 is default for HTTP):
  54. Server server(80);
  55.  
  56. void setup()
  57. {
  58.   for (int i=0;i<8;i++){
  59.     pinMode(i,OUTPUT);
  60.     digitalWrite(i,dout[i]);
  61.   }
  62.  
  63.   // start the Ethernet connection and the server:
  64.  // Ethernet.begin(mac, ip);
  65.   Ethernet.begin(mac, ip , gateway , subnet);
  66.   server.begin();
  67.   //Serial.begin(19200);
  68. }
  69.  
  70. void loop()
  71. {
  72.   // listen for incoming clients
  73.   Client client = server.available();
  74.   if (client) {
  75.     // an http request ends with a blank line
  76.     boolean currentLineIsBlank = true;
  77.    
  78.     while (client.connected()) {
  79.       if (client.available()) {
  80.         char c = client.read();
  81.  
  82.         // serch parameter from "HTTP GET"
  83.         if(gflag){
  84.           if(c != ' '){
  85.               parm += c;
  86.           }else{
  87.               gflag = false;
  88.           }
  89.         }
  90.         if(c == '?' && parm == ""){
  91.           gflag = true;
  92.         }
  93.         //Serial.print(c);
  94.  
  95.  
  96.         // if you've gotten to the end of the line (received a newline
  97.         // character) and the line is blank, the http request has ended,
  98.         // so you can send a reply
  99.  
  100.         if (c == '\n' && currentLineIsBlank) {
  101.           // send a standard http response header
  102.  
  103.          if(parm == ""){ // normal HTTP access
  104.             client.println("HTTP/1.1 200 OK");
  105.             client.println("Content-Type: text/html");
  106.             client.println();
  107.  
  108.             client.println("<html><head><title>Hello Arduino</title>");
  109.             client.println("<meta name='viewport' content='width=240px' />");
  110.            
  111.             client.println("<script type='text/javascript'>");
  112.  
  113.             client.println("function createXMLHttpRequest(cbFunc){");
  114.             client.println("  var XObj = new XMLHttpRequest();");
  115.             client.println("  if(XObj) XObj.onreadystatechange = cbFunc;return XObj;}");
  116.  
  117.             client.println("function setData(val){htObj = createXMLHttpRequest(displayData);");
  118.             client.println("  if(htObj){htObj.open('GET','/?btn='+val,true);htObj.send(null);}}");
  119.  
  120.             client.println("function getData(){htObj = createXMLHttpRequest(displayData);");
  121.             client.println("  if(htObj){htObj.open('GET','/?=',true);htObj.send(null);}}");
  122.  
  123.             client.println("function displayData(){ if((htObj.readyState == 4) && (htObj.status == 200)){");
  124.             client.println("  document.getElementById('result').innerHTML =  htObj.responseText;}}");
  125.  
  126.             client.println("function strT(){ getData(); timerID=setTimeout('strT()',");
  127.             client.println("  document.getElementById('tf1').value);}");
  128.  
  129.             client.println("function clrT(){clearTimeout(timerID);}");
  130.            
  131.             client.println("</script></head><body onLoad='getData()'><form action='/' method='GET'><br />");
  132.  
  133.             client.println("<input id='btn0' type='button' value=' D0 ' onClick='setData(0)'>");
  134.             client.println("<input id='btn1' type='button' value=' D1 ' onClick='setData(1)'>");
  135.             client.println("<input id='btn2' type='button' value=' D2 ' onClick='setData(2)'>");
  136.             client.println("<input id='btn3' type='button' value=' D3 ' onClick='setData(3)'><br /><br />");
  137.             client.println("<input id='btn4' type='button' value=' D4 ' onClick='setData(4)'>");
  138.             client.println("<input id='btn5' type='button' value=' D5 ' onClick='setData(5)'>");
  139.             client.println("<input id='btn6' type='button' value=' D6 ' onClick='setData(6)'>");
  140.             client.println("<input id='btn7' type='button' value=' D7 ' onClick='setData(7)'><br /><br />");
  141.  
  142.             client.println("</form><form><input id='btn100' type='button' value=' START ' onClick='strT()'>");
  143.             client.println("<input id='btn200' type='button' value=' STOP ' onClick='clrT()'>");
  144.             client.println("<input id='tf1' type='text' size='6' value='1000'>");
  145.             client.println("</form><br /><div id='result'></div>");
  146.  
  147.             client.println("<br /></body></html>");
  148.  
  149.          }else{ // using XMLhttpObject access
  150.  
  151.             int check = parm.indexOf('=');
  152.             if(check != -1){
  153.               //Set Digital Port
  154.               int check2 = parm.indexOf('btn');
  155.               if(check2 != -1){
  156.                 int port = (parm.substring(check+1)).toInt();
  157.                 dout[port] = !dout[port];
  158.                 digitalWrite(port, dout[port]);
  159.               }
  160.  
  161.              //Write JSONP Data (Digital & Analog Ports Status, Callback function name is 'cb')
  162.                client.print("cb({\"D\":[");
  163.                  for(int i=0; i<8; i++ ){
  164.                    client.print(dout[i]);
  165.                    if(i<7){
  166.                      client.print(",");
  167.                    }
  168.                  }
  169.                client.print("],\"A\":[");
  170.                  for(int i=0; i<6; i++ ){
  171.                    client.print(analogRead(i));
  172.                    if(i<5){
  173.                      client.print(",");
  174.                    }
  175.                  }
  176.                client.print("]});");
  177.              
  178.             }
  179.             parm = "";
  180.           }
  181.  
  182.  
  183.           break;
  184.         }
  185.  
  186.  
  187.         if (c == '\n') {
  188.           // you're starting a new line
  189.           currentLineIsBlank = true;
  190.         }
  191.         else if (c != '\r') {
  192.           // you've gotten a character on the current line
  193.           currentLineIsBlank = false;
  194.         }
  195.       }
  196.     }
  197.     // give the web browser time to receive the data
  198.     delay(1);
  199.  
  200.     // close the connection:
  201.     client.stop();
  202.   }
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement