Skip to content
This repository was archived by the owner on Feb 4, 2023. It is now read-only.

Commit aa0b743

Browse files
authored
v1.4.0 to add Teensy41 and QNEthernet
### Releases v1.4.0 1. Add support to **Teensy 4.1 using QNEthernet Library** 2. Add example for `QNEthernet`
1 parent 3e6d9fc commit aa0b743

File tree

2 files changed

+259
-0
lines changed

2 files changed

+259
-0
lines changed
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
/****************************************************************************************************************************
2+
FullyFeatured_QNEthernet.ino
3+
4+
AsyncMqttClient_Generic is a library for ESP32, ESP8266, Protenta_H7, Teensy41_QNEthernet_PubF7, etc. with current AsyncTCP support
5+
6+
Based on and modified from :
7+
8+
1) async-mqtt-client (https://github.com/marvinroger/async-mqtt-client)
9+
10+
Built by Khoi Hoang https://github.com/khoih-prog/AsyncMqttClient_Generic
11+
*****************************************************************************************************************************/
12+
13+
#include "defines.h"
14+
15+
// Check connection every 1s
16+
#define MQTT_CHECK_INTERVAL_MS 1000
17+
18+
#include <AsyncMqtt_Generic.h>
19+
20+
#include <Ticker.h> // https://github.com/sstaub/Ticker
21+
22+
//#define MQTT_HOST IPAddress(192, 168, 2, 110)
23+
#define MQTT_HOST "broker.emqx.io" // Broker address
24+
#define MQTT_PORT 1883
25+
26+
const char *PubTopic = "async-mqtt/Teensy41_QNEthernet_Pub"; // Topic to publish
27+
28+
AsyncMqttClient mqttClient;
29+
30+
void connectToMqtt();
31+
void connectToMqttCheck();
32+
33+
// Repeat forever, millis() resolution
34+
Ticker connectToMqttTicker(connectToMqttCheck, MQTT_CHECK_INTERVAL_MS, 0, MILLIS);
35+
36+
bool connectedEthernet = false;
37+
bool connectedMQTT = false;
38+
39+
void connectToMqttCheck()
40+
{
41+
if (Ethernet.linkStatus() == LinkON)
42+
{
43+
//Ethernet.maintain();
44+
45+
if (!connectedMQTT)
46+
{
47+
mqttClient.connect();
48+
}
49+
50+
if (!connectedEthernet)
51+
{
52+
Serial.println("Ethernet reconnected.");
53+
connectedEthernet = true;
54+
}
55+
}
56+
else
57+
{
58+
if (connectedEthernet)
59+
{
60+
Serial.println("Ethernet disconnected");
61+
connectedEthernet = false;
62+
}
63+
}
64+
}
65+
66+
void connectToMqtt()
67+
{
68+
Serial.println("Connecting to MQTT...");
69+
mqttClient.connect();
70+
}
71+
72+
void printSeparationLine()
73+
{
74+
Serial.println("************************************************");
75+
}
76+
77+
void onMqttConnect(bool sessionPresent)
78+
{
79+
Serial.print("Connected to MQTT broker: "); Serial.print(MQTT_HOST);
80+
Serial.print(", port: "); Serial.println(MQTT_PORT);
81+
Serial.print("PubTopic: "); Serial.println(PubTopic);
82+
83+
connectedMQTT = true;
84+
85+
printSeparationLine();
86+
Serial.print("Session present: "); Serial.println(sessionPresent);
87+
88+
uint16_t packetIdSub = mqttClient.subscribe(PubTopic, 2);
89+
Serial.print("Subscribing at QoS 2, packetId: "); Serial.println(packetIdSub);
90+
91+
mqttClient.publish(PubTopic, 0, true, "Teensy41_QNEthernet_Pub Test1");
92+
Serial.println("Publishing at QoS 0");
93+
94+
uint16_t packetIdPub1 = mqttClient.publish(PubTopic, 1, true, "Teensy41_QNEthernet_Pub Test2");
95+
Serial.print("Publishing at QoS 1, packetId: "); Serial.println(packetIdPub1);
96+
97+
uint16_t packetIdPub2 = mqttClient.publish(PubTopic, 2, true, "Teensy41_QNEthernet_Pub Test3");
98+
Serial.print("Publishing at QoS 2, packetId: "); Serial.println(packetIdPub2);
99+
100+
printSeparationLine();
101+
}
102+
103+
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason)
104+
{
105+
(void) reason;
106+
107+
connectedMQTT = false;
108+
109+
Serial.println("Disconnected from MQTT.");
110+
}
111+
112+
void onMqttSubscribe(const uint16_t& packetId, const uint8_t& qos)
113+
{
114+
Serial.println("Subscribe acknowledged.");
115+
Serial.print(" packetId: "); Serial.println(packetId);
116+
Serial.print(" qos: "); Serial.println(qos);
117+
}
118+
119+
void onMqttUnsubscribe(const uint16_t& packetId)
120+
{
121+
Serial.println("Unsubscribe acknowledged.");
122+
Serial.print(" packetId: "); Serial.println(packetId);
123+
}
124+
125+
void onMqttMessage(char* topic, char* payload, const AsyncMqttClientMessageProperties& properties,
126+
const size_t& len, const size_t& index, const size_t& total)
127+
{
128+
char message[len + 1];
129+
130+
memcpy(message, payload, len);
131+
message[len] = 0;
132+
133+
Serial.println("Publish received.");
134+
Serial.print(" topic: "); Serial.println(topic);
135+
Serial.print(" message: "); Serial.println(message);
136+
Serial.print(" qos: "); Serial.println(properties.qos);
137+
Serial.print(" dup: "); Serial.println(properties.dup);
138+
Serial.print(" retain: "); Serial.println(properties.retain);
139+
Serial.print(" len: "); Serial.println(len);
140+
Serial.print(" index: "); Serial.println(index);
141+
Serial.print(" total: "); Serial.println(total);
142+
}
143+
144+
void onMqttPublish(const uint16_t& packetId)
145+
{
146+
Serial.println("Publish acknowledged.");
147+
Serial.print(" packetId: "); Serial.println(packetId);
148+
}
149+
150+
void setup()
151+
{
152+
Serial.begin(115200);
153+
while (!Serial);
154+
155+
delay(500);
156+
157+
Serial.print("\nStarting FullyFeatured_QNEthernet on "); Serial.println(BOARD_NAME);
158+
Serial.println(ASYNC_MQTT_GENERIC_VERSION);
159+
160+
#if USING_DHCP
161+
// Start the Ethernet connection, using DHCP
162+
Serial.print("Initialize Ethernet using DHCP => ");
163+
Ethernet.begin();
164+
#else
165+
// Start the Ethernet connection, using static IP
166+
Serial.print("Initialize Ethernet using static IP => ");
167+
Ethernet.begin(myIP, myNetmask, myGW);
168+
Ethernet.setDNSServerIP(mydnsServer);
169+
#endif
170+
171+
if (!Ethernet.waitForLocalIP(5000))
172+
{
173+
Serial.println(F("Failed to configure Ethernet"));
174+
175+
if (!Ethernet.linkStatus())
176+
{
177+
Serial.println(F("Ethernet cable is not connected."));
178+
}
179+
180+
// Stay here forever
181+
while (true)
182+
{
183+
delay(1);
184+
}
185+
}
186+
else
187+
{
188+
Serial.print(F("Connected! IP address:")); Serial.println(Ethernet.localIP());
189+
}
190+
191+
#if USING_DHCP
192+
delay(1000);
193+
#else
194+
delay(2000);
195+
#endif
196+
197+
connectedEthernet = true;
198+
199+
mqttClient.onConnect(onMqttConnect);
200+
mqttClient.onDisconnect(onMqttDisconnect);
201+
mqttClient.onSubscribe(onMqttSubscribe);
202+
mqttClient.onUnsubscribe(onMqttUnsubscribe);
203+
mqttClient.onMessage(onMqttMessage);
204+
mqttClient.onPublish(onMqttPublish);
205+
206+
mqttClient.setServer(MQTT_HOST, MQTT_PORT);
207+
208+
connectToMqttTicker.start(); //start the ticker.
209+
210+
connectToMqtt();
211+
}
212+
213+
void loop()
214+
{
215+
connectToMqttTicker.update(); //update the ticker.
216+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/****************************************************************************************************************************
2+
defines.h
3+
AsyncMqttClient_Generic is a library for ESP32, ESP8266, Protenta_H7, STM32F7, etc. with current AsyncTCP support
4+
5+
Based on and modified from :
6+
7+
1) async-mqtt-client (https://github.com/marvinroger/async-mqtt-client)
8+
9+
Built by Khoi Hoang https://github.com/khoih-prog/AsyncMqttClient_Generic
10+
***************************************************************************************************************************************/
11+
12+
#ifndef defines_h
13+
#define defines_h
14+
15+
#if !( defined(CORE_TEENSY) && defined(__IMXRT1062__) && defined(ARDUINO_TEENSY41) )
16+
#error Only Teensy 4.1 supported
17+
#endif
18+
19+
// Debug Level from 0 to 4
20+
#define _TEENSY41_ASYNC_TCP_LOGLEVEL_ 1
21+
#define _ASYNC_MQTT_LOGLEVEL_ 4
22+
23+
#define SHIELD_TYPE "Teensy4.1 QNEthernet"
24+
25+
#include "QNEthernet.h" // https://github.com/ssilverman/QNEthernet
26+
using namespace qindesign::network;
27+
28+
#if (_ASYNC_MQTT_LOGLEVEL_ > 3)
29+
#warning Using QNEthernet lib for Teensy 4.1. Must also use Teensy Packages Patch or error
30+
#endif
31+
#define USING_DHCP true
32+
//#define USING_DHCP false
33+
34+
#if !USING_DHCP
35+
// Set the static IP address to use if the DHCP fails to assign
36+
IPAddress myIP(192, 168, 2, 222);
37+
IPAddress myNetmask(255, 255, 255, 0);
38+
IPAddress myGW(192, 168, 2, 1);
39+
//IPAddress mydnsServer(192, 168, 2, 1);
40+
IPAddress mydnsServer(8, 8, 8, 8);
41+
#endif
42+
43+
#endif //defines_h

0 commit comments

Comments
 (0)