1/******************************************************************************* 2 * Copyright (c) 2014 IBM Corp. and others 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 *******************************************************************************/ 17 18#define MQTTCLIENT_QOS2 1 19 20#include <SPI.h> 21#include <Ethernet.h> 22#include <IPStack.h> 23#include <Countdown.h> 24#include <MQTTClient.h> 25 26char printbuf[100]; 27 28int arrivedcount = 0; 29 30void messageArrived(MQTT::MessageData& md) 31{ 32 MQTT::Message &message = md.message; 33 34 sprintf(printbuf, "Message %d arrived: qos %d, retained %d, dup %d, packetid %d\n", 35 ++arrivedcount, message.qos, message.retained, message.dup, message.id); 36 Serial.print(printbuf); 37 sprintf(printbuf, "Payload %s\n", (char*)message.payload); 38 Serial.print(printbuf); 39} 40 41EthernetClient c; // replace by a YunClient if running on a Yun 42IPStack ipstack(c); 43MQTT::Client<IPStack, Countdown> client = MQTT::Client<IPStack, Countdown>(ipstack); 44 45byte mac[] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 }; // replace with your device's MAC 46const char* topic = "arduino-sample"; 47 48void connect() 49{ 50 char hostname[] = "iot.eclipse.org"; 51 int port = 1883; 52 sprintf(printbuf, "Connecting to %s:%d\n", hostname, port); 53 Serial.print(printbuf); 54 int rc = ipstack.connect(hostname, port); 55 if (rc != 1) 56 { 57 sprintf(printbuf, "rc from TCP connect is %d\n", rc); 58 Serial.print(printbuf); 59 } 60 61 Serial.println("MQTT connecting"); 62 MQTTPacket_connectData data = MQTTPacket_connectData_initializer; 63 data.MQTTVersion = 3; 64 data.clientID.cstring = (char*)"arduino-sample"; 65 rc = client.connect(data); 66 if (rc != 0) 67 { 68 sprintf(printbuf, "rc from MQTT connect is %d\n", rc); 69 Serial.print(printbuf); 70 } 71 Serial.println("MQTT connected"); 72 73 rc = client.subscribe(topic, MQTT::QOS2, messageArrived); 74 if (rc != 0) 75 { 76 sprintf(printbuf, "rc from MQTT subscribe is %d\n", rc); 77 Serial.print(printbuf); 78 } 79 Serial.println("MQTT subscribed"); 80} 81 82void setup() 83{ 84 Serial.begin(9600); 85 Ethernet.begin(mac); // replace by Bridge.begin() if running on a Yun 86 Serial.println("MQTT Hello example"); 87 connect(); 88} 89 90void loop() 91{ 92 if (!client.isConnected()) 93 connect(); 94 95 MQTT::Message message; 96 97 arrivedcount = 0; 98 99 // Send and receive QoS 0 message 100 char buf[100]; 101 sprintf(buf, "Hello World! QoS 0 message"); 102 Serial.println(buf); 103 message.qos = MQTT::QOS0; 104 message.retained = false; 105 message.dup = false; 106 message.payload = (void*)buf; 107 message.payloadlen = strlen(buf)+1; 108 int rc = client.publish(topic, message); 109 while (arrivedcount == 0) 110 client.yield(1000); 111 112 // Send and receive QoS 1 message 113 sprintf(buf, "Hello World! QoS 1 message"); 114 Serial.println(buf); 115 message.qos = MQTT::QOS1; 116 message.payloadlen = strlen(buf)+1; 117 rc = client.publish(topic, message); 118 while (arrivedcount == 1) 119 client.yield(1000); 120 121 // Send and receive QoS 2 message 122 sprintf(buf, "Hello World! QoS 2 message"); 123 Serial.println(buf); 124 message.qos = MQTT::QOS2; 125 message.payloadlen = strlen(buf)+1; 126 rc = client.publish(topic, message); 127 while (arrivedcount == 2) 128 client.yield(1000); 129 130 delay(2000); 131} 132