naivxnaivet

6AMPASTE

May 28th, 2020
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.56 KB | None | 0 0
  1. bool trigger = false;
  2.  
  3. unsigned long previousMillisReset = 0; // will store last time LED was updated
  4. const long intervalReset = 20000;
  5.  
  6. unsigned long previousMillis = 0; // will store last time LED was updated
  7. const long interval = 1000;
  8.  
  9. unsigned long previousResetMillis = 0;
  10. bool resetNow = false;
  11.  
  12. #include <ESP8266TelnetClient.h>
  13. #include <ESP8266WiFi.h>
  14. #include <ESP8266WiFiMulti.h>
  15. #include <Wire.h>
  16. #include <LiquidCrystal_I2C.h>
  17.  
  18. IPAddress mikrotikRouterIp (192, 168, 22, 1);
  19.  
  20. const char* user = "admin";
  21. const char* pwd = "Secreto#020202";
  22. const char* ssid = "MikroTik Wifi";
  23. const char* password = "Secreto#020202";
  24. String M_command;
  25.  
  26. ESP8266WiFiMulti WiFiMulti;
  27. WiFiClient client;
  28.  
  29. ESP8266telnetClient tc(client);
  30.  
  31.  
  32. LiquidCrystal_I2C lcd(0x27, 20, 4);
  33. int costc = 420; //
  34.  
  35.  
  36. int coinCount = 0;
  37. int y = 0;
  38. byte hourr = 0;
  39. byte minn = 0;
  40. byte secc = 0;
  41. byte dayy = 0;
  42. int temp = 0;
  43. int startTime = 0;
  44. int userCode = 0;
  45. int cantWaitTheCode = 0;
  46. int checkTime = 0;
  47. char const *c;
  48.  
  49. void ICACHE_RAM_ATTR coinAccept()
  50. {
  51. coinCount++;
  52. digitalWrite(LED_BUILTIN, LOW);
  53. }
  54.  
  55. void ICACHE_RAM_ATTR buttonPush()
  56. {
  57.  
  58. if (coinCount > 0)
  59. //if(coinCounnt >= 0)
  60. {
  61. cantWaitTheCode = 1;
  62. }
  63.  
  64. else if (coinCount == 0)
  65. {
  66. cantWaitTheCode = 3;
  67. }
  68.  
  69. }
  70.  
  71.  
  72.  
  73. void setup()
  74. {
  75. Serial.begin(9600);
  76. WiFi.mode(WIFI_STA);
  77. WiFiMulti.addAP(ssid, password);
  78. attachInterrupt(D5, coinAccept, RISING);
  79. attachInterrupt(D6, buttonPush, RISING);
  80. pinMode(D5, INPUT);
  81. pinMode(D5, INPUT);
  82. pinMode(LED_BUILTIN, OUTPUT);
  83. //pinMode(D7, OUTPUT);
  84.  
  85. lcd.begin();
  86. displayMainMessage();
  87. digitalWrite(LED_BUILTIN, HIGH);
  88. delay(1000);
  89.  
  90. // Serial.println();
  91. // Serial.println();
  92. // Serial.println("Wait for WI-Fi...");
  93. while (WiFiMulti.run() != WL_CONNECTED)
  94. {
  95. // Serial.print(".");
  96. delay(500);
  97. }
  98.  
  99. // Serial.println("");
  100. // Serial.println("WiFi connected");
  101. // Serial.print("IP Address : ");
  102. // Serial.println(WiFi.localIP());
  103. // Serial.println("Connecting....");
  104.  
  105.  
  106. tc.setPromptChar('>');
  107. //manual login pagka may problema
  108. char key = 0;
  109. Serial.println("\r\npress enter to begin:");
  110. do
  111. {
  112. key = Serial.read();
  113. Serial.println(key);
  114. }
  115.  
  116. //automatic login
  117. while (key <= 0);
  118.  
  119. tc.login(mikrotikRouterIp, user, pwd);
  120.  
  121. }
  122.  
  123. void loop()
  124. {
  125.  
  126. checkCondition();
  127. displayInsertCoin();
  128. // res();
  129. clearLcd();
  130. }
  131.  
  132. void displayMainMessage()
  133. {
  134. cantWaitTheCode = 0;
  135. y = 0;
  136. lcd.setCursor(0, 0);
  137. lcd.print(F("Jhundeck Wi-fi Vendo"));
  138. lcd.setCursor(0, 1);
  139. lcd.print(F("Jhundeck Wi-fi Vendo"));
  140. lcd.setCursor(0, 2);
  141. lcd.print(F("Jhundeck Wi-fi Vendo"));
  142. lcd.setCursor(0, 3);
  143. lcd.print(F("Jhundeck Wi-fi Vendo"));
  144. }
  145.  
  146.  
  147.  
  148. void displayInsertCoin()
  149. {
  150. if (coinCount >= 1)
  151. {
  152. lcd.setCursor(0, 0);
  153. lcd.print(F("Insert Coins : "));
  154. lcd.setCursor(0, 1);
  155. lcd.print(F("Coin Value is " ));
  156. lcd.setCursor(0, 2);
  157. lcd.print(coinCount);
  158. lcd.print(F(" "));
  159. temp = coinCount * costc; // ilang minute yung piso
  160. hourr = floor(temp / 3600);
  161. temp %= 3600; //temp = temp%3600
  162. minn = floor (temp / 60);
  163. secc = temp % 60;
  164. dayy = floor(hourr / 24);
  165. lcd.setCursor(0, 3);
  166. lcd.print(dayy);
  167. lcd.print(F("D:"));
  168. lcd.print(hourr);
  169. lcd.print(F("H:"));
  170. lcd.print(minn);
  171. lcd.print(F("M:"));
  172. lcd.print(secc);
  173. lcd.print(F("S"));
  174. lcd.print(F(" "));
  175. }
  176. }
  177.  
  178.  
  179. void checkCondition()
  180. {
  181. digitalWrite(LED_BUILTIN, HIGH);
  182. if (coinCount > 0)
  183. {
  184. displayInsertCoin();
  185. if (coinCount > 0 && cantWaitTheCode == 1)
  186. {
  187. Serial.println("the value is:");
  188. Serial.println(coinCount);
  189. temp = coinCount * costc; // ilang minute yung piso
  190. hourr = floor(temp / 3600);
  191. temp %= 3600; //temp = temp%3600
  192. minn = floor (temp / 60);
  193. secc = temp % 60;
  194. dayy = floor(hourr / 24);
  195. userCode = random(10000, 99999); //ITO YUNG PAPALITAN, LEFT = LOWEST, RIGHT = UPPER
  196. M_command += "/ip hotspot user add name=";
  197. M_command += userCode;
  198. M_command += " limit-uptime=";
  199. M_command += temp;
  200. int command_len = M_command.length() + 1;
  201. char M_F_command[command_len];
  202. M_command.toCharArray(M_F_command, command_len);
  203. tc.sendCommand(M_F_command);
  204. lcd.setCursor(0, 0);
  205. lcd.print(F("CODE : "));
  206. lcd.setCursor(0, 1);
  207. lcd.print(userCode);
  208. lcd.setCursor(7, 1);
  209. lcd.print(F(" "));
  210. lcd.setCursor(0, 2);
  211. lcd.print(F("Valid for "));
  212. lcd.setCursor(0, 3);
  213. lcd.print(dayy);
  214. lcd.print(F("D:"));
  215. lcd.print(hourr);
  216. lcd.print(F("H:"));
  217. lcd.print(minn);
  218. lcd.print(F("M:"));
  219. lcd.print(secc);
  220. lcd.print(F("S"));
  221. lcd.print(F(" "));
  222. M_command = "";
  223. memset(M_F_command , 0, sizeof(M_F_command ));
  224. trigger = true;
  225. coinCount = 0;
  226. cantWaitTheCode = 2;
  227. }
  228. }
  229. }
  230.  
  231. void clearLcd()
  232. {
  233.  
  234. if (cantWaitTheCode == 2 && coinCount == 0)
  235. {
  236. unsigned long currentMillis = millis();
  237. if (currentMillis - previousMillisReset >= intervalReset)
  238. {
  239. previousMillisReset = currentMillis;
  240. cantWaitTheCode = 0;
  241. lcd.clear();
  242. trigger = false;
  243. displayMainMessage();
  244. }
  245. }
  246. else if (cantWaitTheCode == 3 && trigger == true)
  247. {
  248. cantWaitTheCode = 0;
  249. lcd.clear();
  250.  
  251. trigger = false;
  252. displayMainMessage();
  253. }
  254. }
Add Comment
Please, Sign In to add comment