Created
February 26, 2016 20:26
-
-
Save jpwsutton/d364f46a54575f359fbe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define WARN Serial.println | |
#define MQTTCLIENT_QOS2 1 | |
#include <ESP8266WiFi.h> | |
#include <IPStack.h> | |
#include <Countdown.h> | |
#include <MQTTClient.h> | |
int arrivedcount = 0; | |
const char* ssid = "SSID"; | |
const char* pass = "password"; | |
void messageArrived(MQTT::MessageData& md) | |
{ | |
MQTT::Message &message = md.message; | |
Serial.print("Message "); | |
Serial.print(++arrivedcount); | |
Serial.print(" arrived: qos "); | |
Serial.print(message.qos); | |
Serial.print(", retained "); | |
Serial.print(message.retained); | |
Serial.print(", dup "); | |
Serial.print(message.dup); | |
Serial.print(", packetid "); | |
Serial.println(message.id); | |
Serial.print("Payload "); | |
Serial.println((char*)message.payload); | |
} | |
WiFiClient c; // replace by a YunClient if running on a Yun | |
IPStack ipstack(c); | |
MQTT::Client<IPStack, Countdown, 50, 1> client = MQTT::Client<IPStack, Countdown, 50, 1>(ipstack); | |
byte mac[] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 }; // replace with your device's MAC | |
const char* topic = "arduino-sample"; | |
void connect() | |
{ | |
char hostname[] = "iot.eclipse.org"; | |
int port = 1883; | |
Serial.print("Connecting to "); | |
Serial.print(hostname); | |
Serial.print(":"); | |
Serial.println(port); | |
int rc = ipstack.connect(hostname, port); | |
if (rc != 1) | |
{ | |
Serial.print("rc from TCP connect is "); | |
Serial.println(rc); | |
} | |
Serial.println("MQTT connecting"); | |
MQTTPacket_connectData data = MQTTPacket_connectData_initializer; | |
Serial.println("1"); | |
data.MQTTVersion = 3; | |
Serial.println("2"); | |
data.clientID.cstring = (char*)"arduino-sample"; | |
Serial.println("3"); | |
rc = client.connect(data); | |
Serial.println("4"); | |
if (rc != 0) | |
{ | |
Serial.print("rc from MQTT connect is "); | |
Serial.println(rc); | |
} | |
Serial.println("MQTT connected"); | |
rc = client.subscribe(topic, MQTT::QOS2, messageArrived); | |
if (rc != 0) | |
{ | |
Serial.print("rc from MQTT subscribe is "); | |
Serial.println(rc); | |
} | |
Serial.println("MQTT subscribed"); | |
} | |
void setup() | |
{ | |
Serial.begin(9600); | |
setup_wifi(); | |
Serial.println("MQTT Hello example"); | |
connect(); | |
} | |
void setup_wifi() { | |
delay(10); | |
// We start by connecting to a WiFi network | |
Serial.println(); | |
Serial.print("Connecting to "); | |
Serial.println(ssid); | |
WiFi.begin(ssid, pass); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); | |
} | |
MQTT::Message message; | |
void loop() | |
{ | |
if (!client.isConnected()) | |
connect(); | |
arrivedcount = 0; | |
// Send and receive QoS 0 message | |
char buf[100]; | |
strcpy(buf, "Hello World! QoS 0 message"); | |
message.qos = MQTT::QOS0; | |
message.retained = false; | |
message.dup = false; | |
message.payload = (void*)buf; | |
message.payloadlen = strlen(buf)+1; | |
int rc = client.publish(topic, message); | |
while (arrivedcount == 0) | |
{ | |
Serial.println("Waiting for QoS 0 message"); | |
client.yield(1000); | |
} | |
// Send and receive QoS 1 message | |
strcpy(buf, "Hello World! QoS 1 message"); | |
message.qos = MQTT::QOS1; | |
message.payloadlen = strlen(buf)+1; | |
rc = client.publish(topic, message); | |
while (arrivedcount == 1) | |
{ | |
Serial.println("Waiting for QoS 1 message"); | |
client.yield(1000); | |
} | |
// Send and receive QoS 2 message | |
strcpy(buf, "Hello World! QoS 2 message"); | |
message.qos = MQTT::QOS2; | |
message.payloadlen = strlen(buf)+1; | |
rc = client.publish(topic, message); | |
while (arrivedcount == 2) | |
{ | |
Serial.println("Waiting for QoS 2 message"); | |
client.yield(1000); | |
} | |
delay(2000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi! I tested this code but I wasn't able to get QOS 1 and 2 from the server. I tried both my MQTT broker and iot.eclipse.org. Did you get the same problem as well?