v12345vtm

waterproof errorcorrecting ultrasonic

Jul 26th, 2017
491
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.64 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <ESP8266WebServer.h>
  3. #include <EEPROM.h>
  4.  
  5. ESP8266WebServer server(80);
  6.  
  7. const char* ssid = "test12345"; //accespoint naam
  8. const char* passphrase = "test12345"; // code accespoint
  9. String st;
  10. String content;
  11. int statusCode;
  12. String versie = "inofile = waterproofUltrasoonNodemcu2";
  13. String esid = "";
  14.  
  15. String epass = "";
  16.  
  17.  
  18. String qsid = "";
  19. String qpass = "";
  20.  
  21.  
  22. //ultrasoon items
  23. #define echoPin 4 // Echo Pin van de ultrasoon hr-04 module
  24. #define trigPin 5 // Trigger Pin
  25. long duration, distance , vorigedistance ; // Duration used to calculate distance
  26.  
  27.  
  28. void setup() {
  29. delay(10);
  30. Serial.begin(115200);
  31. delay(200);
  32. EEPROM.begin(512);
  33. delay(200);
  34. Serial.println();
  35. Serial.println(versie);
  36. Serial.println("Startup");
  37.  
  38.  
  39. //ultasoon
  40.  
  41. pinMode(trigPin, OUTPUT);
  42. pinMode(echoPin, INPUT);
  43.  
  44. // Serial.println("Reading EEPROM ");
  45. // for (int i = 0; i < 512; i++)
  46. // {
  47. // char value = EEPROM.read(i);
  48. // Serial.print(i);
  49. // Serial.print("\t");
  50. // // Serial.print(value, DEC);
  51. // Serial.println(value);
  52. // delay(40);
  53. // }
  54.  
  55.  
  56. // read eeprom for ssid and pass
  57. Serial.println("Reading EEPROM ssid");
  58. esid = "";
  59. for (int i = 0; i < 32; ++i)
  60. {
  61. esid += char(EEPROM.read(i));
  62. Serial.println(esid);
  63. Serial.println(i);
  64. }
  65. Serial.print("SSID: ");
  66. Serial.println(esid);
  67. Serial.println("Reading EEPROM pass");
  68. epass = "";
  69. for (int i = 32; i < 96; ++i)
  70. {
  71. epass += char(EEPROM.read(i));
  72. Serial.println(epass);
  73. Serial.println(i);
  74. }
  75. Serial.print("PASS: ");
  76. Serial.println(epass);
  77.  
  78. Serial.println("debug esid: ");
  79. Serial.println(esid.c_str());
  80. Serial.println(epass.c_str());
  81.  
  82. if ( esid.length() > 1 ) {
  83.  
  84. Serial.println("debug2: ");
  85. Serial.println(esid.c_str());
  86. Serial.println(epass.c_str());
  87.  
  88. WiFi.begin(esid.c_str(), epass.c_str());
  89. if (testWifi()) {
  90. launchWeb(0);
  91. return;
  92. }
  93. }
  94. setupAP();
  95. }
  96.  
  97. bool testWifi(void) {
  98. int c = 0;
  99. Serial.println("Waiting for Wifi to connect");
  100. while ( c < 20 ) {
  101. if (WiFi.status() == WL_CONNECTED) {
  102. return true;
  103. }
  104. delay(500);
  105. Serial.print(WiFi.status());
  106. c++;
  107. }
  108. Serial.println("");
  109. Serial.println("Connect timed out, opening AP");
  110. return false;
  111. }
  112.  
  113. void launchWeb(int webtype) {
  114. Serial.println("");
  115. Serial.println("WiFi connected");
  116. Serial.print("Local IP: ");
  117. Serial.println(WiFi.localIP());
  118.  
  119. Serial.print("gekoppelde ssid: ");
  120. Serial.println(WiFi.SSID());
  121. Serial.print("SoftAP IP: ");
  122. Serial.println(WiFi.softAPIP());
  123.  
  124. if (webtype == 1) {
  125. Serial.println ("geen wifi gekend in eeprom , fabrieksreset");
  126. }
  127.  
  128. if (webtype == 0) {
  129. Serial.println ("wifi gekend in wifimodule");
  130. }
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137. createWebServer(webtype);
  138. // Start the server
  139. server.begin();
  140. Serial.println("Server started");
  141. }
  142.  
  143. void setupAP(void) {
  144. WiFi.mode(WIFI_STA);
  145. WiFi.disconnect();
  146. delay(100);
  147. int n = WiFi.scanNetworks();
  148. Serial.println("scan done");
  149. if (n == 0)
  150. Serial.println("no networks found");
  151. else
  152. {
  153. Serial.print(n);
  154. Serial.println(" networks found");
  155. for (int i = 0; i < n; ++i)
  156. {
  157. // Print SSID and RSSI for each network found
  158. Serial.print(i + 1);
  159. Serial.print(": ");
  160. Serial.print(WiFi.SSID(i));
  161. Serial.print(" (");
  162. Serial.print(WiFi.RSSI(i));
  163. Serial.print(")");
  164. Serial.println((WiFi.encryptionType(i) == ENC_TYPE_NONE) ? " " : "*");
  165. delay(10);
  166. }
  167. }
  168. Serial.println("");
  169. st = "<ol>";
  170. for (int i = 0; i < n; ++i)
  171. {
  172. // Print SSID and RSSI for each network found
  173. st += "<li>";
  174. st += WiFi.SSID(i);
  175. st += " (";
  176. st += WiFi.RSSI(i);
  177. st += ")";
  178. st += (WiFi.encryptionType(i) == ENC_TYPE_NONE) ? " " : "*";
  179. st += "</li>";
  180. }
  181. st += "</ol>";
  182. delay(100);
  183. WiFi.softAP(ssid, passphrase, 6);
  184. Serial.println("softap");
  185. launchWeb(1);
  186. //Serial.println("over");
  187. }
  188.  
  189. void createWebServer(int webtype)
  190. {
  191. if ( webtype == 1 ) {
  192. server.on("/", []() {
  193. IPAddress ip = WiFi.softAPIP();
  194. String ipStr = String(ip[0]) + '.' + String(ip[1]) + '.' + String(ip[2]) + '.' + String(ip[3]);
  195. content = "<!DOCTYPE HTML>\r\n<html>Hello from ESP8266 at accespoint ";
  196. content += ipStr;
  197. content += "<p>";
  198. content += "<br>";
  199. content += versie;
  200. content += "<br>";
  201. content += "<br> ultrasoon = ";
  202. content += distance;
  203. content += "<br>";
  204.  
  205.  
  206. content += " <a href=""";
  207. content += "/ultrasoonmeting";
  208. content += """ target=""_blank""> live meting ultrasoon</a> " ;
  209.  
  210.  
  211. content += " <a href=""";
  212. content += "/cleareeprom";
  213. content += """ target=""_blank""> clear de eprom</a> " ;
  214. content += st;
  215. content += "</p><form method='get' action='setting'><label>SSID: </label><input name='ssid' length=32><input name='pass' length=64><input type='submit'></form>";
  216. content += "</html>";
  217. server.send(200, "text/html", content);
  218. });
  219.  
  220.  
  221. server.on("/setting", []() {
  222. qsid = server.arg("ssid");
  223. qpass = server.arg("pass");
  224. if (qsid.length() > 0 && qpass.length() > 0) {
  225. Serial.println("clearing eeprom");
  226. for (int i = 0; i < 96; ++i)
  227. {
  228. EEPROM.write(i, 0);
  229. }
  230. Serial.println(qsid);
  231. Serial.println("");
  232. Serial.println(qpass);
  233. Serial.println("");
  234. Serial.println("writing eeprom ssid:");
  235. for (int i = 0; i < qsid.length(); ++i)
  236. {
  237. EEPROM.write(i, qsid[i]);
  238. Serial.print("Wrote: ");
  239. Serial.println(qsid[i]);
  240. }
  241. Serial.println("writing eeprom pass:");
  242. for (int i = 0; i < qpass.length(); ++i)
  243. {
  244. EEPROM.write(32 + i, qpass[i]);
  245. Serial.print("Wrote: ");
  246. Serial.println(qpass[i]);
  247. }
  248. EEPROM.commit();
  249. content = "{\"Success\":\"saved to eeprom... reset to boot into new wifi\"}";
  250. statusCode = 200;
  251. } else {
  252. content = "{\"Error\":\"404 not found\"}";
  253. statusCode = 404;
  254. Serial.println("Sending 404");
  255. }
  256. server.send(statusCode, "application/json", content);
  257. });
  258. }
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272. else if (webtype == 0) {
  273. server.on("/", []() {
  274. IPAddress ip = WiFi.localIP();
  275. String ipStr = String(ip[0]) + '.' + String(ip[1]) + '.' + String(ip[2]) + '.' + String(ip[3]);
  276. content = "<!DOCTYPE HTML>\r\n<html>Hello from ESP8266 at client verbonden met router";
  277. content += ipStr;
  278. content += "<p>";
  279. content += "<br> ultrasoon = ";
  280. content += distance;
  281. content += "<br>";
  282. content += "<br>";
  283. content += versie;
  284. content += "<br>";
  285. content += st;
  286. content += " <a href=""";
  287. content += "/cleareeprom";
  288. content += """ target=""_blank""> clear de eprom</a> " ;
  289.  
  290. content += " <a href=""";
  291. content += "/ultrasoonmeting";
  292. content += """ target=""_blank""> live meting ultrasoon</a> " ;
  293.  
  294.  
  295. content += "</p><form method='get' action='setting'><label>SSID: </label><input name='ssid' length=32><input name='pass' length=64><input type='submit'></form>";
  296. content += "</html>";
  297. server.send(200, "text/html", content);
  298. });
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306. server.on("/setting", []() {
  307. String qsid = server.arg("ssid");
  308. String qpass = server.arg("pass");
  309. if (qsid.length() > 0 && qpass.length() > 0) {
  310. Serial.println("clearing eeprom");
  311. for (int i = 0; i < 96; ++i) {
  312. EEPROM.write(i, 0);
  313. }
  314. Serial.println(qsid);
  315. Serial.println("");
  316. Serial.println(qpass);
  317. Serial.println("");
  318.  
  319. Serial.println("writing eeprom ssid:");
  320. for (int i = 0; i < qsid.length(); ++i)
  321. {
  322. EEPROM.write(i, qsid[i]);
  323. Serial.print("Wrote: ");
  324. Serial.println(qsid[i]);
  325. }
  326. Serial.println("writing eeprom pass:");
  327. for (int i = 0; i < qpass.length(); ++i)
  328. {
  329. EEPROM.write(32 + i, qpass[i]);
  330. Serial.print("Wrote: ");
  331. Serial.println(qpass[i]);
  332. }
  333. EEPROM.commit();
  334. content = "{\"Success\":\"saved to eeprom... reset to boot into new wifi\"}";
  335. statusCode = 200;
  336. } else {
  337. content = "{\"Error\":\"404 not found\"}";
  338. statusCode = 404;
  339. Serial.println("Sending 404");
  340. }
  341. server.send(statusCode, "application/json", content);
  342. });
  343.  
  344.  
  345.  
  346.  
  347.  
  348. //////////////
  349.  
  350. server.on("/ultrasoonmeting", []() {
  351.  
  352. content = "<!DOCTYPE HTML>\r\n<html>meting ultrasoon";
  353.  
  354. content = "<!DOCTYPE html PUBLIC ""-//W3C//DTD HTML 3.2//EN""> " ;
  355. content += "<html>" ;
  356. content += " <body> " ;
  357. content += " <script> " ;
  358. content += " setTimeout(function(){ location.reload() }, 1000); " ;
  359. content += " </script> " ;
  360. content += "<br>";
  361. content += versie;
  362. content += "<br>";
  363. content += "<p>";
  364. content += "<br> (elke sec word pagina herladen : ultrasoon = ";
  365. content += distance;
  366. content += "<br>";
  367.  
  368.  
  369. content += "waternivo:<progress value=\"";
  370. content += distance ;
  371. content += "\" max=\"400\"></progress>";
  372. content += "<p><strong>Note:</strong> The progress tag is not supported in Internet Explorer 9 and earlier versions.</p>";
  373.  
  374. long verschil = 400 - distance;
  375. content += "waternivo:<progress value=\"" + String(verschil) + "\" max=\"400\"></progress>";
  376.  
  377.  
  378. // content += "</p><form method='get' action='setting'><label>SSID: </label><input name='ssid' length=32><input name='pass' length=64><input type='submit'></form>";
  379. content += " </body> " ;
  380. content += " </html> " ;
  381. server.send(200, "text/html", content);
  382. });
  383.  
  384. ///////////////
  385.  
  386.  
  387.  
  388. server.on("/cleareeprom", []() {
  389. content = "<!DOCTYPE HTML>\r\n<html>";
  390. content += "<p>Clearing the EEPROM</p></html>";
  391. server.send(200, "text/html", content);
  392. Serial.println("clearing eeprom");
  393. qsid = "vermoost";
  394. qpass = "kapot"; // anders onthoud hij de oude string locate en kan je niet resetten
  395. Serial.println("manueel vith wiss eeprom ssid:");
  396. for (int i = 0; i < qsid.length(); ++i)
  397. {
  398. EEPROM.write(i, qsid[i]);
  399. Serial.print("Wrote: ");
  400. Serial.println(qsid[i]);
  401. }
  402. Serial.println("manueel wis vith writing eeprom pass:");
  403. for (int i = 0; i < qpass.length(); ++i)
  404. {
  405. EEPROM.write(32 + i, qpass[i]);
  406. Serial.print("Wrote: ");
  407. Serial.println(qpass[i]);
  408. }
  409. EEPROM.commit();
  410. });
  411.  
  412.  
  413. }
  414. }
  415.  
  416. void loop() {
  417. server.handleClient();
  418. delay(500);
  419. meetultrasoon();
  420. delay(500);
  421. }
  422.  
  423.  
  424.  
  425.  
  426.  
  427.  
  428.  
  429. ////////////
  430.  
  431. void meetultrasoon() {
  432. /* The following trigPin/echoPin cycle is used to determine the
  433. distance of the nearest object by bouncing soundwaves off of it. */
  434. // digitalWrite(trigPin, LOW);
  435. // delayMicroseconds(20);
  436. digitalWrite(trigPin, HIGH);
  437. delayMicroseconds(40);
  438. digitalWrite(trigPin, LOW);
  439. duration = pulseIn(echoPin, HIGH);
  440. //Calculate the distance (in cm) based on the speed of sound.
  441. distance = duration / 58.2;
  442.  
  443. long verschil = abs(distance - vorigedistance); //absolute waarde verschil + of -
  444. // Serial.print("verschil = ");
  445. // Serial.println(verschil);
  446. if (verschil > 5) {
  447. //hermeet als je teveel verschil hebt met de vorige meting
  448. delay(500);
  449. digitalWrite(trigPin, HIGH);
  450. delayMicroseconds(40);
  451. digitalWrite(trigPin, LOW);
  452. duration = pulseIn(echoPin, HIGH);
  453. distance = duration / 58.2; //nieuwe meting
  454. Serial.println("hermeting gedaan wegens plots een verschil van meer dan 5cm");
  455. }
  456. vorigedistance = distance ; // opslaan oude waarde
  457.  
  458.  
  459.  
  460. if (distance == 0)
  461. {
  462. //onze seconden wachter moet op 15staan eer we naaar de cloud sturen
  463. distance = vorigedistance; //de vorige waarde hergebruiken
  464. Serial.println ("error0 ");
  465. }
  466. //volgens datasheet zien we maar tot 400cm ver , maar bij storing krijgen we soms waardes als 5645 , alles meer dan 400 smijten we weg
  467. if (distance > 400)
  468. {
  469. //onze seconden wachter moet op 15staan eer we naaar de cloud sturen
  470. distance = vorigedistance; //de vorige waarde hergebruiken
  471. Serial.println ("error400");
  472. }
  473.  
  474. Serial.print(distance);
  475. Serial.println (" cm");
  476. }
  477. ///////////////
Advertisement
Add Comment
Please, Sign In to add comment