Advertisement
j0h

teensyServerWorkInProgress

j0h
Nov 10th, 2021
1,561
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.87 KB | None | 0 0
  1. /*
  2. pots on 8,9 analog channels change background colour and fill color of svg
  3. */
  4.  
  5. #include <SPI.h>
  6. #include <NativeEthernet.h>
  7. byte mac[] = {
  8.   0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
  9. };
  10. IPAddress ip(10, 1, 0, 177);
  11. EthernetServer server(80);
  12. void initClient(EthernetClient client);
  13.  
  14.    
  15. void setup() {
  16. delay(5000);  //you want this delay. reason: tl;dr
  17.   Serial.begin(9600);
  18.   Ethernet.begin(mac, ip);
  19.  
  20. if (Ethernet.linkStatus() == LinkOFF) {
  21.     Serial.println("Ethernet cable is not connected.");
  22.   }
  23.  
  24.   // start the server
  25.   server.begin();
  26.   Serial.print("server is at ");
  27.   Serial.println(Ethernet.localIP());
  28. }
  29.  
  30.  
  31. void loop() {
  32.   // listen for incoming clients
  33.   EthernetClient client = server.available();
  34.   if (client) {
  35.     boolean currentLineIsBlank = true;
  36.     while (client.connected()) {
  37.       if (client.available()) {
  38.         char c = client.read();
  39.  //        Serial.write(c);
  40.            /*
  41. client disconnected
  42. GET / HTTP/1.1
  43. Host: 10.1.0.177
  44. User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:94.0) Gecko/20100101 Firefox/94.0
  45. Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*\/*;q=0.8
  46. Accept-Language: en-US,en;q=0.5
  47. Accept-Encoding: gzip, deflate
  48. Connection: keep-alive
  49. Upgrade-Insecure-Requests: 1
  50. Cache-Control: max-age=0
  51. client disconnected
  52.  
  53.         */
  54.  
  55.        if (c == '\n' && currentLineIsBlank) {
  56.        initClient(client);  
  57.             int sensorReading0 = analogRead(8);
  58.             int sensorReading1 = analogRead(9);
  59. client.print("<svg version=\"1.1\" \n baseProfile=\"full\"\n width=\"300\" height=\"200\"\n  xmlns=\"http://www.w3.org/2000/svg\">\n <rect width=\"100%\" height=\"100%\" fill=\"#");
  60. client.print(sensorReading0);
  61. client.print("\" /> <circle cx=\"150\" cy=\"100\" r=\"90\" fill=\"#");
  62. client.print(sensorReading1);
  63. client.print("\" />\n</svg>");
  64.           client.println("<br />");
  65.           client.println("</html>");
  66.           break;
  67.         }
  68.         if (c == '\n') {
  69.           // you're starting a new line
  70.           currentLineIsBlank = true;
  71.         } else if (c != '\r') {
  72.           // you've gotten a character on the current line
  73.           currentLineIsBlank = false;
  74.         }
  75.       }
  76.     }
  77.     // give the web browser time to receive the data
  78.     delay(1);
  79.     // close the connection:
  80.     client.stop();
  81.     Serial.println("client disconnected");
  82.   }
  83. }
  84.  
  85. void initClient(EthernetClient client){
  86.      // send a standard http response header
  87.           client.println("HTTP/1.1 200 OK");
  88.           client.println("Content-Type: text/html");
  89.           client.println("Connection: close");  // the connection will be closed after completion of the response
  90.           client.println("Refresh: 5");  // refresh the page automatically every 5 sec
  91.           client.println();
  92.           client.println("<!DOCTYPE HTML>");
  93.           client.println("<html>");
  94.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement