BERG Cloud / Mbed 2 deprecated LittleCounter-Example

Dependencies:   BERGCloud mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002     LittleCounter - A simple demonstration of how you use the BERG Cloud mbed
00003                     client libraries to fetch and send data to BERG Cloud. For
00004                     more info see http://bergcloud.com/
00005 
00006     This example code is in the public domain.
00007 
00008     https://github.com/bergcloud/devshield-mbed
00009 */
00010 
00011 #include "mbed.h"
00012 #include "BERGCloudMbed.h"
00013 #include <sstream> // std::stringstream
00014 
00015 // Select the pins to be used for the SPI interface
00016 #if defined(TARGET_LPC176X) || defined(TARGET_LPC11UXX)
00017 // mbed DIP40 pinout
00018 #define MOSI_PIN p5
00019 #define MISO_PIN p6
00020 #define SCLK_PIN p7
00021 #define nSSEL_PIN p8
00022 #endif
00023 
00024 #if defined(TARGET_KL25Z)
00025 // Arduino compatible pinout
00026 #define MOSI_PIN D11
00027 #define MISO_PIN D12
00028 #define SCLK_PIN D13
00029 #define nSSEL_PIN D10
00030 #endif
00031 
00032 #if !defined(MOSI_PIN) || !defined(MISO_PIN) || !defined(SCLK_PIN) || !defined(nSSEL_PIN)
00033 #error Please add the pin names for your microcontroller.
00034 #endif
00035 
00036 // The Project Key ties this code into a Project on developer.bergcloud.com
00037 const uint8_t PROJECT_KEY[BC_KEY_SIZE_BYTES] = \
00038     {0x8B,0x05,0xF7,0x25,0x10,0x54,0x0A,0xE4,0x7C,0x35,0xEE,0xE7,0x26,0xDC,0xD5,0xA8};
00039 
00040 // The version of your code
00041 #define VERSION 0x0001
00042 
00043 // Define your commands and events here, according to the schema from bergcloud.com
00044 #define COMMAND_SET_COUNTER 0x01
00045 #define COMMAND_DISPLAY_TEXT 0x02
00046 #define EVENT_COUNTER_CHANGED 0x01
00047 
00048 // Forward declarations
00049 void loop(void);
00050 
00051 // These two methods correspond to the two commands defined within the Little Counter
00052 // project on the BERG Cloud developer site
00053 void handleSetCounter(BERGCloudMessage&);
00054 void handleGreet(BERGCloudMessage&);
00055 
00056 // Create an instance of the BERGCloudMbed class
00057 BERGCloudMbed BERGCloud;
00058 
00059 // The counter we will increment and send up to the cloud
00060 int32_t counter;
00061 
00062 int main(void)
00063 {
00064   BERGCloud.begin(MOSI_PIN, MISO_PIN, SCLK_PIN, nSSEL_PIN);
00065   printf("--- mbed reset ---\r\n");
00066 
00067   counter = 0; // Initialise our counter
00068 
00069   // Attempt to connect with our project key and build version
00070   if (BERGCloud.connect(PROJECT_KEY, VERSION)) {
00071     printf("Connected to network");
00072   } else {
00073     printf("BERGCloud.connect() returned false.");
00074   }
00075   
00076   // Run continuously
00077   while (1)
00078   {
00079     // The loop() function handles commands and events
00080     loop();
00081   }
00082   
00083 }
00084 
00085 void loop(void)
00086 { 
00087   // The ID of the command we've received. 
00088   uint8_t commandID;
00089   
00090   // The command and event objects we use below    
00091   BERGCloudMessage command, event;
00092 
00093   // Some simple string manipulation
00094 
00095   ///////////////////////////////////////////////////////////////
00096   // Fetching commands                                         //
00097   ///////////////////////////////////////////////////////////////
00098   
00099   printf("Poll for command... ");
00100   if (BERGCloud.pollForCommand(command, commandID)) {
00101 
00102     // Print the 
00103     printf("got command with ID: %u\r\n", commandID);
00104     
00105     // Here we can map the command IDs to method calls within our code
00106     switch (commandID) {
00107       case COMMAND_SET_COUNTER:
00108         // Command one contains an integer
00109         handleSetCounter(command);
00110         break;
00111       case COMMAND_DISPLAY_TEXT:
00112         // Command two is a simple message containing a string
00113         handleGreet(command);
00114         break;
00115       // Default case, we don't have a match for the Command ID
00116       default:
00117         printf("WARNING: Unknown command\r\n");   
00118     }
00119   } else {
00120     // No command!
00121     printf("none.\r\n");
00122   }
00123   
00124   ///////////////////////////////////////////////////////////////
00125   // Sending events                                            //
00126   ///////////////////////////////////////////////////////////////
00127 
00128   printf("Sending an event... ");
00129 
00130   // In this Little Counter example we send up a string and a counter
00131   //
00132   // Packing is very straight forward. Just define your
00133   // BERGCloudMessage object and call pack() passing in each type
00134   // you wish to encode.
00135   
00136   // There is currently a 64 byte limit on the size of events, so be
00137   // careful with how much data you're sending up!
00138   
00139   event.pack("BERG");  // Pack a string
00140   event.pack(counter); // Pack an unsigned int32
00141   
00142   // Send the event object
00143   if (BERGCloud.sendEvent(EVENT_COUNTER_CHANGED, event))  {
00144     printf("ok\r\n");
00145   } else {
00146     printf("failed/busy\r\n");
00147   }
00148   
00149   counter++; // Increment our counter each time we loop around
00150 
00151   // A simple delay to rate limit what we send up to the cloud
00152   wait_ms(5000);
00153 }
00154 
00155 void handleSetCounter(BERGCloudMessage &command) {
00156   string prefixText;
00157   int32_t newCounterVal;
00158   stringstream counterText;
00159   
00160   prefixText = "Counter set to";
00161 
00162   // We're expecting an integer value for the counter
00163   if (command.unpack(newCounterVal)) {
00164     
00165     printf("Decoded newCounterVal as: %i\r\n", newCounterVal);
00166     
00167     // Set the global to our new value
00168     counter = newCounterVal;
00169 
00170     // Convert to string (see std::stringstream)
00171     counterText << newCounterVal;
00172 
00173     // Show the string on the OLED screen with display()
00174     BERGCloud.clearDisplay();
00175     
00176     // Print in reverse order
00177     BERGCloud.display(counterText.str().c_str());
00178     BERGCloud.display(prefixText.c_str());
00179   } else {
00180     printf("WARNING: unpacking the new counter value failed\r\n");
00181   }
00182 }
00183 
00184 void handleGreet(BERGCloudMessage &command) {
00185   string prefixText, suffixText, finalText;
00186   int32_t number;
00187   
00188   prefixText = "Hello, ";
00189 
00190   // We're expecting a string and a number for display-text, so
00191   // we attempt to decode these types in turn
00192 
00193   if (command.unpack(suffixText)) {
00194     printf("Decoded text: '%s'\r\n", suffixText.c_str());
00195 
00196     // Concatenate our strings
00197     finalText = prefixText + suffixText;
00198 
00199     // Show the string on the OLED screen with display()
00200     BERGCloud.clearDisplay();
00201     BERGCloud.display(finalText.c_str());
00202   } else {
00203     printf("WARNING: unpacking text failed\r\n");
00204   }
00205   
00206   // For this command, we can optionally be passed an integer
00207   // and we log this out on the serial console.
00208   // 
00209   // This is to demonstrate serializing multiple values within
00210   // one command.
00211 
00212   if (command.unpack(number)) {
00213     printf("Decoded number: %i\r\n", number);
00214   } else {
00215     printf("No additional number given\r\n");
00216   }
00217 }