Includes support for IKS01A1 per HelloWorld_IKS01A1

Dependencies:   NetworkSocketAPI X_NUCLEO_IDW01M1v2 X_NUCLEO_IKS01A1 mbed

Fork of Nucleo_read_a0_thingspace by Tom Amershek

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 // read analog0 and send to thingspace.io
00002 // toma 2016-11-01
00003 
00004 #include "mbed.h"
00005 #include "stdio.h"
00006 #include "SpwfInterface.h"
00007 #include "TCPSocket.h"
00008 #include <string>
00009 #include "x_nucleo_iks01a1.h"
00010 
00011 /*************************************
00012 //FRDM-K64: D9->UART1_TX, D7->UART1_RX
00013 Pin connections:
00014     FRDM      IDW01M1
00015    ------    ---------
00016     +3v3 <--> +3v3
00017     GND  <--> GND
00018     D9   <--> D8
00019     D7   <--> D2
00020 
00021 SpwfSAInterface spwf(D9, D7, false);
00022 *************************************/
00023 /*************************************
00024 //LPCXpresso11U68: D9->UART1_TX, D7->UART1_RX
00025 Pin connections:
00026     LPC      IDW01M1
00027    ------    ---------
00028     +3v3 <--> +3v3
00029     GND  <--> GND
00030     A1   <--> D8
00031     A2   <--> D2
00032 
00033 SpwfSAInterface spwf(A1, A2, false);
00034 *************************************/
00035 
00036 //NUCLEO: D8->UART1_TX (PA_9), D2->UART1_RX (PA_10)
00037 
00038 using namespace std;
00039 
00040 AnalogIn analog_input_A0(A0);
00041 Serial serial_port(USBTX, USBRX);
00042 DigitalOut myLed(LED1);
00043 SpwfSAInterface spwf(D8, D2, false);
00044 
00045 /* Instantiate the expansion board */
00046 static X_NUCLEO_IKS01A1 *mems_expansion_board = X_NUCLEO_IKS01A1::Instance(D14, D15);
00047 
00048 /* Retrieve the composing elements of the expansion board */
00049 static GyroSensor *gyroscope = mems_expansion_board->GetGyroscope();
00050 static MotionSensor *accelerometer = mems_expansion_board->GetAccelerometer();
00051 static MagneticSensor *magnetometer = mems_expansion_board->magnetometer;
00052 static HumiditySensor *humidity_sensor = mems_expansion_board->ht_sensor;
00053 static PressureSensor *pressure_sensor = mems_expansion_board->pt_sensor;
00054 static TempSensor *temp_sensor1 = mems_expansion_board->ht_sensor;
00055 static TempSensor *temp_sensor2 = mems_expansion_board->pt_sensor;
00056 
00057 int errConnect;
00058 int errSend;
00059 
00060 /* Helper function for printing floats & doubles */
00061 static char *printDouble(char* str, double v, int decimalDigits=2)
00062 {
00063   int i = 1;
00064   int intPart, fractPart;
00065   int len;
00066   char *ptr;
00067 
00068   /* prepare decimal digits multiplicator */
00069   for (;decimalDigits!=0; i*=10, decimalDigits--);
00070 
00071   /* calculate integer & fractinal parts */
00072   intPart = (int)v;
00073   fractPart = (int)((v-(double)(int)v)*i);
00074 
00075   /* fill in integer part */
00076   sprintf(str, "%i.", intPart);
00077 
00078   /* prepare fill in of fractional part */
00079   len = strlen(str);
00080   ptr = &str[len];
00081 
00082   /* fill in leading fractional zeros */
00083   for (i/=10;i>1; i/=10, ptr++) {
00084     if(fractPart >= i) break;
00085     *ptr = '0';
00086   }
00087 
00088   /* fill in (rest of) fractional part */
00089   sprintf(ptr, "%i", fractPart);
00090 
00091   return str;
00092 }
00093 
00094 int main()
00095 {
00096     uint8_t id;
00097     float value1, value2;
00098     char buffer1[32], buffer2[32];
00099     int32_t axes[3];
00100     
00101     float panel_voltage;
00102     DigitalOut led(LED1);
00103     serial_port.baud(115200);
00104     
00105     printf("\r\n\r\n*** system restart");
00106     printf("\r\n\r\nanalog0 example using thingspace.io ...\n");
00107     
00108     char *ssid = "";
00109     char *seckey = "";
00110     const char *mac;
00111         
00112     printf("X-NUCLEO-IDW01M1v2 mbed application\r\n");     
00113         
00114     int spwfResponse;
00115     spwfResponse = 0;
00116     
00117     while(spwfResponse != 1) {
00118         printf("connecting to access point ...\r\n");
00119         spwfResponse = spwf.connect(ssid, seckey, NSAPI_SECURITY_WPA2);//WPA
00120         printf("\nresponse:  %i\n", spwfResponse);
00121         if(spwfResponse != 1)
00122         {
00123             printf("error making connecting to access point\r\n");
00124         }
00125         else
00126         {
00127             printf("success connecting to access point\r\n");
00128             const char *ip = spwf.get_ip_address();
00129             mac = spwf.get_mac_address();
00130             printf("\r\nip address = %s\r\n", (ip) ? ip : "error getting ip address");
00131             printf("mac address = %s\r\n", (mac) ? mac : "error getting the mac address\n");
00132         }
00133     }
00134     
00135     SocketAddress addrDweetServer(&spwf, "thingspace.io");
00136     printf("\r\nthingspace.verizon.com resolved to: %s\r\n\r\n", addrDweetServer.get_ip_address());
00137 
00138     TCPSocket socket(&spwf);
00139 
00140     // connect socket
00141     errConnect = socket.connect("thingspace.io", 80);
00142     if(errConnect!=0) {
00143         printf("\r\ncould not connect to socket; error = %d\r\n", errConnect);
00144     } else {
00145         printf("socket connected\r\n");
00146     }
00147 
00148     // get the last 2 bytes of the mac for the thing name
00149     std::string macString = mac;
00150     macString.erase(0,9);
00151     macString.erase(2,1);
00152     macString.erase(4,1);
00153     const char *macBytes = macString.c_str();
00154     
00155     humidity_sensor->ReadID(&id);
00156     pressure_sensor->ReadID(&id);
00157     magnetometer->ReadID(&id);
00158     gyroscope->ReadID(&id);
00159     wait(3);
00160 
00161     while(1) {
00162        
00163         std::string readings = "";
00164         temp_sensor1->GetTemperature(&value1);
00165         humidity_sensor->GetHumidity(&value2);
00166         
00167         readings = readings + "&temperature1=";
00168         readings = readings + printDouble(buffer1,value1);
00169         readings = readings + "&humidity=";
00170         readings = readings + printDouble(buffer2,value2);
00171                 
00172         temp_sensor2->GetFahrenheit(&value1);
00173         pressure_sensor->GetPressure(&value2);
00174         
00175         readings = readings + "&temperature2=";
00176         readings = readings + printDouble(buffer1,value1);
00177         readings = readings + "&pressure=";
00178         readings = readings + printDouble(buffer2,value2);
00179         
00180         char magBuff[64] = "";
00181         magnetometer->Get_M_Axes(axes);
00182         sprintf(magBuff, "&magnometer0=%ld&magnometer1=%ld&magnometer2=%ld", axes[0], axes[1], axes[2]);
00183         readings = readings + magBuff;      
00184         
00185         char accBuff[64] = "";
00186         accelerometer->Get_X_Axes(axes);
00187         sprintf(accBuff, "&accelerometer0=%ld&accelerometer1=%ld&accelerometer2=%ld", axes[0], axes[1], axes[2]);
00188         readings = readings + accBuff;          
00189         
00190         char gyroBuff[64] = "";
00191         gyroscope->Get_G_Axes(axes);
00192         sprintf(gyroBuff, "&gyroscope0=%ld&gyroscope1=%ld&gyroscope2=%ld", axes[0], axes[1], axes[2]);
00193         readings = readings + gyroBuff;
00194         
00195         // printf(readings.c_str());
00196           
00197         panel_voltage = analog_input_A0.read();
00198         // printf("voltage:  %f\r\n", panel_voltage);
00199         
00200         // A0 is voltage tolerant to 3.3V, and analog read returns a percentage of the maximum
00201         // need to convert the percentage back to a representative number
00202         panel_voltage = panel_voltage * 3300; // change the value to be in the 0 to 3300 range
00203         // printf("a0 reads %.3f mV\n", panel_voltage); // use 3 decimals of precision
00204         
00205         // enable LED if voltage exceeds 2000 mV
00206         if (panel_voltage > 2000) { 
00207             myLed = 1;
00208         } 
00209         else {
00210             myLed = 0;
00211         }
00212         
00213         // don't bother if never connected ...        
00214         if (spwfResponse == 1) {    
00215             // get length of jsonContent as string without streams 
00216             // adapted from http://codereview.stackexchange.com/questions/51270/socket-http-post-request
00217             
00218             char dweetBuffer[512] = "";
00219             
00220             // create GET HTTP header for dweeting
00221             strcpy(dweetBuffer, "GET /dweet/for/nucleo-");     
00222             // uncomment to use the last 6 digits of WiFi MAC
00223             strcat(dweetBuffer, macBytes);
00224             char valueRead[16];
00225             sprintf(valueRead, "?a0=%f", panel_voltage);
00226             strcat(dweetBuffer, valueRead);
00227             strcat(dweetBuffer, readings.c_str());
00228             strcat(dweetBuffer, " HTTP/1.1\r\n\r\n"); 
00229                        
00230             serial_port.printf("\r\n%s", dweetBuffer);
00231 
00232             char bufferRx[1024] = "";
00233             int countRx = 0;
00234             serial_port.printf("sending and receiving data ...\r\n");
00235             errSend = socket.send(dweetBuffer, strlen(dweetBuffer));
00236             countRx = socket.recv(bufferRx, sizeof bufferRx);
00237             printf("sent %d bytes and received %d bytes\r\n\r\n", errSend, countRx);
00238             printf(bufferRx);
00239             printf("\r\n\r\n*** 5-second pause ...\r\n\r\n");
00240         }
00241         wait(5.0); // 5000 ms delay before looping to next read
00242     }
00243 }