コード例 #1
0
int main()
{
	printf("Starting the demo\n");

	struct pico_mqtt* mqtt = pico_mqtt_create();

	if(pico_mqtt_set_client_id( mqtt, "12345"))
	{
		printf("An error occured while setting the client_id\n");
	}

	if(pico_mqtt_set_username( mqtt, "my_name"))
	{
		printf("An error occured while setting the username\n");
	}

	if(pico_mqtt_set_password( mqtt, "secret"))
	{
		printf("An error occured while setting the password\n");
	}

	if(pico_mqtt_connect(mqtt,"localhost", "1883", 10))
	{
		printf("An error occured during connecting\n");
	}

	sleep(5);

	pico_mqtt_destroy( mqtt );

	return 0;
}
コード例 #2
0
/**
 * @brief: the start of the MQTT client program
 * @usage: To publish "garden/humidity" data: ./PROG_NAME [yourName] random
 *         To consume "garden/humidity" data: ./PROG_NAME [yourName] interactive
 */
int main( int argc, char* argv[] )
{
    struct pico_mqtt* mqtt = NULL;
    program_t activeProgram = program_invalid;
    char* sensor_name = argv[1];
    char client_id[128];

    /***************************************************************************
     * Parse program arguments
     **************************************************************************/
    if (argc != 3)
    {
        help(argv[0]);
        return -1;
    }

    if (strcmp(argv[2],"random") == 0)
    {
        activeProgram = program_simple_humidity_sensor;
    }
    else if (strcmp(argv[2], "interactive") == 0)
    {
        activeProgram = program_interactive_humidity_sensor;
    }
    else
    {
        help(argv[0]);
        return -1;
    }

    // Install signal handler on ^C
    if (signal(SIGINT, sig_handler) == SIG_ERR)
    {
        printf("\ncan't catch SIGINT\n");
    }

    /***************************************************************************
     * [MQTT] Connect this client to the MQTT server on the network
     **************************************************************************/
    printf("Starting the MQTT client\n");

    mqtt = pico_mqtt_create();

    strncpy(client_id, "sensor", 128);
    strncat(client_id, sensor_name, 128);
    if(pico_mqtt_set_client_id( mqtt, client_id))
    {
        printf("An error occurred while setting the client_id\n");
    }

    if(pico_mqtt_set_username( mqtt, USER))
    {
        printf("An error occurred while setting the username\n");
    }

    if(pico_mqtt_set_password( mqtt, PWD))
    {
        printf("An error occured while setting the password\n");
    }

    if(pico_mqtt_connect(mqtt, HOSTNAME, PORT, 10))
    {
        printf("An error occured during connecting\n");
    }

    printf("clientId: %s successfully connected to server %s:%s\n", client_id, HOSTNAME, PORT);


    /***************************************************************************
     * [MQTT] Start an MQTT program (while loop)
     **************************************************************************/
    switch(activeProgram)
    {
    case program_simple_humidity_sensor:
        printf("Starting the simple humidity sensor\n");
        //This will start an endless loop
        main_humidity_sensor( mqtt, sensor_name );
        break;
    case program_interactive_humidity_sensor:
        printf("Starting the interactive humidity sensor\n");
        // This will read humidity sensor data, print it and publish sprinkler data
        main_better_humidity_sensor( mqtt, sensor_name );
        break;
    default:
        // An error occurred
        break;
    }


    /***************************************************************************
     * [MQTT] Break connection and clear resources
     **************************************************************************/
    pico_mqtt_disconnect( mqtt );
    pico_mqtt_destroy( mqtt );

    return 0;
}