This repository has been archived by the owner on Feb 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
v1.5.0 to add support to ESP8266 W5x00/ENC28J60
### Releases v1.5.0 1. Add support to ESP8266 W5x00 using [lwIP_w5100](https://github.com/esp8266/Arduino/tree/master/libraries/lwIP_w5100) or [lwIP_w5500](https://github.com/esp8266/Arduino/tree/master/libraries/lwIP_w5500) library 2. Add support to ESP8266 ENC28J60 using [lwIP_enc28j60](https://github.com/esp8266/Arduino/tree/master/libraries/lwIP_enc28j60) library 3. Add example [FullyFeatured_ESP8266_Ethernet](examples/ESP8266/FullyFeatured_ESP8266_Ethernet) 4. Update `Packages' Patches`
- Loading branch information
1 parent
7e8df4c
commit 6b024e2
Showing
2 changed files
with
296 additions
and
0 deletions.
There are no files selected for viewing
231 changes: 231 additions & 0 deletions
231
examples/ESP8266/FullyFeatured_ESP8266_Ethernet/FullyFeatured_ESP8266_Ethernet.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,231 @@ | ||
/**************************************************************************************************************************** | ||
FullyFeatured_ESP8266_Ethernet.ino | ||
AsyncMqttClient_Generic is a library for ESP32, ESP8266, Protenta_H7, STM32F7, etc. with current AsyncTCP support | ||
Based on and modified from : | ||
1) async-mqtt-client (https://github.com/marvinroger/async-mqtt-client) | ||
Built by Khoi Hoang https://github.com/khoih-prog/AsyncMqttClient_Generic | ||
*****************************************************************************************************************************/ | ||
|
||
#include "defines.h" | ||
|
||
// Check connection every 2s | ||
#define MQTT_CHECK_INTERVAL_MS 2000 | ||
|
||
#include <Ticker.h> | ||
#include <AsyncMqtt_Generic.h> | ||
|
||
//#define MQTT_HOST IPAddress(192, 168, 2, 110) | ||
#define MQTT_HOST "broker.emqx.io" // Broker address | ||
#define MQTT_PORT 1883 | ||
|
||
const char *PubTopic = "async-mqtt/ESP8266_Pub"; // Topic to publish | ||
|
||
AsyncMqttClient mqttClient; | ||
|
||
void connectToMqtt(); | ||
void connectToMqttCheck(); | ||
|
||
// Repeat forever, millis() resolution | ||
Ticker connectToMqttTicker; | ||
Ticker mqttReconnectTimer; | ||
|
||
bool connectedEthernet = false; | ||
bool connectedMQTT = false; | ||
|
||
void initEthernet() | ||
{ | ||
SPI.begin(); | ||
SPI.setClockDivider(SPI_CLOCK_DIV4); | ||
SPI.setBitOrder(MSBFIRST); | ||
SPI.setDataMode(SPI_MODE0); | ||
|
||
#if !USING_DHCP | ||
eth.config(localIP, gateway, netMask, gateway); | ||
#endif | ||
|
||
eth.setDefault(); | ||
|
||
if (!eth.begin()) | ||
{ | ||
Serial.println("No Ethernet hardware ... Stop here"); | ||
|
||
while (true) | ||
{ | ||
delay(1000); | ||
} | ||
} | ||
else | ||
{ | ||
Serial.print("Connecting to network : "); | ||
|
||
while (!eth.connected()) | ||
{ | ||
Serial.print("."); | ||
delay(1000); | ||
} | ||
} | ||
|
||
connectedEthernet = true; | ||
|
||
Serial.println(); | ||
|
||
#if USING_DHCP | ||
Serial.print("Ethernet DHCP IP address: "); | ||
#else | ||
Serial.print("Ethernet Static IP address: "); | ||
#endif | ||
|
||
Serial.println(eth.localIP()); | ||
} | ||
|
||
void connectToMqttCheck() | ||
{ | ||
if (eth.connected()) | ||
{ | ||
AMQTT_LOGDEBUG("C"); | ||
|
||
if (!connectedMQTT) | ||
{ | ||
mqttClient.connect(); | ||
} | ||
|
||
if (!connectedEthernet) | ||
{ | ||
Serial.println("Ethernet reconnected."); | ||
connectedEthernet = true; | ||
} | ||
} | ||
else | ||
{ | ||
AMQTT_LOGDEBUG("N"); | ||
|
||
if (connectedEthernet) | ||
{ | ||
Serial.println("Ethernet disconnected"); | ||
connectedEthernet = false; | ||
} | ||
} | ||
} | ||
|
||
void connectToMqtt() | ||
{ | ||
Serial.println("Connecting to MQTT..."); | ||
mqttClient.connect(); | ||
} | ||
|
||
void printSeparationLine() | ||
{ | ||
Serial.println("************************************************"); | ||
} | ||
|
||
void onMqttConnect(bool sessionPresent) | ||
{ | ||
Serial.print("Connected to MQTT broker: "); Serial.print(MQTT_HOST); | ||
Serial.print(", port: "); Serial.println(MQTT_PORT); | ||
Serial.print("PubTopic: "); Serial.println(PubTopic); | ||
|
||
connectedMQTT = true; | ||
|
||
printSeparationLine(); | ||
Serial.print("Session present: "); Serial.println(sessionPresent); | ||
|
||
uint16_t packetIdSub = mqttClient.subscribe(PubTopic, 2); | ||
Serial.print("Subscribing at QoS 2, packetId: "); Serial.println(packetIdSub); | ||
|
||
mqttClient.publish(PubTopic, 0, true, "ESP8266_Ethernet Test1"); | ||
Serial.println("Publishing at QoS 0"); | ||
|
||
uint16_t packetIdPub1 = mqttClient.publish(PubTopic, 1, true, "ESP8266_Ethernet Test2"); | ||
Serial.print("Publishing at QoS 1, packetId: "); Serial.println(packetIdPub1); | ||
|
||
uint16_t packetIdPub2 = mqttClient.publish(PubTopic, 2, true, "ESP8266_Ethernet Test3"); | ||
Serial.print("Publishing at QoS 2, packetId: "); Serial.println(packetIdPub2); | ||
|
||
printSeparationLine(); | ||
} | ||
|
||
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) | ||
{ | ||
(void) reason; | ||
|
||
connectedMQTT = false; | ||
|
||
Serial.println("Disconnected from MQTT."); | ||
|
||
if (eth.connected()) | ||
{ | ||
mqttReconnectTimer.once(2, connectToMqtt); | ||
} | ||
} | ||
|
||
void onMqttSubscribe(const uint16_t& packetId, const uint8_t& qos) | ||
{ | ||
Serial.println("Subscribe acknowledged."); | ||
Serial.print(" packetId: "); Serial.println(packetId); | ||
Serial.print(" qos: "); Serial.println(qos); | ||
} | ||
|
||
void onMqttUnsubscribe(const uint16_t& packetId) | ||
{ | ||
Serial.println("Unsubscribe acknowledged."); | ||
Serial.print(" packetId: "); Serial.println(packetId); | ||
} | ||
|
||
void onMqttMessage(char* topic, char* payload, const AsyncMqttClientMessageProperties& properties, | ||
const size_t& len, const size_t& index, const size_t& total) | ||
{ | ||
char message[len + 1]; | ||
|
||
memcpy(message, payload, len); | ||
message[len] = 0; | ||
|
||
Serial.println("Publish received."); | ||
Serial.print(" topic: "); Serial.println(topic); | ||
Serial.print(" message: "); Serial.println(message); | ||
Serial.print(" qos: "); Serial.println(properties.qos); | ||
Serial.print(" dup: "); Serial.println(properties.dup); | ||
Serial.print(" retain: "); Serial.println(properties.retain); | ||
Serial.print(" len: "); Serial.println(len); | ||
Serial.print(" index: "); Serial.println(index); | ||
Serial.print(" total: "); Serial.println(total); | ||
} | ||
|
||
void onMqttPublish(const uint16_t& packetId) | ||
{ | ||
Serial.println("Publish acknowledged."); | ||
Serial.print(" packetId: "); Serial.println(packetId); | ||
} | ||
|
||
void setup() | ||
{ | ||
Serial.begin(115200); | ||
while (!Serial && millis() < 5000); | ||
|
||
delay(300); | ||
|
||
Serial.print("\nStarting FullyFeatured_ESP8266_Ethernet on "); Serial.print(ARDUINO_BOARD); | ||
Serial.print(F(" using ")); Serial.println(SHIELD_TYPE); | ||
Serial.println(ASYNC_MQTT_GENERIC_VERSION); | ||
|
||
initEthernet(); | ||
|
||
mqttClient.onConnect(onMqttConnect); | ||
mqttClient.onDisconnect(onMqttDisconnect); | ||
mqttClient.onSubscribe(onMqttSubscribe); | ||
mqttClient.onUnsubscribe(onMqttUnsubscribe); | ||
mqttClient.onMessage(onMqttMessage); | ||
mqttClient.onPublish(onMqttPublish); | ||
mqttClient.setServer(MQTT_HOST, MQTT_PORT); | ||
|
||
connectToMqttTicker.attach_ms(MQTT_CHECK_INTERVAL_MS, connectToMqttCheck); | ||
|
||
connectToMqtt(); | ||
} | ||
|
||
void loop() | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/**************************************************************************************************************************** | ||
defines.h | ||
AsyncMqttClient_Generic is a library for ESP32, ESP8266, Protenta_H7, STM32F7, etc. with current AsyncTCP support | ||
Based on and modified from : | ||
1) async-mqtt-client (https://github.com/marvinroger/async-mqtt-client) | ||
Built by Khoi Hoang https://github.com/khoih-prog/AsyncMqttClient_Generic | ||
***************************************************************************************************************************************/ | ||
|
||
#ifndef defines_h | ||
#define defines_h | ||
|
||
#define _ASYNC_MQTT_LOGLEVEL_ 1 | ||
|
||
////////////////////////////////////////////////////////// | ||
|
||
#define USING_W5500 true | ||
#define USING_W5100 false | ||
#define USING_ENC28J60 false | ||
|
||
#include <SPI.h> | ||
|
||
#define CSPIN 16 // 5 | ||
|
||
#if USING_W5500 | ||
#include "W5500lwIP.h" | ||
#define SHIELD_TYPE "ESP8266_W5500 Ethernet" | ||
|
||
Wiznet5500lwIP eth(CSPIN); | ||
|
||
#elif USING_W5100 | ||
#include <W5100lwIP.h> | ||
#define SHIELD_TYPE "ESP8266_W5100 Ethernet" | ||
|
||
Wiznet5100lwIP eth(CSPIN); | ||
|
||
#elif USING_ENC28J60 | ||
#include <ENC28J60lwIP.h> | ||
#define SHIELD_TYPE "ESP8266_ENC28J60 Ethernet" | ||
|
||
ENC28J60lwIP eth(CSPIN); | ||
#else | ||
// default if none selected | ||
#include "W5500lwIP.h" | ||
|
||
Wiznet5500lwIP eth(CSPIN); | ||
#endif | ||
|
||
#include <WiFiClient.h> // WiFiClient (-> TCPClient) | ||
|
||
using TCPClient = WiFiClient; | ||
|
||
////////////////////////////////////////////////////////// | ||
|
||
#define USING_DHCP true | ||
|
||
#if !USING_DHCP | ||
IPAddress localIP(192, 168, 2, 222); | ||
IPAddress gateway(192, 168, 2, 1); | ||
IPAddress netMask(255, 255, 255, 0); | ||
#endif | ||
|
||
#endif //defines_h |