naivxnaivet

Jhundeck With Promo edit

Jun 2nd, 2020
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.35 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 15
  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 = 2800;
  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 D7
  70. #define buttonPinSes A0
  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. lcd.begin();
  143. lcd.backlight();
  144. promo1 = ep_read(epAdd1);
  145. promo2 = ep_read(epAdd2);
  146. promo3 = ep_read(epAdd3);
  147. promo4 = ep_read(epAdd4);
  148. promo5 = ep_read(epAdd5);
  149. perPisoMin = ep_read(epAdd6);
  150. promoOne = ep_read(epAdd11);
  151. promoTwo = ep_read(epAdd12);
  152. promoThree = ep_read(epAdd13);
  153. promoFour = ep_read(epAdd14);
  154. promoFive = ep_read(epAdd15);
  155.  
  156. Serial.begin(9600);
  157. pinMode(buttonPinRes, INPUT_PULLUP);
  158. pinMode(buttonPinTes, INPUT_PULLUP);
  159. pinMode(buttonPinSes, INPUT);
  160.  
  161. WiFi.mode(WIFI_STA);
  162. WiFiMulti.addAP(ssid, password);
  163. attachInterrupt(D5, coinAccept, RISING);
  164. attachInterrupt(D6, buttonPush, RISING);
  165. pinMode(D5, INPUT);
  166. pinMode(D5, INPUT);
  167. pinMode(LED_BUILTIN, OUTPUT);
  168. //pinMode(D7, OUTPUT);
  169.  
  170. lcd.setCursor(5, 0);
  171. lcd.print(F("WELCOME TO"));
  172. lcd.setCursor(0, 1);
  173. lcd.print(F("JHUNDECK HOTSPOT"));
  174. digitalWrite(LED_BUILTIN, HIGH);
  175.  
  176. delay(1000);
  177.  
  178. // Serial.println();
  179. // Serial.println();
  180. // Serial.println("Wait for WI-Fi...");
  181. while (WiFiMulti.run() != WL_CONNECTED)
  182. {
  183. // Serial.print(".");
  184. delay(500);
  185. }
  186.  
  187. // Serial.println("");
  188. // Serial.println("WiFi connected");
  189. // Serial.print("IP Address : ");
  190. // Serial.println(WiFi.localIP());
  191. // Serial.println("Connecting....");
  192.  
  193.  
  194. tc.setPromptChar('>');
  195. //manual login pagka may problema
  196. char key = 0;
  197. Serial.println("\r\npress enter to begin:");
  198. do
  199. {
  200. key = Serial.read();
  201. Serial.println(key);
  202. }
  203.  
  204. //automatic login
  205. while (key <= 0);
  206.  
  207. tc.login(mikrotikRouterIp, user, pwd);
  208. computation = perPisoMin * 60;
  209. }
  210.  
  211.  
  212.  
  213. void loop()
  214. {
  215. displayMainMessage();
  216. computation = perPisoMin * 60;
  217. editPresets();
  218. readBtnRes();
  219. readBtnTes();
  220. readBtnSes();
  221.  
  222.  
  223. digitalWrite(LED_BUILTIN, HIGH);
  224. if (coinCount > 0)
  225. {
  226. displayMain = false;
  227. check();
  228. startTime = millis();
  229. lcd.setCursor(0, 0);
  230. lcd.print(F("coins : " ));
  231. lcd.print(coinCount);
  232. lcd.print(F(" "));
  233. temp = promoFinal; // ilang minute yung piso
  234. hourr = floor(temp / 3600);
  235. temp %= 3600; //temp = temp%3600
  236. minn = floor (temp / 60);
  237. secc = temp % 60;
  238. dayy = floor(hourr / 24);
  239. lcd.setCursor(0, 1);
  240. lcd.print(dayy);
  241. lcd.print(F("D:"));
  242. lcd.print(hourr);
  243. lcd.print(F("H:"));
  244. lcd.print(minn);
  245. lcd.print(F("M:"));
  246. lcd.print(secc);
  247. lcd.print(F("S"));
  248.  
  249. checkTime = millis() - startTime;
  250. if (cantWaitTheCode == 1 && coinCount > 0)
  251. {
  252. Serial.println("the value is:");
  253. Serial.println(coinCount);
  254. userCode = random(10000, 99999); //ITO YUNG PAPALITAN, LEFT = LOWEST, RIGHT = UPPER
  255. M_command += "/ip hotspot user add name=";
  256. M_command += userCode;
  257. M_command += " limit-uptime=";
  258. M_command += temp;
  259. int command_len = M_command.length() + 1;
  260. char M_F_command[command_len];
  261. M_command.toCharArray(M_F_command, command_len);
  262. tc.sendCommand(M_F_command);
  263.  
  264. lcd.setCursor(0, 0);
  265. lcd.print(F(" JHUNDECK WIFI "));
  266. lcd.setCursor(0, 1);
  267. lcd.print(F("code : "));
  268. lcd.print(userCode);
  269. lcd.print(" ");
  270. delay(15000);
  271. M_command = "";
  272. memset(M_F_command , 0, sizeof(M_F_command ));
  273. temp = 0;
  274. coinCount = 0;
  275. startTime = 0;
  276.  
  277.  
  278.  
  279.  
  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. counter2++;
  296. Serial.println(counter2);
  297.  
  298.  
  299. }
  300. }
  301. }
  302. lastButtonStateRes = reading;
  303. }
  304.  
  305. void readBtnTes()
  306. {
  307. int reading = digitalRead(buttonPinTes);
  308. if (reading != lastButtonStateTes) {
  309. lastDebounceTimeTes = millis();
  310. }
  311. if ((millis() - lastDebounceTimeTes) > debounceDelay) {
  312. if (reading != buttonStateTes) {
  313. buttonStateTes = reading;
  314. if (buttonStateTes == LOW)
  315. {
  316. editRates++;
  317.  
  318. // '' Serial.println(editRates);
  319. }
  320. }
  321. }
  322. lastButtonStateTes = reading;
  323. }
  324.  
  325.  
  326. void readBtnSes() {
  327. int reading = digitalRead(buttonPinSes);
  328. if (reading != lastButtonStateSes) {
  329. lastDebounceTimeRes = millis();
  330. }
  331. if ((millis() - lastDebounceTimeSes) > debounceDelay) {
  332. if (reading != buttonStateSes) {
  333. buttonStateSes = reading;
  334. if (buttonStateSes == LOW)
  335. {
  336. counter2--;
  337. Serial.println(counter2);
  338.  
  339. }
  340. }
  341. }
  342. lastButtonStateSes = reading;
  343.  
  344.  
  345. }
  346.  
  347.  
  348. void check()
  349. {
  350. computation = perPisoMin * 60;
  351. if (coinCount < promo1)
  352. {
  353. promoFinal = coinCount * computation;
  354. costc = promoFinal;
  355. }
  356.  
  357. else if (coinCount >= promo1 && coinCount < promo2)
  358. {
  359. promoTime = promoOne * promoHour;
  360. promoComp = coinCount % promo1; //40 yung coin count, pumasok sa coin count na 30 tas may sampong piso na per rate na in between
  361. inBetween = promoComp * computation; //REMAINDER MULTIPLY MO SA PRESYO PER PISO TAS ADD MO YUNG REMAINDER
  362. promoFinal = inBetween + promoTime;
  363. costc = promoFinal;
  364. }
  365. else if (coinCount >= promo2 && coinCount < promo3)
  366. {
  367. promoTime = promoTwo * promoHour;
  368. promoComp = coinCount % promo2; //40 yung coin count, pumasok sa coin count na 30 tas may sampong piso na per rate na in between
  369. inBetween = promoComp * computation; //REMAINDER MULTIPLY MO SA PRESYO PER PISO TAS ADD MO YUNG REMAINDER
  370. promoFinal = inBetween + promoTime;
  371. costc = promoFinal;
  372. }
  373. else if (coinCount >= promo3 && coinCount < promo4)
  374. {
  375. promoTime = promoThree * promoHour;
  376. promoComp = coinCount % promo3; //40 yung coin count, pumasok sa coin count na 30 tas may sampong piso na per rate na in between
  377. inBetween = promoComp * computation; //REMAINDER MULTIPLY MO SA PRESYO PER PISO TAS ADD MO YUNG REMAINDER
  378. promoFinal = inBetween + promoTime;
  379. costc = promoFinal;
  380. }
  381. else if (coinCount >= promo4 && coinCount < promo5)
  382. {
  383. promoComp = coinCount % promo4; //40 yung coin count, pumasok sa coin count na 30 tas may sampong piso na per rate na in between
  384. inBetween = promoComp * computation; //REMAINDER MULTIPLY MO SA PRESYO PER PISO TAS ADD MO YUNG REMAINDER
  385. promoTime = promoFour * promoHour;
  386. promoFinal = inBetween + promoTime; //FINAL
  387. costc = promoFinal;
  388. }
  389. else if (coinCount >= promo5)
  390. {
  391. promoComp = coinCount % promo5; //40 yung coin count, pumasok sa coin count na 30 tas may sampong piso na per rate na in between
  392. inBetween = promoComp * computation; //REMAINDER MULTIPLY MO SA PRESYO PER PISO TAS ADD MO YUNG REMAINDER
  393. promoTime = promoFive * promoHour;
  394. promoFinal = inBetween + promoTime; //FINAL
  395. costc = promoFinal;
  396. }
  397. }
  398.  
  399.  
  400. int ep_read(int add)
  401. {
  402. unsigned int v = 0;
  403. unsigned int i;
  404. for (i = 0; i < ep_limit; i++)
  405. {
  406. v = v + EEPROM.read(add);
  407. add++;
  408. }
  409. return v;
  410. }
  411.  
  412. void ep_write(int add, const int val)
  413. {
  414. int v = val;
  415. unsigned int i;
  416. for (i = 0; i < ep_limit; i++) {
  417. if (255 < v) {
  418. v = v - 255;
  419. EEPROM.write(add, 255);
  420. } else {
  421. EEPROM.write(add, v);
  422. v = 0;
  423. }
  424. add++;
  425. }
  426. }
  427.  
  428. void editPresets()
  429. {
  430. switch (editRates)
  431. {
  432.  
  433. case 0:
  434. // Serial.println("WAITING FOR COMMAND");
  435. editMode = false;
  436. displayMain = true;
  437. break;
  438.  
  439. case 1:
  440. editMode = true;
  441. displayMain = false;
  442. Serial.println("EDIT PISO PER MINUTE");
  443. Serial.println(perPisoMin);
  444. lcd.setCursor(0, 0);
  445. lcd.print("EDIT MODE ");
  446. lcd.setCursor(0, 1);
  447. lcd.print("MINUTE PER PISO");
  448. lcd.setCursor(0, 2);
  449. lcd.print(counter2);
  450.  
  451. readBtnRes();
  452. readBtnSes();
  453. perPisoMin = counter2;
  454. ep_write(epAdd6, perPisoMin);
  455. counter3 = 0;
  456. break;
  457.  
  458. case 2:
  459. editMode = true;
  460. displayMain = false;
  461. Serial.println("EDIT PROMO 1 PRICE ");
  462. Serial.println(promo1);
  463. lcd.setCursor(0, 0);
  464. lcd.print("EDIT MODE ");
  465. lcd.setCursor(0, 1);
  466. lcd.print("PROMO 1 PRICE");
  467. lcd.setCursor(0, 2);
  468.  
  469. lcd.print(counter2);
  470.  
  471. readBtnSes();
  472. readBtnRes();
  473. promo1 = counter2;
  474. ep_write(epAdd1, promo1);
  475. break;
  476.  
  477. case 3:
  478. Serial.println("EDIT PROMO 1 HOURS");
  479. Serial.println(promoOne);
  480. lcd.setCursor(0, 0);
  481. lcd.print("EDIT MODE ");
  482. lcd.setCursor(0, 1);
  483. lcd.print("PROMO 1 HOURS");
  484. lcd.setCursor(0, 2);
  485. lcd.print(counter2);
  486.  
  487. editMode = true;
  488. readBtnSes();
  489. readBtnRes();
  490. promoOne = counter2;
  491. ep_write(epAdd11, promoOne);
  492. break;
  493.  
  494.  
  495. case 4:
  496. Serial.println("EDIT PROMO 2 PRICE");
  497. Serial.println(promo2);
  498. lcd.setCursor(0, 0);
  499. lcd.print("EDIT MODE ");
  500. lcd.setCursor(0, 1);
  501. lcd.print("PROMO 2 PRICE");
  502. lcd.setCursor(0, 2);
  503. lcd.print(counter2);
  504. editMode = true;
  505. editMode = true;
  506. readBtnSes();
  507. readBtnRes();
  508. promo2 = counter2;
  509. ep_write(epAdd2, promo2);
  510. break;
  511. //
  512. case 5:
  513. Serial.println("EDIT PROMO 2 HOURS");
  514. Serial.println(promoTwo);
  515. lcd.setCursor(0, 0);
  516. lcd.print("EDIT MODE");
  517. lcd.setCursor(0, 1);
  518. lcd.print("PROMO 1 HOURS");
  519. lcd.setCursor(0, 2);
  520. lcd.print(counter2);
  521. editMode = true;
  522. editMode = true;
  523. readBtnSes();
  524. readBtnRes();
  525. promoTwo = counter2;
  526. ep_write(epAdd12, promoTwo);
  527. break;
  528. case 6:
  529. Serial.println("EDIT PROMO 3 PRICE");
  530. Serial.println(promo3);
  531. lcd.setCursor(0, 0);
  532. lcd.print("EDIT MODE");
  533. lcd.setCursor(0, 1);
  534. lcd.print("PROMO 3 PRICE");
  535. lcd.setCursor(0, 2);
  536. lcd.print(counter2);
  537. editMode = true;
  538. editMode = true;
  539. readBtnSes();
  540. readBtnRes();
  541. promo3 = counter2;
  542. ep_write(epAdd3, promo3);
  543. break;
  544. //
  545. case 7:
  546. Serial.println("EDIT PROMO 3 HOURS");
  547. Serial.println(promoThree);
  548. lcd.setCursor(0, 0);
  549. lcd.print("EDIT MODE");
  550. lcd.setCursor(0, 1);
  551. lcd.print("PROMO 3 HOURS");
  552. lcd.setCursor(0, 2);
  553. lcd.print(counter2);
  554. editMode = true;
  555. editMode = true;
  556. readBtnSes();
  557. readBtnRes();
  558. promoThree = counter2;
  559. ep_write(epAdd13, promoThree);
  560. break;
  561.  
  562. case 8:
  563. Serial.println("EDIT PROMO 4 PRICE");
  564. Serial.println(promo4);
  565. lcd.setCursor(0, 0);
  566. lcd.print("EDIT MODE");
  567. lcd.setCursor(0, 1);
  568. lcd.print("PROMO 4 PRICE");
  569. lcd.setCursor(0, 2);
  570. lcd.print(counter2);
  571. editMode = true;
  572. editMode = true;
  573. readBtnSes();
  574. readBtnRes();
  575. promo4 = counter2;
  576. ep_write(epAdd4, promo4);
  577. break;
  578.  
  579. case 9:
  580. Serial.println("EDIT PROMO 4 HOURS");
  581. Serial.println(promoFour);
  582. lcd.setCursor(0, 0);
  583. lcd.print("EDIT MODE");
  584. lcd.setCursor(0, 1);
  585. lcd.print("PROMO 4 HOURS");
  586. lcd.setCursor(0, 2);
  587. lcd.print(counter2);
  588. editMode = true;
  589. editMode = true;
  590. readBtnSes();
  591. readBtnRes();
  592. promoFour = counter2;
  593. ep_write(epAdd14, promoFour);
  594. break;
  595.  
  596. case 10:
  597. Serial.println("EDIT PROMO 5 PRICE");
  598. Serial.println(promo5);
  599. lcd.setCursor(0, 0);
  600. lcd.print("EDIT MODE");
  601. lcd.setCursor(0, 1);
  602. lcd.print("PROMO PRICE HOURS");
  603. lcd.setCursor(0, 2);
  604. lcd.print(counter2);
  605. editMode = true;
  606. editMode = true;
  607. readBtnSes();
  608. readBtnRes();
  609. promo5 = counter2;
  610. ep_write(epAdd5, promo5);
  611. break;
  612.  
  613. case 11:
  614. Serial.println("EDIT PROMO 5 HOURS");
  615. lcd.setCursor(0, 0);
  616. lcd.print("EDIT MODE");
  617. lcd.setCursor(0, 1);
  618. lcd.print("PROMO 5 HOURS");
  619. lcd.setCursor(0, 2);
  620. lcd.print(counter2);
  621. editMode = true;
  622. Serial.println(promoFive);
  623. editMode = true;
  624. readBtnSes();
  625. readBtnRes();
  626. promoFive = counter2;
  627. ep_write(epAdd15, promoFive);
  628. break;
  629.  
  630. case 12:
  631. Serial.println("SAVED");
  632. lcd.print("Saved");
  633. delay(2000);
  634. lcd.clear();
  635.  
  636. editRates = 0;
  637. break;
  638. }
  639.  
  640. }
  641.  
  642. void displayMainMessage()
  643. {
  644. if (displayMain == true)
  645. {
  646. lcd.setCursor(0, 0);
  647. lcd.print(F(" JHUNDECK WIFI "));
  648. lcd.setCursor(0, 1);
  649. lcd.print(F("Pls. Insert Coin/s"));
  650. lcd.setCursor(0, 2);
  651. lcd.print(coinCount);
  652. }
  653. }
Add Comment
Please, Sign In to add comment