My goal with this project was to create a quick operations console to interface with embedded hardware for messaging and switching. As I am part of an active project called Kivy.org I figured it would be a perfect tool to provide the user interface for the console. Kivy is a cross platform development framework based on Python and the C language. For further reading I urge you to click over to the http://kivy.org website and download a copy of the framework for your operating system and run some examples. The API is ultra easy and very feature rich including 2D & 3D support with standard widget libraries and Advanced multitouch gesturing support that all runs in the hardware GPU on Microsoft windows operating systems, Apple Mac OSx operating systems including IOS for the iPhone and iPad as well as support for Android and Linux. The example code I am providing in this blog entry can easily be built simultaneously for each of the above operating systems. See http://mctouch.wordpress.com

Dependencies:   EthernetNetIf TextLCD mbed

Committer:
mctouch
Date:
Thu Jun 28 21:29:07 2012 +0000
Revision:
0:7852eddd2a3c
For blog entry

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mctouch 0:7852eddd2a3c 1 #include "mbed.h"
mctouch 0:7852eddd2a3c 2 #include "EthernetNetIf.h"
mctouch 0:7852eddd2a3c 3 #include "MQTTClient.h"
mctouch 0:7852eddd2a3c 4 #include "TextLCD.h"
mctouch 0:7852eddd2a3c 5
mctouch 0:7852eddd2a3c 6 EthernetNetIf ethernet;
mctouch 0:7852eddd2a3c 7
mctouch 0:7852eddd2a3c 8 Serial pc(USBTX, USBRX);
mctouch 0:7852eddd2a3c 9
mctouch 0:7852eddd2a3c 10 DigitalOut myled1(LED1);
mctouch 0:7852eddd2a3c 11 DigitalOut myled2(LED2);
mctouch 0:7852eddd2a3c 12 DigitalOut myled3(LED3);
mctouch 0:7852eddd2a3c 13 DigitalOut myled4(LED4);
mctouch 0:7852eddd2a3c 14
mctouch 0:7852eddd2a3c 15 TextLCD lcd(p15, p16, p17, p18, p19, p20, TextLCD::LCD16x2B); // rs, e, d4-d7
mctouch 0:7852eddd2a3c 16
mctouch 0:7852eddd2a3c 17 IpAddr serverIpAddr(192,168,0,2); /*Sever ip address*/
mctouch 0:7852eddd2a3c 18
mctouch 0:7852eddd2a3c 19 void callback(char* topic, char* payload); /*Callback function prototype*/
mctouch 0:7852eddd2a3c 20
mctouch 0:7852eddd2a3c 21 MQTTClient mqtt(serverIpAddr, 1883, callback);
mctouch 0:7852eddd2a3c 22
mctouch 0:7852eddd2a3c 23 void callback(char* topic, char* payload) {
mctouch 0:7852eddd2a3c 24 printf("Topic: %s\r\n", topic);
mctouch 0:7852eddd2a3c 25 printf("Payload: %s\r\n\r\n", payload);
mctouch 0:7852eddd2a3c 26 //Send incoming payloads back to topic "/mbed".
mctouch 0:7852eddd2a3c 27 printf("Topic: ");
mctouch 0:7852eddd2a3c 28 printf(topic, "\r\n");
mctouch 0:7852eddd2a3c 29 printf("Payload: ");
mctouch 0:7852eddd2a3c 30 printf(payload, "\r\n");
mctouch 0:7852eddd2a3c 31
mctouch 0:7852eddd2a3c 32 char *string_1 = "LED1";
mctouch 0:7852eddd2a3c 33 char *string_2 = "LED2";
mctouch 0:7852eddd2a3c 34 char *string_3 = "LED3";
mctouch 0:7852eddd2a3c 35 char *string_4 = "LED4";
mctouch 0:7852eddd2a3c 36
mctouch 0:7852eddd2a3c 37 if (strcmp(payload, string_1)==0) {
mctouch 0:7852eddd2a3c 38 printf(payload);
mctouch 0:7852eddd2a3c 39 printf("Setting LED1");
mctouch 0:7852eddd2a3c 40 myled1 = 1;
mctouch 0:7852eddd2a3c 41 } else if (strcmp(payload, string_2)==0) {
mctouch 0:7852eddd2a3c 42 printf(payload);
mctouch 0:7852eddd2a3c 43 printf("'Setting LED2");
mctouch 0:7852eddd2a3c 44 myled2 = 1;
mctouch 0:7852eddd2a3c 45 } else if (strcmp(payload, string_3)==0) {
mctouch 0:7852eddd2a3c 46 printf(payload);
mctouch 0:7852eddd2a3c 47 printf("Setting LED3");
mctouch 0:7852eddd2a3c 48 myled3 = 1;
mctouch 0:7852eddd2a3c 49 } else if (strcmp(payload, string_4)==0) {
mctouch 0:7852eddd2a3c 50 printf(payload);
mctouch 0:7852eddd2a3c 51 printf("Setting LED4");
mctouch 0:7852eddd2a3c 52 myled4 = 1;
mctouch 0:7852eddd2a3c 53 } else {
mctouch 0:7852eddd2a3c 54 lcd.printf(payload);
mctouch 0:7852eddd2a3c 55 }
mctouch 0:7852eddd2a3c 56
mctouch 0:7852eddd2a3c 57
mctouch 0:7852eddd2a3c 58 //pc.printf(payload);
mctouch 0:7852eddd2a3c 59 //while(1) {pc.putc(pc.getc()
mctouch 0:7852eddd2a3c 60
mctouch 0:7852eddd2a3c 61 mqtt.publish("/mbed", payload);
mctouch 0:7852eddd2a3c 62
mctouch 0:7852eddd2a3c 63
mctouch 0:7852eddd2a3c 64 }
mctouch 0:7852eddd2a3c 65
mctouch 0:7852eddd2a3c 66 int main() {
mctouch 0:7852eddd2a3c 67 printf("\r\n############### MQTTClient Session ###########\r\n\r\n");
mctouch 0:7852eddd2a3c 68
mctouch 0:7852eddd2a3c 69 EthernetErr ethErr = ethernet.setup();
mctouch 0:7852eddd2a3c 70 if (ethErr) {
mctouch 0:7852eddd2a3c 71 printf("Ethernet Error %d\r\n", ethErr);
mctouch 0:7852eddd2a3c 72 return -1;
mctouch 0:7852eddd2a3c 73 }
mctouch 0:7852eddd2a3c 74
mctouch 0:7852eddd2a3c 75 char clientID[] = "mbed"; /*Client nanme show for MQTT server*/
mctouch 0:7852eddd2a3c 76 char pub_topic[] = "/mbed"; /*Publish to topic : "/mbed" */
mctouch 0:7852eddd2a3c 77 char sub_topic[] = "/kivy"; /*Subscribe to topic : "/mirror" */
mctouch 0:7852eddd2a3c 78
mctouch 0:7852eddd2a3c 79 if (!mqtt.connect(clientID)) {
mctouch 0:7852eddd2a3c 80 printf("\r\nConnect to server failed ..\r\n");
mctouch 0:7852eddd2a3c 81 return -1;
mctouch 0:7852eddd2a3c 82 }
mctouch 0:7852eddd2a3c 83 printf("\r\nConnect to server sucessed ..\r\n");
mctouch 0:7852eddd2a3c 84
mctouch 0:7852eddd2a3c 85 mqtt.publish(pub_topic, "Hello here is mbed...");
mctouch 0:7852eddd2a3c 86 mqtt.subscribe(sub_topic);
mctouch 0:7852eddd2a3c 87
mctouch 0:7852eddd2a3c 88 int flag = 0;
mctouch 0:7852eddd2a3c 89 /*Keep alive for 300s or 5mins*/
mctouch 0:7852eddd2a3c 90 while (flag < 300) {
mctouch 0:7852eddd2a3c 91 Net::poll();
mctouch 0:7852eddd2a3c 92 wait(1);
mctouch 0:7852eddd2a3c 93 flag++;
mctouch 0:7852eddd2a3c 94 mqtt.live();
mctouch 0:7852eddd2a3c 95 }
mctouch 0:7852eddd2a3c 96
mctouch 0:7852eddd2a3c 97 mqtt.disconnect();
mctouch 0:7852eddd2a3c 98
mctouch 0:7852eddd2a3c 99 printf("#### End of the session.. ####\r\n\r\n");
mctouch 0:7852eddd2a3c 100 }