Sample test program for the MQTT PubSubClient library
Dependencies: C12832_lcd EthernetInterface MQTT mbed-rtos mbed
Revision 0:3f9dd63f4f98, committed 2013-05-26
- Comitter:
- jwende
- Date:
- Sun May 26 16:52:40 2013 +0000
- Child:
- 1:5570162c5994
- Commit message:
- Sample client implementation for for MQTT PubSubClient library
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/C12832_lcd.lib Sun May 26 16:52:40 2013 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/dreschpe/code/C12832_lcd/#468cdccff7af
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/EthernetInterface.lib Sun May 26 16:52:40 2013 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/EthernetInterface/#52d857c9e3ba
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MQTT.lib Sun May 26 16:52:40 2013 +0000 @@ -0,0 +1,1 @@ +MQTT#ca855d29545b
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Sun May 26 16:52:40 2013 +0000
@@ -0,0 +1,56 @@
+#include "mbed.h"
+#include "EthernetInterface.h"
+#include "C12832_lcd.h"
+#include "PubSubClient.h"
+
+C12832_LCD lcd;
+Serial pc(USBTX, USBRX);
+
+char* serverIpAddr = "192.168.2.111"; /*Sever ip address*/
+int port = 1883; /*Sever Port*/
+void callback(char* topic, char* payload, unsigned int len); /*Callback function prototype*/
+PubSubClient mqtt(serverIpAddr, port, callback);
+EthernetInterface eth;
+
+void callback(char* topic, char* payload, unsigned int len)
+{
+ lcd.printf("Topic: %s\r\n", topic);
+ lcd.printf("Payload: %s\r\n\r\n", payload);
+ //Send incoming payloads back to topic "/mbed".
+ mqtt.publish("mbed", payload, len);
+}
+
+int main() {
+
+ eth.init(); //Use DHCP
+ eth.connect();
+ lcd.cls();
+ lcd.locate(0,3);
+ pc.printf("IP Address is %s\n", eth.getIPAddress());
+
+ pc.printf("MQTTClient Tester");
+
+
+ char clientID[] = "mbed"; /*Client nanme show for MQTT server*/
+ char pub_topic[] = "mbed"; /*Publish to topic : "/mbed" */
+ char sub_topic[] = "mirror"; /*Subscribe to topic : "/mirror" */
+
+ if(!mqtt.connect(clientID)){
+ pc.printf("\r\nConnect to server failed ..\r\n");
+ return -1;
+ }
+
+ pc.printf("\r\nConnect to server sucessed ..\r\n");
+
+ mqtt.publish(pub_topic, "Hello here is mbed...");
+ mqtt.subscribe(sub_topic);
+
+
+ pc.printf("#### End of the test.. ####");
+
+ //eth.disconnect();
+
+ while(1) {
+ mqtt.loop();
+ }
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-rtos.lib Sun May 26 16:52:40 2013 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed-rtos/#db1fc233faa9
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Sun May 26 16:52:40 2013 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/b3110cd2dd17 \ No newline at end of file