Advertisement
depwl9992

Arduino Webserver_Terminal

Aug 16th, 2013
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.12 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <Ethernet.h>
  3. #include <Arduino_WebServer.h>
  4. #include <SD.h>
  5. // Enter a MAC address and IP address for your controller below.
  6. // The IP address will be dependent on your local network:
  7. byte mac[] = { 0x90,0xA2,0xDA,0x00,0xD9,0x7E };
  8.  
  9. int ipAddr[4] = { 161, 0, 0, 7 };
  10. IPAddress ip(ipAddr[0],ipAddr[1],ipAddr[2],ipAddr[3]);
  11. int port=8080;
  12. EthernetServer server(port);
  13. EthernetClient client;
  14. SdFile root;
  15. SdVolume volume;
  16. boolean fileTerm = false;
  17.  
  18. /** Main setup function */
  19. void setup() {
  20.  // Open serial communications and wait for port to open:
  21.   Serial.begin(9600);
  22.    while (!Serial) {
  23.     ; // wait for serial port to connect. Needed for Leonardo only
  24.   }
  25.  
  26.  
  27.   // start the Ethernet connection and the server:
  28.   Ethernet.begin(mac, ip);
  29.   server.begin();
  30.   Serial.print("Server is at ");
  31.   Serial.println(Ethernet.localIP() + ":" + port);
  32.  
  33.   Serial.println("Initializing SD Card...");
  34.   pinMode(10,OUTPUT);
  35.   if(!SD.begin(4)) {
  36.     Serial.println("Initialization fail!");
  37.     return;
  38.   }
  39.  
  40.   Serial.println("Initialization done.");
  41.  
  42. }
  43.  
  44. /** Main loop function */
  45. void loop() {
  46.   // listen for incoming clients
  47.   client = server.available();
  48.   if (client) {
  49.     Serial.println("new client");
  50.     // an http request ends with a blank line
  51.     boolean currentLineIsBlank = true;
  52.  
  53.     while (client.connected()) {
  54.       if (client.available()) {
  55.         char c = client.read();
  56.         Serial.write(c); // write ethernet rcv to serial
  57.        
  58.         // if you've gotten to the end of the line (received a newline
  59.         // character) and the line is blank, the http request has ended,
  60.         // so you can send a reply
  61.         if (c == '\n' && currentLineIsBlank) {
  62.           // send a standard http response header
  63.           PrintHTML();
  64.           break;
  65.         }
  66.         if (c == '\n') {
  67.           // you're starting a new line
  68.           currentLineIsBlank = true;
  69.         }
  70.         else if (c != '\r') {
  71.           // you've gotten a character on the current line
  72.           currentLineIsBlank = false;
  73.         }
  74.       }
  75.     }
  76.     // give the web browser time to receive the data
  77.     delay(1);
  78.     // close the connection:
  79.     client.stop();
  80.     Serial.println("client disconnected");
  81.   }
  82.  
  83.   char character;
  84.   String content="";
  85.   while (Serial.available()) {
  86.     character = Serial.read();
  87.     content.concat(character);
  88.     delay(1);
  89.   }
  90.  
  91.   if (content != "") {
  92.     Serial.println(content);
  93.    
  94.     /** Error from Serial command string.
  95.      * 0 = No error
  96.      * 1 = Invalid command
  97.      */
  98.      int err = testInput(content);
  99.   }
  100. }
  101.  
  102.  
  103. int testInput(String content) {
  104.   if (content == "term") {
  105.     fileTerm = true;
  106.     Serial.println("Starting Terminal Mode");
  107.     return 0;
  108.   }
  109.  
  110.   if (content == "exit" && fileTerm == true) {
  111.     fileTerm = false;
  112.     Serial.println("Exitting Terminal Mode");
  113.     return 0;
  114.   }
  115.   return 1;
  116. }
  117.  
  118. /** Print an html line to the ethernet client */
  119. void html(String inpLine) {
  120.   // Write input to the web client connection.
  121.   client.println(inpLine);
  122. }
  123.  
  124. void PrintHTML() {
  125.   client.println("HTTP/1.1 200 OK");
  126.   client.println("Content-Type: text/html");
  127.   client.println("Connection: close");
  128.   client.println();
  129.   client.println("<!DOCTYPE HTML>");
  130.   client.println("<html>");
  131.   // add a meta refresh tag, so the browser pulls again every 5 seconds:
  132.   client.println("<head><meta http-equiv=\"refresh\" content=\"10\">");
  133.  
  134.   client.println("<title>Arduino Uno R3</title>");
  135.   client.println("</head>\n<body>");
  136.   client.println("<h1>Welcome to the Arduino Uno R3 Webserver!</h1>");
  137.  
  138.   // output the value of each analog input pin
  139.   client.println("<h3>Analog inputs</h3>");
  140.   client.println("<h4>10-bit Full-Scale Range (0 to 1023)</h4>");
  141.   for (int analogChannel = 0; analogChannel <= 5; analogChannel++) {
  142.     int sensorReading = analogRead(analogChannel);
  143.     client.println("Analog Input ");
  144.     client.println((String)analogChannel);
  145.     client.println(" is ");
  146.     client.println((String)sensorReading);
  147.     client.println("<br />");      
  148.   }
  149.   client.println("</body></html>");
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement