Skip to content

Commit

Permalink
Added examples for W5500, thanks to real-bombinho
Browse files Browse the repository at this point in the history
  • Loading branch information
hsaturn committed Apr 21, 2023
1 parent ef4a026 commit 66cb80f
Show file tree
Hide file tree
Showing 4 changed files with 233 additions and 0 deletions.
111 changes: 111 additions & 0 deletions examples/W5500/client-with-W5500-ESP8266/client-with-W5500-ESP8266.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#include <SPI.h>
#include <Ethernet.h>

#include "TinyMqtt.h" // https://github.com/hsaturn/TinyMqtt

#define WIZRST D3 //Specify pin to use for reseting W5500
#define WIZCS D1 //Specify the pin for SPI CS

#define PORT 1883
MqttBroker broker(PORT);
unsigned long Time;
unsigned long freeRam;

/** Basic Mqtt Broker
*
* +-----------------------------+
* | ESP |
* | +--------+ |
* | | broker | | 1883 <--- External client/s
* | +--------+ |
* | |
* +-----------------------------+
*
* Your ESP will become a MqttBroker.
* You can test it with any client such as mqtt-spy for example
*
*/

#if defined(USE_ETHERNET)

byte mac[] = { 0x00, 0xAA, 0xBB, 0xE0, 0x01, 0x25 }; //MAC Address
//IPAddress ip = (192,168,0,88); //Fixed IP Address

#else

const char* ssid = "xxxxxxx";
const char* password = "xxxxxxxx";

#endif


void WizReset() {
Serial.print("Resetting Wiz W5500 Ethernet Board... ");
pinMode(WIZRST, OUTPUT);
digitalWrite(WIZRST, HIGH);
delay(250);
digitalWrite(WIZRST, LOW);
delay(50);
digitalWrite(WIZRST, HIGH);
delay(350);
Serial.println("Done.");
}

void setup()
{
Serial.begin(115200);

#if defined(USE_ETHERNET)

Ethernet.init(WIZCS); // SPI CS Pin
SPI.begin();
WizReset();
Console << TinyConsole::green << "Starting Ethernet...." << endl;
Ethernet.begin(mac); //Connect using DHCP
//Ethernet.begin(mac,ip); //Connect using Fixed IP
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Console << TinyConsole::red << "Ethernet shield was not found. Sorry, can't run without hardware. :(" << endl;
} else
if (Ethernet.linkStatus() == LinkOFF) {
Console << TinyConsole::red << "Ethernet cable is not connected." << endl;
}
Console << TinyConsole::green << "Local Ethernet IP address: " << Ethernet.localIP() << endl;
broker.begin();
Console << "Broker ready (eth) : " << Ethernet.localIP() << " on port " << PORT << endl;

#else

if (strlen(ssid)==0)
Console << TinyConsole::red << "****** PLEASE MODIFY ssid/password *************" << endl;
WiFi.disconnect(); //Remove previous SSID & Password
WiFi.begin(ssid, password); // Connect to the network
Console << TinyConsole::white << "WiFi Connecting to " << ssid << " ...";
int i = 0;
while (WiFi.status() != WL_CONNECTED) { // Wait for the Wi-Fi to connect
delay(1000);
Console << TinyConsole::white << ".";
i++;
if (i > 20) break;
}
Console << endl << endl;
Console << TinyConsole::green << "Connected to " << ssid << " IP address: " << WiFi.localIP() << endl;
broker.begin();
Console << "Broker ready (wifi) : " << WiFi.localIP() << " on port " << PORT << endl;

#endif
}

void loop()
{
broker.loop();
if(millis()-Time>10000) {
Time=millis();
if(ESP.getFreeHeap()!=freeRam)
{
freeRam=ESP.getFreeHeap();
Serial.print("RAM:");
Serial.println(freeRam);
}
}

}
Binary file not shown.
117 changes: 117 additions & 0 deletions examples/W5500/client-with-W5500-RP2040/client-with-W5500-RP2040.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#include <SPI.h>
#include <Ethernet.h>

#include "TinyMqtt.h" // https://github.com/hsaturn/TinyMqtt

#define WIZRST 20 //Specify pin to use for reseting W5500 = GPIO20
#define WIZCS 17 //Specify the pin for SPI CS = GPIO17 = D5

#define PORT 1883
MqttBroker broker(PORT);
unsigned long Time;
unsigned long freeRam;

/** Basic Mqtt Broker
*
* +-----------------------------+
* | ESP |
* | +--------+ |
* | | broker | | 1883 <--- External client/s
* | +--------+ |
* | |
* +-----------------------------+
*
* Your ESP will become a MqttBroker.
* You can test it with any client such as mqtt-spy for example
*
*/

#if defined(USE_ETHERNET)

byte mac[] = { 0x00, 0xAA, 0xBB, 0xE0, 0x01, 0x25 }; //MAC Address
//IPAddress ip = (192,168,0,88); //Fixed IP Address

#else

const char* ssid = "xxxxxxx";
const char* password = "xxxxxxxx";

#endif


void WizReset() {
Serial.print("Resetting Wiz W5500 Ethernet Board... ");
pinMode(WIZRST, OUTPUT);
digitalWrite(WIZRST, HIGH);
delay(250);
digitalWrite(WIZRST, LOW);
delay(50);
digitalWrite(WIZRST, HIGH);
delay(350);
Serial.println("Done.");
}

void setup()
{
Serial.begin(115200);

#if defined(USE_ETHERNET)

Ethernet.init(WIZCS);
SPI.begin();
WizReset();
Console << TinyConsole::green << "Starting Ethernet...." << endl;
Ethernet.begin(mac); //Connect using DHCP
//Ethernet.begin(mac,ip); //Connect using Fixed IP
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Console << TinyConsole::red << "Ethernet shield was not found. Sorry, can't run without hardware. :(" << endl;
} else
if (Ethernet.linkStatus() == LinkOFF) {
Console << TinyConsole::red << "Ethernet cable is not connected." << endl;
}
Console << TinyConsole::green << "Local Ethernet IP address: " << Ethernet.localIP() << endl;
broker.begin();
Console << "Broker ready (eth) : " << Ethernet.localIP() << " on port " << PORT << endl;

#else

if (strlen(ssid)==0)
Console << TinyConsole::red << "****** PLEASE MODIFY ssid/password *************" << endl;
WiFi.disconnect(); //Remove previous SSID & Password
WiFi.begin(ssid, password); // Connect to the network
Console << TinyConsole::white << "WiFi Connecting to " << ssid << " ...";
int i = 0;
while (WiFi.status() != WL_CONNECTED) { // Wait for the Wi-Fi to connect
delay(1000);
Console << TinyConsole::white << ".";
i++;
if (i > 20) break;
}
Console << endl << endl;
Console << TinyConsole::green << "Connected to " << ssid << " IP address: " << WiFi.localIP() << endl;
broker.begin();
Console << "Broker ready (wifi) : " << WiFi.localIP() << " on port " << PORT << endl;

#endif
}

void loop()
{
broker.loop();
if(millis()-Time>10000) {
Time=millis();
#ifdef ARDUINO_ARCH_RP2040
if(rp2040.getFreeHeap()!=freeRam) {
freeRam=rp2040.getFreeHeap();
#else
if(ESP.getFreeHeap()!=freeRam) {
freeRam=ESP.getFreeHeap();
#endif


Serial.print("RAM:");
Serial.println(freeRam);
}
}

}
5 changes: 5 additions & 0 deletions examples/W5500/client-with-W5500-RP2040/readme
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
In TinyString.h the operator

TinyString& operator +=(int);

may need to be added.

0 comments on commit 66cb80f

Please sign in to comment.