updated code kung may interested pa ver1
#include <SPI.h>
#include <Wire.h>
#include <Entropy.h>
#include <Ethernet.h>
#include <TelnetClient.h>
#include <LiquidCrystal_I2C.h>
#define randomSeed(s) srandom(s)
int mins = 0;
int hours = 0;
int days = 0;
int newmins = 0;
int newhours = 0;
int buttonPress = 0;
const int ledpin = 5;
const int coinpin = 2;
const int buttonpin = 3;
const int disablepin = 4;
volatile float coins = 0;
unsigned long startMillis = 0;
unsigned long currentMillis = 0;
String script;
uint32_t seed_value;
char letters[36] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0'};
LiquidCrystal_I2C lcd(0x27, 20, 4);
EthernetClient client;
telnetClient tc(client);
byte clientMAC[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress clientIp (192, 168, 88, 2);
//put here your router's ip address
IPAddress mikrotikRouterIp (192, 168, 88, 1);
void setup() {
pinMode(buttonpin, INPUT);
pinMode(ledpin, OUTPUT);
pinMode(disablepin, OUTPUT);
lcd.begin();
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("initializing....");
lcd.setCursor(0, 2);
lcd.print(" by asantos1");
delay(3000);
digitalWrite(disablepin, HIGH);
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo and Due
}
Entropy.initialize();
attachInterrupt(digitalPinToInterrupt(coinpin), coinInterrupt, RISING);
pinMode(buttonpin, INPUT);
pinMode(ledpin, OUTPUT);
pinMode(disablepin, OUTPUT);
}
void coinInterrupt() {
coins ++;
startMillis = millis(); //initial start time
}
void loop() {
if (coins < 1) {
//Serial.println("Please Insert Coin");
lcd.setCursor(0, 0);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(" Insert Coin ");
lcd.setCursor(0, 2);
lcd.print(" ");
lcd.setCursor(0, 3);
lcd.print(" ");
}
currentMillis = millis(); //get the current time
if ((coins > 0) && (currentMillis - startMillis >= 100)) {
if (coins < 5) {
mins = coins * 10; // 15min/peso
}
if (coins >= 5 && coins < 10) {
mins = coins * 20; // 20min/peso
}
if (coins >= 10 && coins < 15) {
mins = coins * 30; // 30min/peso
}
if (coins >= 15) {
mins = coins * 45; // 45min/peso
}
hours = floor(mins / 60);
days = floor(hours / 24);
newhours = hours - (days * 24);
newmins = mins - (hours * 60);
Serial.println(coins);
Serial.println(days);
Serial.println(newhours);
Serial.println(newmins);
lcd.setCursor(0, 0);
lcd.print(" Credit: ");
lcd.print(coins, 0);
lcd.print(".00 ");
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.print(days);
lcd.print(" day ");
lcd.print(newhours);
lcd.print(" hr ");
lcd.print(newmins);
lcd.print(" min");
}
currentMillis = millis(); //get the current time
if ((coins > 0) && (currentMillis - startMillis >= 10000)) {
lcd.setCursor(0, 2);
lcd.print(" Press Button to");
lcd.setCursor(0, 3);
lcd.print(" Generate Code");
}
currentMillis = millis(); //get the current time
if ((coins > 0) && (currentMillis - startMillis >= 12000)) {
digitalWrite(disablepin, LOW);
digitalWrite(ledpin, HIGH);
while (digitalRead(buttonpin) == LOW) {
//do nothing
}
digitalWrite(ledpin, LOW);
seed_value = Entropy.random();
//Serial.println(seed_value);
randomSeed(seed_value);
char c[2] = {letters[random(36)]};
char o[2] = {letters[random(36)]};
char d[2] = {letters[random(36)]};
char e[2] = {letters[random(36)]};
lcd.setCursor(0, 0);
lcd.print(" CODE GENERATED ");
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 2);
lcd.print(" ");
lcd.print(c);
lcd.print(" ");
lcd.print(o);
lcd.print(" ");
lcd.print(d);
lcd.print(" ");
lcd.print(e);
lcd.print(" ");
lcd.setCursor(0, 3);
lcd.print(" ");
script = "/ip hotspot user add name=";
script += c;
script += o;
script += d;
script += e;
script += " limit-uptime=";
script += mins;
script += " profile=hotspot";
int script_len = script.length() + 1;
char mscript[script_len];
script.toCharArray(mscript, script_len);
Ethernet.begin (clientMAC);
//want to use dhcp?
if (!Ethernet.begin (clientMAC)) {
Serial.println("\r\nDHCP error");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("DCHP error");
while (1);
}
tc.setPromptChar('>');
//PUT HERE YOUR USERNAME/PASSWORD
if (tc.login(mikrotikRouterIp, "admin", "asantos1")) {
delay(1000);
tc.sendCommand(mscript);
delay(5000);
tc.disconnect();
lcd.setCursor(0, 0);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.print(c);
lcd.print(" ");
lcd.print(o);
lcd.print(" ");
lcd.print(d);
lcd.print(" ");
lcd.print(e);
lcd.print(" ");
lcd.setCursor(0, 2);
lcd.print(" ");
lcd.setCursor(0, 3);
lcd.print(" THANK YOU ");
delay(5000);
coins = 0;
digitalWrite(disablepin, HIGH);
}
}
}