Advertisement
Guest User

Untitled

a guest
Feb 18th, 2020
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.29 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <Ethernet.h>
  3. #include <SD.h>
  4. #include <Bridge.h>
  5.  
  6. #define SS_SD_CARD 4
  7. #define SS_ETHERNET 20
  8.  
  9. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  10. IPAddress ip(192, 168, 0, 2);
  11. IPAddress subnet (255, 255, 255, 0);
  12. IPAddress gateway(192, 168, 0, 1);
  13. EthernetServer server(80);
  14.  
  15. File webPage;
  16. int RED = 8; //LED to turn on/off
  17. int tval = 0;
  18. String HTTP_req; // stores the HTTP request
  19.  
  20. void setup()
  21. {
  22. pinMode(SS_SD_CARD, OUTPUT);
  23. pinMode(SS_ETHERNET, OUTPUT);
  24. digitalWrite(SS_SD_CARD, HIGH);
  25. digitalWrite(SS_ETHERNET, LOW);
  26.  
  27. Serial.begin(9600);
  28. while (!Serial) {
  29. ; // wait for serial port to connect. Needed for native USB port only
  30. }
  31. Serial.println("Ethernet WebServer Example");
  32.  
  33. // start the Ethernet connection and the server:
  34. Ethernet.begin(mac, ip);
  35.  
  36. // Check for Ethernet hardware present
  37. if (Ethernet.hardwareStatus() == EthernetNoHardware) {
  38. Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
  39. while (true) {
  40. delay(1); // do nothing, no point running without Ethernet hardware
  41. }
  42. }
  43. if (Ethernet.linkStatus() == LinkOFF) {
  44. Serial.println("Ethernet cable is not connected.");
  45. }
  46. //start the server
  47. server.begin();
  48. Serial.print("server is at ");
  49. Serial.println(Ethernet.localIP());
  50.  
  51. // initialize SD card
  52. Serial.println("Initializing SD card...");
  53. if (!SD.begin(4)) {
  54. Serial.println("ERROR - SD card initialization failed!");
  55. return; // init failed
  56. }
  57. Serial.println("SUCCESS - SD card initialized.");
  58. // check for index.htm file
  59. if (!SD.exists("index.htm")) {
  60. Serial.println("ERROR - Can't find index.htm file!");
  61. return; // can't find index file
  62. }
  63. Serial.println("SUCCESS - Found index.htm file.");
  64. HTTP_req = "";
  65. //Set LED to output
  66. pinMode(RED, OUTPUT);
  67. }
  68.  
  69. void loop()
  70. {
  71. // listen for incoming clients
  72. EthernetClient client = server.available();
  73. if (client) {
  74. Serial.println("new client");
  75. // an http request ends with a blank line
  76. boolean currentLineIsBlank = true;
  77. while (client.connected()) {
  78. if (client.available()) {
  79. char c = client.read(); // read 1 byte (character) from client
  80. // if you've gotten to the end of the line (received a newline
  81. // character) and the line is blank, the http request has ended,
  82. // so you can send a reply
  83. if ( HTTP_req.length() < 80)
  84. HTTP_req += c; // save the HTTP request 1 char at a time
  85. if (c == '\n' && currentLineIsBlank) {
  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");
  90. client.println();
  91. // send web page
  92. if (HTTP_req.indexOf("ajaxrefresh") >= 0 ) {
  93. ajaxRequest(client); //update the analog values
  94. break;
  95. }
  96. else if (HTTP_req.indexOf("ledstatus") >= 0 ) {
  97. ledChangeStatus(client); //change the LED state
  98. break;
  99. }
  100. else {
  101. tval = HTTP_req.indexOf("tval");
  102. throttleChangeStatus(client);
  103. Serial.print(HTTP_req);
  104. webPage = SD.open("index.htm"); // open web page file
  105. if (webPage) {
  106. while (webPage.available()) {
  107. client.write(webPage.read()); // send web page to client
  108. }
  109. webPage.close();
  110. }
  111. break;
  112. }
  113. if (c == '\n') {
  114. // you're starting a new line
  115. currentLineIsBlank = true;
  116. } else if (c != '\r') {
  117. // you've gotten a character on the current line
  118. currentLineIsBlank = false;
  119. }
  120. }
  121. }
  122. }
  123. // give the web browser time to receive the data
  124. delay(1);
  125. // close the connection:
  126. client.stop();
  127. HTTP_req = "";
  128. Serial.println("client disconnected");
  129. } // end if (client)
  130. }
  131.  
  132. // send the state of the switch to the web browser
  133. void ajaxRequest(EthernetClient client)
  134. {
  135. for (int analogChannel = 0; analogChannel < 5; analogChannel++) {
  136. int sensorReading = analogRead(analogChannel);
  137. client.print("analog input ");
  138. client.print(analogChannel);
  139. client.print(" is ");
  140. client.print(sensorReading);
  141. client.println("<br />");
  142. }
  143. }
  144.  
  145. void ledChangeStatus(EthernetClient client)
  146. {
  147. int state = digitalRead(RED);
  148. Serial.println(state);
  149. if (state == 1) {
  150. digitalWrite(RED, LOW);
  151. client.print("OFF");
  152. }
  153. else {
  154. digitalWrite(RED, HIGH);
  155. client.print("ON");
  156. }
  157. }
  158.  
  159. void throttleChangeStatus(EthernetClient client)
  160. {
  161. Serial.println(tval);
  162. client.print(tval);
  163. }
  164.  
  165.  
  166. <!DOCTYPE HTML>
  167. <html lang="en">
  168. <h1>Analogue Values</h1>
  169. <div id="analoge_data">Arduino analog input values loading.....</div>
  170. <h1>Arduino LED Status</h1>
  171. <div>
  172. <span id="led_status">
  173. Off
  174. </span> | <button onclick="changeLEDStatus()">Change Status</button>
  175. </div>
  176. <h1>Throttle Value</h1>
  177. <span id="Throttle">NOT RECEIVED</span>
  178. <script>
  179. window.addEventListener("gamepadconnected", (event) => {
  180. console.log("A gamepad connected:");
  181. console.log(event.gamepad);
  182. controller = event.gamepad;
  183. });
  184. window.addEventListener("gamepaddisconnected", (event) => {
  185. console.log("A gamepad disconnected:");
  186. console.log(event.gamepad);
  187. });
  188. var gamepads = navigator.getGamepads();
  189. var intervalID = window.setInterval(getInput, 10);
  190.  
  191. function getInput() {
  192. tVal = controller.axes[2];
  193. nocache = "&nocache=" + Math.random() * 10;
  194. var request = new XMLHttpRequest();
  195. request.onreadystatechange = function () {
  196. if (this.readyState == 4) {
  197. if (this.status == 200) {
  198. if (this.responseText != null) {
  199. document.getElementById("Throttle").innerHTML = this.responseText;
  200. }
  201. }
  202. }
  203. }
  204. console.log(request.open("GET", "?tval" + tVal + nocache, true));
  205. request.send(null);
  206. }
  207.  
  208. </script>
  209. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement