Пример #1
0
int main(int argc, char** argv)
{
	int rc = 0;
	
	if (argc < 2)
		usage();
	
	char* topic = argv[1];

	if (strchr(topic, '#') || strchr(topic, '+'))
		opts.showtopics = 1;
	if (opts.showtopics)
		printf("topic is %s\n", topic);

	getopts(argc, argv);	

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

	signal(SIGINT, cfinish);
	signal(SIGTERM, cfinish);
 
	MQTTPacket_connectData data = MQTTPacket_connectData_initializer;       
	data.willFlag = 0;
	data.MQTTVersion = 3;
	data.clientID.cstring = opts.clientid;
	data.username.cstring = opts.username;
	data.password.cstring = opts.password;

	data.keepAliveInterval = 10;
	data.cleansession = 1;
	printf("will flag %d\n", data.willFlag);
	
	myconnect(ipstack, client, data);
    
	rc = client.subscribe(topic, opts.qos, messageArrived);
	printf("Subscribed %d\n", rc);

	while (!toStop)
	{
		client.yield(1000);	

		//if (!client.isconnected)
		//	myconnect(ipstack, client, data);
	}
	
	printf("Stopping\n");

	rc = client.disconnect();

	ipstack.disconnect();

	return 0;
}
Пример #2
0
bool UnsubscribeFromTopic(const char* topic)
{
	// If the controller reaches here, it would unsubscribe and disconnect
	rc = client.unsubscribe(topic);
	if (rc != 0)
	{
		printf("rc from unsubscribe was %d\n", rc);
	}
}
Пример #3
0
void Disconnect()
{
	rc = client.disconnect();
	if (rc != 0)
		printf("rc from disconnect was %d\n", rc);
	else
		printf("Disconnected now.", rc);

	ipStackVar.disconnect();

}
Пример #4
0
bool SubscribeToTopic(const char* topic, MQTT::QoS qos)
{
	rc = client.subscribe(topic, qos, messageArrived);
	if (rc != 0)
	{
		printf("rc from MQTT subscribe is %d\n", rc);
		return false;
	}
	else
	{
		cout << "Subscribed to " << topic << " successfully" << endl;
		return true;
	}
}
Пример #5
0
void myconnect(IPStack& ipstack, MQTT::Client<IPStack, Countdown, 1000>& client, MQTTPacket_connectData& data)
{
	printf("Connecting to %s:%d\n", opts.host, opts.port);
	int rc = ipstack.connect(opts.host, opts.port);
	if (rc != 0)
	    printf("rc from TCP connect is %d\n", rc);

	rc = client.connect(data);
	if (rc != 0)
	{
		printf("Failed to connect, return code %d\n", rc);
		exit(-1);	
	}
	printf("Connected\n");
}
Пример #6
0
void InitializeMQTTCommunication()
{
	version = 0.3;
	printf("Version is %f\n", version);

	const char* hostname = "52.207.148.30";						// iot.eclipse.org
	printf("Connecting to %s:%d\n", hostname, PORT_NUM);
	rc = ipStackVar.connect(hostname, PORT_NUM);
	if (rc != 0)
		printf("rc from TCP connect is %d\n", rc);

	printf("MQTT connecting\n");
	MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
	data.MQTTVersion = 3;
	data.clientID.cstring = (char*)"mbed-icraggs";
	rc = client.connect(data);
	if (rc != 0)
		printf("rc from MQTT connect is %d\n", rc);
	printf("MQTT connected\n");
}
Пример #7
0
bool PublishToTopic(mqttMessageType &mqttMessage, const char* topic, MQTT::QoS qos)
{
	MQTT::Message message;
	message.qos = qos;
	message.retained = false;
	message.dup = false;

	mqttMessage.host = "XYZ";
	mqttMessage.shortMessage = "This is a short_message";
	char buffer[250];
	sprintf(buffer,
			"{\"host\": \"%s\", "
			"\"short_message\": \"%s\", "
			"\"Lattitude\": \"%f\", "
			"\"Longitude\": \"%f\", "
			"\"Message Title\": \"%s\", "
			"\"Message\": \"%s\" "
			"}",
			mqttMessage.host, mqttMessage.shortMessage.c_str(),
			mqttMessage.lattitude, mqttMessage.longitude,
			mqttMessage.messageTitle.c_str(), mqttMessage.message.c_str()
			);

	cout << buffer;
	message.payload = (void*)buffer;
	message.payloadlen = strlen(buffer) + 1;

	rc = client.publish(topic, message);
	if (rc != 0) {
		printf("Error %d from sending QoS 0 message\n", rc);
	}
	else
	{
		cout << "Message published successfully to " << topic << endl;
	}

	return true;
}
Пример #8
0
int main(int argc, char* argv[])
{   
    IPStack ipstack = IPStack();
    float version = 0.3;
    const char* topic = "mbed-sample";
    
    printf("Version is %f\n", version);
              
    MQTT::Client<IPStack, Countdown> client = MQTT::Client<IPStack, Countdown>(ipstack);
    
    client.setConnectionLostHandler(connect);

    MQTT::Client<IPStack, Countdown>::connectionLostInfo info = {&client, &ipstack};
    int rc = connect(&info);
    
    rc = client.subscribe(topic, MQTT::QOS2, messageArrived);   
    if (rc != 0)
        printf("rc from MQTT subscribe is %d\n", rc);

    MQTT::Message message;

    // QoS 0
    char buf[100];
    sprintf(buf, "Hello World!  QoS 0 message from app version %f", version);
    message.qos = MQTT::QOS0;
    message.retained = false;
    message.dup = false;
    message.payload = (void*)buf;
    message.payloadlen = strlen(buf)+1;
    rc = client.publish(topic, &message);
    while (arrivedcount == 0)
        client.yield(100);
        
    // QoS 1
	printf("Now QoS 1\n");
    sprintf(buf, "Hello World!  QoS 1 message from app version %f", version);
    message.qos = MQTT::QOS1;
    message.payloadlen = strlen(buf)+1;
    rc = client.publish(topic, &message);
    while (arrivedcount == 1)
        client.yield(100);
        
    // QoS 2
    sprintf(buf, "Hello World!  QoS 2 message from app version %f", version);
    message.qos = MQTT::QOS2;
    message.payloadlen = strlen(buf)+1;
    rc = client.publish(topic, &message);
    while (arrivedcount == 2)
        client.yield(100);
    
    rc = client.unsubscribe(topic);
    if (rc != 0)
        printf("rc from unsubscribe was %d\n", rc);
    
    rc = client.disconnect();
    if (rc != 0)
        printf("rc from disconnect was %d\n", rc);
    
    ipstack.disconnect();
    
    printf("Finishing with %d messages received\n", arrivedcount);
    
    return 0;
}
Пример #9
0
void ReceiveData()
{
	client.yield(100);
	sleep(1);
}