Advertisement
j0h

teensyServer

j0h
Nov 7th, 2021 (edited)
425
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.40 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <NativeEthernet.h>
  3. /**Function defs**/
  4. void svgHeader();
  5. void svgFooter();
  6. void polyLineBegin();
  7. void polyLineEnd();
  8. void startClient();
  9. void endClient();
  10.  
  11.  
  12.  
  13.  
  14. byte mac[] = {
  15.   0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
  16. };
  17. //IPAddress ip(192, 168, 0, 177);
  18. IPAddress ip(10, 1, 0, 177);
  19. EthernetServer server(80);
  20.  
  21. void setup() {
  22.     delay(5000); //you want this here belive me. reason: tl;dr
  23.   // Open serial communications and wait for port to open:
  24.   Serial.begin(9600);
  25.   while (!Serial) {
  26.     ; // wait for serial port to connect. Needed for native USB port only
  27.   }
  28.   Serial.println("Ethernet WebServer Example");
  29.  
  30.   // start the Ethernet connection and the server:
  31.   Ethernet.begin(mac, ip);
  32.  
  33.   // Check for Ethernet hardware present
  34.   if (Ethernet.hardwareStatus() == EthernetNoHardware) {
  35.     Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
  36.     while (true) {
  37.       delay(1); // do nothing, no point running without Ethernet hardware
  38.     }
  39.   }
  40.   if (Ethernet.linkStatus() == LinkOFF) {
  41.     Serial.println("Ethernet cable is not connected.");
  42.   }
  43.  
  44.   // start the server
  45.   server.begin();
  46.   Serial.print("server is at ");
  47.   Serial.println(Ethernet.localIP());
  48. }
  49.  
  50. /*Global Vars*
  51. * located here as to wait for defintion of client as related to server in
  52. * setup function
  53. */
  54. EthernetClient client = server.available();
  55. int potX = A8;    
  56. int potY = A9;    
  57. int sensorValX = 0;  
  58. int sensorValY = 0;  
  59. int oldX = 0;
  60. int oldY = 0;
  61.  
  62. void loop() {
  63.     // listen for incoming clients
  64. //  EthernetClient client = server.available();
  65.   if (client) {
  66.     // an http request ends with a blank line
  67.     boolean currentLineIsBlank = true;
  68.     while (client.connected()) {
  69.        if (client.available()) {
  70.           char c = client.read();
  71.           Serial.write(c);
  72.         // if you've gotten to the end of the line (received a newline
  73.         // character) and the line is blank, the http request has ended,
  74.         // so you can send a reply
  75.         if (c == '\n' && currentLineIsBlank) {
  76.           startClient();
  77.           // output the value of each analog input pin        
  78.   sensorValX = analogRead(potX);
  79.   sensorValY = analogRead(potY);
  80. /*dont draw if pots not changing    
  81.   Ah but the sensors are noisy, so thresholding
  82. */
  83. int threshold =3;
  84. int xVariance = abs(sensorValX-oldX);
  85. int yVariance = abs(sensorValY-oldY);
  86. if (((xVariance >=threshold) && (yVariance >= threshold)) ||xVariance >=threshold || yVariance >=threshold){
  87.     oldX=sensorValX;
  88.     oldY=sensorValY;
  89.    
  90.     client.print(sensorValX);
  91.     client.print(",");
  92.     client.print(sensorValY);
  93.     client.print(",");
  94.     delay(100);
  95.  
  96.     }else{
  97.        ; //dont do shi.
  98.     }
  99. endClient();
  100. }
  101. //          client.println("<a href=\"/?button1on\">Save Drawing</a>");
  102.           break;
  103.         }
  104.       }
  105.     }
  106.     // give the web browser time to receive the data
  107.     delay(1);
  108.     // close the connection:
  109.     client.stop();
  110.     Serial.println("client disconnected");
  111.   } // end loop
  112.  
  113. /**Functions**/
  114. void startClient(){
  115.  
  116.           // send a standard http response header
  117.           client.println("HTTP/1.1 200 OK");
  118.           client.println("Content-Type: text/html");
  119.           client.println("Connection: close");  // the connection will be closed after completion of the response
  120.           client.println("Refresh: 5");  // refresh the page automatically every 5 sec
  121.           client.println();
  122.           client.println("<!DOCTYPE HTML>");
  123.           client.println("<html>");
  124.           client.println("<body>");
  125.  
  126.   }
  127. void endClient(){
  128.   client.println("</body>");
  129.   client.println("</html>");
  130.  
  131.   }
  132.  
  133. void polyLineBegin(){
  134.   //Serial.println ("<polyline points=\""); //x,y points go here
  135.   client.println ("<polyline points=\"");
  136.   }
  137.  
  138. void polyLineEnd(){
  139.   // Serial.println ("0, 0\" stroke=\"red\" fill=\"transparent\" stroke-width=\"5\"/>");  //a dirty hack to avoid dealing with 1 last trailing comma.
  140.    client.println ("0, 0\" stroke=\"red\" fill=\"transparent\" stroke-width=\"5\"/>");
  141.   }
  142.  
  143. void svgHeader(){
  144. //Serial.println("<svg width=\"1023\" height=\"1023\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\">");
  145. client.println("<svg width=\"1023\" height=\"1023\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\">");
  146.   };  
  147.  
  148. void svgFooter(){
  149. //Serial.println ("</svg>");
  150.   client.println("</svg>");
  151.   };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement