-
Notifications
You must be signed in to change notification settings - Fork 76
/
CosaMQTTclient.ino
119 lines (100 loc) · 3.54 KB
/
CosaMQTTclient.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
* @file CosaMQTT.ino
* @version 1.0
*
* @section License
* Copyright (C) 2014-2015, Mikael Patel
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* @section Description
* W5100 Ethernet Controller device driver example code; MQTT client.
*
* @section Circuit
* This sketch is designed for the Ethernet Shield.
*
* W5100/ethernet
* +------------+
* (D10)--------------29-|CSN |
* (D11)--------------28-|MOSI |
* (D12)--------------27-|MISO |
* (D13)--------------30-|SCK |
* (D2)-----[ ]-------56-|IRQ |
* +------------+
*
* This file is part of the Arduino Che Cosa project.
*/
#include <MQTT.h>
#include "Cosa/Watchdog.hh"
#include "Cosa/Trace.hh"
#include "Cosa/IOStream/Driver/UART.hh"
#include "Cosa/Socket/Driver/W5100.hh"
// Network configuration
#define MOSQUITTO "test.mosquitto.org"
#define RABBITMQ "dev.rabbitmq.com"
#define ECLIPSE "iot.eclipse.org"
#define QM2M "q.m2m.io"
#define SERVER QM2M
// Semi-simple MQTT client; trace incoming topic/value and echo back
class MQTTClient : public MQTT::Client {
public:
virtual void on_publish(char* topic, void* buf, size_t count);
};
void
MQTTClient::on_publish(char* topic, void* buf, size_t count)
{
// Print the topic and value to trace stream
trace << PSTR("on_publish::count = ") << count << endl;
trace << PSTR(" topic = ") << topic << endl;
trace << PSTR(" payload = ") << (char*) buf << endl;
// Echo back the value to another topic
publish(PSTR("public/cosa/a/e"), buf, count);
}
// MQTT client name
const char CLIENT[] __PROGMEM = "CosaMQTTclient";
MQTTClient client;
// W5100 Ethernet Controller with MAC-address
const uint8_t mac[6] __PROGMEM = { 0xde, 0xad, 0xbe, 0xef, 0xfe, 0xed };
W5100 ethernet(mac);
void setup()
{
uart.begin(9600);
trace.begin(&uart, PSTR("CosaMQTT: started"));
Watchdog::begin();
// Start ethernet controller and request network address for hostname
ASSERT(ethernet.begin_P(CLIENT));
// Start MQTT client with socket and connect to server
ASSERT(client.begin(ethernet.socket(Socket::TCP)));
ASSERT(!client.connect(SERVER, CLIENT));
// Publish data with the different quality of service levels
TRACE(client.publish_P(PSTR("public/cosa/client"), CLIENT, sizeof(CLIENT)));
uint8_t buf[16];
memset(buf, 'a', sizeof(buf));
TRACE(client.publish(PSTR("public/cosa/a/a"), buf, sizeof(buf)));
memset(buf, 'b', sizeof(buf));
TRACE(client.publish(PSTR("public/cosa/a/b"), buf, sizeof(buf),
MQTT::ACKNOWLEDGED_DELIVERY, false));
memset(buf, 'c', sizeof(buf));
TRACE(client.publish(PSTR("public/cosa/a/c"), buf, sizeof(buf),
MQTT::ASSURED_DELIVERY, false));
}
void loop()
{
// Subscribe to a topic
TRACE(client.subscribe(PSTR("public/cosa/a/d")));
// Service incoming publish messages. Wait max 10 seconds per message
for (uint8_t i = 0; i < 6; i++) TRACE(client.service(10000L));
// Unsubscribe and disconnect
TRACE(client.unsubscribe(PSTR("public/cosa/a/d")));
TRACE(client.disconnect());
// And terminate
ASSERT(true == false);
}