Skip to content

Commit d3b26f4

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 d3b26f4

File tree

1 file changed

+154
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)