Lawrence Mok / Mbed 2 deprecated m2x-demo-all-cc3000

Dependencies:   M2XStreamClient cc3000_hostdriver_mbedsocket jsonlite mbed-rtos mbed

Fork of m2x-demo-all by AT&T M2X Team

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include <jsonlite.h>
00002 #include "M2XStreamClient.h"
00003 
00004 #include "mbed.h"
00005 #include "cc3000.h"
00006 
00007 #define DEBUG 1
00008 #if DEBUG
00009   #define dprintf printf
00010 #else
00011   #define dprintf(...)
00012 #endif
00013 
00014 char feedId[] = "<feed id>"; // Feed you want to post to
00015 char m2xKey[] = "<m2x api key>"; // Your M2X access key
00016 char streamName[] = "<stream name>"; // Stream you want to post to
00017 
00018 char SSID[] = "your_ssid";
00019 char KEY[] = "your_key";
00020 //Adafruit CC3000 shield on Arduino pin definitions.
00021 cc3000 net(D3, D5, D10, SPI(D11, D12, D13), SSID, KEY, WPA2, false);
00022 Serial pc(USBTX, USBRX);
00023 
00024 Client client;
00025 M2XStreamClient m2xClient(&client, m2xKey);
00026 
00027 typedef struct scanResults {
00028   unsigned long numNetworksFound;
00029   unsigned long results;
00030   unsigned isValid:1;
00031   unsigned rssi:7;
00032   unsigned securityMode:2;
00033   unsigned ssidLength:6;
00034   unsigned short frameTime;
00035   unsigned char ssid_name[32];
00036   unsigned char bssid[6];
00037 } scanResults;
00038 
00039 /** return RSSI in dBm for given ssid if found.  returns 0 on failure */
00040 int8_t getRSSI(char *ssid, int len) {
00041   int ret;
00042   scanResults sr;
00043   int apCounter;
00044   
00045   //TODO request a scan
00046   
00047   if ((ret = net._wlan.ioctl_get_scan_results(2000, (unsigned char *)&sr)) != 0) {
00048     printf("get scan results failed ret=%d\r\n", ret);
00049     return 0;
00050   }
00051   apCounter = sr.numNetworksFound;
00052   dprintf("APs found: %d\r\n", apCounter);
00053 
00054   do {
00055     if (sr.isValid) {
00056       if (DEBUG) {
00057         char ssidbuf[32];
00058         memcpy(ssidbuf, sr.ssid_name, sr.ssidLength);
00059         ssidbuf[sr.ssidLength] = 0;
00060         dprintf("ssid=%s rssi=%3d\r\n", ssidbuf, sr.rssi);
00061       }
00062       if (memcmp(ssid, sr.ssid_name, len) == 0) {
00063         return sr.rssi - 128;
00064       }
00065     }
00066     if (--apCounter> 0) {
00067       if ((ret = net._wlan.ioctl_get_scan_results(2000, (unsigned char *)&sr)) != 0) {
00068         printf("get scan results failed ret=%d\r\n", ret);
00069         return 0;
00070       }
00071     }
00072   } while (apCounter > 0);
00073   dprintf("End of AP list.\r\n");
00074   return 0;
00075 }
00076 
00077 void on_data_point_found(const char* at, const char* value, int index, void* context) {
00078   //TODO value is wrong. check m2x library
00079   printf("Found a data point, index: %d\r\n", index);
00080   printf("At: %s Value: %s\r\n", at, value);
00081 }
00082 
00083 int main() {
00084   pc.baud(115200);
00085   
00086   if (DEBUG) {
00087     // allow time to get a serial console
00088     for (int i=10; i>=0; i--) {
00089       wait(0.5);
00090       dprintf("Starting in %d\r\n", i);
00091     }
00092   }
00093 
00094   printf("net.init...\r\n");
00095   net.init();
00096   printf("net.init done\r\n");
00097   if (net.connect() == -1) {
00098     printf("Failed to connect. Please verify connection details and try again.\r\n");
00099   } else {
00100     printf("net.connect done. IP address: %s \r\n", net.getIPAddress());
00101   }
00102 
00103   int rssi;
00104   int i = 0;
00105   
00106   while (++i < 10) {
00107   
00108     // read RSSI
00109     rssi = getRSSI(SSID, sizeof(SSID) - 1);
00110     if (rssi == 0) {
00111         printf("Failed to get RSSI\r\n");
00112         delay(30000);
00113         continue;
00114     }
00115 
00116     // post RSSI
00117     dprintf("Posting rssi=%d\r\n", rssi);
00118     int response = m2xClient.post(feedId, streamName, rssi);
00119     printf("Post response code: %d\r\n", response);
00120     if (response == -1) while (true) ;
00121     
00122     // read RSSI
00123     dprintf("Reading rssi\r\n");
00124     response = m2xClient.fetchValues(feedId, streamName, on_data_point_found, NULL, NULL, NULL, "5");
00125     printf("Fetch response code: %d\r\n", response);
00126     if (response == -1) while (true) ;
00127 
00128     // wait 30 secs and then loop
00129     delay(30000);
00130   }
00131   dprintf("Exiting\r\n");
00132   net.disconnect();
00133 }