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

Commit 9fbcce3

Browse files
authored
v1.3.0 to support Portenta_H7
### Releases v1.3.0 1. Add support to **Portenta_H7**, using either `Murata WiFi` or `Vision-shield Ethernet` 2. Add examples for `Portenta_H7_Ethernet` and `Portenta_H7_WiFi`
1 parent a89ee2c commit 9fbcce3

File tree

4 files changed

+523
-0
lines changed

4 files changed

+523
-0
lines changed
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
/****************************************************************************************************************************
2+
FullyFeatured_PortentaH7_Ethernet.ino
3+
4+
AsyncMqttClient_Generic is a library for ESP32, ESP8266, Protenta_H7, STM32F7, 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+
#include <AsyncMqtt_Generic.h>
16+
17+
using namespace rtos;
18+
Thread connectThread;
19+
20+
//#define MQTT_HOST IPAddress(192, 168, 2, 110)
21+
#define MQTT_HOST "broker.emqx.io" // Broker address
22+
#define MQTT_PORT 1883
23+
24+
const char *PubTopic = "async-mqtt/Portenta_H7_Ethernet_Pub"; // Topic to publish
25+
26+
AsyncMqttClient mqttClient;
27+
28+
bool connectedEthernet = false;
29+
bool connectedMQTT = false;
30+
31+
// Check connection every 1s
32+
#define MQTT_CHECK_INTERVAL_MS 1000
33+
34+
void connectToMqttLoop()
35+
{
36+
while (true)
37+
{
38+
if (Ethernet.linkStatus() == LinkON)
39+
{
40+
if (!connectedMQTT)
41+
{
42+
mqttClient.connect();
43+
}
44+
45+
if (!connectedEthernet)
46+
{
47+
Serial.println("Ethernet reconnected.");
48+
connectedEthernet = true;
49+
}
50+
}
51+
else
52+
{
53+
if (connectedEthernet)
54+
{
55+
Serial.println("Ethernet disconnected");
56+
connectedEthernet = false;
57+
}
58+
}
59+
60+
delay(MQTT_CHECK_INTERVAL_MS);
61+
}
62+
}
63+
64+
void connectToMqtt()
65+
{
66+
Serial.println("Connecting to MQTT...");
67+
mqttClient.connect();
68+
}
69+
70+
void printSeparationLine()
71+
{
72+
Serial.println("************************************************");
73+
}
74+
75+
void onMqttConnect(bool sessionPresent)
76+
{
77+
Serial.print("Connected to MQTT broker: "); Serial.print(MQTT_HOST);
78+
Serial.print(", port: "); Serial.println(MQTT_PORT);
79+
Serial.print("PubTopic: "); Serial.println(PubTopic);
80+
81+
connectedMQTT = true;
82+
83+
printSeparationLine();
84+
Serial.print("Session present: "); Serial.println(sessionPresent);
85+
86+
uint16_t packetIdSub = mqttClient.subscribe(PubTopic, 2);
87+
Serial.print("Subscribing at QoS 2, packetId: "); Serial.println(packetIdSub);
88+
89+
mqttClient.publish(PubTopic, 0, true, "Portenta_H7_Ethernet Test1");
90+
Serial.println("Publishing at QoS 0");
91+
92+
uint16_t packetIdPub1 = mqttClient.publish(PubTopic, 1, true, "Portenta_H7_Ethernet Test2");
93+
Serial.print("Publishing at QoS 1, packetId: "); Serial.println(packetIdPub1);
94+
95+
uint16_t packetIdPub2 = mqttClient.publish(PubTopic, 2, true, "Portenta_H7_Ethernet Test3");
96+
Serial.print("Publishing at QoS 2, packetId: "); Serial.println(packetIdPub2);
97+
98+
printSeparationLine();
99+
}
100+
101+
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason)
102+
{
103+
(void) reason;
104+
105+
connectedMQTT = false;
106+
107+
Serial.println("Disconnected from MQTT.");
108+
}
109+
110+
void onMqttSubscribe(const uint16_t& packetId, const uint8_t& qos)
111+
{
112+
Serial.println("Subscribe acknowledged.");
113+
Serial.print(" packetId: "); Serial.println(packetId);
114+
Serial.print(" qos: "); Serial.println(qos);
115+
}
116+
117+
void onMqttUnsubscribe(const uint16_t& packetId)
118+
{
119+
Serial.println("Unsubscribe acknowledged.");
120+
Serial.print(" packetId: "); Serial.println(packetId);
121+
}
122+
123+
void onMqttMessage(char* topic, char* payload, const AsyncMqttClientMessageProperties& properties,
124+
const size_t& len, const size_t& index, const size_t& total)
125+
{
126+
char message[len + 1];
127+
128+
memcpy(message, payload, len);
129+
message[len] = 0;
130+
131+
Serial.println("Publish received.");
132+
Serial.print(" topic: "); Serial.println(topic);
133+
Serial.print(" message: "); Serial.println(message);
134+
Serial.print(" qos: "); Serial.println(properties.qos);
135+
Serial.print(" dup: "); Serial.println(properties.dup);
136+
Serial.print(" retain: "); Serial.println(properties.retain);
137+
Serial.print(" len: "); Serial.println(len);
138+
Serial.print(" index: "); Serial.println(index);
139+
Serial.print(" total: "); Serial.println(total);
140+
}
141+
142+
void onMqttPublish(const uint16_t& packetId)
143+
{
144+
Serial.println("Publish acknowledged.");
145+
Serial.print(" packetId: "); Serial.println(packetId);
146+
}
147+
148+
void setup()
149+
{
150+
Serial.begin(115200);
151+
while (!Serial);
152+
153+
Serial.print("\nStarting FullyFeatured_PortentaH7_Ethernet on "); Serial.println(BOARD_NAME);
154+
Serial.println(ASYNC_MQTT_GENERIC_VERSION);
155+
156+
// start the ethernet connection and the server
157+
158+
// Use Static IP
159+
//Ethernet.begin(mac[index], ip);
160+
// Use DHCP dynamic IP and built-in mac
161+
Ethernet.begin();
162+
// Use DHCP dynamic IP and random mac
163+
//uint16_t index = micros() % NUMBER_OF_MAC;
164+
//Ethernet.begin(mac[index]);
165+
166+
Serial.print("Connected to network. IP = ");
167+
Serial.println(Ethernet.localIP());
168+
169+
connectedEthernet = true;
170+
171+
mqttClient.onConnect(onMqttConnect);
172+
mqttClient.onDisconnect(onMqttDisconnect);
173+
mqttClient.onSubscribe(onMqttSubscribe);
174+
mqttClient.onUnsubscribe(onMqttUnsubscribe);
175+
mqttClient.onMessage(onMqttMessage);
176+
mqttClient.onPublish(onMqttPublish);
177+
178+
mqttClient.setServer(MQTT_HOST, MQTT_PORT);
179+
180+
// Add "connectToMqttLoop" loop to control connection To Mqtt
181+
connectThread.start(connectToMqttLoop);
182+
183+
connectToMqtt();
184+
}
185+
186+
void loop()
187+
{
188+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_PORTENTA_H7_M4) )
16+
#error For Portenta_H7 core M7 only
17+
#endif
18+
19+
// Use from 0 to 4. Higher number, more debugging messages and memory usage.
20+
#define ASYNC_MQTT_DEBUG_PORT Serial
21+
#define _ASYNC_MQTT_LOGLEVEL_ 1
22+
23+
#define USE_ETHERNET_PORTENTA_H7 true
24+
#define USE_WIFI_PORTENTA_H7 false
25+
26+
#include <Portenta_Ethernet.h>
27+
28+
#if (_ASYNC_MQTT_LOGLEVEL_ > 3)
29+
#warning Using Portenta_Ethernet lib for Portenta_H7
30+
#endif
31+
32+
// Enter a MAC address and IP address for your controller below.
33+
#define NUMBER_OF_MAC 20
34+
35+
byte mac[][NUMBER_OF_MAC] =
36+
{
37+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x01 },
38+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x02 },
39+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x03 },
40+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x04 },
41+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x05 },
42+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x06 },
43+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x07 },
44+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x08 },
45+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x09 },
46+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0A },
47+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0B },
48+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0C },
49+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0D },
50+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0E },
51+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0F },
52+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x10 },
53+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x11 },
54+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x12 },
55+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x13 },
56+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x14 },
57+
};
58+
59+
#endif //defines_h

0 commit comments

Comments
 (0)