Skip to content

Commit 446f4cb

Browse files
[TMP] adding an example that had been used to test the new mqtt interface
This example may be deleted before merging
1 parent 5dac58d commit 446f4cb

File tree

1 file changed

+155
-0
lines changed

1 file changed

+155
-0
lines changed
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
/*
2+
* The following example is added for testing purposes and may be deleted before publishing the PR.
3+
* This had been tested with mosquitto with the following config files inside of a local directory `/mosquitto/config`
4+
* ```
5+
* port 1883
6+
* allow_anonymous true
7+
* password_file /mosquitto/config/passwd
8+
* ```
9+
* passwd file containing auth arduino:arduino:
10+
* ```
11+
* arduino:$7$101$CkoJckWT8WWX9+Z1$mzMdh0TD99P41AEAT55GXDIPf8FLKXp7LS3BTpEvhBUnXMYj4myWSGMH7aShCufuvsbtUXChk+KCW3AncUgtuA==
12+
* ```
13+
* run with docker:
14+
* ```
15+
* docker run -it -p 1883:1883 -v "${PWD}/mosquitto/config:/mosquitto/config" eclipse-mosquitto
16+
* ```
17+
*/
18+
19+
#include <Arduino.h>
20+
#include <Arduino_DebugUtils.h>
21+
#include <ArduinoMqttClient.h>
22+
#include <Ethernet.h>
23+
24+
uint8_t payload[] = {
25+
0xda, 0x00, 0x01, 0x00, 0x00, 0x81, 0x58, 0x20,
26+
0x1c, 0x05, 0xeb, 0xfa, 0xee, 0x5a, 0x38, 0xa5,
27+
0x44, 0x05, 0xe9, 0x1b, 0xaa, 0xeb, 0xeb, 0x40,
28+
0x68, 0x3e, 0xad, 0x1f, 0x93, 0x90, 0xd8, 0x57,
29+
0x4a, 0xe3, 0x35, 0x9d, 0x84, 0xc9, 0x0f, 0x64,
30+
};
31+
32+
char topic[] = "/a/d/f334982b-032f-40e9-a91a-99da31a90dbe/c/up";
33+
uint32_t start;
34+
int i = 0;
35+
36+
MqttClient client;
37+
// ZephyrMqttClient client;
38+
volatile bool packet = false;
39+
40+
void mqtt_cbk(const char* topic) {
41+
Serial.print("Received a message on topic: \"");
42+
Serial.print(topic);
43+
// Serial.print("\" with payload: 0x");
44+
45+
// while(stream.available() > 0) {
46+
// int res = stream.read();
47+
48+
// // if(res < 0x10) {
49+
// // Serial.print('0');
50+
// // }
51+
// // Serial.print(res, HEX);
52+
// }
53+
54+
// while(client.available() > 0) {
55+
// int res = client.read();
56+
57+
// if(res < 0x10) {
58+
// Serial.print('0');
59+
// }
60+
// Serial.print(res, HEX);
61+
// }
62+
// packet = true;
63+
64+
Serial.println();
65+
}
66+
67+
void setup() {
68+
// put your setup code here, to run once:
69+
Serial.begin(115200);
70+
while (!Serial) {
71+
; // wait for serial port to connect. Needed for native USB port only
72+
}
73+
74+
Serial.print("begin ");
75+
Serial.println(i);
76+
// int err = tls_credential_add(APP_CA_CERT_TAG, TLS_CREDENTIAL_CA_CERTIFICATE,
77+
// ca_certificate, sizeof(ca_certificate));
78+
// if (err < 0) {
79+
// DEBUG_ERROR("Failed to register public certificate: %d", err);
80+
// }
81+
82+
// err = tls_credential_add(APP_KEYS, TLS_CREDENTIAL_PUBLIC_CERTIFICATE,
83+
// client_public_key, sizeof(client_public_key));
84+
// if (err < 0) {
85+
// DEBUG_ERROR("Failed to register puiblic key: %d", err);
86+
// }
87+
88+
// err = tls_credential_add(APP_KEYS, TLS_CREDENTIAL_PRIVATE_KEY,
89+
// client_private_key, sizeof(client_private_key));
90+
// if (err < 0) {
91+
// DEBUG_ERROR("Failed to register private key: %d", err);
92+
// }
93+
94+
while (Ethernet.linkStatus() != LinkON) {
95+
Serial.println("waiting for link on");
96+
delay(100);
97+
}
98+
Ethernet.begin();
99+
100+
Serial.println(Ethernet.localIP());
101+
102+
client.setId("f334982b-032f-40e9-a91a-99da31a90dbe");
103+
client.setUsernamePassword("arduino", "arduino");
104+
105+
client.setReceiveCallback(mqtt_cbk);
106+
107+
// int res = client.connect("iot.arduino.cc", 8885);
108+
int res = client.connect(IPAddress(192, 168, 10, 250), 1883);
109+
// int res = client.connect("test.mosquitto.org", 1883);
110+
111+
Serial.print("connection result: ");
112+
Serial.println(res);
113+
114+
res = client.subscribe("test0", MqttQos0);
115+
res = client.subscribe("test1", MqttQos1);
116+
res = client.subscribe("test2", MqttQos2);
117+
118+
Serial.print("subscribe result: ");
119+
Serial.println(res);
120+
121+
start = millis();
122+
}
123+
void loop() {
124+
client.poll();
125+
126+
if(client.available() > 0) {
127+
Serial.println("packet received");
128+
Serial.println(client.available());
129+
130+
while(client.available() > 0) {
131+
int res = client.read();
132+
133+
if(res < 0x10) {
134+
Serial.print('0');
135+
}
136+
Serial.print(res, HEX);
137+
}
138+
139+
packet = false;
140+
}
141+
142+
if(millis() - start > 10000) {
143+
int res = client.publish(
144+
topic,
145+
payload,
146+
sizeof(payload)
147+
);
148+
149+
Serial.print("publish res: ");
150+
Serial.println(res);
151+
152+
start = millis();
153+
}
154+
delay(100);
155+
}

0 commit comments

Comments
 (0)