Dzisiaj wrzucam kawałek kodu który powstał z potrzeby.
Moja córeczka, prawie 3 latka, mała cwaniara, czasami ogląda bajki na YT Kids na TV. Zorientowała się już że jak kochany tatuś odłączy kabelek LAN od TV (bo już za długo ogląda) to za chwilę bajek nie będzie i każe mi to naprawić
Potrzebny jest goły dowolny procesor np. ESP8266 i parę linijek kodu.
Soft testowany na RB4011 z firmware 7.20.5
Poniżej przykład dla portu "ether08".
Code: Select all
/*
Copyright (C) AC SOFTWARE SP. Z O.O.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
const char* mt_ip = "192.168.1.1"; //Adres IP mikrotika
const int mt_port = 8728; //Port API
const char* mt_user = "admin"; //Login
const char* mt_pass = "pass"; //Hasło
// w programie wpisany jest port LAN "ether08", należy go zmienić na własny.
bool poprzedni_stan_vrelay_mikrotik = 1;
bool stan_vrelay_mikrotik = 0;
unsigned long poprzedni_czas;
#define STATUS_LED_GPIO 2
#include <SuplaDevice.h>
#include <supla/network/esp_wifi.h>
#include <supla/device/status_led.h>
#include <supla/storage/littlefs_config.h>
#include <supla/network/esp_web_server.h>
#include <supla/network/html/device_info.h>
#include <supla/network/html/protocol_parameters.h>
#include <supla/network/html/status_led_parameters.h>
#include <supla/network/html/wifi_parameters.h>
#include <supla/control/virtual_relay.h>
Supla::Control::VirtualRelay *vrelay_mikrotik = nullptr;
#include <supla/storage/eeprom.h>
WiFiClient client;
Supla::Eeprom eeprom;
Supla::ESPWifi wifi;
Supla::LittleFsConfig configSupla;
Supla::Device::StatusLed statusLed(STATUS_LED_GPIO, true); // inverted state
Supla::EspWebServer suplaServer;
void setup() {
Serial.begin(115200);
// HTML www component
new Supla::Html::DeviceInfo(&SuplaDevice);
new Supla::Html::WifiParameters;
new Supla::Html::ProtocolParameters;
new Supla::Html::StatusLedParameters;
// Channel #0 Virtual Relay
vrelay_mikrotik = new Supla::Control::VirtualRelay();
vrelay_mikrotik->setDefaultFunction(SUPLA_CHANNELFNC_POWERSWITCH);
// Enable state restore after device restart:
vrelay_mikrotik->setDefaultStateRestore();
vrelay_mikrotik->setInitialCaption("PORT 8");
SuplaDevice.setInitialMode(Supla::InitialMode::StartInCfgMode);
SuplaDevice.setName("MIKROTIK");
SuplaDevice.begin();
}
void loop() {
SuplaDevice.iterate();
Steruj_portami_w_Mikrotik();
}
void Steruj_portami_w_Mikrotik(){
if(millis() - poprzedni_czas >= 1000){
poprzedni_czas = millis();
if (Supla::Network::IsReady()) {
byte stan_vrelay_mikrotik = 0;
if(vrelay_mikrotik->isOn()){
stan_vrelay_mikrotik = 1;
}
if(poprzedni_stan_vrelay_mikrotik != stan_vrelay_mikrotik){
poprzedni_stan_vrelay_mikrotik = stan_vrelay_mikrotik;
if(stan_vrelay_mikrotik == 1){
enablePort("ether08");
Serial.println("Enable port 8");
}else{
disablePort("ether08");
Serial.println("Disable port 8");
}
}
}
}
}
void enablePort(const String& iface) {
if (!mtLogin()) return;
writeWord("/interface/ethernet/set");
writeWord("=numbers=" + iface);
writeWord("=disabled=no"); // WŁĄCZ port
client.write((uint8_t)0x00);
delay(100);
if (client.connected()) client.stop(); //Mikrotik Logout
}
void disablePort(const String& iface) {
if (!mtLogin()) return;
writeWord("/interface/ethernet/set");
writeWord("=numbers=" + iface);
writeWord("=disabled=yes");
client.write((uint8_t)0x00);
delay(100);
if (client.connected()) client.stop(); //Mikrotik Logout
}
void writeWord(const String& word) {
int len = word.length();
client.write((uint8_t)len);
client.print(word);
}
bool mtLogin() {
if (!client.connect(mt_ip, mt_port)) {
Serial.println("Brak połączenia z MikroTik");
return false;
}
writeWord("/login");
writeWord("=name=" + String(mt_user));
writeWord("=password=" + String(mt_pass));
client.write((uint8_t)0x00);
delay(200);
while (client.available()) client.read();
return true;
}
Pozdrawiam, Duch__
