The NXP LCP1768 and mbed Application Board on 2lemetry platform.
Dependencies: C12832_lcd EthernetInterface MMA7660 MQTT mbed-rtos mbed
Fork of MQTTTest by
Introduction
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:
- Sign up for a free "hacker" account in the 2lemetry Portal.
- Import this project into your mbed online compiler workspace.
- Update the 2lemetry specific defines at the top of main.cpp (see below).
- Compile and upload.
- 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>"
Notes
- 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".
Revision 2:edcfec87de9b, committed 2013-12-24
- Comitter:
- rotach
- Date:
- Tue Dec 24 00:05:56 2013 +0000
- Parent:
- 1:5570162c5994
- Commit message:
- Initial commit.
Changed in this revision
MMA7660.lib | Show annotated file Show diff for this revision Revisions of this file |
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 5570162c5994 -r edcfec87de9b MMA7660.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MMA7660.lib Tue Dec 24 00:05:56 2013 +0000 @@ -0,0 +1,1 @@ +https://mbed.org/users/Sissors/code/MMA7660/#a8e20db7901e
diff -r 5570162c5994 -r edcfec87de9b main.cpp --- a/main.cpp Sun May 26 16:55:47 2013 +0000 +++ b/main.cpp Tue Dec 24 00:05:56 2013 +0000 @@ -2,55 +2,74 @@ #include "EthernetInterface.h" #include "C12832_lcd.h" #include "PubSubClient.h" +#include "MMA7660.h" + +#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>" C12832_LCD lcd; Serial pc(USBTX, USBRX); -char* serverIpAddr = "192.168.2.111"; /*Sever ip address*/ +char* serverIpAddr = "q.mq.tt"; int port = 1883; /*Sever Port*/ void callback(char* topic, char* payload, unsigned int len); /*Callback function prototype*/ PubSubClient mqtt(serverIpAddr, port, callback); EthernetInterface eth; +MMA7660 MMA(p28, p27); + void callback(char* topic, char* payload, unsigned int len) { - lcd.printf("Topic: %s\r\n", topic); + // for now just print the payload lcd.printf("Payload: %s\r\n\r\n", payload); - //Send incoming payloads back to topic "/mbed". - mqtt.publish("mbed", payload, len); + wait(2); + lcd.cls(); } int main() { + char clientID[30]; + char topicString[50]; 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"); - + + pc.printf("mbed example on 2lemetry"); - 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" */ + // create MQTT client ID + sprintf(clientID, _2LEMETRY_DEVICE_ID); - if(!mqtt.connect(clientID)){ + if(!mqtt.connect(clientID, _2LEMETRY_USERID, _2LEMETRY_TOKEN)){ pc.printf("\r\nConnect to server failed ..\r\n"); return -1; } - pc.printf("\r\nConnect to server sucessed ..\r\n"); + pc.printf("\r\nConnect to server successful ..\r\n"); + + int i = 0; + char pubString[50]; - mqtt.publish(pub_topic, "Hello here is mbed..."); - mqtt.subscribe(sub_topic); - + // temporarily use pubTopic string to create subscription topic + sprintf(topicString, "%s/%s/%s/cmd", _2LEMETRY_DOMAIN, _2LEMETRY_STUFF, _2LEMETRY_DEVICE_ID); + + mqtt.subscribe(topicString); - pc.printf("#### End of the test.. ####"); - - //eth.disconnect(); + // now overwrite with publish string + sprintf(topicString, "%s/%s/%s", _2LEMETRY_DOMAIN, _2LEMETRY_STUFF, _2LEMETRY_DEVICE_ID); while(1) { - mqtt.loop(); + int x_val, y_val, z_val; + x_val = (int)(MMA.x() * 1000); + y_val = (int)(MMA.y() * 1000); + z_val = (int)(MMA.z() * 1000); + sprintf(pubString, "{\"x\":%d,\"y\":%d,\"z\":%d}", x_val, y_val, z_val); + mqtt.publish(topicString, pubString); + wait(1); + mqtt.loop(); } -} \ No newline at end of file +}