为什么它会在10条消息后停止

时间:2016-05-11 07:45:19

标签: c mqtt launchpad

我是TI LaunchPad cc3200编程的新手。因此,我正在玩Energia IDE的MQTT“HelloWiFi”示例,我稍微修改了示例代码。但如果我运行代码,我的发布会在10条消息后停止。如果我在示例代码中粘贴缩短的循环代码(只有一条消息而不是3条消息),则发布会继续进行。为什么?有什么想法吗?

#define MQTTCLIENT_QOS2 1

#include <SPI.h>
#include <WiFi.h>
#include <WifiIPStack.h>
#include <Countdown.h>
#include <MQTTClient.h>


// your network name also called SSID
char ssid[] = "Tele*****";
// your network password
char password[] = "8757******";

char printbuf[100];

int arrivedcount = 0;

void messageArrived(MQTT::MessageData& md)
{
    MQTT::Message &message = md.message;

   sprintf(printbuf, "Message %d arrived: qos %d, retained %d, dup %d, packetid %d\n", 
        ++arrivedcount, message.qos, message.retained, message.dup, message.id);
  Serial.print(printbuf);
  sprintf(printbuf, "Payload %s\n", (char*)message.payload);
  Serial.print(printbuf);
}


WifiIPStack ipstack;
MQTT::Client<WifiIPStack, Countdown> client = MQTT::Client<WifiIPStack, Countdown>(ipstack);

const char* topic = "wireless-networks";

void connect()
{
  char hostname[] = "iot.eclipse.org";
  int port = 1883;
  sprintf(printbuf, "Connecting to %s:%d\n", hostname, port);
  Serial.print(printbuf);
  int rc = ipstack.connect(hostname, port);
  if (rc != 1)
{
  sprintf(printbuf, "rc from TCP connect is %d\n", rc);
  Serial.print(printbuf);
}

Serial.println("MQTT connecting");
MQTTPacket_connectData data = MQTTPacket_connectData_initializer;       
data.MQTTVersion = 3;
data.clientID.cstring = (char*)"DrahtloseNetzwerk";
rc = client.connect(data);
if (rc != 0)
{
  sprintf(printbuf, "rc from MQTT connect is %d\n", rc);
  Serial.print(printbuf);
}
Serial.println("MQTT connected");

rc = client.subscribe(topic, MQTT::QOS2, messageArrived);   
if (rc != 0)
{
  sprintf(printbuf, "rc from MQTT subscribe is %d\n", rc);
  Serial.print(printbuf);
}
Serial.println("MQTT subscribed");

 }

void setup()
{
  Serial.begin(115200);
  // attempt to connect to Wifi network:
  Serial.print("Attempting to connect to Network named: ");
  // print the network name (SSID);
  Serial.println(ssid); 

  WiFi.begin(ssid, password);
  while ( WiFi.status() != WL_CONNECTED) {
  // print dots while we wait to connect
  Serial.print(".");
  delay(300);
}

Serial.println("\nYou're connected to the network");
Serial.println("Waiting for an ip address");

while (WiFi.localIP() == INADDR_NONE) {
  // print dots while we wait for an ip addresss
  Serial.print(".");
  delay(300);
}

Serial.println("\nIP Address obtained");
// We are connected and have an IP address.
Serial.println(WiFi.localIP());

Serial.println("MQTT Hello example");
connect();

}

void loop()
{
  if (!client.isConnected())
    connect();

  MQTT::Message message;

  arrivedcount = 0;

  // Send and receive QoS 0 message
  char buf[100];
  sprintf(buf, "Hello it's me s0540636!!");
  message.qos = MQTT::QOS2;
  message.retained = false;
  message.dup = false;
  message.payload = (void*)buf;
  message.payloadlen = strlen(buf)+1;
  int rc = client.publish(topic, message);

  while (arrivedcount == 0)
    client.yield(1000);
  delay(1000);
}

0 个答案:

没有答案