John Rotach / Mbed 2 deprecated 2lemetryNXP

Dependencies:   C12832_lcd EthernetInterface MMA7660 MQTT mbed-rtos mbed

Fork of MQTTTest by Joerg Wende

Files at this revision

API Documentation at this revision

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
+}