├── mqtt.h ├── main.cpp ├── README.md └── mqtt.cpp /mqtt.h: -------------------------------------------------------------------------------- 1 | #ifndef SIMPLECLIENT_MQTT_H 2 | #define SIMPLECLIENT_MQTT_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | #define MAX_PAYLOAD 50 9 | #define DEFAULT_KEEP_ALIVE 60 10 | 11 | class mqtt_client : public mosqpp::mosquittopp 12 | { 13 | public: 14 | mqtt_client (const char *id, const char *host, int port); 15 | ~mqtt_client(); 16 | 17 | void on_connect(int rc); 18 | void on_message(const struct mosquitto_message *message); 19 | void on_subscribe(int mid, int qos_count, const int *granted_qos); 20 | }; 21 | 22 | #endif //SIMPLECLIENT_MQTT_H 23 | -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | #include "mqtt.h" 2 | 3 | #define CLIENT_ID "Client_ID" 4 | #define BROKER_ADDRESS "localhost" 5 | #define MQTT_PORT 1883; 6 | #define MQTT_TOPIC "EXAMPLE_TOPIC" 7 | 8 | int main(int argc, char *argv[]) 9 | { 10 | class mqtt_client *iot_client; 11 | int rc; 12 | 13 | char client_id[] = CLIENT_ID; 14 | char host[] = BROKER_ADDRESS; 15 | int port = MQTT_PORT; 16 | 17 | mosqpp::lib_init(); 18 | 19 | if (argc > 1) 20 | strcpy (host, argv[1]); 21 | 22 | iot_client = new mqtt_client(client_id, host, port); 23 | 24 | while(1) 25 | { 26 | rc = iot_client->loop(); 27 | if (rc) 28 | { 29 | iot_client->reconnect(); 30 | } 31 | else 32 | iot_client->subscribe(NULL, MQTT_TOPIC); 33 | } 34 | 35 | mosqpp::lib_cleanup(); 36 | 37 | return 0; 38 | } 39 | 40 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # mosquitopp_client 2 | A sample MQTT client built to use the mosquittopp library. 3 | 4 | I was struggling to find any complete example code for using the C++ bindings for the Mosquitto library on the web, so having got something working, I thought I'd share in case anyone else wanted to see how it works. 5 | 6 | For more information about the Mosquitto broker see: [http://mosquitto.org ]() 7 | 8 | This project needs mosquitto to be installed, and the broker running in order to work. 9 | 10 | This code is released under the MIT Licence: [https://opensource.org/licenses/MIT]() 11 | 12 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 13 | 14 | This permission notice shall be included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /mqtt.cpp: -------------------------------------------------------------------------------- 1 | #include "mqtt.h" 2 | #define PUBLISH_TOPIC "EXAMPLE_TOPIC" 3 | 4 | #ifdef DEBUG 5 | #include 6 | #endif 7 | 8 | mqtt_client::mqtt_client(const char *id, const char *host, int port) : mosquittopp(id) 9 | { 10 | int keepalive = DEFAULT_KEEP_ALIVE; 11 | connect(host, port, keepalive); 12 | } 13 | 14 | mqtt_client::~mqtt_client() 15 | { 16 | } 17 | 18 | void mqtt_client::on_connect(int rc) 19 | { 20 | if (!rc) 21 | { 22 | #ifdef DEBUG 23 | std::cout << "Connected - code " << rc << std::endl; 24 | #endif 25 | } 26 | } 27 | 28 | void mqtt_client::on_subscribe(int mid, int qos_count, const int *granted_qos) 29 | { 30 | #ifdef DEBUG 31 | std::cout << "Subscription succeeded." << std::endl; 32 | #endif 33 | } 34 | 35 | void mqtt_client::on_message(const struct mosquitto_message *message) 36 | { 37 | int payload_size = MAX_PAYLOAD + 1; 38 | char buf[payload_size]; 39 | 40 | if(!strcmp(message->topic, PUBLISH_TOPIC)) 41 | { 42 | memset(buf, 0, payload_size * sizeof(char)); 43 | 44 | /* Copy N-1 bytes to ensure always 0 terminated. */ 45 | memcpy(buf, message->payload, MAX_PAYLOAD * sizeof(char)); 46 | 47 | #ifdef DEBUG 48 | std::cout << buf << std::endl; 49 | #endif 50 | 51 | // Examples of messages for M2M communications... 52 | if (!strcmp(buf, "STATUS")) 53 | { 54 | snprintf(buf, payload_size, "This is a Status Message..."); 55 | publish(NULL, PUBLISH_TOPIC, strlen(buf), buf); 56 | #ifdef DEBUG 57 | std::cout << "Status Request Recieved." << std::endl; 58 | #endif 59 | } 60 | 61 | if (!strcmp(buf, "ON")) 62 | { 63 | snprintf(buf, payload_size, "Turning on..."); 64 | publish(NULL, PUBLISH_TOPIC, strlen(buf), buf); 65 | #ifdef DEBUG 66 | std::cout << "Request to turn on." << std::endl; 67 | #endif 68 | } 69 | 70 | if (!strcmp(buf, "OFF")) 71 | { 72 | snprintf(buf, payload_size, "Turning off..."); 73 | publish(NULL, PUBLISH_TOPIC, strlen(buf), buf); 74 | #ifdef DEBUG 75 | std::cout << "Request to turn off." << std:: endl; 76 | #endif 77 | } 78 | } 79 | } 80 | --------------------------------------------------------------------------------