Advertisement
j0h

TeensyEtchaSketch(work_in_progress)

j0h
Nov 12th, 2021
1,340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.15 KB | None | 0 0
  1.  
  2. #include <SPI.h>
  3. #include <NativeEthernet.h>
  4. #include <SD.h>
  5.  
  6. File dataFile;
  7.  
  8. byte mac[] = {
  9.   0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
  10. };
  11. IPAddress ip(10, 1, 0, 177);
  12. EthernetServer server(80);
  13. //Function defs
  14. void startPage(EthernetClient client);
  15. void endPage(EthernetClient client);
  16. void svgHeader(EthernetClient client);
  17. void svgFooter(EthernetClient client);
  18. void polyLineBegin(EthernetClient client);
  19. void polyLineEnd(EthernetClient client);
  20.  
  21. int Reset = 4;     //programticaly reset the arduino  
  22. int potX = A0;    
  23. int potY = A1;    
  24.  
  25. int sensorValX = 0;  
  26. int sensorValY = 0;  
  27.  
  28. int oldX = 0;
  29. int oldY = 0;
  30.    
  31. void setup() {
  32.  
  33.   delay(5000);  //you want this delay. reason: tl;dr
  34.   Serial.begin(9600);
  35.   Ethernet.begin(mac, ip);
  36.  
  37. if (Ethernet.linkStatus() == LinkOFF) {
  38.     Serial.println("Ethernet cable is not connected.");
  39.   }
  40.  
  41.   // start the server
  42.   server.begin();
  43.   delay(100);
  44.   Serial.print("server is at ");
  45.   Serial.println(Ethernet.localIP());
  46.  
  47.   Serial.print("Initializing SD card...");
  48.  
  49.   if (!SD.begin(BUILTIN_SDCARD)) {
  50.     Serial.println("SD Card init failed!");
  51.     while (1);
  52.   }
  53. /****SD CARD and Ethernet intialized*****/  
  54. //if data file (on SD Card) Doesn't exist, create it.
  55. //if data file (on SD Card) does exist errase it and create a new one
  56.  
  57.  
  58.   if (SD.exists("datalog.txt")) {
  59.     Serial.println("datalog.txt exists: \n Removing it.");
  60.     SD.remove("datalog.txt");
  61.   } else {
  62.     Serial.println("datalog.txt doesn't exist.");
  63.   }
  64.  
  65.   // open a new file and immediately close it:
  66.   Serial.println("Creating datalog.txt...");
  67.   dataFile = SD.open("datalog.txt", FILE_WRITE);
  68.   dataFile.close();
  69.  
  70.   // Check to see if the file exists:
  71.   if (SD.exists("datalog.txt")) {
  72.     Serial.println("datalog.txt exists.");
  73.   } else {
  74.     Serial.println("datalog.txt wasn't created.");
  75. }
  76. } //end setup
  77.  
  78. void loop() {
  79.   int sensorValX = analogRead(8);
  80.   int sensorValY = analogRead(9);
  81.   String dataString = "";
  82.   int threshold =3;
  83.   int xVariance = abs(sensorValX-oldX);
  84.   int yVariance = abs(sensorValY-oldY);
  85.  
  86. if (xVariance >=threshold && yVariance >= threshold ||xVariance >=threshold || yVariance >=threshold){
  87. //if sensor data is greater than threshold, write data to the sd card, then close the file
  88. SD.open("datalog.txt", FILE_WRITE);
  89. dataString += String(sensorValX);
  90. dataString += ",";
  91. dataString += String(sensorValY);
  92.    dataFile = SD.open("datalog.txt", FILE_WRITE);
  93.  
  94.   // if the file is available, write to it:
  95.   if (dataFile) {
  96.     dataFile.println(dataString);
  97.     dataFile.close();
  98. //clear dataString
  99. dataString="";
  100.   } else {
  101.     // if the file isn't open, pop up an error:
  102.     Serial.println("error opening datalog.txt");
  103.   }
  104. dataFile.close();
  105.  
  106. } //end threshold
  107.  
  108.  // listen for incoming clients
  109.   EthernetClient client = server.available();
  110.    if (client) {
  111.     boolean currentLineIsBlank = true;
  112.     while (client.connected()) {
  113.        if (client.available()) {
  114.           char c = client.read();
  115.        // Serial.write(c);   //tells about the client connection
  116.         if (c == '\n' && currentLineIsBlank) {
  117.             startPage(client);  
  118.  svgHeader(client);          
  119.    polyLineBegin(client);
  120.    polyLineEnd(client);
  121.  svgFooter(client);  
  122.          
  123.           endPage(client);
  124.           break;
  125.         }
  126.         if (c == '\n') {
  127.           // you're starting a new line
  128.           currentLineIsBlank = true;
  129.         } else if (c != '\r') {
  130.           // you've gotten a character on the current line
  131.           currentLineIsBlank = false;
  132.         }
  133.       }//end avail
  134.     } //end conect
  135.     // give the web browser time to receive the data
  136.     delay(1);
  137.     // close the connection:
  138.     client.stop();
  139.   } //end client
  140. }  //end loop  
  141.  
  142. void startPage(EthernetClient client){
  143.      // send a standard http response header
  144.           client.println("HTTP/1.1 200 OK");
  145.           client.println("Content-Type: text/html");
  146.           client.println("Connection: close");  // the connection will be closed after completion of the response
  147.           client.println("Refresh: 5");  // refresh the page automatically every 5 sec
  148.           client.println();
  149.           client.println("<!DOCTYPE HTML>");
  150.           client.println("<html>");
  151.   }
  152.  
  153.  void endPage(EthernetClient client){
  154.   client.println("</body>");
  155.   client.println("</html>");
  156.   }
  157.  
  158.  
  159. void polyLineBegin(EthernetClient client){
  160.   client.print("<polyline points=\""); //x,y points go here
  161.   //read data from SD card
  162.     File dataFile = SD.open("datalog.txt");
  163.  
  164.   // if the file is available, read it:
  165.   if (dataFile) {
  166.     while (dataFile.available()) {
  167.       Serial.write(dataFile.read());
  168.       client.write(dataFile.read());
  169.     }
  170.     dataFile.close();
  171.   }
  172. }
  173.  
  174. void polyLineEnd(EthernetClient client){
  175.    client.println ("0, 0\" stroke=\"red\" fill=\"transparent\" stroke-width=\"5\"/>");  //a dirty hack to avoid dealing with 1 last trailing comma.
  176.   }
  177.  
  178. void svgHeader(EthernetClient client){
  179. client.println("<svg width=\"1023\" height=\"1023\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\">");
  180.   };  
  181.  
  182. void svgFooter(EthernetClient client){
  183. client.println ("</svg>");
  184.   }
  185.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement