The NXP LCP1768 and mbed Application Board on 2lemetry platform.

Dependencies:   C12832_lcd EthernetInterface MMA7660 MQTT mbed-rtos mbed

Fork of MQTTTest by Joerg Wende


A simple example of the NXP LCP1768 and mbed Application Board on the 2lemetry platform.

This example publishes the 3-axis accelerometer values over MQTT to the 2lemetry platform. It is meant to be a simple example to get up and going quickly with the platform and explore real-time and persisted data.

Steps to Getting Started

Steps to getting your 1768 online with 2lemetry:

  1. Sign up for a free "hacker" account in the 2lemetry Portal.
  2. Import this project into your mbed online compiler workspace.
  3. Update the 2lemetry specific defines at the top of main.cpp (see below).
  4. Compile and upload.
  5. See your data in real-time on the MQTT tab of the portal.

2lemetry specific defines in main.cpp

#define  _2LEMETRY_USERID       "<2lemetry user id>"
#define  _2LEMETRY_TOKEN        "<2lemetry token>"
#define  _2LEMETRY_DOMAIN       "<2lemetry domain - usually same as user id>"
#define  _2LEMETRY_STUFF        "<2lemetry stuff - a grouping of devices - recommend 'mbed' to get started>"
#define  _2LEMETRY_DEVICE_ID    "<2lemetry device id - unique device id - mac / serial # - recommend 'nxp01' to get started>"


  • Your user ID and token can be found by clicking on your name in the upper right corner of the portal...Profile and then selecting the Credentials tab. Click on "Display Token" to copy your token.
  • Domain in the developer portal is the same as your user ID.
  • Stuff is a hierarchial addition to allow grouping of devices. This would allow you to group all of your NXP devices, all of your Freescale devices, etc. To get started quickly with this example just use "mbed".
  • Device ID is a way to uniquely identify each device. Usually you'd choose something like the MAC address of your device or the device's serial number. To get started quickly with this example just use "nxp01".
