Advertisement
Guest User

Untitled

a guest
Mar 2nd, 2015
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.86 KB | None | 0 0
  1. /*
  2. Arduino Ethernet Script Server
  3.  
  4. Created Mars 4, 2014
  5. Mikael Kindborg, Evothings AB
  6.  
  7. TCP socket server that accept commands for basic scripting
  8. of the Arduino board.
  9.  
  10. This example is written for use with an Ethernet shield.
  11.  
  12. The API consists of the requests listed below.
  13.  
  14. Requests and responses end with a new line.
  15.  
  16. The input parameter n is a pin number ranging from 2 to 9.
  17.  
  18. The response is always a 4-character string with a
  19. hex encoded number ranging from 0 to FFFF.
  20.  
  21. Possible response string values:
  22.  
  23. H (result from digital read)
  24. L (result from digital read)
  25. 0 to 1023 - Analog value (result from analog read)
  26.  
  27. Set pin mode to OUTPUT for pin n: On
  28. Response: None
  29. Example: O5
  30. Note: O is upper case letter o, not digit zero (0).
  31.  
  32. Set pin mode to INPUT for pin n: In
  33. Response: None
  34. Example: I5
  35.  
  36. Write LOW to pin n: Ln
  37. Response: None
  38. Example: L5
  39.  
  40. Write HIGH to pin n: Hn
  41. Response: None
  42. Example: H5
  43.  
  44. READ pin n: Rn
  45. Response: "H" (HIGH) or "L" (LOW)
  46. Example: R5 -> H
  47.  
  48. ANALOG read pin n: An
  49. Response: int value as string (range "0" to "1023")
  50. Example: A5 -> 42
  51. */
  52.  
  53. // Include files.
  54. #include <SPI.h>
  55. #include <Ethernet.h>
  56.  
  57. // Enter a MAC address for your controller below, usually found on a sticker
  58. // on the back of your Ethernet shield.
  59. byte mac[] = { 0x90, 0xA2, 0xDA, 0x0E, 0xD0, 0x93 };
  60.  
  61. // The IP address will be dependent on your local network.
  62. // If you have IP network info, uncomment the lines starting
  63. // with IPAddress and enter relevant data for your network.
  64. // If you don't know, you probably have dynamically allocated IP adresses, then
  65. // you don't need to do anything, move along.
  66. // IPAddress ip(192,168,1, 177);
  67. // IPAddress gateway(192,168,1, 1);
  68. // IPAddress subnet(255, 255, 255, 0);
  69.  
  70. // Create a server listening on the given port.
  71. EthernetServer server(3300);
  72.  
  73. void setup()
  74. {
  75.     // Start serial communication with the given baud rate.
  76.     // NOTE: Remember to set the baud rate in the Serial
  77.     // monitor to the same value.
  78.     Serial.begin(9600);
  79.  
  80.     // Wait for serial port to connect. Needed for Leonardo only.
  81.     while (!Serial) { ; }
  82.  
  83.     // Initialize the Ethernet shield.
  84.     // If you entered fixed ipaddress info, gateway, subnet mask,
  85.     // then uncommment the next line.
  86.     // Ethernet.begin(mac, ip, gateway, subnet);
  87.  
  88.     // If it works to get a dynamic IP from a DHCP server, use this
  89.     // code to test if you're getting a dynamic adress. If this
  90.     // does not work, use the above method of specifying an IP-address.
  91.     // dhcp test starts here
  92.     if (Ethernet.begin(mac) == 0)
  93.     {
  94.         Serial.println("Failed to configure Ethernet using DHCP");
  95.         // No point in carrying on, stop here forever.
  96.         while(true) ;
  97.     }
  98.     // dhcp test end
  99.  
  100.     // Start the server.
  101.     server.begin();
  102.  
  103.     // Print status.
  104.     printServerStatus();
  105. }
  106.  
  107. void loop()
  108. {
  109.     // Listen for incoming client requests.
  110.     EthernetClient client = server.available();
  111.     if (!client)
  112.     {
  113.         return;
  114.     }
  115.  
  116.     Serial.println("Client connected");
  117.  
  118.     String request = readRequest(&client);
  119.     executeRequest(&client, &request);
  120.  
  121.     // Close the connection.
  122.     //client.stop();
  123.  
  124.     Serial.println("Client disonnected");
  125. }
  126.  
  127. // Read the request line,
  128. String readRequest(EthernetClient* client)
  129. {
  130.     String request = "";
  131.  
  132.     // Loop while the client is connected.
  133.     while (client->connected())
  134.     {
  135.         // Read available bytes.
  136.         while (client->available())
  137.         {
  138.             // Read a byte.
  139.             char c = client->read();
  140.  
  141.             // Print the value (for debugging).
  142.             Serial.write(c);
  143.  
  144.             // Exit loop if end of line.
  145.             if ('\n' == c)
  146.             {
  147.                 return request;
  148.             }
  149.  
  150.             // Add byte to request line.
  151.             request += c;
  152.         }
  153.     }
  154.     return request;
  155. }
  156.  
  157. void executeRequest(EthernetClient* client, String* request)
  158. {
  159.     char command = readCommand(request);
  160.     int n = readParam(request);
  161.     if ('O' == command)
  162.     {
  163.         pinMode(n, OUTPUT);
  164.     }
  165.     else if ('I' == command)
  166.     {
  167.         pinMode(n, INPUT);
  168.     }
  169.     else if ('L' == command)
  170.     {
  171.         digitalWrite(n, LOW);
  172.     }
  173.     else if ('H' == command)
  174.     {
  175.         digitalWrite(n, HIGH);
  176.     }
  177.     else if ('R' == command)
  178.     {
  179.         sendResponse(client, String(digitalRead(n)));
  180.     }
  181.     else if ('A' == command)
  182.     {
  183.         sendResponse(client, String(analogRead(n)));
  184.     }
  185. }
  186.  
  187. // Read the command from the request string.
  188. char readCommand(String* request)
  189. {
  190.     String commandString = request->substring(0, 1);
  191.     return commandString.charAt(0);
  192. }
  193.  
  194. // Read the parameter from the request string.
  195. int readParam(String* request)
  196. {
  197.     // This handles a hex digit 0 to F (0 to 15).
  198.     char buffer[2];
  199.     buffer[0] = request->charAt(1);
  200.     buffer[1] = 0;
  201.     return (int) strtol(buffer, NULL, 16);
  202. }
  203.  
  204. void sendResponse(EthernetClient* client, String response)
  205. {
  206.     // Send response to client.
  207.     client->println(response);
  208.  
  209.     // Debug print.
  210.     Serial.println("sendResponse:");
  211.     Serial.println(response);
  212. }
  213.  
  214. void printServerStatus()
  215. {
  216.     Serial.print("Server address:");
  217.     Serial.println(Ethernet.localIP());
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement