Advertisement
j0h

TeensyEtcher

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