Skip to content

Commit 2a1dcb5

Browse files
committed
Added Paho MQTT C client
Signed-off-by: Frederic Pillon <frederic.pillon@st.com>
1 parent 96bea4c commit 2a1dcb5

File tree

2 files changed

+183
-0
lines changed

2 files changed

+183
-0
lines changed
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2014, 2015 IBM Corp.
3+
*
4+
* All rights reserved. This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License v1.0
6+
* and Eclipse Distribution License v1.0 which accompany this distribution.
7+
*
8+
* The Eclipse Public License is available at
9+
* http://www.eclipse.org/legal/epl-v10.html
10+
* and the Eclipse Distribution License is available at
11+
* http://www.eclipse.org/org/documents/edl-v10.php.
12+
*
13+
* Contributors:
14+
* Ian Craggs - initial contribution
15+
* Benjamin Cabe - adapt to IPStack, and add Yun instructions
16+
* Ian Craggs - remove sprintfs to reduce sketch size
17+
*
18+
* Modified by Frederic Pillon to use the STM32 Ethernet Library
19+
* Only includes have changed:
20+
* #include <SPI.h>
21+
* #include <Ethernet.h>
22+
* Replaced by:
23+
* #include <LwIP.h>
24+
* #include <STM32Ethernet.h>
25+
*******************************************************************************/
26+
27+
#define WARN Serial.println
28+
29+
#define MQTTCLIENT_QOS2 1
30+
31+
32+
#include <LwIP.h>
33+
#include <STM32Ethernet.h>
34+
#include <IPStack.h>
35+
#include <Countdown.h>
36+
#include <MQTTClient.h>
37+
38+
int arrivedcount = 0;
39+
40+
void messageArrived(MQTT::MessageData& md)
41+
{
42+
MQTT::Message &message = md.message;
43+
44+
Serial.print("Message ");
45+
Serial.print(++arrivedcount);
46+
Serial.print(" arrived: qos ");
47+
Serial.print(message.qos);
48+
Serial.print(", retained ");
49+
Serial.print(message.retained);
50+
Serial.print(", dup ");
51+
Serial.print(message.dup);
52+
Serial.print(", packetid ");
53+
Serial.println(message.id);
54+
Serial.print("Payload ");
55+
Serial.println((char*)message.payload);
56+
}
57+
58+
59+
EthernetClient c; // replace by a YunClient if running on a Yun
60+
IPStack ipstack(c);
61+
MQTT::Client<IPStack, Countdown, 50, 1> client = MQTT::Client<IPStack, Countdown, 50, 1>(ipstack);
62+
63+
byte mac[] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 }; // replace with your device's MAC
64+
const char* topic = "arduino-sample";
65+
66+
void connect()
67+
{
68+
char hostname[] = "iot.eclipse.org";
69+
int port = 1883;
70+
71+
Serial.print("Connecting to ");
72+
Serial.print(hostname);
73+
Serial.print(":");
74+
Serial.println(port);
75+
76+
int rc = ipstack.connect(hostname, port);
77+
if (rc != 1)
78+
{
79+
Serial.print("rc from TCP connect is ");
80+
Serial.println(rc);
81+
}
82+
83+
Serial.println("MQTT connecting");
84+
MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
85+
data.MQTTVersion = 3;
86+
data.clientID.cstring = (char*)"arduino-sample";
87+
rc = client.connect(data);
88+
if (rc != 0)
89+
{
90+
Serial.print("rc from MQTT connect is ");
91+
Serial.println(rc);
92+
}
93+
Serial.println("MQTT connected");
94+
95+
rc = client.subscribe(topic, MQTT::QOS2, messageArrived);
96+
if (rc != 0)
97+
{
98+
Serial.print("rc from MQTT subscribe is ");
99+
Serial.println(rc);
100+
}
101+
Serial.println("MQTT subscribed");
102+
}
103+
104+
void setup()
105+
{
106+
Serial.begin(9600);
107+
Ethernet.begin(mac);
108+
Serial.println("MQTT Hello example");
109+
connect();
110+
}
111+
112+
MQTT::Message message;
113+
114+
void loop()
115+
{
116+
if (!client.isConnected())
117+
connect();
118+
119+
arrivedcount = 0;
120+
121+
// Send and receive QoS 0 message
122+
char buf[100];
123+
strcpy(buf, "Hello World! QoS 0 message");
124+
message.qos = MQTT::QOS0;
125+
message.retained = false;
126+
message.dup = false;
127+
message.payload = (void*)buf;
128+
message.payloadlen = strlen(buf)+1;
129+
int rc = client.publish(topic, message);
130+
while (arrivedcount == 0)
131+
{
132+
Serial.println("Waiting for QoS 0 message");
133+
client.yield(1000);
134+
}
135+
136+
// Send and receive QoS 1 message
137+
strcpy(buf, "Hello World! QoS 1 message");
138+
message.qos = MQTT::QOS1;
139+
message.payloadlen = strlen(buf)+1;
140+
rc = client.publish(topic, message);
141+
while (arrivedcount == 1)
142+
{
143+
Serial.println("Waiting for QoS 1 message");
144+
client.yield(1000);
145+
}
146+
147+
// Send and receive QoS 2 message
148+
strcpy(buf, "Hello World! QoS 2 message");
149+
message.qos = MQTT::QOS2;
150+
message.payloadlen = strlen(buf)+1;
151+
rc = client.publish(topic, message);
152+
while (arrivedcount == 2)
153+
{
154+
Serial.println("Waiting for QoS 2 message");
155+
client.yield(1000);
156+
}
157+
delay(2000);
158+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Paho MQTT C client
2+
3+
Required to install prebuilt Arduino port of MQTTClient.
4+
5+
See http://www.eclipse.org/paho/clients/c/embedded/
6+
7+
Download the prebuilt Arduino port of MQTTClient and in the Arduino IDE use
8+
**_Sketch -> Include Library -> Add .ZIP Library..._** with the downloaded client zip file.
9+
10+
# Examples
11+
12+
## Hello
13+
This is the basic example provided with the MQTTClient library.
14+
It has been modified to use the [STM32 Ethernet](https://github.com/stm32duino/STM32Ethernet) library.
15+
16+
Only includes have changed:
17+
```
18+
#include <SPI.h>
19+
#include <Ethernet.h>
20+
```
21+
Replaced by:
22+
```
23+
#include <LwIP.h>
24+
#include <STM32Ethernet.h>
25+
```

0 commit comments

Comments
 (0)