Public Repository for IoT demo, leveraging the FRDM-K64F, WNC Cellular Modem, and AT&T M2X platform. Expects GPIO input from a sensor to send a discrete value to M2X.
Dependencies: ConfigFile M2XStreamClient-JMF PinDetect SDFileSystem WNCInterface jsonlite mbed-rtos mbed
Fork of SVP_IoT_M2X_Cellular by
Diff: main.cpp
- Revision:
- 10:6d7e4693dff7
- Parent:
- 8:b18770740a4d
- Child:
- 11:dc15a7897495
diff -r 1625ae080d5a -r 6d7e4693dff7 main.cpp --- a/main.cpp Sat Feb 25 21:04:31 2017 +0000 +++ b/main.cpp Thu Mar 02 18:04:49 2017 +0000 @@ -6,11 +6,10 @@ #include "mbed.h" #include "PinDetect.h" #include "WNCInterface.h" +#include "SDFileSystem.h" +#include "FATFileSystem.h" #include "ConfigFile.h" -LocalFileSystem local("local"); -ConfigFile cfg; - #define MBED_PLATFORM #define M2X_ENABLE_READER @@ -19,6 +18,15 @@ #define CRLF "\n\r" + +WNCInterface eth; +Client client; +MODSERIAL pc(USBTX,USBRX,256,256); + +//LocalFileSystem local("local"); Not supported on K64F +SDFileSystem local(PTE3, PTE1, PTE2, PTE4, "local"); +ConfigFile cfg; + //char deviceId[] = "3263056a467bc4eacd787bb0af9c1ad6"; // Device you want to post to //char m2xKey[] = "0aad274b67c7faa8110769ae9d739033"; // Your M2X API Key or Master API Key @@ -29,11 +37,19 @@ DigitalOut led_red(LED_RED); DigitalOut led_blue(LED_BLUE); -WNCInterface eth; -Client client; +char key1[] = "deviceId"; +char deviceId[BUFSIZ]; +char key2[] = "m2xKey"; +char m2xKey[BUFSIZ]; + +char timestamp[25]; +int length = 25; + M2XStreamClient m2xClient(&client, m2xKey); TimeService timeService(&m2xClient); -MODSERIAL pc(USBTX,USBRX,256,256); + + + //******************************************************************************************************************************************** //* Set the RGB LED's Color @@ -59,21 +75,27 @@ PinDetect pin ( A4, PullUp ); PinDetect button ( SW3, PullUp ); -int pinState; +int pinState; //0 = no trigger, 1 = triggered +int m2xOnline; //0 = offline, 1= online // C function callbacks follow. void keyPressed( void ) { pinState = pinState++; - SetLedColor(1); + if (m2xOnline >= 1) { + SetLedColor(1); + } //pc.printf("Key Pressed\n"); } void keyReleased( void ) { pinState = pinState--; - if(pinState == 0){ + if (m2xOnline >= 1) { SetLedColor(4); - }; + } +// if(pinState == 0){ +// SetLedColor(4); +// }; //pc.printf("Key Released\n"); } @@ -123,6 +145,7 @@ timeService.getTimestamp(timestamp, &length); pc.printf("Current timestamp: %s" CRLF, timestamp); strcpy(endTime,timestamp); + m2xOnline = 1; SetLedColor(4); } } @@ -131,35 +154,38 @@ int main() { + + //char deviceId[] = "3263056a467bc4eacd787bb0af9c1ad6"; // Device you want to post to //char m2xKey[] = "0aad274b67c7faa8110769ae9d739033"; // Your M2X API Key or Master API Key + pc.baud(115200); + pc.printf("Hello World! I am here!" CRLF); - char key1 = "deviceId"; - char deviceId[BUFSIZ]; - char key2 = "m2xKey"; - char m2xKey[BUFSIZ]; + wait(5); + + /* * Read a configuration file from a mbed. */ + if (!cfg.read("/local/settings.txt")) { - error("Failure to read a configuration file.\n"); + pc.printf("Failure to read a configuration file." CRLF); } - + /* * Get a configuration value. */ + if (cfg.getValue(key1, &deviceId[0], sizeof(deviceId))) { - printf("'%s'='%s'\n", key1, deviceId); + pc.printf("'%s'='%s'" CRLF, key1, deviceId); } if (cfg.getValue(key2, &m2xKey[0], sizeof(m2xKey))) { - printf("'%s'='%s'\n", key2, m2xKey); + pc.printf("'%s'='%s'" CRLF, key2, m2xKey); } pinState = 0; - pc.baud(115200); - pc.printf("Hello World! I am here!" CRLF); pin.attach_asserted( &keyPressed ); pin.attach_deasserted( &keyReleased ); button.attach_asserted( &keyPressed ); @@ -189,18 +215,13 @@ // using this setup function:- pin.setAssertValue( 0 ); button.setAssertValue( 0 ); - - - - char timestamp[25]; - int length = 25; int m2xresponse, cnt=1; int pinValue; wait(20); initModem(); //call function to init modem, wait till it comes online, and get IP address - initM2X{}; + initM2X(); // //debug // wait(10); // wait for terminal connected @@ -248,8 +269,10 @@ m2xresponse = m2xClient.updateStreamValue(deviceId, "water-sensor", pinValue); pc.printf("Post response code: %d" CRLF, m2xresponse); if (m2xresponse != 202){ + m2xOnline = 0; SetLedColor(2); pc.printf("Error connecting to M2X" CRLF); + wait(5); //pc.printf("IP Address: %s " CRLF CRLF, eth.getIPAddress()); if (m2xresponse == -1){ pc.printf("Modem Error - attempting to reconnect" CRLF); @@ -257,10 +280,16 @@ initM2X(); } } + if (m2xresponse == 202){ + if (m2xOnline == 0) { //first time m2x came back online + SetLedColor(4); + } + m2xOnline = m2xOnline++; + } timeService.getTimestamp(timestamp, &length); pc.printf("Thats all folks, got to wait 5 seconds... (%s)" CRLF CRLF CRLF, timestamp); // wait 5 secs and then loop - delay(5000); + delay(1000); } } \ No newline at end of file