bangnaga

Arduino 1.0 Ethernet Shield + PHP

May 27th, 2012
111
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Copy Code
  2.  
  3. //ARDUINO 1.0+ ONLY
  4. //ARDUINO 1.0+ ONLY
  5.  
  6. #include <Ethernet.h>
  7. #include <SPI.h>
  8. boolean reading = false;
  9.  
  10. ////////////////////////////////////////////////////////////////////////
  11. //CONFIGURE
  12. ////////////////////////////////////////////////////////////////////////
  13.   //byte ip[] = { 192, 168, 0, 199 };   //Manual setup only
  14.   //byte gateway[] = { 192, 168, 0, 1 }; //Manual setup only
  15.   //byte subnet[] = { 255, 255, 255, 0 }; //Manual setup only
  16.  
  17.   // if need to change the MAC address (Very Rare)
  18.   byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  19.  
  20.   EthernetServer server = EthernetServer(80); //port 80
  21. ////////////////////////////////////////////////////////////////////////
  22.  
  23. void setup(){
  24.   Serial.begin(9600);
  25.  
  26.   //Pins 10,11,12 & 13 are used by the ethernet shield
  27.  
  28.   pinMode(2, OUTPUT);
  29.   pinMode(3, OUTPUT);
  30.   pinMode(4, OUTPUT);
  31.   pinMode(5, OUTPUT);
  32.   pinMode(6, OUTPUT);
  33.   pinMode(7, OUTPUT);
  34.   pinMode(8, OUTPUT);
  35.   pinMode(9, OUTPUT);
  36.  
  37.   Ethernet.begin(mac);
  38.   //Ethernet.begin(mac, ip, gateway, subnet); //for manual setup
  39.  
  40.   server.begin();
  41.   Serial.println(Ethernet.localIP());
  42.  
  43. }
  44.  
  45. void loop(){
  46.  
  47.   // listen for incoming clients, and process qequest.
  48.   checkForClient();
  49.  
  50. }
  51.  
  52. void checkForClient(){
  53.  
  54.   EthernetClient client = server.available();
  55.  
  56.   if (client) {
  57.  
  58.     // an http request ends with a blank line
  59.     boolean currentLineIsBlank = true;
  60.     boolean sentHeader = false;
  61.  
  62.     while (client.connected()) {
  63.       if (client.available()) {
  64.  
  65.         if(!sentHeader){
  66.           // send a standard http response header
  67.           client.println("HTTP/1.1 200 OK");
  68.           client.println("Content-Type: text/html");
  69.           client.println();
  70.           sentHeader = true;
  71.         }
  72.  
  73.         char c = client.read();
  74.  
  75.         if(reading && c == ' ') reading = false;
  76.         if(c == '?') reading = true; //found the ?, begin reading the info
  77.  
  78.         if(reading){
  79.           Serial.print(c);
  80.  
  81.            switch (c) {
  82.             case '2':
  83.               //add code here to trigger on 2
  84.               triggerPin(2, client);
  85.               break;
  86.             case '3':
  87.             //add code here to trigger on 3
  88.               triggerPin(3, client);
  89.               break;
  90.             case '4':
  91.             //add code here to trigger on 4
  92.               triggerPin(4, client);
  93.               break;
  94.             case '5':
  95.             //add code here to trigger on 5
  96.               triggerPin(5, client);
  97.               break;
  98.             case '6':
  99.             //add code here to trigger on 6
  100.               triggerPin(6, client);
  101.               break;
  102.             case '7':
  103.             //add code here to trigger on 7
  104.               triggerPin(7, client);
  105.               break;
  106.             case '8':
  107.             //add code here to trigger on 8
  108.               triggerPin(8, client);
  109.               break;
  110.             case '9':
  111.             //add code here to trigger on 9
  112.               triggerPin(9, client);
  113.               break;
  114.           }
  115.  
  116.         }
  117.  
  118.         if (c == '\n' && currentLineIsBlank)  break;
  119.  
  120.         if (c == '\n') {
  121.           currentLineIsBlank = true;
  122.         }else if (c != '\r') {
  123.           currentLineIsBlank = false;
  124.         }
  125.  
  126.       }
  127.     }
  128.  
  129.     delay(1); // give the web browser time to receive the data
  130.     client.stop(); // close the connection:
  131.  
  132.   }
  133.  
  134. }
  135.  
  136. void triggerPin(int pin, EthernetClient client){
  137. //blink a pin - Client needed just for HTML output purposes.  
  138.   client.print("Turning on pin ");
  139.   client.println(pin);
  140.   client.print("<br>");
  141.  
  142.   digitalWrite(pin, HIGH);
  143.   delay(25);
  144.   digitalWrite(pin, LOW);
  145.   delay(25);
  146. }
  147.  
  148. ==================================================================
  149. Code PHP nya mulai dari sini:
  150.  
  151. <?php
  152. //the arduino will store anything between '<' and '>'
  153. //So if the output was <1kjhghk5> - the  arduino would read 1kjhghk5
  154.  
  155. //Just generates a random alphanumeric string
  156. $what_the_arduino_reads = '1'.base_convert(rand(10000,9999999), 10, 36);
  157.  
  158. echo '<'.$what_the_arduino_reads.'>';
  159. ?>
RAW Paste Data