Initial creation of sample code for connecting STM32 Nucleo to M2X via ACKme Wiconnect

Dependencies:   M2XStreamClient WiConnect jsonlite mbed

Fork of wiconnect-join_example by ACKme

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers example.cpp Source File

example.cpp

00001 /**
00002  * @example join/example.cpp
00003  *
00004  * This is an example of using the join network API to join a WiFi network 
00005  *  and connect to AT&T M2X cloud (https://m2x.att.com) and post data. 
00006  *
00007  * It works as follows:
00008  * 1. Instantiate the WiConnect Library
00009  * 2. Initiate Communication with WiFi Module
00010  * 3. Join a network using the specified parameters
00011  * 4. That's it!
00012  *
00013  *
00014  */
00015 
00016 
00017 /******************************************************************************
00018  * Example Variables
00019  */
00020 
00021 // This is the name of your WiFi network
00022 // Look for this name in your WiFi settings
00023 // (e.g. your phone's list of WiFi networks in the WiFi settings menu)
00024 // tip: add double-quotes around SSID to add spaces to name
00025 #define NETWORK_SSID "hackathon" //"\"<YOUR NETWORK NAME HERE>\""
00026 
00027 // This is the password of your WiFi network
00028 // Leave as empty string (e.g "") to connect to OPEN network
00029 #define NETWORK_PASSWORD "" //"\"<YOUR NETWORK PASSWORD HERE>\""
00030 
00031 
00032 /******************************************************************************
00033  * Includes
00034  */
00035 
00036 // include C library headers
00037 #include <stdio.h> // needed for printf
00038 // include target specific defines
00039 #include "target_config.h"
00040 // include the Wiconnect Host Library API header
00041 #include "Wiconnect.h"
00042 #include "mbed.h"
00043 #include "M2XStreamClient.h"
00044 
00045 
00046 /******************************************************************************
00047  * Global Defines
00048  */
00049 
00050 const char key[] = "ca9c1e4db697886906de09c701879b19";    // Replace with your M2X API key
00051 const char feed[] = "fe08906d21a70b05241234077386e041";   // Replace with your blueprint Feed ID
00052 const char stream[] = "temperature"; // Replace with your stream name  
00053 char name[] = "austin_st_office"; // Name of current location of datasource
00054 
00055 // Serial used for printfs to terminal (i.e. NOT used for WiConnect)
00056 static Serial consoleSerial(STDIO_UART_TX, STDIO_UART_RX);
00057 
00058 
00059 double latitude = 30.3748076;
00060 double longitude = -97.7386896; // You can also read those values from a GPS
00061 double elevation = 300.00;
00062 
00063 
00064 DigitalOut myled(D7);   
00065 AnalogIn tempSensor(A0);  
00066 
00067 
00068 /******************************************************************************
00069  * Starting point of application
00070  */
00071 int main(int argc, char **argv)
00072 {
00073     char amb_temp[6];
00074     int response;
00075     int a;
00076     int adc_scale = 4095; 
00077     int B = 3975;
00078     double resistance; 
00079     double temperature;
00080     double temperature_f;   
00081         
00082     consoleSerial.baud(9600); // console terminal to 115200 baud
00083 
00084     //-------------------------------------------------------------------------
00085     // STEP 1: Instantiate WiConnect Library
00086     //-------------------------------------------------------------------------
00087 
00088     // Setup  wiconnect serial interface configuration
00089     // Here we only specify the rx buffer size and not rx buffer pointer, this means
00090     // The serial RX buffer will be dynamically allocated
00091     SerialConfig serialConfig(WICONNECT_RX_PIN, WICONNECT_TX_PIN, 256, NULL);
00092 
00093     // Instantiate the Wiconnect library
00094     // Here we only specify the buffer size and not buffer pointer, this means
00095     // The internal buffer will be dynamically allocated
00096     Wiconnect wiconnect(serialConfig, 256, NULL, WICONNECT_RESET_PIN);
00097 
00098     //-------------------------------------------------------------------------
00099     // STEP 2: Initiate Communication with WiFi Module
00100     //-------------------------------------------------------------------------
00101 
00102     printf("Initializing WiConnect Library...\r\n");
00103 
00104     // Initialize communication with WiFi module
00105     if(wiconnect.init(true) != WICONNECT_SUCCESS)
00106     {
00107         printf("Failed to initialize communication with WiFi module!\r\n"
00108                 "Make sure the wires are connected correctly\r\n");
00109         for(;;); // infinite loop
00110     }
00111 
00112 
00113     //-------------------------------------------------------------------------
00114     // STEP 3: Join a network using the specified parameters
00115     //-------------------------------------------------------------------------
00116 
00117     printf("Joining network: %s....\r\n", NETWORK_SSID);
00118 
00119     // optional step, set the channel mask to force module to associate to networks on specific channels
00120     // this command enables all channels
00121     wiconnect.setSetting(SETTING_WLAN_CHANNEL_MASK, (uint32_t)0x3FFF);
00122 
00123     if(wiconnect.join(NETWORK_SSID, NETWORK_PASSWORD) != WICONNECT_SUCCESS)
00124     {
00125         printf("Failed to send join command\r\n");
00126         for(;;); // infinite loop
00127     }
00128 
00129     printf("IP Address: %s\r\n", wiconnect.getIpAddress());
00130     printf("Network join example has completed!\r\n");
00131 
00132     //-------------------------------------------------------------------------
00133     // STEP 4: Initialize the M2X Client 
00134     //-------------------------------------------------------------------------
00135     
00136     Client client;    
00137     M2XStreamClient m2xClient(&client, key);
00138 
00139     //-------------------------------------------------------------------------
00140     // STEP 5: Update location of device
00141     //-------------------------------------------------------------------------    
00142     
00143     response = m2xClient.updateLocation(feed, name, latitude, longitude, elevation);
00144     printf("updateLocation response code: %d\r\n", response);
00145     //if (response == -1) while (true) ;
00146 
00147     //-------------------------------------------------------------------------
00148     // STEP 6: Read temperature sensor and post value on M2X Stream
00149     //-------------------------------------------------------------------------            
00150     
00151     while(1)    
00152     {    
00153         myled = 1; // LED is ON    
00154         a = tempSensor.read_u16();
00155                
00156         resistance = (float)(adc_scale-a)*10000/a; //get the resistance of the sensor;              
00157         temperature = 1/(log(resistance/10000)/B+1/298.15)-273.15;  //convert to temperature via datasheet        
00158         temperature_f = (1.8 * temperature) + 32.0;
00159         sprintf(amb_temp, "%0.2f", temperature_f);  
00160         
00161         printf("Temp Sensor Analog Reading is 0x%X = %d   ", a, a);         
00162         printf("Current Temperature: %f C  %f F \n\r", temperature, temperature_f); 
00163                 
00164         response = m2xClient.post(feed, stream, amb_temp);
00165         printf("Post response code: %d\r\n", response);
00166         //if (response == -1) while (true) ;           
00167                 
00168         myled = 0; // LED is OFF        
00169 
00170         delay(1000);        
00171     }  
00172 }
00173