mqtt_lora_thingsboard

Dependencies:   HTS221 VL53L0X BSP_B-L475E-IOT01 MQTT

Committer:
JunkoNakajima
Date:
Wed Feb 27 18:17:05 2019 +0900
Revision:
0:8d0ccf7acecd
Child:
1:5a9fdc848911
initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JunkoNakajima 0:8d0ccf7acecd 1 /* WiFi Example
JunkoNakajima 0:8d0ccf7acecd 2 * Copyright (c) 2016 ARM Limited
JunkoNakajima 0:8d0ccf7acecd 3 *
JunkoNakajima 0:8d0ccf7acecd 4 * Licensed under the Apache License, Version 2.0 (the "License");
JunkoNakajima 0:8d0ccf7acecd 5 * you may not use this file except in compliance with the License.
JunkoNakajima 0:8d0ccf7acecd 6 * You may obtain a copy of the License at
JunkoNakajima 0:8d0ccf7acecd 7 *
JunkoNakajima 0:8d0ccf7acecd 8 * http://www.apache.org/licenses/LICENSE-2.0
JunkoNakajima 0:8d0ccf7acecd 9 *
JunkoNakajima 0:8d0ccf7acecd 10 * Unless required by applicable law or agreed to in writing, software
JunkoNakajima 0:8d0ccf7acecd 11 * distributed under the License is distributed on an "AS IS" BASIS,
JunkoNakajima 0:8d0ccf7acecd 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
JunkoNakajima 0:8d0ccf7acecd 13 * See the License for the specific language governing permissions and
JunkoNakajima 0:8d0ccf7acecd 14 * limitations under the License.
JunkoNakajima 0:8d0ccf7acecd 15 */
JunkoNakajima 0:8d0ccf7acecd 16
JunkoNakajima 0:8d0ccf7acecd 17 #include "mbed.h"
JunkoNakajima 0:8d0ccf7acecd 18 #include "TCPSocket.h"
JunkoNakajima 0:8d0ccf7acecd 19 #include "wifi-ism43362/ISM43362Interface.h"
JunkoNakajima 0:8d0ccf7acecd 20 #include "treasure-data-rest.h"
JunkoNakajima 0:8d0ccf7acecd 21 // Sensors drivers present in the BSP library
JunkoNakajima 0:8d0ccf7acecd 22 #include "stm32l475e_iot01_tsensor.h"
JunkoNakajima 0:8d0ccf7acecd 23 #include "stm32l475e_iot01_hsensor.h"
JunkoNakajima 0:8d0ccf7acecd 24 #include "stm32l475e_iot01_psensor.h"
JunkoNakajima 0:8d0ccf7acecd 25 #include "stm32l475e_iot01_magneto.h"
JunkoNakajima 0:8d0ccf7acecd 26 #include "stm32l475e_iot01_gyro.h"
JunkoNakajima 0:8d0ccf7acecd 27 #include "stm32l475e_iot01_accelero.h"
JunkoNakajima 0:8d0ccf7acecd 28
JunkoNakajima 0:8d0ccf7acecd 29 #define BUFF_SIZE 200
JunkoNakajima 0:8d0ccf7acecd 30
JunkoNakajima 0:8d0ccf7acecd 31 ISM43362Interface net;
JunkoNakajima 0:8d0ccf7acecd 32 // WiFiInterface *wifi;
JunkoNakajima 0:8d0ccf7acecd 33
JunkoNakajima 0:8d0ccf7acecd 34 int main(void){
JunkoNakajima 0:8d0ccf7acecd 35
JunkoNakajima 0:8d0ccf7acecd 36 int count = 0;
JunkoNakajima 0:8d0ccf7acecd 37
JunkoNakajima 0:8d0ccf7acecd 38 printf("\r\nTreasure Data REST API Demo\n");
JunkoNakajima 0:8d0ccf7acecd 39
JunkoNakajima 0:8d0ccf7acecd 40 // Connect to Wifi
JunkoNakajima 0:8d0ccf7acecd 41 printf("\nConnecting to %s...\n", MBED_CONF_APP_WIFI_SSID);
JunkoNakajima 0:8d0ccf7acecd 42 int ret = net.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
JunkoNakajima 0:8d0ccf7acecd 43 if (ret != 0) {
JunkoNakajima 0:8d0ccf7acecd 44 printf("\nConnection error: %d\n", ret);
JunkoNakajima 0:8d0ccf7acecd 45 return -1;
JunkoNakajima 0:8d0ccf7acecd 46 }
JunkoNakajima 0:8d0ccf7acecd 47
JunkoNakajima 0:8d0ccf7acecd 48 printf("Success\n\n");
JunkoNakajima 0:8d0ccf7acecd 49 printf("MAC: %s\n", net.get_mac_address());
JunkoNakajima 0:8d0ccf7acecd 50 printf("IP: %s\n", net.get_ip_address());
JunkoNakajima 0:8d0ccf7acecd 51 printf("Netmask: %s\n", net.get_netmask());
JunkoNakajima 0:8d0ccf7acecd 52 printf("Gateway: %s\n", net.get_gateway());
JunkoNakajima 0:8d0ccf7acecd 53 printf("RSSI: %d\n\n", net.get_rssi());
JunkoNakajima 0:8d0ccf7acecd 54
JunkoNakajima 0:8d0ccf7acecd 55 // Create Treasure data objects (Network, Database, Table, APIKey)
JunkoNakajima 0:8d0ccf7acecd 56 TreasureData_RESTAPI* heap = new TreasureData_RESTAPI(&net,"iot_test","heap_info", MBED_CONF_APP_API_KEY);
JunkoNakajima 0:8d0ccf7acecd 57 TreasureData_RESTAPI* cpu = new TreasureData_RESTAPI(&net,"iot_test","cpu_info", MBED_CONF_APP_API_KEY);
JunkoNakajima 0:8d0ccf7acecd 58 TreasureData_RESTAPI* stack = new TreasureData_RESTAPI(&net,"iot_test","stack_info",MBED_CONF_APP_API_KEY);
JunkoNakajima 0:8d0ccf7acecd 59 TreasureData_RESTAPI* sys = new TreasureData_RESTAPI(&net,"iot_test","sys_info", MBED_CONF_APP_API_KEY);
JunkoNakajima 0:8d0ccf7acecd 60 TreasureData_RESTAPI* sensor = new TreasureData_RESTAPI(&net,"iot_test","sensor_info", MBED_CONF_APP_API_KEY);
JunkoNakajima 0:8d0ccf7acecd 61
JunkoNakajima 0:8d0ccf7acecd 62
JunkoNakajima 0:8d0ccf7acecd 63 // Device Information Objects
JunkoNakajima 0:8d0ccf7acecd 64 mbed_stats_cpu_t cpuinfo;
JunkoNakajima 0:8d0ccf7acecd 65 mbed_stats_heap_t heapinfo;
JunkoNakajima 0:8d0ccf7acecd 66 mbed_stats_stack_t stackinfo;
JunkoNakajima 0:8d0ccf7acecd 67 mbed_stats_sys_t sysinfo;
JunkoNakajima 0:8d0ccf7acecd 68
JunkoNakajima 0:8d0ccf7acecd 69 // Buffers to create strings in
JunkoNakajima 0:8d0ccf7acecd 70 char cpu_buff [BUFF_SIZE] = {0};
JunkoNakajima 0:8d0ccf7acecd 71 char heap_buff [BUFF_SIZE] = {0};
JunkoNakajima 0:8d0ccf7acecd 72 char stack_buff[BUFF_SIZE] = {0};
JunkoNakajima 0:8d0ccf7acecd 73 char sys_buff [BUFF_SIZE] = {0};
JunkoNakajima 0:8d0ccf7acecd 74 char sensor_buff [BUFF_SIZE] = {0};
JunkoNakajima 0:8d0ccf7acecd 75
JunkoNakajima 0:8d0ccf7acecd 76 // Initiate sensor variables
JunkoNakajima 0:8d0ccf7acecd 77 float sensor_value_temp = 0;
JunkoNakajima 0:8d0ccf7acecd 78 float sensor_value_humid = 0;
JunkoNakajima 0:8d0ccf7acecd 79 float sensor_value_pressure = 0;
JunkoNakajima 0:8d0ccf7acecd 80
JunkoNakajima 0:8d0ccf7acecd 81 int16_t pDataXYZ[3] = {0};
JunkoNakajima 0:8d0ccf7acecd 82 float pGyroDataXYZ[3] = {0};
JunkoNakajima 0:8d0ccf7acecd 83
JunkoNakajima 0:8d0ccf7acecd 84 BSP_TSENSOR_Init();
JunkoNakajima 0:8d0ccf7acecd 85 BSP_HSENSOR_Init();
JunkoNakajima 0:8d0ccf7acecd 86 BSP_PSENSOR_Init();
JunkoNakajima 0:8d0ccf7acecd 87
JunkoNakajima 0:8d0ccf7acecd 88 BSP_MAGNETO_Init();
JunkoNakajima 0:8d0ccf7acecd 89 BSP_GYRO_Init();
JunkoNakajima 0:8d0ccf7acecd 90 BSP_ACCELERO_Init();
JunkoNakajima 0:8d0ccf7acecd 91
JunkoNakajima 0:8d0ccf7acecd 92 int x = 0;
JunkoNakajima 0:8d0ccf7acecd 93
JunkoNakajima 0:8d0ccf7acecd 94 // Get device health data, send to Treasure Data every 10 seconds
JunkoNakajima 0:8d0ccf7acecd 95 while(1){
JunkoNakajima 0:8d0ccf7acecd 96 {
JunkoNakajima 0:8d0ccf7acecd 97
JunkoNakajima 0:8d0ccf7acecd 98
JunkoNakajima 0:8d0ccf7acecd 99 // Collect local data
JunkoNakajima 0:8d0ccf7acecd 100 mbed_stats_cpu_get( &cpuinfo);
JunkoNakajima 0:8d0ccf7acecd 101
JunkoNakajima 0:8d0ccf7acecd 102 // Construct strings to send
JunkoNakajima 0:8d0ccf7acecd 103 x = sprintf(cpu_buff,"{\"uptime\":\"%d\",\"idle_time\":\"%d\",\"sleep_time\":\"%d\",\"deep_sleep_time\":\"%d\"}",
JunkoNakajima 0:8d0ccf7acecd 104 cpuinfo.uptime,
JunkoNakajima 0:8d0ccf7acecd 105 cpuinfo.idle_time,
JunkoNakajima 0:8d0ccf7acecd 106 cpuinfo.sleep_time,
JunkoNakajima 0:8d0ccf7acecd 107 cpuinfo.deep_sleep_time);
JunkoNakajima 0:8d0ccf7acecd 108 cpu_buff[x]=0; // null terminate the string
JunkoNakajima 0:8d0ccf7acecd 109
JunkoNakajima 0:8d0ccf7acecd 110 // Send data to Treasure data
JunkoNakajima 0:8d0ccf7acecd 111 printf("\r\n Sending CPU Data: '%s'\r\n",cpu_buff);
JunkoNakajima 0:8d0ccf7acecd 112 cpu->sendData(cpu_buff,strlen(cpu_buff));
JunkoNakajima 0:8d0ccf7acecd 113 }
JunkoNakajima 0:8d0ccf7acecd 114 {
JunkoNakajima 0:8d0ccf7acecd 115 // Collect local data
JunkoNakajima 0:8d0ccf7acecd 116 mbed_stats_heap_get( &heapinfo);
JunkoNakajima 0:8d0ccf7acecd 117
JunkoNakajima 0:8d0ccf7acecd 118 // Construct strings to send
JunkoNakajima 0:8d0ccf7acecd 119 x=sprintf(heap_buff,"{\"current_size\":\"%d\",\"max_size\":\"%d\",\"total_size\":\"%d\",\"reserved_size\":\"%d\",\"alloc_cnt\":\"%d\",\"alloc_fail_cnt\":\"%d\"}",
JunkoNakajima 0:8d0ccf7acecd 120 heapinfo.current_size,
JunkoNakajima 0:8d0ccf7acecd 121 heapinfo.max_size,
JunkoNakajima 0:8d0ccf7acecd 122 heapinfo.total_size,
JunkoNakajima 0:8d0ccf7acecd 123 heapinfo.reserved_size,
JunkoNakajima 0:8d0ccf7acecd 124 heapinfo.alloc_cnt,
JunkoNakajima 0:8d0ccf7acecd 125 heapinfo.alloc_fail_cnt);
JunkoNakajima 0:8d0ccf7acecd 126 heap_buff[x]=0; // null terminate the string
JunkoNakajima 0:8d0ccf7acecd 127
JunkoNakajima 0:8d0ccf7acecd 128 // Send data to Treasure data
JunkoNakajima 0:8d0ccf7acecd 129 printf("\r\n Sending Heap Data: '%s'\r\n",heap_buff);
JunkoNakajima 0:8d0ccf7acecd 130 heap->sendData(heap_buff,strlen(heap_buff));
JunkoNakajima 0:8d0ccf7acecd 131 }
JunkoNakajima 0:8d0ccf7acecd 132 {
JunkoNakajima 0:8d0ccf7acecd 133 // Collect local data
JunkoNakajima 0:8d0ccf7acecd 134 mbed_stats_stack_get( &stackinfo);
JunkoNakajima 0:8d0ccf7acecd 135
JunkoNakajima 0:8d0ccf7acecd 136 // Construct strings to send
JunkoNakajima 0:8d0ccf7acecd 137 x=sprintf(stack_buff,"{\"thread_id\":\"%d\",\"max_size\":\"%d\",\"reserved_size\":\"%d\",\"stack_cnt\":\"%d\"}",
JunkoNakajima 0:8d0ccf7acecd 138 stackinfo.thread_id,
JunkoNakajima 0:8d0ccf7acecd 139 stackinfo.max_size,
JunkoNakajima 0:8d0ccf7acecd 140 stackinfo.reserved_size,
JunkoNakajima 0:8d0ccf7acecd 141 stackinfo.stack_cnt);
JunkoNakajima 0:8d0ccf7acecd 142 stack_buff[x]=0; // null terminate the string
JunkoNakajima 0:8d0ccf7acecd 143
JunkoNakajima 0:8d0ccf7acecd 144 // Send data to Treasure data
JunkoNakajima 0:8d0ccf7acecd 145 printf("\r\n Sending Stack Data: '%s'\r\n",stack_buff);
JunkoNakajima 0:8d0ccf7acecd 146 stack->sendData(stack_buff,strlen(stack_buff));
JunkoNakajima 0:8d0ccf7acecd 147 }
JunkoNakajima 0:8d0ccf7acecd 148 {
JunkoNakajima 0:8d0ccf7acecd 149 // Collect local data
JunkoNakajima 0:8d0ccf7acecd 150 mbed_stats_sys_get( &sysinfo);
JunkoNakajima 0:8d0ccf7acecd 151
JunkoNakajima 0:8d0ccf7acecd 152 // Construct strings to send
JunkoNakajima 0:8d0ccf7acecd 153 x=sprintf(sys_buff,"{\"os_version\":\"%d\",\"cpu_id\":\"%d\",\"compiler_id\":\"%d\",\"compiler_version\":\"%d\"}",
JunkoNakajima 0:8d0ccf7acecd 154 sysinfo.os_version,
JunkoNakajima 0:8d0ccf7acecd 155 sysinfo.cpu_id,
JunkoNakajima 0:8d0ccf7acecd 156 sysinfo.compiler_id,
JunkoNakajima 0:8d0ccf7acecd 157 sysinfo.compiler_version);
JunkoNakajima 0:8d0ccf7acecd 158 sys_buff[x]=0; // null terminate the string
JunkoNakajima 0:8d0ccf7acecd 159
JunkoNakajima 0:8d0ccf7acecd 160 // Send data to Treasure data
JunkoNakajima 0:8d0ccf7acecd 161 printf("\r\n Sending System Data: '%s'\r\n",sys_buff);
JunkoNakajima 0:8d0ccf7acecd 162 sys->sendData(sys_buff,strlen(sys_buff));
JunkoNakajima 0:8d0ccf7acecd 163 }
JunkoNakajima 0:8d0ccf7acecd 164 {
JunkoNakajima 0:8d0ccf7acecd 165 sensor_value_temp = BSP_TSENSOR_ReadTemp();
JunkoNakajima 0:8d0ccf7acecd 166 printf("\nTEMPERATURE = %.2f degC\n", sensor_value_temp);
JunkoNakajima 0:8d0ccf7acecd 167
JunkoNakajima 0:8d0ccf7acecd 168 sensor_value_humid = BSP_HSENSOR_ReadHumidity();
JunkoNakajima 0:8d0ccf7acecd 169 printf("HUMIDITY = %.2f %%\n", sensor_value_humid);
JunkoNakajima 0:8d0ccf7acecd 170
JunkoNakajima 0:8d0ccf7acecd 171 sensor_value_pressure = BSP_PSENSOR_ReadPressure();
JunkoNakajima 0:8d0ccf7acecd 172 printf("PRESSURE is = %.2f mBar\n", sensor_value_pressure);
JunkoNakajima 0:8d0ccf7acecd 173
JunkoNakajima 0:8d0ccf7acecd 174 // BSP_MAGNETO_GetXYZ(pDataXYZ);
JunkoNakajima 0:8d0ccf7acecd 175 // printf("\nMAGNETO_X = %d\n", pDataXYZ[0]);
JunkoNakajima 0:8d0ccf7acecd 176 // printf("MAGNETO_Y = %d\n", pDataXYZ[1]);
JunkoNakajima 0:8d0ccf7acecd 177 // printf("MAGNETO_Z = %d\n", pDataXYZ[2]);
JunkoNakajima 0:8d0ccf7acecd 178 //
JunkoNakajima 0:8d0ccf7acecd 179 // BSP_GYRO_GetXYZ(pGyroDataXYZ);
JunkoNakajima 0:8d0ccf7acecd 180 // printf("\nGYRO_X = %.2f\n", pGyroDataXYZ[0]);
JunkoNakajima 0:8d0ccf7acecd 181 // printf("GYRO_Y = %.2f\n", pGyroDataXYZ[1]);
JunkoNakajima 0:8d0ccf7acecd 182 // printf("GYRO_Z = %.2f\n", pGyroDataXYZ[2]);
JunkoNakajima 0:8d0ccf7acecd 183 //
JunkoNakajima 0:8d0ccf7acecd 184 // BSP_ACCELERO_AccGetXYZ(pDataXYZ);
JunkoNakajima 0:8d0ccf7acecd 185 // printf("\nACCELERO_X = %d\n", pDataXYZ[0]);
JunkoNakajima 0:8d0ccf7acecd 186 // printf("ACCELERO_Y = %d\n", pDataXYZ[1]);
JunkoNakajima 0:8d0ccf7acecd 187 // printf("ACCELERO_Z = %d\n", pDataXYZ[2]);
JunkoNakajima 0:8d0ccf7acecd 188
JunkoNakajima 0:8d0ccf7acecd 189 // Construct strings to send
JunkoNakajima 0:8d0ccf7acecd 190 x=sprintf(sensor_buff,"{\"temperature\":\"%f\",\"humidity\":\"%f\",\"pressure\":\"%f\"}",
JunkoNakajima 0:8d0ccf7acecd 191 sensor_value_temp,
JunkoNakajima 0:8d0ccf7acecd 192 sensor_value_humid,
JunkoNakajima 0:8d0ccf7acecd 193 sensor_value_pressure
JunkoNakajima 0:8d0ccf7acecd 194 );
JunkoNakajima 0:8d0ccf7acecd 195 sensor_buff[x]=0; // null terminate the string
JunkoNakajima 0:8d0ccf7acecd 196
JunkoNakajima 0:8d0ccf7acecd 197 // Send data to Treasure data
JunkoNakajima 0:8d0ccf7acecd 198 printf("\r\n Sending System Data: '%s'\r\n",sensor_buff);
JunkoNakajima 0:8d0ccf7acecd 199 sensor->sendData(sensor_buff,strlen(sensor_buff));
JunkoNakajima 0:8d0ccf7acecd 200
JunkoNakajima 0:8d0ccf7acecd 201 }
JunkoNakajima 0:8d0ccf7acecd 202 wait(10);
JunkoNakajima 0:8d0ccf7acecd 203
JunkoNakajima 0:8d0ccf7acecd 204 }
JunkoNakajima 0:8d0ccf7acecd 205
JunkoNakajima 0:8d0ccf7acecd 206 net.disconnect();
JunkoNakajima 0:8d0ccf7acecd 207
JunkoNakajima 0:8d0ccf7acecd 208 printf("\nDone, x=%d\n",x);
JunkoNakajima 0:8d0ccf7acecd 209
JunkoNakajima 0:8d0ccf7acecd 210
JunkoNakajima 0:8d0ccf7acecd 211 }