Mark Hembrow / Mbed 2 deprecated MQTTTEST

Dependencies:   EthernetNetIf TextLCD mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "EthernetNetIf.h"
00003 #include "MQTTClient.h"
00004 #include "TextLCD.h"
00005 
00006 EthernetNetIf ethernet;
00007 
00008 Serial pc(USBTX, USBRX);
00009 
00010 DigitalOut myled1(LED1);
00011 DigitalOut myled2(LED2);
00012 DigitalOut myled3(LED3);
00013 DigitalOut myled4(LED4);
00014 
00015 TextLCD lcd(p15, p16, p17, p18, p19, p20, TextLCD::LCD16x2B); // rs, e, d4-d7
00016 
00017 IpAddr serverIpAddr(192,168,0,2);  /*Sever ip address*/
00018 
00019 void callback(char* topic, char* payload); /*Callback function prototype*/
00020 
00021 MQTTClient mqtt(serverIpAddr, 1883, callback);
00022 
00023 void callback(char* topic, char* payload) {
00024     printf("Topic: %s\r\n", topic);
00025     printf("Payload: %s\r\n\r\n", payload);
00026     //Send incoming payloads back to topic "/mbed".
00027     printf("Topic:  ");
00028     printf(topic, "\r\n");
00029     printf("Payload:  ");
00030     printf(payload, "\r\n");
00031 
00032     char *string_1 = "LED1";
00033     char *string_2 = "LED2";
00034     char *string_3 = "LED3";
00035     char *string_4 = "LED4";
00036 
00037     if (strcmp(payload, string_1)==0) {
00038         printf(payload);
00039         printf("Setting LED1");
00040         myled1 = 1;
00041     } else if (strcmp(payload, string_2)==0) {
00042         printf(payload);
00043         printf("'Setting LED2");
00044         myled2 = 1;
00045     } else if (strcmp(payload, string_3)==0) {
00046         printf(payload);
00047         printf("Setting LED3");
00048         myled3 = 1;
00049     } else if (strcmp(payload, string_4)==0) {
00050         printf(payload);
00051         printf("Setting LED4");
00052         myled4 = 1;
00053     } else {
00054         lcd.printf(payload);
00055     }
00056 
00057 
00058     //pc.printf(payload);
00059     //while(1) {pc.putc(pc.getc()
00060 
00061     mqtt.publish("/mbed", payload);
00062 
00063 
00064 }
00065 
00066 int main() {
00067     printf("\r\n############### MQTTClient Session  ###########\r\n\r\n");
00068 
00069     EthernetErr ethErr = ethernet.setup();
00070     if (ethErr) {
00071         printf("Ethernet Error %d\r\n", ethErr);
00072         return -1;
00073     }
00074 
00075     char clientID[] = "mbed";   /*Client nanme show for MQTT server*/
00076     char pub_topic[] = "/mbed";   /*Publish to topic : "/mbed" */
00077     char sub_topic[] = "/kivy";   /*Subscribe to topic : "/mirror" */
00078 
00079     if (!mqtt.connect(clientID)) {
00080         printf("\r\nConnect to server failed ..\r\n");
00081         return -1;
00082     }
00083     printf("\r\nConnect to server sucessed ..\r\n");
00084 
00085     mqtt.publish(pub_topic, "Hello here is mbed...");
00086     mqtt.subscribe(sub_topic);
00087 
00088     int flag = 0;
00089     /*Keep alive for 300s or 5mins*/
00090     while (flag < 300) {
00091         Net::poll();
00092         wait(1);
00093         flag++;
00094         mqtt.live();
00095     }
00096 
00097     mqtt.disconnect();
00098 
00099     printf("#### End of the session.. ####\r\n\r\n");
00100 }