naivxnaivet

D0 ADD

Jun 3rd, 2020
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.21 KB | None | 0 0
  1. //WITH RESISTOR ,
  2. //pinMode(buttonPin, INPUT);
  3. //digitalWrite(buttonPin, HIGH); //CLOSE
  4. //digitalWrite(buttonPin, LOW); //OPEN
  5. //
  6. //WITHOUT RESISTOR
  7. //pinMode(buttonPin, INPUT_PULLUP);
  8. //digitalWrite(buttonPin, LOW); //CLOSE
  9. //digitalWrite(buttonPin, HIGH); //OPEN
  10.  
  11. //15 promo1
  12. //20 promo2
  13. //45 promo3
  14. //90 promo4
  15. //110 promo5
  16. //8 perPisoMin
  17.  
  18. #include <LiquidCrystal_I2C.h>
  19. LiquidCrystal_I2C lcd(0x27, 20, 4);
  20.  
  21. bool displayMain = true;
  22.  
  23. bool editMode = false;
  24. unsigned long previousMillis = 0; // will store last time LED was updated
  25. const long interval = 1000;
  26.  
  27. int counter3;
  28.  
  29. #include<EEPROM.h>
  30. #define ep_limit 13
  31.  
  32. int epAdd1 = 0;
  33. int epAdd2 = 30;
  34. int epAdd3 = 60;
  35. int epAdd4 = 90;
  36. int epAdd5 = 120;
  37. int epAdd6 = 150;
  38. int epAdd11 = 180;
  39. int epAdd12 = 210;
  40. int epAdd13 = 230;
  41. int epAdd14 = 250;
  42. int epAdd15 = 300;
  43.  
  44. int coinCount = 0;
  45. int perPisoMin; //ILANG MINUTES PER PISO, EDITABLE BY FUCKING EEPROM
  46. int computation = perPisoMin * 60;
  47. int hourPromo = 3600UL;
  48. int inBetween;
  49. //promo prices
  50. int promo1 = 0;
  51. int promo2 = 0;
  52. int promo3 = 0;
  53. int promo4 = 0;
  54. int promo5 = 0;
  55.  
  56. //promo hour
  57. int promoOne = 0;
  58. int promoTwo = 0;
  59. int promoThree = 0;
  60. int promoFour = 0;
  61. int promoFive = 0;
  62. int promoComp;
  63. long promoHour = 3600UL;
  64. unsigned long promoFinal = 0;
  65. unsigned long promoTime;
  66.  
  67.  
  68. #define buttonPinTes D3
  69. #define buttonPinRes D0
  70. #define buttonPinSes D7
  71. int buttonStateRes;
  72. int buttonStateTes;
  73. int buttonStateSes;
  74. int lastButtonStateTes = LOW;
  75. int lastButtonStateRes = LOW;
  76. int lastButtonStateSes = LOW;
  77. unsigned long lastDebounceTimeRes = 0;
  78. unsigned long lastDebounceTimeTes = 0;
  79. unsigned long lastDebounceTimeSes = 0;
  80. unsigned long debounceDelay = 0;
  81. bool resSelected = false;
  82.  
  83. unsigned int editRates;
  84. unsigned int counter2;
  85.  
  86. #include <ESP8266TelnetClient.h>
  87. #include <ESP8266WiFi.h>
  88. #include <ESP8266WiFiMulti.h>
  89. #include <Wire.h>
  90. #include <LiquidCrystal_I2C.h>
  91.  
  92. IPAddress mikrotikRouterIp (192, 168, 22, 1);
  93.  
  94. const char* user = "admin";
  95. const char* pwd = "Secreto#020202";
  96. const char* ssid = "MikroTik Wifi";
  97. const char* password = "Secreto#020202";
  98. String M_command;
  99.  
  100. ESP8266WiFiMulti WiFiMulti;
  101. WiFiClient client;
  102.  
  103. ESP8266telnetClient tc(client);
  104.  
  105.  
  106. int costc = 300; //
  107.  
  108. int y = 0;
  109. int hourr = 0;
  110. int minn = 0;
  111. int secc = 0;
  112. int dayy = 0;
  113. int temp = 0;
  114. int startTime = 0;
  115. int userCode = 0;
  116. int cantWaitTheCode = 0;
  117. int checkTime = 0;
  118. char const *c;
  119.  
  120. void ICACHE_RAM_ATTR coinAccept()
  121. {
  122. y = 0;
  123. coinCount++;
  124. digitalWrite(LED_BUILTIN, LOW);
  125. }
  126.  
  127. void ICACHE_RAM_ATTR buttonPush()
  128. {
  129. if (coinCount > 0)
  130. //if(coinCounnt >= 0)
  131. {
  132. cantWaitTheCode = 1;
  133. }
  134. //startTime = millis(); // decoy = 1;
  135. digitalWrite(LED_BUILTIN, LOW);
  136. }
  137.  
  138.  
  139.  
  140. void setup()
  141. {
  142. digitalWrite(16, LOW);
  143. lcd.begin();
  144. lcd.backlight();
  145. promo1 = ep_read(epAdd1);
  146. promo2 = ep_read(epAdd2);
  147. promo3 = ep_read(epAdd3);
  148. promo4 = ep_read(epAdd4);
  149. promo5 = ep_read(epAdd5);
  150. perPisoMin = ep_read(epAdd6);
  151. promoOne = ep_read(epAdd11);
  152. promoTwo = ep_read(epAdd12);
  153. promoThree = ep_read(epAdd13);
  154. promoFour = ep_read(epAdd14);
  155. promoFive = ep_read(epAdd15);
  156.  
  157. Serial.begin(9600);
  158. pinMode(buttonPinRes, INPUT);
  159. pinMode(buttonPinTes, INPUT_PULLUP);
  160. pinMode(buttonPinSes, INPUT_PULLUP);
  161.  
  162. WiFi.mode(WIFI_STA);
  163. WiFiMulti.addAP(ssid, password);
  164. attachInterrupt(D5, coinAccept, RISING);
  165. attachInterrupt(D6, buttonPush, RISING);
  166. pinMode(D5, INPUT);
  167. pinMode(D5, INPUT);
  168. // pinMode(LED_BUILTIN, OUTPUT);
  169. //pinMode(D7, OUTPUT);
  170.  
  171. lcd.setCursor(5, 0);
  172. lcd.print(F("WELCOME TO"));
  173. lcd.setCursor(0, 1);
  174. lcd.print(F("JHUNDECK HOTSPOT"));
  175. digitalWrite(LED_BUILTIN, HIGH);
  176.  
  177. delay(1000);
  178.  
  179. // Serial.println();
  180. // Serial.println();
  181. // Serial.println("Wait for WI-Fi...");
  182. while (WiFiMulti.run() != WL_CONNECTED)
  183. {
  184. // Serial.print(".");
  185. delay(500);
  186. }
  187.  
  188. // Serial.println("");
  189. // Serial.println("WiFi connected");
  190. // Serial.print("IP Address : ");
  191. // Serial.println(WiFi.localIP());
  192. // Serial.println("Connecting....");
  193.  
  194.  
  195. tc.setPromptChar('>');
  196. //manual login pagka may problema
  197. char key = 0;
  198. Serial.println("\r\npress enter to begin:");
  199. do
  200. {
  201. key = Serial.read();
  202. Serial.println(key);
  203. }
  204.  
  205. //automatic login
  206. while (key <= 0);
  207.  
  208. tc.login(mikrotikRouterIp, user, pwd);
  209. computation = perPisoMin * 60;
  210. }
  211.  
  212.  
  213.  
  214. void loop()
  215. {
  216. displayMainMessage();
  217. computation = perPisoMin * 60;
  218. editPresets();
  219. readBtnRes();
  220. readBtnTes();
  221. readBtnSes();
  222.  
  223.  
  224. digitalWrite(LED_BUILTIN, HIGH);
  225. if (coinCount > 0)
  226. {
  227. displayMain = false;
  228. check();
  229. startTime = millis();
  230. lcd.setCursor(0, 0);
  231. lcd.print(F("coins : " ));
  232. lcd.print(coinCount);
  233. lcd.print(F(" "));
  234. temp = promoFinal; // ilang minute yung piso
  235. hourr = floor(temp / 3600);
  236. temp %= 3600; //temp = temp%3600
  237. minn = floor (temp / 60);
  238. secc = temp % 60;
  239. dayy = floor(hourr / 24);
  240. lcd.setCursor(0, 1);
  241. lcd.print(dayy);
  242. lcd.print(F("D:"));
  243. lcd.print(hourr);
  244. lcd.print(F("H:"));
  245. lcd.print(minn);
  246. lcd.print(F("M:"));
  247. lcd.print(secc);
  248. lcd.print(F("S"));
  249.  
  250. checkTime = millis() - startTime;
  251. if (cantWaitTheCode == 1 && coinCount > 0)
  252. {
  253. displayMain = false;
  254. check();
  255. temp = promoFinal;
  256. Serial.println("the value is:");
  257. Serial.println(coinCount);
  258. userCode = random(10000, 99999); //ITO YUNG PAPALITAN, LEFT = LOWEST, RIGHT = UPPER
  259. M_command += "/ip hotspot user add name=";
  260. M_command += userCode;
  261. M_command += " limit-uptime=";
  262. M_command += temp;
  263. int command_len = M_command.length() + 1;
  264. char M_F_command[command_len];
  265. M_command.toCharArray(M_F_command, command_len);
  266. tc.sendCommand(M_F_command);
  267.  
  268. lcd.setCursor(0, 0);
  269. lcd.print(F(" JHUNDECK WIFI "));
  270. lcd.setCursor(0, 1);
  271. lcd.print(F("code : "));
  272. lcd.print(userCode);
  273. lcd.print(" ");
  274. delay(15000);
  275. M_command = "";
  276. memset(M_F_command , 0, sizeof(M_F_command ));
  277. temp = 0;
  278. coinCount = 0;
  279. startTime = 0;
  280. cantWaitTheCode = 0;
  281. displayMain = true;
  282. }
  283. }
  284. }
  285. void readBtnRes() {
  286. int reading = digitalRead(buttonPinRes);
  287. if (reading != lastButtonStateRes) {
  288. lastDebounceTimeRes = millis();
  289. }
  290. if ((millis() - lastDebounceTimeRes) > debounceDelay) {
  291. if (reading != buttonStateRes) {
  292. buttonStateRes = reading;
  293. if (buttonStateRes == LOW)
  294. {
  295. counter++;
  296. }
  297. }
  298. }
  299. lastButtonStateRes = reading;
  300. }
  301.  
  302. void readBtnTes()
  303. {
  304. int reading = digitalRead(buttonPinTes);
  305. if (reading != lastButtonStateTes) {
  306. lastDebounceTimeTes = millis();
  307. }
  308. if ((millis() - lastDebounceTimeTes) > debounceDelay) {
  309. if (reading != buttonStateTes) {
  310. buttonStateTes = reading;
  311. if (buttonStateTes == LOW)
  312. {
  313. editRates++;
  314.  
  315. // '' Serial.println(editRates);
  316. }
  317. }
  318. }
  319. lastButtonStateTes = reading;
  320. }
  321.  
  322.  
  323. void readBtnSes() {
  324. int reading = digitalRead(buttonPinSes);
  325. if (reading != lastButtonStateSes) {
  326. lastDebounceTimeRes = millis();
  327. }
  328. if ((millis() - lastDebounceTimeSes) > debounceDelay) {
  329. if (reading != buttonStateSes) {
  330. buttonStateSes = reading;
  331. if (buttonStateSes == LOW)
  332. {
  333. counter2--;
  334. Serial.println(counter2);
  335.  
  336. }
  337. }
  338. }
  339. lastButtonStateSes = reading;
  340.  
  341.  
  342. }
  343.  
  344.  
  345. void check()
  346. {
  347. promo1 = ep_read(epAdd1);
  348. promo2 = ep_read(epAdd2);
  349. promo3 = ep_read(epAdd3);
  350. promo4 = ep_read(epAdd4);
  351. promo5 = ep_read(epAdd5);
  352. perPisoMin = ep_read(epAdd6);
  353. promoOne = ep_read(epAdd11);
  354. promoTwo = ep_read(epAdd12);
  355. promoThree = ep_read(epAdd13);
  356. promoFour = ep_read(epAdd14);
  357. promoFive = ep_read(epAdd15);
  358.  
  359. computation = perPisoMin * 60;
  360. if (coinCount < promo1)
  361. {
  362. promoFinal = coinCount * computation;
  363. costc = promoFinal;
  364. }
  365.  
  366. else if (coinCount >= promo1 && coinCount < promo2)
  367. {
  368. promoTime = promoOne * promoHour;
  369. promoComp = coinCount % promo1; //40 yung coin count, pumasok sa coin count na 30 tas may sampong piso na per rate na in between
  370. inBetween = promoComp * computation; //REMAINDER MULTIPLY MO SA PRESYO PER PISO TAS ADD MO YUNG REMAINDER
  371. promoFinal = inBetween + promoTime;
  372. costc = promoFinal;
  373. }
  374. else if (coinCount >= promo2 && coinCount < promo3)
  375. {
  376. promoTime = promoTwo * promoHour;
  377. promoComp = coinCount % promo2; //40 yung coin count, pumasok sa coin count na 30 tas may sampong piso na per rate na in between
  378. inBetween = promoComp * computation; //REMAINDER MULTIPLY MO SA PRESYO PER PISO TAS ADD MO YUNG REMAINDER
  379. promoFinal = inBetween + promoTime;
  380. costc = promoFinal;
  381. }
  382. else if (coinCount >= promo3 && coinCount < promo4)
  383. {
  384. promoTime = promoThree * promoHour;
  385. promoComp = coinCount % promo3; //40 yung coin count, pumasok sa coin count na 30 tas may sampong piso na per rate na in between
  386. inBetween = promoComp * computation; //REMAINDER MULTIPLY MO SA PRESYO PER PISO TAS ADD MO YUNG REMAINDER
  387. promoFinal = inBetween + promoTime;
  388. costc = promoFinal;
  389. }
  390. else if (coinCount >= promo4 && coinCount < promo5)
  391. {
  392. promoComp = coinCount % promo4; //40 yung coin count, pumasok sa coin count na 30 tas may sampong piso na per rate na in between
  393. inBetween = promoComp * computation; //REMAINDER MULTIPLY MO SA PRESYO PER PISO TAS ADD MO YUNG REMAINDER
  394. promoTime = promoFour * promoHour;
  395. promoFinal = inBetween + promoTime; //FINAL
  396. costc = promoFinal;
  397. }
  398. else if (coinCount >= promo5)
  399. {
  400. promoComp = coinCount % promo5; //40 yung coin count, pumasok sa coin count na 30 tas may sampong piso na per rate na in between
  401. inBetween = promoComp * computation; //REMAINDER MULTIPLY MO SA PRESYO PER PISO TAS ADD MO YUNG REMAINDER
  402. promoTime = promoFive * promoHour;
  403. promoFinal = inBetween + promoTime; //FINAL
  404. costc = promoFinal;
  405. }
  406. temp = promoFinal;
  407. }
  408.  
  409.  
  410. int ep_read(int add)
  411. {
  412. unsigned int v = 0;
  413. unsigned int i;
  414. for (i = 0; i < ep_limit; i++)
  415. {
  416. v = v + EEPROM.read(add);
  417. add++;
  418. }
  419. return v;
  420. }
  421.  
  422. void ep_write(int add, const int val)
  423. {
  424. int v = val;
  425. unsigned int i;
  426. for (i = 0; i < ep_limit; i++) {
  427. if (255 < v) {
  428. v = v - 255;
  429. EEPROM.write(add, 255);
  430. } else {
  431. EEPROM.write(add, v);
  432. v = 0;
  433. }
  434. add++;
  435. }
  436. }
  437.  
  438. void editPresets()
  439. {
  440.  
  441. switch (editRates)
  442. {
  443.  
  444. case 0:
  445. // Serial.println("WAITING FOR COMMAND");
  446. editMode = false;
  447. displayMain = true;
  448. break;
  449.  
  450. case 1:
  451. editMode = true;
  452. displayMain = false;
  453. Serial.println("EDIT PISO PER MINUTE");
  454. Serial.println(perPisoMin);
  455. lcd.setCursor(0, 0);
  456. lcd.print("EDIT MODE ");
  457. lcd.setCursor(0, 1);
  458. lcd.print("MINUTE PER PISO");
  459. lcd.setCursor(0, 2);
  460. lcd.print(counter2);
  461.  
  462. readBtnRes();
  463. readBtnSes();
  464. perPisoMin = counter2;
  465. ep_write(epAdd6, perPisoMin);
  466. counter3 = 0;
  467. break;
  468.  
  469. case 2:
  470. editMode = true;
  471. displayMain = false;
  472. Serial.println("EDIT PROMO 1 PRICE ");
  473. Serial.println(promo1);
  474. lcd.setCursor(0, 0);
  475. lcd.print("EDIT MODE ");
  476. lcd.setCursor(0, 1);
  477. lcd.print("PROMO 1 PRICE");
  478. lcd.setCursor(0, 2);
  479.  
  480. lcd.print(counter2);
  481.  
  482. readBtnSes();
  483. readBtnRes();
  484. promo1 = counter2;
  485. ep_write(epAdd1, promo1);
  486. break;
  487.  
  488. case 3:
  489. Serial.println("EDIT PROMO 1 HOURS");
  490. Serial.println(promoOne);
  491. lcd.setCursor(0, 0);
  492. lcd.print("EDIT MODE ");
  493. lcd.setCursor(0, 1);
  494. lcd.print("PROMO 1 HOURS");
  495. lcd.setCursor(0, 2);
  496. lcd.print(counter2);
  497.  
  498. editMode = true;
  499. readBtnSes();
  500. readBtnRes();
  501. promoOne = counter2;
  502. ep_write(epAdd11, promoOne);
  503. break;
  504.  
  505.  
  506. case 4:
  507. Serial.println("EDIT PROMO 2 PRICE");
  508. Serial.println(promo2);
  509. lcd.setCursor(0, 0);
  510. lcd.print("EDIT MODE ");
  511. lcd.setCursor(0, 1);
  512. lcd.print("PROMO 2 PRICE");
  513. lcd.setCursor(0, 2);
  514. lcd.print(counter2);
  515. editMode = true;
  516. editMode = true;
  517. readBtnSes();
  518. readBtnRes();
  519. promo2 = counter2;
  520. ep_write(epAdd2, promo2);
  521. break;
  522. //
  523. case 5:
  524. Serial.println("EDIT PROMO 2 HOURS");
  525. Serial.println(promoTwo);
  526. lcd.setCursor(0, 0);
  527. lcd.print("EDIT MODE");
  528. lcd.setCursor(0, 1);
  529. lcd.print("PROMO 2 HOURS");
  530. lcd.setCursor(0, 2);
  531. lcd.print(counter2);
  532. editMode = true;
  533. editMode = true;
  534. readBtnSes();
  535. readBtnRes();
  536. promoTwo = counter2;
  537. ep_write(epAdd12, promoTwo);
  538. break;
  539. case 6:
  540. Serial.println("EDIT PROMO 3 PRICE");
  541. Serial.println(promo3);
  542. lcd.setCursor(0, 0);
  543. lcd.print("EDIT MODE");
  544. lcd.setCursor(0, 1);
  545. lcd.print("PROMO 3 PRICE");
  546. lcd.setCursor(0, 2);
  547. lcd.print(counter2);
  548. editMode = true;
  549. editMode = true;
  550. readBtnSes();
  551. readBtnRes();
  552. promo3 = counter2;
  553. ep_write(epAdd3, promo3);
  554. break;
  555. //
  556. case 7:
  557. Serial.println("EDIT PROMO 3 HOURS");
  558. Serial.println(promoThree);
  559. lcd.setCursor(0, 0);
  560. lcd.print("EDIT MODE");
  561. lcd.setCursor(0, 1);
  562. lcd.print("PROMO 3 HOURS");
  563. lcd.setCursor(0, 2);
  564. lcd.print(counter2);
  565. editMode = true;
  566. editMode = true;
  567. readBtnSes();
  568. readBtnRes();
  569. promoThree = counter2;
  570. ep_write(epAdd13, promoThree);
  571. break;
  572.  
  573. case 8:
  574. Serial.println("EDIT PROMO 4 PRICE");
  575. Serial.println(promo4);
  576. lcd.setCursor(0, 0);
  577. lcd.print("EDIT MODE");
  578. lcd.setCursor(0, 1);
  579. lcd.print("PROMO 4 PRICE");
  580. lcd.setCursor(0, 2);
  581. lcd.print(counter2);
  582. editMode = true;
  583. editMode = true;
  584. readBtnSes();
  585. readBtnRes();
  586. promo4 = counter2;
  587. ep_write(epAdd4, promo4);
  588. break;
  589.  
  590. case 9:
  591. Serial.println("EDIT PROMO 4 HOURS");
  592. Serial.println(promoFour);
  593. lcd.setCursor(0, 0);
  594. lcd.print("EDIT MODE");
  595. lcd.setCursor(0, 1);
  596. lcd.print("PROMO 4 HOURS");
  597. lcd.setCursor(0, 2);
  598. lcd.print(counter2);
  599. editMode = true;
  600. editMode = true;
  601. readBtnSes();
  602. readBtnRes();
  603. promoFour = counter2;
  604. ep_write(epAdd14, promoFour);
  605. break;
  606.  
  607. case 10:
  608. Serial.println("EDIT PROMO 5 PRICE");
  609. Serial.println(promo5);
  610. lcd.setCursor(0, 0);
  611. lcd.print("EDIT MODE");
  612. lcd.setCursor(0, 1);
  613. lcd.print("PROMO 5 PRICE ");
  614. lcd.setCursor(0, 2);
  615. lcd.print(counter2);
  616. editMode = true;
  617. editMode = true;
  618. readBtnSes();
  619. readBtnRes();
  620. promo5 = counter2;
  621. ep_write(epAdd5, promo5);
  622. break;
  623.  
  624. case 11:
  625. Serial.println("EDIT PROMO 5 HOURS");
  626. lcd.setCursor(0, 0);
  627. lcd.print("EDIT MODE");
  628. lcd.setCursor(0, 1);
  629. lcd.print("PROMO 5 HOURS");
  630. lcd.setCursor(0, 2);
  631. lcd.print(counter2);
  632. editMode = true;
  633. Serial.println(promoFive);
  634. editMode = true;
  635. readBtnSes();
  636. readBtnRes();
  637. promoFive = counter2;
  638. ep_write(epAdd15, promoFive);
  639. break;
  640.  
  641. case 12:
  642. Serial.println("SAVED");
  643. lcd.print("Saved");
  644. delay(2000);
  645. lcd.clear();
  646. promo1 = ep_read(epAdd1);
  647. promo2 = ep_read(epAdd2);
  648. promo3 = ep_read(epAdd3);
  649. promo4 = ep_read(epAdd4);
  650. promo5 = ep_read(epAdd5);
  651. perPisoMin = ep_read(epAdd6);
  652. promoOne = ep_read(epAdd11);
  653. promoTwo = ep_read(epAdd12);
  654. promoThree = ep_read(epAdd13);
  655. promoFour = ep_read(epAdd14);
  656. promoFive = ep_read(epAdd15);
  657. editRates = 0;
  658. break;
  659. }
  660. computation = perPisoMin * 60;
  661. }
  662.  
  663. void displayMainMessage()
  664. {
  665. if (displayMain == true)
  666. {
  667. lcd.setCursor(0, 0);
  668. lcd.print(F(" JHUNDECK WIFI "));
  669. lcd.setCursor(0, 1);
  670. lcd.print(F("Pls. Insert Coin/s"));
  671. lcd.setCursor(0, 2);
  672. lcd.print(coinCount);
  673. }
  674. }
Add Comment
Please, Sign In to add comment