Send data from STM32 to TD via Wifi

Dependencies:   BSP_B-L475E-IOT01

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* WiFi Example
00002  * Copyright (c) 2016 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "mbed.h"
00018 #include "TCPSocket.h"
00019 #include "wifi-ism43362/ISM43362Interface.h"
00020 #include "treasure-data-rest.h"
00021 // Sensors drivers present in the BSP library
00022 #include "stm32l475e_iot01_tsensor.h"
00023 #include "stm32l475e_iot01_hsensor.h"
00024 #include "stm32l475e_iot01_psensor.h"
00025 #include "stm32l475e_iot01_magneto.h"
00026 #include "stm32l475e_iot01_gyro.h"
00027 #include "stm32l475e_iot01_accelero.h"
00028 
00029 #define BUFF_SIZE   200
00030 
00031 ISM43362Interface net;
00032 // WiFiInterface *wifi;
00033 
00034 int main(void){
00035 
00036     int count = 0;
00037 
00038     printf("\r\nTreasure Data REST API Demo\n");
00039 
00040     // Connect to Wifi
00041     printf("\nConnecting to %s...\n", MBED_CONF_APP_WIFI_SSID);
00042     int ret = net.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
00043     if (ret != 0) {
00044         printf("\nConnection error: %d\n", ret);
00045         return -1;
00046     }
00047 
00048     printf("Success\n\n");
00049     printf("MAC: %s\n", net.get_mac_address());
00050     printf("IP: %s\n", net.get_ip_address());
00051     printf("Netmask: %s\n", net.get_netmask());
00052     printf("Gateway: %s\n", net.get_gateway());
00053     printf("RSSI: %d\n\n", net.get_rssi());
00054 
00055     // Create Treasure data objects (Network, Database, Table, APIKey)
00056     TreasureData_RESTAPI* heap  = new TreasureData_RESTAPI(&net,"iot_test","heap_info", MBED_CONF_APP_API_KEY);
00057     TreasureData_RESTAPI* cpu   = new TreasureData_RESTAPI(&net,"iot_test","cpu_info",  MBED_CONF_APP_API_KEY);
00058     TreasureData_RESTAPI* stack = new TreasureData_RESTAPI(&net,"iot_test","stack_info",MBED_CONF_APP_API_KEY);
00059     TreasureData_RESTAPI* sys   = new TreasureData_RESTAPI(&net,"iot_test","sys_info",  MBED_CONF_APP_API_KEY);
00060     TreasureData_RESTAPI* sensor   = new TreasureData_RESTAPI(&net,"iot_test","sensor_info",  MBED_CONF_APP_API_KEY);
00061 
00062 
00063     // Device Information Objects
00064     mbed_stats_cpu_t    cpuinfo;
00065     mbed_stats_heap_t   heapinfo;
00066     mbed_stats_stack_t  stackinfo;
00067     mbed_stats_sys_t    sysinfo;
00068 
00069     // Buffers to create strings in
00070     char cpu_buff  [BUFF_SIZE] = {0};
00071     char heap_buff [BUFF_SIZE] = {0};
00072     char stack_buff[BUFF_SIZE] = {0};
00073     char sys_buff  [BUFF_SIZE] = {0};
00074     char sensor_buff  [BUFF_SIZE] = {0};
00075     
00076     // Initiate sensor variables
00077     float sensor_value_temp = 0;
00078     float sensor_value_humid = 0;
00079     float sensor_value_pressure = 0;
00080 
00081     int16_t pDataXYZ[3] = {0};
00082     float pGyroDataXYZ[3] = {0};
00083  
00084     BSP_TSENSOR_Init();
00085     BSP_HSENSOR_Init();
00086     BSP_PSENSOR_Init();
00087          
00088     BSP_MAGNETO_Init();
00089     BSP_GYRO_Init();
00090     BSP_ACCELERO_Init();
00091 
00092     int x = 0;
00093 
00094     // Get device health data, send to Treasure Data every 10 seconds
00095     while(1){
00096         {
00097             
00098             
00099             // Collect local data
00100             mbed_stats_cpu_get(  &cpuinfo);
00101 
00102             // Construct strings to send
00103             x = sprintf(cpu_buff,"{\"uptime\":\"%d\",\"idle_time\":\"%d\",\"sleep_time\":\"%d\",\"deep_sleep_time\":\"%d\"}",
00104                                     cpuinfo.uptime,
00105                                     cpuinfo.idle_time,
00106                                     cpuinfo.sleep_time,
00107                                     cpuinfo.deep_sleep_time);
00108             cpu_buff[x]=0; // null terminate the string
00109 
00110             // Send data to Treasure data
00111             printf("\r\n Sending CPU Data: '%s'\r\n",cpu_buff);
00112             cpu->sendData(cpu_buff,strlen(cpu_buff));
00113         }
00114         {
00115             // Collect local data
00116             mbed_stats_heap_get(  &heapinfo);
00117 
00118             // Construct strings to send
00119             x=sprintf(heap_buff,"{\"current_size\":\"%d\",\"max_size\":\"%d\",\"total_size\":\"%d\",\"reserved_size\":\"%d\",\"alloc_cnt\":\"%d\",\"alloc_fail_cnt\":\"%d\"}",
00120                                 heapinfo.current_size,
00121                                 heapinfo.max_size,
00122                                 heapinfo.total_size,
00123                                 heapinfo.reserved_size,
00124                                 heapinfo.alloc_cnt,
00125                                 heapinfo.alloc_fail_cnt);
00126             heap_buff[x]=0; // null terminate the string
00127 
00128             // Send data to Treasure data
00129             printf("\r\n Sending Heap Data: '%s'\r\n",heap_buff);
00130             heap->sendData(heap_buff,strlen(heap_buff));
00131         }
00132         {
00133             // Collect local data
00134             mbed_stats_stack_get(  &stackinfo);
00135 
00136             // Construct strings to send
00137             x=sprintf(stack_buff,"{\"thread_id\":\"%d\",\"max_size\":\"%d\",\"reserved_size\":\"%d\",\"stack_cnt\":\"%d\"}",
00138                                 stackinfo.thread_id,
00139                                 stackinfo.max_size,
00140                                 stackinfo.reserved_size,
00141                                 stackinfo.stack_cnt);
00142             stack_buff[x]=0; // null terminate the string
00143 
00144             // Send data to Treasure data
00145             printf("\r\n Sending Stack Data: '%s'\r\n",stack_buff);
00146             stack->sendData(stack_buff,strlen(stack_buff));
00147         }
00148         {
00149             // Collect local data
00150             mbed_stats_sys_get(  &sysinfo);
00151 
00152             // Construct strings to send
00153             x=sprintf(sys_buff,"{\"os_version\":\"%d\",\"cpu_id\":\"%d\",\"compiler_id\":\"%d\",\"compiler_version\":\"%d\"}",
00154                                 sysinfo.os_version,
00155                                 sysinfo.cpu_id,
00156                                 sysinfo.compiler_id,
00157                                 sysinfo.compiler_version);
00158             sys_buff[x]=0; // null terminate the string
00159 
00160             // Send data to Treasure data
00161             printf("\r\n Sending System Data: '%s'\r\n",sys_buff);
00162             sys->sendData(sys_buff,strlen(sys_buff));
00163         }
00164         {
00165             sensor_value_temp = BSP_TSENSOR_ReadTemp();
00166             printf("\nTEMPERATURE = %.2f degC\n", sensor_value_temp);
00167      
00168             sensor_value_humid = BSP_HSENSOR_ReadHumidity();
00169             printf("HUMIDITY    = %.2f %%\n", sensor_value_humid);
00170      
00171             sensor_value_pressure = BSP_PSENSOR_ReadPressure();
00172             printf("PRESSURE is = %.2f mBar\n", sensor_value_pressure);
00173             
00174 //            BSP_MAGNETO_GetXYZ(pDataXYZ);
00175 //            printf("\nMAGNETO_X = %d\n", pDataXYZ[0]);
00176 //            printf("MAGNETO_Y = %d\n", pDataXYZ[1]);
00177 //            printf("MAGNETO_Z = %d\n", pDataXYZ[2]);
00178 //     
00179 //            BSP_GYRO_GetXYZ(pGyroDataXYZ);
00180 //            printf("\nGYRO_X = %.2f\n", pGyroDataXYZ[0]);
00181 //            printf("GYRO_Y = %.2f\n", pGyroDataXYZ[1]);
00182 //            printf("GYRO_Z = %.2f\n", pGyroDataXYZ[2]);
00183 //     
00184 //            BSP_ACCELERO_AccGetXYZ(pDataXYZ);
00185 //            printf("\nACCELERO_X = %d\n", pDataXYZ[0]);
00186 //            printf("ACCELERO_Y = %d\n", pDataXYZ[1]);
00187 //            printf("ACCELERO_Z = %d\n", pDataXYZ[2]);
00188             
00189             // Construct strings to send
00190             x=sprintf(sensor_buff,"{\"temperature\":\"%f\",\"humidity\":\"%f\",\"pressure\":\"%f\"}",
00191                                 sensor_value_temp,
00192                                 sensor_value_humid,
00193                                 sensor_value_pressure
00194                                 );
00195             sensor_buff[x]=0; // null terminate the string
00196      
00197             // Send data to Treasure data
00198             printf("\r\n Sending System Data: '%s'\r\n",sensor_buff);
00199             sensor->sendData(sensor_buff,strlen(sensor_buff));
00200             
00201         }
00202         wait(10);
00203 
00204     }
00205 
00206     net.disconnect();
00207 
00208     printf("\nDone, x=%d\n",x);
00209 
00210 
00211 }