Avnet / Mbed 2 deprecated WNCInterface_M2XMQTTdemo

Dependencies:   M2XMQTTClient WNCInterface mbed-rtos mbed minimal-json minimal-mqtt-JMF

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "WNCInterface.h"
00003 
00004 //#define DEBUG
00005 #define MBED_PLATFORM
00006 #define LOOP_DELAY_MS  8000
00007 
00008 #include "minimal-mqtt.h"
00009 #include "minimal-json.h"
00010 #include "M2XMQTTClient.h"
00011 
00012 #define CRLF    "\n\r"
00013 
00014 //Device ID:        8bcfff8a8514678f0fc6b56cb0f55c87
00015 //Master Key:       3d1f3c0f42a8c541205f706f62c65330
00016 
00017 char deviceId[] = "8bcfff8a8514678f0fc6b56cb0f55c87"; // Device Primary API Key
00018 char m2xKey[]   = "3d1f3c0f42a8c541205f706f62c65330"; // Your M2X Master API Key
00019 char name[] = "Wake Forest"; // Name of current location of datasource
00020 
00021 const char *hstreamName = "humidity";
00022 const char *tstreamName = "temperature";
00023 
00024 double latitude = -37.97884;
00025 double longitude = -57.54787; // You can also read those values from a GPS
00026 double elevation = 15;
00027 
00028 const char *streamNames[] = { tstreamName, hstreamName };
00029 int streamNum  = sizeof(streamNames)/sizeof(const char *);
00030 const int counts[] = { 2, 1 };
00031 const char *ats[] = { "2016-09-12T12:12:12.234Z", 
00032                       "2016-09-12T12:12:12.567Z", 
00033                       "2016-09-12T12:12:12.000Z" };
00034 double values[] = { 27.9, 81.2, 16.1 };
00035 
00036 Client client;
00037 M2XMQTTClient m2xClient(&client, m2xKey);
00038 WNCInterface eth;
00039 MODSERIAL pc(USBTX,USBRX,256,256);
00040 
00041 int main() {
00042   int response, cnt=1;
00043   double tval = 0.9;
00044   double hval = 101.0;
00045 
00046   pc.baud(115200);
00047   pc.printf(CRLF CRLF "M2X MQTT Test starting..." CRLF);
00048 
00049   pc.printf("init() returned 0x%04X" CRLF, eth.init(NULL,&pc));
00050   eth.connect();
00051   pc.printf("IP Address: %s" CRLF, eth.getIPAddress());
00052   eth.doDebug(0);
00053   
00054   while (true) {
00055     tval += 0.1;
00056     if( tval > 100.0 ) tval = 0.9;
00057     pc.printf("\r\n\r\nSending readings #%d\r\n",cnt++);
00058     response = m2xClient.updateStreamValue(deviceId, tstreamName, tval);
00059     pc.printf("Sending temperature value: %lf, Response= %d" CRLF, tval, response);
00060 
00061     if( hval < 2.0 ) hval = 101.0;
00062     hval -= 1.0;
00063     pc.printf("Sending humidity value: %lf", hval);
00064     response = m2xClient.updateStreamValue(deviceId, hstreamName, hval);
00065     pc.printf(", Response= %d" CRLF, response);
00066 
00067     pc.printf("Calling postDeviceUpdates...");
00068     response = m2xClient.postDeviceUpdates(deviceId, 
00069                 streamNum,
00070                 streamNames, 
00071                 counts, 
00072                 ats, 
00073                 values);
00074     pc.printf(" Response = %d" CRLF, response);
00075     pc.printf("Calling updateLocation...");
00076     response = m2xClient.updateLocation(deviceId, name, latitude, longitude, elevation);
00077     pc.printf(" Response =  %d" CRLF, response);
00078     elevation++;
00079     
00080     delay(LOOP_DELAY_MS);
00081   }
00082 }
00083