// Standard C #include #include #include #include #include // System #include // Mosquitto #include // Project #include "config.h" #include "mqtt.h" #ifndef MAX_COMMAND_LENGTH # define MAX_COMMAND_LENGTH 100 #endif static bool streq(const char *lhs, const char *rhs) { return strcmp(lhs, rhs) == 0; } static void application_cleanup(void) { mosquitto_lib_cleanup(); } static void application_init(void) { mosquitto_lib_init(); (void) atexit(application_cleanup); } int main(void) { application_init(); struct mosquitto *client = mqtt_connect( NULL, USERNAME, PASSWORD, HOSTNAME, PORT, NULL); if ( !client ) { return EXIT_FAILURE; } // logic for ( bool quit = false; !quit; ) { printf("> "); char command[MAX_COMMAND_LENGTH]; if ( fgets(command, sizeof command, stdin) == NULL ) { quit = true; continue; } // remove 'newline' symbol at the end command[strcspn(command, "\n")] = '\0'; if ( streq(command, "") ) { ; // do nothing } else if ( streq(command, "quit") ) { quit = true; } else if ( streq(command, "send") ) { static const char payload[] = "ich bin ein payload"; static const char topic[] = "testtopic"; int err = mosquitto_publish( client, NULL, topic, sizeof payload, payload, 0, false); if ( err ) { (void) fprintf(stderr, "error on send: %s\n", mosquitto_strerror(err)); } } else { (void) fprintf(stderr, "unknown command: '%s'\n", command); } } mqtt_shutdown(client); return EXIT_SUCCESS; }