Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.60 KB | None | 0 0
  1.  
  2. #include <wifi.h>
  3. #include <ESPAsyncWebServer.h>
  4.  
  5. bool btWS_Fellover = true;
  6.  
  7. // Replace with your network credentials
  8. const char *ssid = "COOLGUY";
  9. const char *password = "12345678"; //must be 8 characters long
  10.  
  11.  
  12. IPAddress local_ip(192,168,128,1);
  13. IPAddress gateway(192,168,1,1);
  14. IPAddress subnet(255,255,255,0);
  15.  
  16. AsyncWebServer server(80);
  17.  
  18.  
  19. const char MAIN_page[] PROGMEM = R"=====(
  20. <!DOCTYPE html>
  21. <html>
  22.  
  23. <head>
  24. <meta charset="utf-8">
  25. <title>RaB1 Wifi server R9a</title>
  26. <link rel="shortcut icon" href="#"/>
  27. </head>
  28.  
  29.  
  30.  
  31. <style>
  32. :root {
  33. --main-bg-color: gray;
  34.  
  35. }
  36. html {
  37. margin: 0;
  38. padding: 0;
  39.  
  40. }
  41.  
  42. body{
  43. width: 80%;
  44. margin: 0 auto;
  45. font: 100% Arial, Helvetica, sans-serif;
  46. padding: 1em 1em;
  47. background: white;
  48.  
  49. }
  50. .form input {
  51. position: absolute;
  52. left: -9999px;
  53. }
  54.  
  55.  
  56. .Setup{
  57. position:absolute;
  58.  
  59. top:1%;
  60. left:10%;
  61. height:80%;
  62. width:80%;
  63. background:white;
  64. }
  65.  
  66. .Forward{
  67. position:absolute;
  68.  
  69. top:1%;
  70. left:10%;
  71. height:20%;
  72. width:80%;
  73. background: var(--main-bg-color,black);
  74. }
  75.  
  76. #Forward:checked + .Forward{
  77. background: green;
  78. }
  79.  
  80. .Forward:after {
  81. font: 380% Arial, Helvetica, sans-serif;
  82. font-weight: bold;
  83. content: 'Forward';
  84. color: white;
  85. position: relative;
  86. top: 25%;
  87. left: 35%;
  88. }
  89.  
  90. .Left{
  91. position:absolute;
  92.  
  93. top:22%;
  94. left:10%;
  95. height:20%;
  96. width:39%;
  97. background:var(--main-bg-color,black);
  98. }
  99.  
  100. #Left:checked + .Left{
  101. background: green;
  102. }
  103.  
  104. .Left:after {
  105. font: 380% Arial, Helvetica, sans-serif;
  106. font-weight: bold;
  107. content: 'Left';
  108. color: white;
  109. position: relative;
  110. top: 25%;
  111. left: 35%;
  112. }
  113.  
  114. .Right{
  115. position:absolute;
  116.  
  117. top:22%;
  118. right:10%;
  119. height:20%;
  120. width:39%;
  121. background:var(--main-bg-color,black);
  122. }
  123.  
  124. #Right:checked + .Right{
  125. background: green;
  126. }
  127.  
  128. .Right:after {
  129. font: 380% Arial, Helvetica, sans-serif;
  130. font-weight: bold;
  131. content: 'Right';
  132. color: white;
  133. position: relative;
  134. top: 25%;
  135. left: 30%;
  136. }
  137.  
  138. .Reverse{
  139. position:absolute;
  140.  
  141. top:43%;
  142. left:10%;
  143. height:20%;
  144. width:80%;
  145. background:var(--main-bg-color,black);
  146. }
  147.  
  148. #Reverse:checked + .Reverse{
  149. background: green;
  150. }
  151.  
  152. .Reverse:after {
  153. font: 380% Arial, Helvetica, sans-serif;
  154. font-weight: bold;
  155. content: 'Reverse';
  156. color: white;
  157. position: relative;
  158. top: 25%;
  159. left: 35%;
  160. }
  161.  
  162. .Stop{
  163. position:absolute;
  164.  
  165. top:64%;
  166. left:10%;
  167. height:20%;
  168. width:80%;
  169. background:red;
  170. }
  171.  
  172. #Stop:checked + .Stop{
  173. background: orange;
  174. }
  175.  
  176. .Stop:after {
  177. font: 380% Arial, Helvetica, sans-serif;
  178. font-weight: bold;
  179. content: 'Stop';
  180. color: white;
  181. position: relative;
  182. top: 25%;
  183. left: 40%;
  184. }
  185. .Datum{
  186. position:absolute;
  187. top:84%;
  188. left:10%;
  189. font: 100% Arial, Helvetica, sans-serif;
  190. font-weight: bold;
  191. }
  192.  
  193. .Graph{
  194. position:absolute;
  195. top:90%;
  196. left:10%;
  197. font: 200% Arial, Helvetica, sans-serif;
  198. font-weight: bold;
  199.  
  200. }
  201.  
  202.  
  203. </style>
  204.  
  205. <body>
  206.  
  207.  
  208. <div class="form">
  209. <div>
  210. <input type="radio" onclick="sendData(1)" id="Forward" value="1" name="radio" />
  211. <label class="Forward" for="Forward"></label>
  212. </div>
  213. <div>
  214. <input type="radio" onclick="sendData(2)" id="Left" value="2" name="radio" />
  215. <label class="Left" for="Left"></label>
  216. </div>
  217. <div>
  218. <input type="radio" onclick="sendData(3)" id="Right" value="3" name="radio" />
  219. <label class="Right" for="Right"></label>
  220. </div>
  221. <div>
  222. <input type="radio" onclick="sendData(4)" id="Reverse" value="3" name="radio" />
  223. <label class="Reverse" for="Reverse"></label>
  224. </div>
  225. <div>
  226. <input type="radio" onclick="sendData(0)" id="Stop" value="4" name="radio" checked="checked"/>
  227. <label class="Stop" for="Stop"></label>
  228. </div>
  229.  
  230.  
  231. <div>
  232. <label class = "Datum">
  233. <!-- Gravity Distance <br> <span id="SensorData">-- -- -- --</span> -->
  234.  
  235. SensorData - Gravity: <span id="GravityMeas">0</span>
  236. Distance - : <span id="DistanceMeas">0</span>
  237.  
  238. </label>
  239. </div>
  240.  
  241. <div>
  242. <label class = "Graph">
  243. <canvas id="myCanvas" width= "10" height="50" onclick="sendData(5)" style="border:1px solid #d3d3d3;">
  244. Your browser does not support the HTML5 canvas tag.</canvas>
  245. </label>
  246. </div>
  247.  
  248. </div>
  249.  
  250. <script>
  251.  
  252. var c = document.getElementById("myCanvas");
  253. var ctx = c.getContext("2d");
  254. var XAxis = 0;
  255. var XClear = 0;
  256. var YAxis = 0;
  257. var YAxisPrevious = 0;
  258. var canWidth = 0;
  259. var canHeight = 0;
  260. var lineColour = "red";
  261. var lineColourChanged = 0;
  262. var Flipped;
  263.  
  264. var counttmr = 0;
  265. ctx.moveTo(0,0);
  266. ctx.canvas.width = window.innerWidth*0.8;
  267. ctx.canvas.height = window.innerHeight*0.08;
  268. canWidth = ctx.canvas.width;
  269. canHeight = ctx.canvas.height;
  270. ctx.beginPath();
  271.  
  272.  
  273. function sendData(ButtonPressed) {
  274. var xhttp = new XMLHttpRequest();
  275.  
  276. if(ButtonPressed == 5)
  277. {
  278. if(lineColour == "red")
  279. {
  280. lineColour = "green";
  281. lineColourChanged = 1;
  282. }
  283. else
  284. {
  285. lineColour = "red";
  286. lineColourChanged = 1;
  287. }
  288. }
  289.  
  290.  
  291. xhttp.open("GET", "setPressedButton?StateButton="+ButtonPressed, true);
  292. xhttp.send();
  293. }
  294.  
  295.  
  296. setInterval(function() {
  297. // Call a function repetatively with 2 Second interval
  298. getData();
  299. }, 250); //250mSeconds update rate
  300.  
  301. function getData() {
  302. var xhttp = new XMLHttpRequest();
  303. var WorkingSensorData;
  304.  
  305.  
  306. counttmr = counttmr + 1;
  307. if(counttmr >= 100)
  308. {
  309. counttmr = 0;
  310. //document.documentElement.style.setProperty('--main-bg-color', 'blue');
  311. }
  312.  
  313. xhttp.onreadystatechange = function() {
  314. if (this.readyState == 4 && this.status == 200) {
  315. WorkingSensorData = (this.responseText).split("; ");
  316.  
  317. YAxis = parseInt(WorkingSensorData[0],10);
  318.  
  319. document.getElementById("GravityMeas").innerHTML = WorkingSensorData[0];
  320. document.getElementById("DistanceMeas").innerHTML = WorkingSensorData[1];
  321.  
  322. }
  323. };
  324.  
  325.  
  326. xhttp.open("GET", "readSensorData", true);
  327. xhttp.send();
  328.  
  329.  
  330.  
  331. ctx.beginPath();
  332. ctx.lineWidth = "5";
  333. ctx.strokeStyle = "white";
  334. ctx.moveTo((XAxis+4),0);
  335. ctx.lineTo((XAxis+4),200);
  336. ctx.stroke();
  337. if(lineColourChanged == 1)
  338. {
  339. ctx.fillStyle = lineColour;
  340. lineColourChanged = 0;
  341. ctx.fillRect((XAxis-9),(YAxis-5),10,10);
  342. }
  343. ctx.beginPath();
  344.  
  345. ctx.moveTo(XAxis,YAxisPrevious);
  346. XAxis = XAxis + 1;
  347.  
  348. if(XAxis > canWidth)
  349. {
  350. XAxis = 0;
  351.  
  352. ctx.moveTo(0,YAxis);
  353.  
  354. }
  355.  
  356.  
  357. ctx.lineWidth = "2";
  358. ctx.strokeStyle = lineColour;
  359. ctx.lineTo(XAxis,YAxis);
  360. YAxisPrevious = YAxis;
  361. ctx.stroke();
  362.  
  363. }
  364.  
  365. </script>
  366. </body>
  367. </html>
  368. </script>
  369. </body>
  370. </html>
  371.  
  372. )=====";
  373.  
  374.  
  375.  
  376. unsigned char ucButtonState;
  377. unsigned char ucWorkingButtonState;
  378.  
  379. unsigned int uiDistance;
  380. double dWS_PIDInput;
  381. String buttonState = "0";
  382.  
  383.  
  384. String DistanceMeas = "0";
  385.  
  386. String SensorData;
  387.  
  388.  
  389. void setupWEbServer(void)
  390. {
  391.  
  392.  
  393.  
  394. Serial.print(F("Configuring access point..."));
  395.  
  396.  
  397. Serial.print(F("Connecting to "));
  398. Serial.println(ssid);
  399.  
  400. WiFi.softAP(ssid, password);
  401. WiFi.softAPConfig(local_ip, gateway, subnet);
  402. delay(100);
  403.  
  404.  
  405. IPAddress myIP = WiFi.softAPIP();
  406. Serial.print(F("AP IP address: "));
  407. Serial.println(myIP);
  408.  
  409.  
  410. server.on("/", HTTP_GET, [](AsyncWebServerRequest *request)
  411. {
  412. request->send(200, "text/html", MAIN_page);
  413. });
  414.  
  415.  
  416. server.on("/setPressedButton", HTTP_GET, [](AsyncWebServerRequest *request)
  417. {
  418.  
  419. AsyncWebParameter* p;
  420.  
  421. if(request->hasParam("StateButton"))
  422. p = request->getParam("StateButton");
  423. // Serial.printf("GET[%s]: %s\n", p->name().c_str(), p->value().c_str());
  424.  
  425. String t_state = p->value().c_str();//server->arg("StateButton"); //Refer xhttp.open("GET", "setButton?StateButton="+buttonPressed, true);
  426. buttonState = p->value().c_str();
  427.  
  428. ucButtonState = buttonState.toInt();
  429.  
  430.  
  431. request->send(200, "text/plain", buttonState); //Send web page
  432.  
  433. });
  434.  
  435. server.on("/readSensorData", HTTP_GET, [](AsyncWebServerRequest *request)
  436. {
  437.  
  438. SensorData = String(dWS_PIDInput) + "; " + String(uiDistance);
  439.  
  440.  
  441. request->send(200, "text/plain", SensorData); //Send ADC value only to client ajax request
  442.  
  443. });
  444.  
  445.  
  446. server.begin();
  447. Serial.println(F("HTTP server started"));
  448.  
  449.  
  450. Serial.println(F(""));
  451. }
  452.  
  453. void loopWEBServerButtonresponce(void)
  454. {
  455.  
  456.  
  457.  
  458. switch(ucButtonState)
  459. {
  460. case 0:
  461. default:
  462. {
  463.  
  464. //Serial.println("Stop");
  465. ucWorkingButtonState = 0;
  466. ucButtonState = 9;
  467.  
  468.  
  469. break;
  470. }
  471. case 1:
  472. {
  473.  
  474. // Serial.println("Forward");
  475. ucButtonState = 9;
  476. ucWorkingButtonState = 1;
  477.  
  478. break;
  479. }
  480. case 2:
  481. {
  482.  
  483. //Serial.println("Left");
  484. ucButtonState = 9;
  485. ucWorkingButtonState = 2;
  486. break;
  487. }
  488. case 3:
  489. {
  490.  
  491. //Serial.println("Right");
  492. ucButtonState = 9;
  493. ucWorkingButtonState = 3;
  494. break;
  495. }
  496. case 4:
  497. {
  498.  
  499.  
  500. // Serial.println("Reverse");
  501. ucButtonState = 9;
  502. ucWorkingButtonState = 4;
  503. break;
  504. }
  505. case 5: //toggle servo
  506. {
  507.  
  508. ucButtonState = 9;
  509.  
  510.  
  511. break;
  512. }
  513. case 9:
  514. {
  515.  
  516. ucButtonState = 9;
  517. break;
  518. }
  519. }
  520.  
  521. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement