Advertisement
Guest User

Assignment-17

a guest
May 24th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.66 KB | None | 0 0
  1. // Onderstaande regels worden gebruikt om relatief veel tekst te verwerken. Aangezien de Arduino maar weinig intern geheugen heeft (1 KB)
  2. // worden deze teksen opgeslagen en verwerkt vanuit het programmageheugen. Je wordt niet geacht dit te begrijpen (maar dat mag wel).
  3. //----------
  4. const char cs0[] PROGMEM = "<STRONG>Opdracht 17 van het vak embedded systems 1</STRONG>";
  5. const char cs1[] PROGMEM = "Dit voorbeeld is gebaseerd op het script in Voorbeelden->Ethernet->Webserver";
  6. const char cs2[] PROGMEM = "De website is dynamische gemaakt door sensorwaarden van kanaal 0 toe te voegen.";
  7. const char cs3[] PROGMEM = "<B>Breid het programma uit</B> met de mogelijkheid om variabelen mee te geven.";
  8. const char cs4[] PROGMEM = "Dit kan o.a. door GET-variabelen, via de URL (192.168.1.3/?p8=1).";
  9. const char cs5[] PROGMEM = "Gebruik de functie <STRONG style='color:Black'>parseHeader(httpHeader, arg, val))</STRONG>";
  10. const char* const string_table[] PROGMEM = {cs0, cs1, cs2, cs3, cs4, cs5};
  11. char buffer[100];  
  12. //----------
  13.  
  14. //Defines
  15. #define maxLength     20  // header length, don't make it to long; Arduino doesn't have much memory
  16. #define sensorPin     0   // sensor on channel A0
  17. #define ledPin        8
  18. #define infoPin       9  
  19.  
  20. //Includes
  21. #include <SPI.h>
  22. #include <Ethernet.h>
  23.  
  24. // Enter a MAC address and IP address for your controller below. The IP address will be dependent on your local network:
  25. //byte mac[] = { 0x40, 0x6C, 0x8F, 0x36, 0x84, 0x8A };
  26. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };   // Ethernet adapter shield S. Oosterhaven
  27. IPAddress ip(192, 168, 1, 141);
  28.  
  29. // Initialize the Ethernet server library (port 80 is default for HTTP):
  30. EthernetServer server(80);
  31.  
  32. String httpHeader;           // = String(maxLength);
  33. int arg = 0, val = 0;        // to store get/post variables from the URL (argument and value, http:\\192.168.1.3\website?p8=1)
  34.  
  35. void setup() {
  36.  
  37.    //Init I/O-pins
  38.    DDRD = 0xFC;              // p7..p2: output
  39.    DDRB = 0x3F;              // p14,p15: input, p13..p8: output
  40.    pinMode(ledPin, OUTPUT);
  41.    pinMode(infoPin, OUTPUT);
  42.    
  43.    //Default states
  44.    digitalWrite(ledPin, LOW);
  45.    digitalWrite(infoPin, LOW);
  46.  
  47.    // Open serial communications and wait for port to open:
  48.    Serial.begin(9600);
  49.  
  50.    // Start the Ethernet connection and the server:
  51.    // Try to get an IP address from the DHCP server
  52.    // if DHCP fails, use static address
  53.    
  54.      Ethernet.begin(mac, ip);
  55.    
  56.  
  57.    //Start the ethernet server and give some debug info
  58.    server.begin();
  59.    Serial.println("Embedded Webserver with I/O-control v1.5");
  60.    Serial.println("Ethernetboard connected (pins 10, 11, 12, 13 and SPI)");
  61.    Serial.print("Server is at "); Serial.println(Ethernet.localIP());
  62.    Serial.print("ledpin at pin "); Serial.println(ledPin);
  63.    Serial.print("infoPin at pin "); Serial.println(ledPin);
  64. }
  65.  
  66.  
  67. void loop() {
  68.   // listen for incoming clients
  69.   EthernetClient client = server.available();
  70.  
  71.   //Webpage part
  72.   if (client) {
  73.     Serial.println("New client connected");
  74.     // an http request ends with a blank line
  75.     boolean currentLineIsBlank = true;
  76.    
  77.     while (client.connected()) {
  78.       if (client.available()) {
  79.         //read characters from client in the HTTP header
  80.         char c = client.read();
  81.         //store characters to string
  82.         if (httpHeader.length() < maxLength) httpHeader += c;  // don't need to store the whole Header
  83.         //Serial.write(c);                                     // for debug only
  84.        
  85.         // at end of the line (new line) and the line is blank, the http request has ended, so you can send a reply
  86.         if (c == '\n' && currentLineIsBlank) {
  87.           // client HTTP-request received
  88.           httpHeader.replace(" HTTP/1.1", ";");                // clean Header, and put a ; behind (last) arguments
  89.           httpHeader.trim();                                   // remove extra chars like space
  90.           Serial.println(httpHeader);                          // first part of header, for debug only
  91.              
  92.           // send a standard http response header
  93.           client.println("HTTP/1.1 200 OK");
  94.           client.println("Content-Type: text/html");
  95.           client.println("Connection: close");          // the connection will be closed after completion of the response
  96.           //client.println("Refresh: 3");               // refresh the page automatically every 3 sec
  97.           client.println();
  98.           client.println("<!DOCTYPE HTML>");
  99.           client.println("<HTML>");
  100.           client.println("<HEAD><TITLE>Embedded I/O-Webserver</TITLE><link rel=\"icon\" href=\"data:,\"></HEAD>");
  101.           client.println("<STYLE> body{width:800px;font-family:verdana;background-color:LightBlue;} ");
  102.           client.println("</STYLE>");
  103.           client.println("<BODY>");
  104.           client.println("<H4 style='color:DarkBlue'>Embedded I/O-Webserver</H4>");
  105.  
  106.        // show intro-text, it is OK to remove the following 7 lines
  107.        client.println("<P style='font-size:80%; color:Gray'>");
  108.        for (int i = 0; i <= 5; i++)
  109.        {
  110.            strcpy_P(buffer, (char*)pgm_read_word(&(string_table[i])));   // Necessary casts and dereferencing, just copy
  111.            client.println(buffer); client.println("<br>");
  112.        }
  113.        client.println("</P>");
  114.          
  115.           // output the value of analog input pin A0
  116.           int sensorValue = analogRead(sensorPin);
  117.           client.println("<P style='color:DarkBlue'>");  
  118.  
  119.           if(sensorValue > 600) {
  120.             client.println("<P style='color:DarkRed'>");
  121.             client.print("Analog sensor, channel "); client.print(sensorPin); client.print(": ");
  122.             client.print(sensorValue);
  123.           }
  124.           else {
  125.           client.print("Analog sensor, channel "); client.print(sensorPin); client.print(": ");
  126.           client.print(sensorValue);
  127.           }
  128.          
  129.           client.println("</P>");
  130.          
  131.           //grab commands from the url
  132.           client.println("<P>");
  133.           if (parseHeader(httpHeader, arg, val)) {   // search for argument and value, eg. p8=1
  134.               //Serial.print(arg); Serial.print(" "); Serial.println(val);  // for debug only
  135.               digitalWrite(arg, val);                // Recall: pins 10..13 used for the Ethernet shield
  136.               client.print("Pin ");client.print(arg); client.print(" = "); client.println(val);
  137.           }
  138.           else client.println("No IO-pins to control");
  139.           client.println("</P>");
  140.          
  141.           // end of website
  142.           client.println("</BODY>");
  143.           client.println("</HTML>");
  144.           break;
  145.         }
  146.        
  147.         if (c == '\n') {
  148.           // you're starting a new line
  149.           currentLineIsBlank = true;
  150.         }
  151.         else if (c != '\r') {
  152.           // you've gotten a character on the current line
  153.           currentLineIsBlank = false;
  154.         }
  155.       }
  156.     }
  157.     // give the web browser time to receive the data
  158.     delay(1);
  159.     // close the connection:
  160.     client.stop();
  161.     httpHeader = "";
  162.     Serial.println("Client disconnected");
  163.   }
  164. }
  165.  
  166. // GET-vars after "?"   192.168.1.3/?p8=1
  167. // parse header. Argument starts with p (only p2 .. p9)
  168. // input:  header = HTTPheader from client
  169. // output: a = argument (bijv. p8)  // let op a en v zijn uitvoerparameters, vandaar de &a en &v
  170. // output: v = value (bijv. 1)
  171. // result: true if arguments are valid
  172. bool parseHeader(String header, int &a, int &v)
  173. {
  174.       a = header.charAt(header.indexOf('p', 1) + 1) - 48;
  175.       v = header.charAt(header.indexOf('=', 1) + 1) - 48;
  176.  
  177.       Serial.println(a);
  178.       Serial.println(v);
  179.      
  180.       return a == 8 && (v == 1 || v == 0);
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement