Esempio n. 1
0
bool MqttClient::connect(String clientName, String username, String password)
{
	if (getConnectionState() != eTCS_Ready)
	{
		close();
		debugf("MQTT closed previous connection");
	}

	debugf("MQTT start connection");
	mqtt_init(&broker, clientName.c_str());
	if (clientName.length() > 0)
		mqtt_init_auth(&broker, username.c_str(), password.c_str());

	if(server) {
		TcpClient::connect(server, port);
	}
	else {
		TcpClient::connect(serverIp, port);
	}

	mqtt_set_alive(&broker, keepAlive);
	broker.socket_info = (void*)this;
	broker.send = staticSendPacket;

	int res = mqtt_connect(&broker);
	setTimeOut(USHRT_MAX);
	return res > 0;
}
Esempio n. 2
0
/***********************************************************
 *
 *   return :    0 success ,1 error
 *
 *************************************************************/
int32 Mqtt_SendConnetPacket( mqtt_broker_handle_t *pstBroketHandle, int32 socketid,const int8* username,const int8* password )
{       
    int32 ret;
    
    if( (username==NULL) || (password==NULL) )// 匿名登录 GAgent V4 will not runing this code.
    {
        return 1;
    }
    else // 用户登录
    {
        mqtt_init( pstBroketHandle,username );
        mqtt_init_auth(pstBroketHandle,username,password );
    }
    mqtt_set_alive(pstBroketHandle, CLOUD_MQTT_SET_ALIVE);
    pstBroketHandle->socketid = socketid;
    pstBroketHandle->mqttsend = send_packet;

    ret = mqtt_connect(pstBroketHandle);
    if (ret != 0)
        {
            GAgent_Printf(GAGENT_WARNING,"mqtt send connect packet is failed with:%d", ret); 
            return 1;
        }
    GAgent_Printf(GAGENT_INFO, "Mqtt_SendConnetPacket OK, write:%d", ret); 
    return 0;
}
Esempio n. 3
0
// Connect to the MQTT broker.  Call this function whenever connection needs
// to be re-established.
// returns:  0 - everything OK
//          -1 - packet error
//          -2 - failed to get connack
int App_ConnectMqtt()
{  
    int packet_length;

    mqtt_init(&broker, WIO_CLIENTID);
    mqtt_init_auth(&broker, WIO_USERNAME, WIO_PASSWORD);
    mqtt_connect(&broker);
    // wait for CONNACK	
    packet_length = mqtt_read_packet(6000);

    if(packet_length < 0) 
    {
        DisplayLCD(LCD_LINE4, "MQTT packet error");
        return -1;
    }
    if(MQTTParseMessageType(rxm.message) != MQTT_MSG_CONNACK) 
    { 
        return -2;
    }  
    if(rxm.message[3] != 0x00)
    {
        return -2;
    }
    App_PrepareIncomingData();
    AtLibGs_FlushIncomingMessage();
    mqttConnected=1;
    return 0;
}
Esempio n. 4
0
void init_mqtt(void)
{
	mqtt_init(&broker, "client-id1");
	mqtt_init_auth(&broker, "aleph201409", "al2014eph");
	init_socket(&broker, keepalive);

	// >>>>> CONNECT
	mqtt_connect(&broker);
	read_packet();
}
Esempio n. 5
0
void app_mqtt_init(mqtt_broker_handle_t *broker)
{
	mqtt_init(broker, "HF_mc300");
	mqtt_init_auth(broker, NULL, NULL);
	mqtt_set_alive(broker, MQTT_KEEPALIVE_SEC);
}
Esempio n. 6
0
int main(int argc, char* argv[])
{
	int packet_length;
	uint16_t msg_id, msg_id_rcv;
	mqtt_broker_handle_t broker;

   mqtt_init(&broker, "sancho");
   mqtt_init_auth(&broker, "quijote", "rocinante");
   init_socket(&broker, "192.168.10.40", 1883, keepalive);

	// >>>>> CONNECT
	mqtt_connect(&broker);
	// <<<<< CONNACK
	packet_length = read_packet(1);
	if(packet_length < 0)
	{
		fprintf(stderr, "Error(%d) on read packet!\n", packet_length);
		return -1;
	}

	if(MQTTParseMessageType(packet_buffer) != MQTT_MSG_CONNACK)
	{
		fprintf(stderr, "CONNACK expected!\n");
		return -2;
	}

	if(packet_buffer[3] != 0x00)
	{
		fprintf(stderr, "CONNACK failed!\n");
		return -2;
	}


	// >>>>> PUBLISH QoS 0
	printf("Publish: QoS 0\n");
	mqtt_publish(&broker, "public/myexample/example", "Test libemqtt message.", 0);
	// >>>>> PUBLISH QoS 1
	printf("Publish: QoS 1\n");
	mqtt_publish_with_qos(&broker, "hello/emqtt", "Example: QoS 1", 0, 1, &msg_id);
	// <<<<< PUBACK
	packet_length = read_packet(1);
	if(packet_length < 0)
	{
		fprintf(stderr, "Error(%d) on read packet!\n", packet_length);
		return -1;
	}

	if(MQTTParseMessageType(packet_buffer) != MQTT_MSG_PUBACK)
	{
		fprintf(stderr, "PUBACK expected!\n");
		return -2;
	}

	MQTTParseMessageId(packet_buffer, msg_id_rcv);
	if(msg_id != msg_id_rcv)
	{
		fprintf(stderr, "%d message id was expected, but %d message id was found!\n", msg_id, msg_id_rcv);
		return -3;
	}

	// >>>>> PUBLISH QoS 2
	printf("Publish: QoS 2\n");
	mqtt_publish_with_qos(&broker, "hello/emqtt", "Example: QoS 2", 1, 2, &msg_id); // Retain
	// <<<<< PUBREC
	packet_length = read_packet(1);
	if(packet_length < 0)
	{
		fprintf(stderr, "Error(%d) on read packet!\n", packet_length);
		return -1;
	}

	if(MQTTParseMessageType(packet_buffer) != MQTT_MSG_PUBREC)
	{
		fprintf(stderr, "PUBREC expected!\n");
		return -2;
	}

	MQTTParseMessageId(packet_buffer, msg_id_rcv);
	if(msg_id != msg_id_rcv)
	{
		fprintf(stderr, "%d message id was expected, but %d message id was found!\n", msg_id, msg_id_rcv);
		return -3;
	}

	// >>>>> PUBREL
	mqtt_pubrel(&broker, msg_id);
	// <<<<< PUBCOMP
	packet_length = read_packet(1);
	if(packet_length < 0)
	{
		fprintf(stderr, "Error(%d) on read packet!\n", packet_length);
		return -1;
	}

	if(MQTTParseMessageType(packet_buffer) != MQTT_MSG_PUBCOMP)
	{
		fprintf(stderr, "PUBCOMP expected!\n");
		return -2;
	}

	MQTTParseMessageId(packet_buffer, msg_id_rcv);
	if(msg_id != msg_id_rcv)
	{
		fprintf(stderr, "%d message id was expected, but %d message id was found!\n", msg_id, msg_id_rcv);
		return -3;
	}

	// >>>>> DISCONNECT
	mqtt_disconnect(&broker);
	close_socket(&broker);
	return 0;
}
int server_connect(int doConnect)
{
    int16_t packet_length;
    char host[30];  // temp space for hostname string

    serverConnected = 0;

    if (doConnect)
    {
        sprintf(host, THINGFABRIC_BROKER_HOSTNAME);
        char *hostname = host;
        hostname_to_ip(hostname , broker_ip);

        if ((broker_ip == NULL) || (broker_ip[0] == 0))
        {
            return 0;
        }

        printf("\n%s resolved to %s\n" , hostname , broker_ip);

        sleep(5);

        // now connect using user/password, publish sensor values on
        // appropriate topic (<domain>/<device type>/<device id>
        char clientIDStr[100];
        sprintf(clientIDStr, "%s/%s", THINGFABRIC_DEVICE_TYPE, THINGFABRIC_DEVICE_ID);
        mqtt_init(&broker, clientIDStr);
        mqtt_init_auth(&broker, THINGFABRIC_USERNAME, THINGFABRIC_PASSWORD);
        init_socket(&broker, broker_ip, THINGFABRIC_BROKER_PORT);

        serverConnected = 1;

        mqtt_connect(&broker);
        // wait for CONNACK
        packet_length = read_packet(1);
        if(packet_length < 0)
        {
            fprintf(stderr, "Error(%d) on read packet!\n", packet_length);
            serverConnected = 0;
        }

        if(MQTTParseMessageType(packet_buffer) != MQTT_MSG_CONNACK)
        {
            fprintf(stderr, "CONNACK expected!\n");
            serverConnected = 0;
        }

        if(packet_buffer[3] != 0x00)
        {
            fprintf(stderr, "CONNACK failed!\n");
            serverConnected = 0;
        }

        if (serverConnected)
        {
            sprintf(pubTopic, "%s/%s/%s", THINGFABRIC_DOMAIN, THINGFABRIC_DEVICE_TYPE, THINGFABRIC_DEVICE_ID);
            printf("%s\n", pubTopic);

            // configure the ping timer
            signal(SIGALRM, alive);
            alarm(keepalive);
        }
        else
        {
            fprintf(stderr, "Error connecting to MQTT server\n");
        }
    }
    else
    {
        printf("Disconnecting from server\n");
        mqtt_disconnect(&broker);
        close_socket(&broker);
        serverConnected = 0;
    }

    return serverConnected;
}
Esempio n. 8
0
int main(int argc, char* argv[])
{
	int packet_length;
	uint16_t msg_id, msg_id_rcv;
	mqtt_broker_handle_t broker;

	mqtt_init(&broker, "avengalvon");
	mqtt_init_auth(&broker, "cid", "campeador");
	init_socket(&broker, "127.0.0.1", 1883);

	// >>>>> CONNECT
	mqtt_connect(&broker);
	// <<<<< CONNACK
	packet_length = read_packet(1);
	if(packet_length < 0)
	{
		fprintf(stderr, "Error(%d) on read packet!\n", packet_length);
		return -1;
	}

	if(MQTTParseMessageType(packet_buffer) != MQTT_MSG_CONNACK)
	{
		fprintf(stderr, "CONNACK expected!\n");
		return -2;
	}

	if(packet_buffer[3] != 0x00)
	{
		fprintf(stderr, "CONNACK failed!\n");
		return -2;
	}

	// >>>>> PUBLISH QoS 0
	printf("Publish: QoS 0\n");
	mqtt_publish(&broker, "hello/emqtt", "Example: QoS 0", 0);

	// >>>>> PUBLISH QoS 1
	printf("Publish: QoS 1\n");
	mqtt_publish_with_qos(&broker, "hello/emqtt", "Example: QoS 1", 0, 1, &msg_id);
	// <<<<< PUBACK
	packet_length = read_packet(1);
	if(packet_length < 0)
	{
		fprintf(stderr, "Error(%d) on read packet!\n", packet_length);
		return -1;
	}

	if(MQTTParseMessageType(packet_buffer) != MQTT_MSG_PUBACK)
	{
		fprintf(stderr, "PUBACK expected!\n");
		return -2;
	}

	msg_id_rcv = mqtt_parse_msg_id(packet_buffer);
	if(msg_id != msg_id_rcv)
	{
		fprintf(stderr, "%d message id was expected, but %d message id was found!\n", msg_id, msg_id_rcv);
		return -3;
	}

	// >>>>> PUBLISH QoS 2
	printf("Publish: QoS 2\n");
	mqtt_publish_with_qos(&broker, "hello/emqtt", "Example: QoS 2", 1, 2, &msg_id); // Retain
	// <<<<< PUBREC
	packet_length = read_packet(1);
	if(packet_length < 0)
	{
		fprintf(stderr, "Error(%d) on read packet!\n", packet_length);
		return -1;
	}

	if(MQTTParseMessageType(packet_buffer) != MQTT_MSG_PUBREC)
	{
		fprintf(stderr, "PUBREC expected!\n");
		return -2;
	}

	msg_id_rcv = mqtt_parse_msg_id(packet_buffer);
	if(msg_id != msg_id_rcv)
	{
		fprintf(stderr, "%d message id was expected, but %d message id was found!\n", msg_id, msg_id_rcv);
		return -3;
	}

	// >>>>> PUBREL
	mqtt_pubrel(&broker, msg_id);
	// <<<<< PUBCOMP
	packet_length = read_packet(1);
	if(packet_length < 0)
	{
		fprintf(stderr, "Error(%d) on read packet!\n", packet_length);
		return -1;
	}

	if(MQTTParseMessageType(packet_buffer) != MQTT_MSG_PUBCOMP)
	{
		fprintf(stderr, "PUBCOMP expected!\n");
		return -2;
	}

	msg_id_rcv = mqtt_parse_msg_id(packet_buffer);
	if(msg_id != msg_id_rcv)
	{
		fprintf(stderr, "%d message id was expected, but %d message id was found!\n", msg_id, msg_id_rcv);
		return -3;
	}

	// >>>>> DISCONNECT
	mqtt_disconnect(&broker);
	close_socket(&broker);
	return 0;
}