Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool trigger = false;
- unsigned long previousMillisReset = 0; // will store last time LED was updated
- const long intervalReset = 20000;
- unsigned long previousMillis = 0; // will store last time LED was updated
- const long interval = 1000;
- unsigned long previousResetMillis = 0;
- bool resetNow = false;
- #include <ESP8266TelnetClient.h>
- #include <ESP8266WiFi.h>
- #include <ESP8266WiFiMulti.h>
- #include <Wire.h>
- #include <LiquidCrystal_I2C.h>
- IPAddress mikrotikRouterIp (192, 168, 22, 1);
- const char* user = "admin";
- const char* pwd = "Secreto#020202";
- const char* ssid = "MikroTik Wifi";
- const char* password = "Secreto#020202";
- String M_command;
- ESP8266WiFiMulti WiFiMulti;
- WiFiClient client;
- ESP8266telnetClient tc(client);
- LiquidCrystal_I2C lcd(0x27, 20, 4);
- int costc = 420; //
- int coinCount = 0;
- int y = 0;
- byte hourr = 0;
- byte minn = 0;
- byte secc = 0;
- byte dayy = 0;
- int temp = 0;
- int startTime = 0;
- int userCode = 0;
- int cantWaitTheCode = 0;
- int checkTime = 0;
- char const *c;
- void ICACHE_RAM_ATTR coinAccept()
- {
- coinCount++;
- digitalWrite(LED_BUILTIN, LOW);
- }
- void ICACHE_RAM_ATTR buttonPush()
- {
- if (coinCount > 0)
- //if(coinCounnt >= 0)
- {
- cantWaitTheCode = 1;
- }
- else if (coinCount == 0)
- {
- cantWaitTheCode = 3;
- }
- }
- void setup()
- {
- Serial.begin(9600);
- WiFi.mode(WIFI_STA);
- WiFiMulti.addAP(ssid, password);
- attachInterrupt(D5, coinAccept, RISING);
- attachInterrupt(D6, buttonPush, RISING);
- pinMode(D5, INPUT);
- pinMode(D5, INPUT);
- pinMode(LED_BUILTIN, OUTPUT);
- //pinMode(D7, OUTPUT);
- lcd.begin();
- displayMainMessage();
- digitalWrite(LED_BUILTIN, HIGH);
- delay(1000);
- // Serial.println();
- // Serial.println();
- // Serial.println("Wait for WI-Fi...");
- while (WiFiMulti.run() != WL_CONNECTED)
- {
- // Serial.print(".");
- delay(500);
- }
- // Serial.println("");
- // Serial.println("WiFi connected");
- // Serial.print("IP Address : ");
- // Serial.println(WiFi.localIP());
- // Serial.println("Connecting....");
- tc.setPromptChar('>');
- //manual login pagka may problema
- char key = 0;
- Serial.println("\r\npress enter to begin:");
- do
- {
- key = Serial.read();
- Serial.println(key);
- }
- //automatic login
- while (key <= 0);
- tc.login(mikrotikRouterIp, user, pwd);
- }
- void loop()
- {
- checkCondition();
- displayInsertCoin();
- // res();
- clearLcd();
- }
- void displayMainMessage()
- {
- cantWaitTheCode = 0;
- y = 0;
- lcd.setCursor(0, 0);
- lcd.print(F("Jhundeck Wi-fi Vendo"));
- lcd.setCursor(0, 1);
- lcd.print(F("Jhundeck Wi-fi Vendo"));
- lcd.setCursor(0, 2);
- lcd.print(F("Jhundeck Wi-fi Vendo"));
- lcd.setCursor(0, 3);
- lcd.print(F("Jhundeck Wi-fi Vendo"));
- }
- void displayInsertCoin()
- {
- if (coinCount >= 1)
- {
- lcd.setCursor(0, 0);
- lcd.print(F("Insert Coins : "));
- lcd.setCursor(0, 1);
- lcd.print(F("Coin Value is " ));
- lcd.setCursor(0, 2);
- lcd.print(coinCount);
- lcd.print(F(" "));
- temp = coinCount * costc; // ilang minute yung piso
- hourr = floor(temp / 3600);
- temp %= 3600; //temp = temp%3600
- minn = floor (temp / 60);
- secc = temp % 60;
- dayy = floor(hourr / 24);
- lcd.setCursor(0, 3);
- lcd.print(dayy);
- lcd.print(F("D:"));
- lcd.print(hourr);
- lcd.print(F("H:"));
- lcd.print(minn);
- lcd.print(F("M:"));
- lcd.print(secc);
- lcd.print(F("S"));
- lcd.print(F(" "));
- }
- }
- void checkCondition()
- {
- digitalWrite(LED_BUILTIN, HIGH);
- if (coinCount > 0)
- {
- displayInsertCoin();
- if (coinCount > 0 && cantWaitTheCode == 1)
- {
- Serial.println("the value is:");
- Serial.println(coinCount);
- temp = coinCount * costc; // ilang minute yung piso
- hourr = floor(temp / 3600);
- temp %= 3600; //temp = temp%3600
- minn = floor (temp / 60);
- secc = temp % 60;
- dayy = floor(hourr / 24);
- userCode = random(10000, 99999); //ITO YUNG PAPALITAN, LEFT = LOWEST, RIGHT = UPPER
- M_command += "/ip hotspot user add name=";
- M_command += userCode;
- M_command += " limit-uptime=";
- M_command += temp;
- int command_len = M_command.length() + 1;
- char M_F_command[command_len];
- M_command.toCharArray(M_F_command, command_len);
- tc.sendCommand(M_F_command);
- lcd.setCursor(0, 0);
- lcd.print(F("CODE : "));
- lcd.setCursor(0, 1);
- lcd.print(userCode);
- lcd.setCursor(7, 1);
- lcd.print(F(" "));
- lcd.setCursor(0, 2);
- lcd.print(F("Valid for "));
- lcd.setCursor(0, 3);
- lcd.print(dayy);
- lcd.print(F("D:"));
- lcd.print(hourr);
- lcd.print(F("H:"));
- lcd.print(minn);
- lcd.print(F("M:"));
- lcd.print(secc);
- lcd.print(F("S"));
- lcd.print(F(" "));
- M_command = "";
- memset(M_F_command , 0, sizeof(M_F_command ));
- trigger = true;
- coinCount = 0;
- cantWaitTheCode = 2;
- }
- }
- }
- void clearLcd()
- {
- if (cantWaitTheCode == 2 && coinCount == 0)
- {
- unsigned long currentMillis = millis();
- if (currentMillis - previousMillisReset >= intervalReset)
- {
- previousMillisReset = currentMillis;
- cantWaitTheCode = 0;
- lcd.clear();
- trigger = false;
- displayMainMessage();
- }
- }
- else if (cantWaitTheCode == 3 && trigger == true)
- {
- cantWaitTheCode = 0;
- lcd.clear();
- trigger = false;
- displayMainMessage();
- }
- }
Add Comment
Please, Sign In to add comment