Tue Dec 24 00:05:56 2013 +0000
Initial commit.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jwende 0:3f9dd63f4f98 1 #include "mbed.h"
jwende 0:3f9dd63f4f98 2 #include "EthernetInterface.h"
jwende 0:3f9dd63f4f98 3 #include "C12832_lcd.h"
jwende 0:3f9dd63f4f98 4 #include "PubSubClient.h"
rotach 2:edcfec87de9b 5 #include "MMA7660.h"
rotach 2:edcfec87de9b 6
rotach 2:edcfec87de9b 7 #define _2LEMETRY_USERID "<2lemetry user id>"
rotach 2:edcfec87de9b 8 #define _2LEMETRY_TOKEN "<2lemetry token>"
rotach 2:edcfec87de9b 9 #define _2LEMETRY_DOMAIN "<2lemetry domain - usually same as user id>"
rotach 2:edcfec87de9b 10 #define _2LEMETRY_STUFF "<2lemetry stuff - a grouping of devices - recommend 'mbed' to get started>"
rotach 2:edcfec87de9b 11 #define _2LEMETRY_DEVICE_ID "<2lemetry device id - unique device id - mac / serial # - recommend 'nxp01' to get started>"
jwende 0:3f9dd63f4f98 12
jwende 0:3f9dd63f4f98 13 C12832_LCD lcd;
jwende 0:3f9dd63f4f98 14 Serial pc(USBTX, USBRX);
jwende 0:3f9dd63f4f98 15
rotach 2:edcfec87de9b 16 char* serverIpAddr = "";
jwende 0:3f9dd63f4f98 17 int port = 1883; /*Sever Port*/
jwende 0:3f9dd63f4f98 18 void callback(char* topic, char* payload, unsigned int len); /*Callback function prototype*/
jwende 0:3f9dd63f4f98 19 PubSubClient mqtt(serverIpAddr, port, callback);
jwende 0:3f9dd63f4f98 20 EthernetInterface eth;
jwende 0:3f9dd63f4f98 21
rotach 2:edcfec87de9b 22 MMA7660 MMA(p28, p27);
rotach 2:edcfec87de9b 23
jwende 0:3f9dd63f4f98 24 void callback(char* topic, char* payload, unsigned int len)
jwende 0:3f9dd63f4f98 25 {
rotach 2:edcfec87de9b 26 // for now just print the payload
jwende 0:3f9dd63f4f98 27 lcd.printf("Payload: %s\r\n\r\n", payload);
rotach 2:edcfec87de9b 28 wait(2);
rotach 2:edcfec87de9b 29 lcd.cls();
jwende 0:3f9dd63f4f98 30 }
jwende 0:3f9dd63f4f98 31
jwende 0:3f9dd63f4f98 32 int main() {
rotach 2:edcfec87de9b 33 char clientID[30];
rotach 2:edcfec87de9b 34 char topicString[50];
jwende 0:3f9dd63f4f98 35
jwende 0:3f9dd63f4f98 36 eth.init(); //Use DHCP
jwende 0:3f9dd63f4f98 37 eth.connect();
jwende 0:3f9dd63f4f98 38 lcd.cls();
jwende 0:3f9dd63f4f98 39 lcd.locate(0,3);
jwende 0:3f9dd63f4f98 40 pc.printf("IP Address is %s\n", eth.getIPAddress());
rotach 2:edcfec87de9b 41
rotach 2:edcfec87de9b 42 pc.printf("mbed example on 2lemetry");
jwende 0:3f9dd63f4f98 43
rotach 2:edcfec87de9b 44 // create MQTT client ID
rotach 2:edcfec87de9b 45 sprintf(clientID, _2LEMETRY_DEVICE_ID);
jwende 0:3f9dd63f4f98 46
rotach 2:edcfec87de9b 47 if(!mqtt.connect(clientID, _2LEMETRY_USERID, _2LEMETRY_TOKEN)){
jwende 0:3f9dd63f4f98 48 pc.printf("\r\nConnect to server failed ..\r\n");
jwende 0:3f9dd63f4f98 49 return -1;
jwende 0:3f9dd63f4f98 50 }
jwende 0:3f9dd63f4f98 51
rotach 2:edcfec87de9b 52 pc.printf("\r\nConnect to server successful ..\r\n");
rotach 2:edcfec87de9b 53
rotach 2:edcfec87de9b 54 int i = 0;
rotach 2:edcfec87de9b 55 char pubString[50];
jwende 0:3f9dd63f4f98 56
rotach 2:edcfec87de9b 57 // temporarily use pubTopic string to create subscription topic
rotach 2:edcfec87de9b 58 sprintf(topicString, "%s/%s/%s/cmd", _2LEMETRY_DOMAIN, _2LEMETRY_STUFF, _2LEMETRY_DEVICE_ID);
rotach 2:edcfec87de9b 59
rotach 2:edcfec87de9b 60 mqtt.subscribe(topicString);
jwende 0:3f9dd63f4f98 61
rotach 2:edcfec87de9b 62 // now overwrite with publish string
rotach 2:edcfec87de9b 63 sprintf(topicString, "%s/%s/%s", _2LEMETRY_DOMAIN, _2LEMETRY_STUFF, _2LEMETRY_DEVICE_ID);
jwende 0:3f9dd63f4f98 64
jwende 0:3f9dd63f4f98 65 while(1) {
rotach 2:edcfec87de9b 66 int x_val, y_val, z_val;
rotach 2:edcfec87de9b 67 x_val = (int)(MMA.x() * 1000);
rotach 2:edcfec87de9b 68 y_val = (int)(MMA.y() * 1000);
rotach 2:edcfec87de9b 69 z_val = (int)(MMA.z() * 1000);
rotach 2:edcfec87de9b 70 sprintf(pubString, "{\"x\":%d,\"y\":%d,\"z\":%d}", x_val, y_val, z_val);
rotach 2:edcfec87de9b 71 mqtt.publish(topicString, pubString);
rotach 2:edcfec87de9b 72 wait(1);
rotach 2:edcfec87de9b 73 mqtt.loop();
jwende 0:3f9dd63f4f98 74 }
rotach 2:edcfec87de9b 75 }