terrorsl/sMQTTBroker

Using Ethernet instead of wifi

SerkanKK opened this issue · 8 comments

how can i use ethernet instead of wifi .. i want to use with WT32-ETH01
How i can use Ethernet and SmqttBroker together.

#include <ETH.h> 
IPAddress local_ip(192, 168, 1, 112);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress dns1(8, 8, 8, 8);
IPAddress dns2 = (uint32_t)0x00000000;
ETH.config(local_ip, gateway, subnet, dns1, dns2); 

i was changed sMQTTBroker.cpp i was added #ifdef BROKERUSEWIFI now its connect with etnernet
but im not sure just add this line is enought or not.. am not expert with C++ and ESP.
Please can you look it .. is it ok or not..

void sMQTTBroker::update()
{
#if defined(ESP8266) || defined(ESP32)
  #ifdef BROKERUSEWIFI
	if (WiFi.isConnected() == false)
	{
		Serial.println("if (WiFi.isConnected() == false)");
		sMQTTLostConnectionEvent event;
		onEvent(&event);
		return;
	}
  #endif
    
	WiFiClient client = _server->available();
	if (client)
	{

Hello, I think in order to avoid the Wi-Fi connection check - you did everything right. Otherwise, everything should work, but unfortunately I don’t have such a module and I can’t check it. It will be very good if you have the opportunity to test the performance. I will also think about how best to add the ability to work with such modules.

i dont know what should do for good test but i was done simple test last sunday. (plug and unplug network cable its continiues work )

instead of "if use WT32_ETH01 define SMQTT_WT32_ETH01 disable wifi connection"

add Default Connection (Ethernet or WiFi) and check if Ethernet Avaible work with ethernet if ethernet not avaible and SSID,Password addet work with Wifi .. just my thought

my english not good , i hope i explained what i wanted to say with this bad english

hello, I made a mistake describing the macro, it does not turn off wifi, but does not check the wifi connection. the library itself is not responsible for the connection and it was assumed that this was done outside of it. I would like to understand in more detail how you use this board, namely how you connect clients to the broker and the external network? this will help me understand what needs to be changed and whether it is necessary. Thanks

I used advanged example i was add Ethernet conection and delete Wi-Fi connection.
i will look and check again and i will write you.
And if you need I may open connection with anydesk.

i used your advanged example and eth example..
its work when i add #define SMQTT_WT32_ETH01 to sMQTTBroker.cpp
if i add #define SMQTT_WT32_ETH01 in my ethmqtt.ino file its not work..

ethmqtt
`#include<sMQTTBroker.h>
#include <ETH.h>
#include <WiFi.h>

class MyBroker:public sMQTTBroker
{
public:
bool onConnect(sMQTTClient client, const std::string &username, const std::string &password)
{
// check username and password, if ok return true
return true;
};
void onRemove(sMQTTClient
)
{
};
void onPublish(sMQTTClient *client,const std::string &topic, const std::string &payload)
{
// client publish a message to the topic
// you can parse the message, save it to a database, or whatever, based on your goals
}
bool onEvent(sMQTTEvent *event)
{
switch(event->Type())
{
case NewClient_sMQTTEventType:
{
sMQTTNewClientEvent e=(sMQTTNewClientEvent)event;
e->Login();
e->Password();
}
break;
case LostConnect_sMQTTEventType:
WiFi.reconnect();
break;
}
return true;
}
};

#define ETH_ADDR 1
#define ETH_POWER_PIN 16//-1 //16 // Do not use it, it can cause conflict during the software reset.
#define ETH_POWER_PIN_ALTERNATIVE 16 //17
#define ETH_MDC_PIN 23
#define ETH_MDIO_PIN 18
#define ETH_TYPE ETH_PHY_LAN8720
#define ETH_CLK_MODE ETH_CLOCK_GPIO17_OUT // ETH_CLOCK_GPIO0_IN

IPAddress local_ip(192, 168, 1, 112);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress dns1(8, 8, 8, 8);
IPAddress dns2 = (uint32_t)0x00000000;

static bool eth_connected = false;

void WiFiEvent(WiFiEvent_t event)
{
switch (event) {
case SYSTEM_EVENT_ETH_START:
Serial.println("ETH Started");
//set eth hostname here
ETH.setHostname("esp32-ethernet");
break;
case SYSTEM_EVENT_ETH_CONNECTED:
Serial.println("ETH Connected");
break;
case SYSTEM_EVENT_ETH_GOT_IP:
Serial.print("ETH MAC: ");
Serial.print(ETH.macAddress());
Serial.print(", IPv4: ");
Serial.print(ETH.localIP());
if (ETH.fullDuplex()) {
Serial.print(", FULL_DUPLEX");
}
Serial.print(", ");
Serial.print(ETH.linkSpeed());
Serial.println("Mbps");
eth_connected = true;
break;
case SYSTEM_EVENT_ETH_DISCONNECTED:
Serial.println("ETH Disconnected");
eth_connected = false;
break;
case SYSTEM_EVENT_ETH_STOP:
Serial.println("ETH Stopped");
eth_connected = false;
break;
default:
break;
}
}

MyBroker broker;
unsigned long Time;
unsigned long freeRam;

void setup() {
pinMode(ETH_POWER_PIN_ALTERNATIVE, OUTPUT);
digitalWrite(ETH_POWER_PIN_ALTERNATIVE, HIGH);
Serial.begin(115200);
delay(2500);
Serial.println("hello");

WiFi.onEvent(WiFiEvent);

ETH.begin(ETH_ADDR, ETH_POWER_PIN, ETH_MDC_PIN, ETH_MDIO_PIN, ETH_TYPE, ETH_CLK_MODE); // Enable ETH
//ETH.config(local_ip, gateway, subnet, dns1, dns2); // Static IP, leave without this line to get IP via DHCP
while(!((uint32_t)ETH.localIP())){}; // Waiting for IP (leave this line group to get IP via DHCP)

const unsigned short mqttPort=1883;
broker.init(mqttPort);
// all done
// your magic code
Time=millis();
freeRam=ESP.getFreeHeap();

Serial.println("MQTT Broker server started");
delay(5500);
}

void loop() {
broker.update();
// your magic code
if(millis()-Time>1000)
{
Time=millis();
if(ESP.getFreeHeap()!=freeRam)
{
freeRam=ESP.getFreeHeap();
Serial.print("RAM:");
Serial.println(freeRam);
}
}
}`

Ok, I'll check this

Stale issue message