Advertisement
j0h

pots change colour

j0h
Nov 10th, 2021
1,379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.05 KB | None | 0 0
  1. /*
  2.   Web Server
  3. Running on teensy4.1 potentiometers on analog pin 8,9
  4. changing the pot value changes the colour values in the
  5. svg image.
  6.  
  7.  */
  8.  
  9. #include <SPI.h>
  10. #include <NativeEthernet.h>
  11.  
  12. // Enter a MAC address and IP address for your controller below.
  13. // The IP address will be dependent on your local network:
  14. byte mac[] = {
  15.   0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
  16. };
  17. IPAddress ip(10, 1, 0, 177);
  18.  
  19. // Initialize the Ethernet server library
  20. // with the IP address and port you want to use
  21. // (port 80 is default for HTTP):
  22. EthernetServer server(80);
  23.  
  24. void setup() {
  25.   // You can use Ethernet.init(pin) to configure the CS pin
  26.   //Ethernet.init(10);  // Most Arduino shields
  27.   //Ethernet.init(5);   // MKR ETH shield
  28.   //Ethernet.init(0);   // Teensy 2.0
  29.   //Ethernet.init(20);  // Teensy++ 2.0
  30.   //Ethernet.init(15);  // ESP8266 with Adafruit Featherwing Ethernet
  31.   //Ethernet.init(33);  // ESP32 with Adafruit Featherwing Ethernet
  32.  
  33.   // Open serial communications and wait for port to open:
  34.   Serial.begin(9600);
  35.   Serial.println("Ethernet WebServer Example");
  36.  
  37.   // start the Ethernet connection and the server:
  38.   Ethernet.begin(mac, ip);
  39.  
  40.   // Check for Ethernet hardware present
  41.   if (Ethernet.hardwareStatus() == EthernetNoHardware) {
  42.     Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
  43.     while (true) {
  44.       delay(1); // do nothing, no point running without Ethernet hardware
  45.     }
  46.   }
  47.   if (Ethernet.linkStatus() == LinkOFF) {
  48.     Serial.println("Ethernet cable is not connected.");
  49.   }
  50.  
  51.   // start the server
  52.   server.begin();
  53.   Serial.print("server is at ");
  54.   Serial.println(Ethernet.localIP());
  55. }
  56.  
  57.  
  58. void loop() {
  59.   // listen for incoming clients
  60.   EthernetClient client = server.available();
  61.   if (client) {
  62.     Serial.println("new client");
  63.     // an http request ends with a blank line
  64.     boolean currentLineIsBlank = true;
  65.     while (client.connected()) {
  66.       if (client.available()) {
  67.         char c = client.read();
  68.         Serial.write(c);
  69.         // if you've gotten to the end of the line (received a newline
  70.         // character) and the line is blank, the http request has ended,
  71.         // so you can send a reply
  72.         if (c == '\n' && currentLineIsBlank) {
  73.           // send a standard http response header
  74.           client.println("HTTP/1.1 200 OK");
  75.           client.println("Content-Type: text/html");
  76.           client.println("Connection: close");  // the connection will be closed after completion of the response
  77.           client.println("Refresh: 5");  // refresh the page automatically every 5 sec
  78.           client.println();
  79.           client.println("<!DOCTYPE HTML>");
  80.           client.println("<html>");
  81.           // output the value of each analog input pin
  82.          
  83.             int sensorReading0 = analogRead(8);
  84.               int sensorReading1 = analogRead(9);
  85. 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=\"#");
  86. client.print(sensorReading0);
  87. client.print("\" /> <circle cx=\"150\" cy=\"100\" r=\"90\" fill=\"#");
  88. client.print(sensorReading1);
  89. client.print("\" />\n</svg>");
  90.             client.println("<br />");
  91.          
  92. client.println("</br>");
  93. //Serial.println("<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=\"black\" />\n <circle cx=\"150\" cy=\"100\" r=\"90\" fill=\"blue\" />\n</svg>");
  94.  
  95.  
  96.          
  97.           client.println("</html>");
  98.           break;
  99.         }
  100.         if (c == '\n') {
  101.           // you're starting a new line
  102.           currentLineIsBlank = true;
  103.         } else if (c != '\r') {
  104.           // you've gotten a character on the current line
  105.           currentLineIsBlank = false;
  106.         }
  107.       }
  108.     }
  109.     // give the web browser time to receive the data
  110.     delay(1);
  111.     // close the connection:
  112.     client.stop();
  113.     Serial.println("client disconnected");
  114.   }
  115. }
  116.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement