MQTT Client example program. Ethernet connection is via an ENC28J60 module.
Dependencies: UIPEthernet MQTTClient
Diff: main.cpp
- Revision:
- 9:18b414a8c5f7
- Parent:
- 8:7b4e0d15249f
- Child:
- 10:27440d67d4f2
diff -r 7b4e0d15249f -r 18b414a8c5f7 main.cpp --- a/main.cpp Sun Nov 29 18:18:46 2015 +0000 +++ b/main.cpp Sun Nov 29 19:00:09 2015 +0000 @@ -10,8 +10,6 @@ Serial pc(USBTX, USBRX); -#define DHCP 1 // comment out this line if you'd like to use static IP address - // UIPEthernet is the name of a global instance of UIPEthernetClass. // Do not change the name! It is used within the UIPEthernet library. // Adapt the SPI pin names to your mbed platform/board if not present yet. @@ -30,6 +28,20 @@ // MAC number must be unique within the connected network. Modify as appropriate. const uint8_t MY_MAC[6] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 }; +// MQTT broker is like a post office. +// Its task is to distribute messages published by clients to all subscribers (other clients). +// So the 'example/hello' messages published by this client will be sent to the broker. +// Then the broker will send them to all clients which subscribed to such topic (example/hello). +// Also this client will receive all messages with topics it subscribed to (if published such by other clients). +// 'Mosquitto' is a free implementation of MQTT broker for Linux (e.g. Raspberry Pi, Ubuntu etc.) +IPAddress serverIP(192, 168, 1, 30); // IP address of your MQTT broker (change to match) +EthernetClient ethernetClient; +void onMqttMessage(char* topic, uint8_t* payload, unsigned int length); +MQTTClient mqttClient(serverIP, 1883, onMqttMessage, ethernetClient); +char message_buff[100]; // buffer to store received messages + +#define DHCP 1 // comment out this line if you'd like to use static IP address + #if !defined(DHCP) // In case you'd like to use static IP address: // IP address must be unique and compatible with your network. @@ -37,19 +49,6 @@ const IPAddress MY_IP(192, 168, 1, 181); #endif -const int INTERVAL = 10; // Interval for publishing the messages (in seconds) -char message_buff[100]; - -// MQTT broker is like a post office. -// Its task is to distribute messages published by clients to all subscribers (other clients). -// So the 'example/hello' messages published by this client will be sent to the broker. -// Then the broker will send them to all clients which subscribed to such topic (example/hello). -// Also this client will receive all messages with topics it subscribed to. -// 'Mosquitto' is a free implementation of MQTT broker for Linux (e.g. Raspberry Pi, Ubuntu etc.) -IPAddress serverIP(192, 168, 1, 30); // IP address of your MQTT broker (adapt) -EthernetClient ethernetClient; -void onMqttMessage(char* topic, uint8_t* payload, unsigned int length); -MQTTClient mqttClient(serverIP, 1883, onMqttMessage, ethernetClient); /** * @brief @@ -57,7 +56,6 @@ * @param * @retval */ - int main(void) { const int MAX_TRIES = 5; const unsigned long INTERVAL = 5; @@ -67,7 +65,6 @@ unsigned long t = 0; unsigned long lastTime = t; - // initialize the ethernet device #if defined(DHCP) @@ -97,7 +94,7 @@ if(connected) { pc.printf("MQTT broker connected.\r\n"); - // The client can subscribe to many topics. + // We can subscribe to various topics published by other clients. pc.printf("Subscribing to topics:\r\n"); pc.printf(" outdoor/temperature\r\n"); pc.printf(" boiler/outlet/temperature\r\n"); @@ -142,5 +139,5 @@ } message_buff[i] = '\0'; - pc.printf(" Payload: %s\r\n", message_buff); + pc.printf(" Payload: %s\r\n", message_buff); }