Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: C12832_lcd EthernetInterface MQTT mbed-rtos mbed
main.cpp
00001 #include "mbed.h" 00002 #include "EthernetInterface.h" 00003 #include "C12832_lcd.h" 00004 #include "PubSubClient.h" 00005 00006 C12832_LCD lcd; 00007 Serial pc(USBTX, USBRX); 00008 00009 char* serverIpAddr = "192.168.2.111"; /*Sever ip address*/ 00010 int port = 1883; /*Sever Port*/ 00011 void callback(char* topic, char* payload, unsigned int len); /*Callback function prototype*/ 00012 PubSubClient mqtt(serverIpAddr, port, callback); 00013 EthernetInterface eth; 00014 00015 void callback(char* topic, char* payload, unsigned int len) 00016 { 00017 lcd.printf("Topic: %s\r\n", topic); 00018 lcd.printf("Payload: %s\r\n\r\n", payload); 00019 //Send incoming payloads back to topic "/mbed". 00020 mqtt.publish("mbed", payload, len); 00021 } 00022 00023 int main() { 00024 00025 eth.init(); //Use DHCP 00026 eth.connect(); 00027 lcd.cls(); 00028 lcd.locate(0,3); 00029 pc.printf("IP Address is %s\n", eth.getIPAddress()); 00030 00031 pc.printf("MQTTClient Tester"); 00032 00033 00034 char clientID[] = "mbed"; /*Client nanme show for MQTT server*/ 00035 char pub_topic[] = "mbed"; /*Publish to topic : "/mbed" */ 00036 char sub_topic[] = "mirror"; /*Subscribe to topic : "/mirror" */ 00037 00038 if(!mqtt.connect(clientID)){ 00039 pc.printf("\r\nConnect to server failed ..\r\n"); 00040 return -1; 00041 } 00042 00043 pc.printf("\r\nConnect to server sucessed ..\r\n"); 00044 00045 mqtt.publish(pub_topic, "Hello here is mbed..."); 00046 mqtt.subscribe(sub_topic); 00047 00048 00049 pc.printf("#### End of the test.. ####"); 00050 00051 //eth.disconnect(); 00052 00053 while(1) { 00054 mqtt.loop(); 00055 } 00056 }
Generated on Fri Jul 15 2022 01:57:01 by
1.7.2