initial release
Dependencies: BSP_B-L475E-IOT01 MQTT
Fork of Multiprotocol by
main.cpp@5:69b70c8fc368, 2018-08-22 (annotated)
- Committer:
- fabio_gatti
- Date:
- Wed Aug 22 21:43:31 2018 +0000
- Revision:
- 5:69b70c8fc368
- Parent:
- 4:591b0f33f8b0
initial release
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
fabio_gatti | 0:705a0a684de2 | 1 | // main.cpp |
fabio_gatti | 3:647e37ef0eec | 2 | #include <string> |
fabio_gatti | 0:705a0a684de2 | 3 | |
fabio_gatti | 0:705a0a684de2 | 4 | #include "mbed.h" |
fabio_gatti | 0:705a0a684de2 | 5 | #include "ISM43362Interface.h" |
fabio_gatti | 0:705a0a684de2 | 6 | #include "TCPSocket.h" |
fabio_gatti | 5:69b70c8fc368 | 7 | //#include "HTS221Sensor.h" |
fabio_gatti | 3:647e37ef0eec | 8 | |
fabio_gatti | 3:647e37ef0eec | 9 | #include "thingsboard_account.h" |
fabio_gatti | 5:69b70c8fc368 | 10 | // Sensors drivers present in the BSP library |
fabio_gatti | 5:69b70c8fc368 | 11 | #include "stm32l475e_iot01_tsensor.h" |
fabio_gatti | 5:69b70c8fc368 | 12 | #include "stm32l475e_iot01_hsensor.h" |
fabio_gatti | 5:69b70c8fc368 | 13 | #include "stm32l475e_iot01_psensor.h" |
fabio_gatti | 5:69b70c8fc368 | 14 | #include "stm32l475e_iot01_magneto.h" |
fabio_gatti | 5:69b70c8fc368 | 15 | #include "stm32l475e_iot01_gyro.h" |
fabio_gatti | 5:69b70c8fc368 | 16 | #include "stm32l475e_iot01_accelero.h" |
fabio_gatti | 3:647e37ef0eec | 17 | |
fabio_gatti | 5:69b70c8fc368 | 18 | // laser driver |
fabio_gatti | 5:69b70c8fc368 | 19 | //#include "lis3mdl_class.h" |
fabio_gatti | 5:69b70c8fc368 | 20 | #include "VL53L0X.h" |
fabio_gatti | 3:647e37ef0eec | 21 | |
fabio_gatti | 3:647e37ef0eec | 22 | // H file per MQTT |
fabio_gatti | 0:705a0a684de2 | 23 | #include "MQTTmbed.h" |
fabio_gatti | 0:705a0a684de2 | 24 | #include "MQTTClient.h" |
fabio_gatti | 3:647e37ef0eec | 25 | |
fabio_gatti | 3:647e37ef0eec | 26 | // H file per COAP |
fabio_gatti | 3:647e37ef0eec | 27 | #include "sn_nsdl.h" |
fabio_gatti | 3:647e37ef0eec | 28 | #include "sn_coap_protocol.h" |
fabio_gatti | 3:647e37ef0eec | 29 | #include "sn_coap_header.h" |
fabio_gatti | 0:705a0a684de2 | 30 | |
fabio_gatti | 0:705a0a684de2 | 31 | // Definitions --------------------------------------------------------- |
fabio_gatti | 0:705a0a684de2 | 32 | |
fabio_gatti | 0:705a0a684de2 | 33 | // Change it with your WiFi network name |
fabio_gatti | 2:983b7f5dde1e | 34 | //#define WIFI_NETWORK_NAME "farnell_iot_lab" |
fabio_gatti | 5:69b70c8fc368 | 35 | #define WIFI_NETWORK_NAME "rucola" |
fabio_gatti | 0:705a0a684de2 | 36 | // Change it with your WiFi password name |
fabio_gatti | 2:983b7f5dde1e | 37 | //#define WIFI_NETWORK_PASSWORD "smartlab" |
fabio_gatti | 5:69b70c8fc368 | 38 | #define WIFI_NETWORK_PASSWORD "Rosmarino_10" |
fabio_gatti | 3:647e37ef0eec | 39 | |
fabio_gatti | 5:69b70c8fc368 | 40 | #define WIFI_SECURITY NSAPI_SECURITY_WPA_WPA2 |
fabio_gatti | 0:705a0a684de2 | 41 | |
fabio_gatti | 5:69b70c8fc368 | 42 | #define COMM_PROTO 2 |
fabio_gatti | 2:983b7f5dde1e | 43 | |
fabio_gatti | 2:983b7f5dde1e | 44 | // scegliere il protocollo di trasporto dati |
fabio_gatti | 2:983b7f5dde1e | 45 | // COMM_PROTO = 0 -> accesso MQTT (default); |
fabio_gatti | 2:983b7f5dde1e | 46 | // COMM_PROTO = 1 -> access HTTP; |
fabio_gatti | 2:983b7f5dde1e | 47 | // COMM_PROTO = 2 -> accesso COAP; |
fabio_gatti | 2:983b7f5dde1e | 48 | #ifndef COMM_PROTO |
fabio_gatti | 2:983b7f5dde1e | 49 | #define COMM_PROTO 0 |
fabio_gatti | 2:983b7f5dde1e | 50 | #endif |
fabio_gatti | 2:983b7f5dde1e | 51 | |
fabio_gatti | 3:647e37ef0eec | 52 | // scegliere il token al device |
fabio_gatti | 4:591b0f33f8b0 | 53 | #define DEVICE_ID 1 |
fabio_gatti | 5:69b70c8fc368 | 54 | #define DEVICE_ACCESS_TOKEN SMART_BIN_1_DEVICE_ACCESS_TOKEN |
fabio_gatti | 5:69b70c8fc368 | 55 | |
fabio_gatti | 5:69b70c8fc368 | 56 | //parametri rilevamento laser |
fabio_gatti | 5:69b70c8fc368 | 57 | #define MIN_DISTANCE_OBSTACLE 80 //distanza di rilevamento in mm |
fabio_gatti | 5:69b70c8fc368 | 58 | #define NUMBER_OF_OBSTACLE_FULL 3 //numero di letture per dichiarare il cestino pieno |
fabio_gatti | 5:69b70c8fc368 | 59 | #define TILT_THRESHOLD 700 //valore di soglia per ribaltamento pari a sin(angolo)*1000 |
fabio_gatti | 3:647e37ef0eec | 60 | |
fabio_gatti | 3:647e37ef0eec | 61 | // ciclo di frequenza lettura |
fabio_gatti | 3:647e37ef0eec | 62 | #define SENSOR_READING_PERIOD 5000 //in ms |
fabio_gatti | 3:647e37ef0eec | 63 | |
fabio_gatti | 3:647e37ef0eec | 64 | #if COMM_PROTO==0 //MQTT protocol |
fabio_gatti | 3:647e37ef0eec | 65 | |
fabio_gatti | 3:647e37ef0eec | 66 | #define MQTT_HOST "demo.thingsboard.io" |
fabio_gatti | 3:647e37ef0eec | 67 | #define MQTT_PORT 1883 |
fabio_gatti | 3:647e37ef0eec | 68 | #define MQTT_TOPIC "v1/devices/me/telemetry" |
fabio_gatti | 5:69b70c8fc368 | 69 | #define MQTT_MAX_MSG_LENGHT 500 //max mqtt lenght in byte |
fabio_gatti | 2:983b7f5dde1e | 70 | |
fabio_gatti | 3:647e37ef0eec | 71 | #elif COMM_PROTO==1 //HTTP protocol |
fabio_gatti | 3:647e37ef0eec | 72 | #define HTTP_HOST "demo.thingsboard.io" |
fabio_gatti | 3:647e37ef0eec | 73 | #define HTTP_PORT 80 |
fabio_gatti | 3:647e37ef0eec | 74 | #define HTTP_STR_1 "/api/v1/" |
fabio_gatti | 3:647e37ef0eec | 75 | #define HTTP_STR_2 "/telemetry" |
fabio_gatti | 1:ddef44f2db39 | 76 | |
fabio_gatti | 3:647e37ef0eec | 77 | #elif COMM_PROTO==2 //COAP protocol |
fabio_gatti | 3:647e37ef0eec | 78 | #define COAP_HOST "demo.thingsboard.io" |
fabio_gatti | 3:647e37ef0eec | 79 | #define COAP_PORT 5683 |
fabio_gatti | 3:647e37ef0eec | 80 | #define COAP_STR_1 "/api/v1/" |
fabio_gatti | 3:647e37ef0eec | 81 | #define COAP_STR_2 "/telemetry" |
fabio_gatti | 3:647e37ef0eec | 82 | |
fabio_gatti | 3:647e37ef0eec | 83 | #define SHOW_COAP_RESPONSE false |
fabio_gatti | 2:983b7f5dde1e | 84 | |
fabio_gatti | 3:647e37ef0eec | 85 | #endif //end protocol |
fabio_gatti | 2:983b7f5dde1e | 86 | |
fabio_gatti | 3:647e37ef0eec | 87 | // strutture comuni |
fabio_gatti | 3:647e37ef0eec | 88 | |
fabio_gatti | 3:647e37ef0eec | 89 | #if COMM_PROTO == 0 |
fabio_gatti | 0:705a0a684de2 | 90 | class MQTTNetwork |
fabio_gatti | 0:705a0a684de2 | 91 | { |
fabio_gatti | 0:705a0a684de2 | 92 | public: |
fabio_gatti | 0:705a0a684de2 | 93 | MQTTNetwork(NetworkInterface* aNetwork) : network(aNetwork) { |
fabio_gatti | 0:705a0a684de2 | 94 | socket = new TCPSocket(); |
fabio_gatti | 0:705a0a684de2 | 95 | } |
fabio_gatti | 0:705a0a684de2 | 96 | |
fabio_gatti | 0:705a0a684de2 | 97 | ~MQTTNetwork() { |
fabio_gatti | 0:705a0a684de2 | 98 | delete socket; |
fabio_gatti | 0:705a0a684de2 | 99 | } |
fabio_gatti | 0:705a0a684de2 | 100 | |
fabio_gatti | 0:705a0a684de2 | 101 | int read(unsigned char* buffer, int len, int timeout) { |
fabio_gatti | 0:705a0a684de2 | 102 | return socket->recv(buffer, len); |
fabio_gatti | 0:705a0a684de2 | 103 | } |
fabio_gatti | 0:705a0a684de2 | 104 | |
fabio_gatti | 0:705a0a684de2 | 105 | int write(unsigned char* buffer, int len, int timeout) { |
fabio_gatti | 0:705a0a684de2 | 106 | return socket->send(buffer, len); |
fabio_gatti | 0:705a0a684de2 | 107 | } |
fabio_gatti | 0:705a0a684de2 | 108 | |
fabio_gatti | 0:705a0a684de2 | 109 | int connect(const char* hostname, int port) { |
fabio_gatti | 0:705a0a684de2 | 110 | socket->open(network); |
fabio_gatti | 0:705a0a684de2 | 111 | return socket->connect(hostname, port); |
fabio_gatti | 0:705a0a684de2 | 112 | } |
fabio_gatti | 0:705a0a684de2 | 113 | |
fabio_gatti | 0:705a0a684de2 | 114 | int disconnect() { |
fabio_gatti | 0:705a0a684de2 | 115 | return socket->close(); |
fabio_gatti | 0:705a0a684de2 | 116 | } |
fabio_gatti | 0:705a0a684de2 | 117 | |
fabio_gatti | 0:705a0a684de2 | 118 | private: |
fabio_gatti | 0:705a0a684de2 | 119 | NetworkInterface* network; |
fabio_gatti | 0:705a0a684de2 | 120 | TCPSocket* socket; |
fabio_gatti | 0:705a0a684de2 | 121 | }; |
fabio_gatti | 3:647e37ef0eec | 122 | |
fabio_gatti | 3:647e37ef0eec | 123 | #elif COMM_PROTO==2 //COAP protocol |
fabio_gatti | 3:647e37ef0eec | 124 | UDPSocket socket; // Socket to talk CoAP over |
fabio_gatti | 3:647e37ef0eec | 125 | Thread recvfromThread; // Thread to receive messages over CoAP |
fabio_gatti | 3:647e37ef0eec | 126 | struct coap_s* coapHandle; |
fabio_gatti | 3:647e37ef0eec | 127 | coap_version_e coapVersion = COAP_VERSION_1; |
fabio_gatti | 3:647e37ef0eec | 128 | |
fabio_gatti | 3:647e37ef0eec | 129 | // CoAP HAL |
fabio_gatti | 3:647e37ef0eec | 130 | void* coap_malloc(uint16_t size) { |
fabio_gatti | 3:647e37ef0eec | 131 | return malloc(size); |
fabio_gatti | 3:647e37ef0eec | 132 | } |
fabio_gatti | 3:647e37ef0eec | 133 | |
fabio_gatti | 3:647e37ef0eec | 134 | void coap_free(void* addr) { |
fabio_gatti | 3:647e37ef0eec | 135 | free(addr); |
fabio_gatti | 3:647e37ef0eec | 136 | } |
fabio_gatti | 3:647e37ef0eec | 137 | |
fabio_gatti | 3:647e37ef0eec | 138 | // tx_cb and rx_cb are not used in this program |
fabio_gatti | 3:647e37ef0eec | 139 | uint8_t coap_tx_cb(uint8_t *a, uint16_t b, sn_nsdl_addr_s *c, void *d) { |
fabio_gatti | 3:647e37ef0eec | 140 | printf("coap tx cb\n"); |
fabio_gatti | 3:647e37ef0eec | 141 | return 0; |
fabio_gatti | 3:647e37ef0eec | 142 | } |
fabio_gatti | 3:647e37ef0eec | 143 | |
fabio_gatti | 3:647e37ef0eec | 144 | int8_t coap_rx_cb(sn_coap_hdr_s *a, sn_nsdl_addr_s *b, void *c) { |
fabio_gatti | 3:647e37ef0eec | 145 | printf("coap rx cb\n"); |
fabio_gatti | 3:647e37ef0eec | 146 | return 0; |
fabio_gatti | 3:647e37ef0eec | 147 | } |
fabio_gatti | 3:647e37ef0eec | 148 | |
fabio_gatti | 3:647e37ef0eec | 149 | |
fabio_gatti | 2:983b7f5dde1e | 150 | #endif |
fabio_gatti | 0:705a0a684de2 | 151 | |
fabio_gatti | 5:69b70c8fc368 | 152 | #ifdef TARGET_DISCO_L475VG_IOT01A |
fabio_gatti | 5:69b70c8fc368 | 153 | |
fabio_gatti | 5:69b70c8fc368 | 154 | static DevI2C devI2c(PB_11,PB_10); |
fabio_gatti | 5:69b70c8fc368 | 155 | static DigitalOut shutdown_pin(PC_6); |
fabio_gatti | 5:69b70c8fc368 | 156 | static VL53L0X range(&devI2c, &shutdown_pin, PC_7); |
fabio_gatti | 5:69b70c8fc368 | 157 | |
fabio_gatti | 5:69b70c8fc368 | 158 | #endif |
fabio_gatti | 5:69b70c8fc368 | 159 | |
fabio_gatti | 5:69b70c8fc368 | 160 | DigitalOut led(LED1); |
fabio_gatti | 3:647e37ef0eec | 161 | Serial pc(USBTX, USBRX); //use these pins for serial coms. |
fabio_gatti | 0:705a0a684de2 | 162 | int main() |
fabio_gatti | 0:705a0a684de2 | 163 | { |
fabio_gatti | 5:69b70c8fc368 | 164 | |
fabio_gatti | 5:69b70c8fc368 | 165 | bool blnTilted = false; // variabile per l'assetto del cestino |
fabio_gatti | 5:69b70c8fc368 | 166 | bool blnIsFull=false; // variabile per lo stato (pieno/vuoto) del cestino |
fabio_gatti | 5:69b70c8fc368 | 167 | int LaserCountObstacle=0; //contatore di ostacoli rilevati dal laser |
fabio_gatti | 5:69b70c8fc368 | 168 | |
fabio_gatti | 0:705a0a684de2 | 169 | int count = 0; |
fabio_gatti | 3:647e37ef0eec | 170 | pc.baud(115200); |
fabio_gatti | 5:69b70c8fc368 | 171 | |
fabio_gatti | 5:69b70c8fc368 | 172 | |
fabio_gatti | 5:69b70c8fc368 | 173 | //initing sensori |
fabio_gatti | 5:69b70c8fc368 | 174 | float sensor_pressure_value = 0; |
fabio_gatti | 5:69b70c8fc368 | 175 | float sensor_temperature_value = 0; |
fabio_gatti | 5:69b70c8fc368 | 176 | float sensor_humidity_value = 0; |
fabio_gatti | 5:69b70c8fc368 | 177 | |
fabio_gatti | 5:69b70c8fc368 | 178 | int16_t pDataXYZ[3] = {0}; |
fabio_gatti | 5:69b70c8fc368 | 179 | //float pGyroDataXYZ[3] = {0}; |
fabio_gatti | 5:69b70c8fc368 | 180 | |
fabio_gatti | 5:69b70c8fc368 | 181 | range.init_sensor(VL53L0X_DEFAULT_ADDRESS); |
fabio_gatti | 5:69b70c8fc368 | 182 | |
fabio_gatti | 5:69b70c8fc368 | 183 | BSP_TSENSOR_Init(); |
fabio_gatti | 5:69b70c8fc368 | 184 | BSP_HSENSOR_Init(); |
fabio_gatti | 5:69b70c8fc368 | 185 | BSP_PSENSOR_Init(); |
fabio_gatti | 5:69b70c8fc368 | 186 | |
fabio_gatti | 5:69b70c8fc368 | 187 | //BSP_MAGNETO_Init(); |
fabio_gatti | 5:69b70c8fc368 | 188 | //BSP_GYRO_Init(); |
fabio_gatti | 5:69b70c8fc368 | 189 | BSP_ACCELERO_Init(); |
fabio_gatti | 5:69b70c8fc368 | 190 | |
fabio_gatti | 5:69b70c8fc368 | 191 | //initing sensori |
fabio_gatti | 5:69b70c8fc368 | 192 | |
fabio_gatti | 3:647e37ef0eec | 193 | |
fabio_gatti | 3:647e37ef0eec | 194 | printf(" --- START SESSION ---\n"); |
fabio_gatti | 0:705a0a684de2 | 195 | ISM43362Interface wifi(MBED_CONF_APP_WIFI_SPI_MOSI, |
fabio_gatti | 0:705a0a684de2 | 196 | MBED_CONF_APP_WIFI_SPI_MISO, |
fabio_gatti | 0:705a0a684de2 | 197 | MBED_CONF_APP_WIFI_SPI_SCLK, |
fabio_gatti | 0:705a0a684de2 | 198 | MBED_CONF_APP_WIFI_SPI_NSS, |
fabio_gatti | 0:705a0a684de2 | 199 | MBED_CONF_APP_WIFI_RESET, |
fabio_gatti | 0:705a0a684de2 | 200 | MBED_CONF_APP_WIFI_DATAREADY, |
fabio_gatti | 0:705a0a684de2 | 201 | MBED_CONF_APP_WIFI_WAKEUP, false); |
fabio_gatti | 0:705a0a684de2 | 202 | |
fabio_gatti | 0:705a0a684de2 | 203 | // Scanning WiFi networks ------------------------------------------ |
fabio_gatti | 0:705a0a684de2 | 204 | |
fabio_gatti | 0:705a0a684de2 | 205 | WiFiAccessPoint *ap; |
fabio_gatti | 0:705a0a684de2 | 206 | |
fabio_gatti | 0:705a0a684de2 | 207 | count = wifi.scan(NULL, 0); |
fabio_gatti | 0:705a0a684de2 | 208 | printf("%d networks available.\n", count); |
fabio_gatti | 0:705a0a684de2 | 209 | |
fabio_gatti | 0:705a0a684de2 | 210 | /* Limit number of network arbitrary to 15 */ |
fabio_gatti | 0:705a0a684de2 | 211 | count = count < 15 ? count : 15; |
fabio_gatti | 0:705a0a684de2 | 212 | |
fabio_gatti | 0:705a0a684de2 | 213 | ap = new WiFiAccessPoint[count]; |
fabio_gatti | 0:705a0a684de2 | 214 | count = wifi.scan(ap, count); |
fabio_gatti | 0:705a0a684de2 | 215 | for (int i = 0; i < count; i++) { |
fabio_gatti | 0:705a0a684de2 | 216 | printf("Network: %s RSSI: %hhd\n", ap[i].get_ssid(), ap[i].get_rssi()); |
fabio_gatti | 0:705a0a684de2 | 217 | } |
fabio_gatti | 0:705a0a684de2 | 218 | |
fabio_gatti | 0:705a0a684de2 | 219 | delete[] ap; |
fabio_gatti | 0:705a0a684de2 | 220 | |
fabio_gatti | 0:705a0a684de2 | 221 | // Connecting to WiFi network -------------------------------------- |
fabio_gatti | 0:705a0a684de2 | 222 | |
fabio_gatti | 0:705a0a684de2 | 223 | printf("\nConnecting to %s...\n", WIFI_NETWORK_NAME); |
fabio_gatti | 0:705a0a684de2 | 224 | int ret = wifi.connect(WIFI_NETWORK_NAME, WIFI_NETWORK_PASSWORD, WIFI_SECURITY); |
fabio_gatti | 0:705a0a684de2 | 225 | if (ret != 0) { |
fabio_gatti | 0:705a0a684de2 | 226 | printf("\nConnection error\n"); |
fabio_gatti | 0:705a0a684de2 | 227 | return -1; |
fabio_gatti | 0:705a0a684de2 | 228 | } |
fabio_gatti | 0:705a0a684de2 | 229 | |
fabio_gatti | 0:705a0a684de2 | 230 | printf("Success\n\n"); |
fabio_gatti | 0:705a0a684de2 | 231 | printf("MAC: %s\n", wifi.get_mac_address()); |
fabio_gatti | 0:705a0a684de2 | 232 | printf("IP: %s\n", wifi.get_ip_address()); |
fabio_gatti | 0:705a0a684de2 | 233 | printf("Netmask: %s\n", wifi.get_netmask()); |
fabio_gatti | 0:705a0a684de2 | 234 | printf("Gateway: %s\n", wifi.get_gateway()); |
fabio_gatti | 0:705a0a684de2 | 235 | printf("RSSI: %d\n\n", wifi.get_rssi()); |
fabio_gatti | 0:705a0a684de2 | 236 | |
fabio_gatti | 3:647e37ef0eec | 237 | #if COMM_PROTO == 0 //MQTT |
fabio_gatti | 2:983b7f5dde1e | 238 | printf("Collegamento MQTT server: " MQTT_HOST "\n"); |
fabio_gatti | 0:705a0a684de2 | 239 | |
fabio_gatti | 2:983b7f5dde1e | 240 | MQTTNetwork network(&wifi); |
fabio_gatti | 5:69b70c8fc368 | 241 | MQTT::Client<MQTTNetwork, Countdown,MQTT_MAX_MSG_LENGHT> client(network); // attenzione alla MAX DIM MSG MQTT |
fabio_gatti | 0:705a0a684de2 | 242 | |
fabio_gatti | 2:983b7f5dde1e | 243 | char assess_token[] = DEVICE_ACCESS_TOKEN; |
fabio_gatti | 2:983b7f5dde1e | 244 | |
fabio_gatti | 2:983b7f5dde1e | 245 | MQTTPacket_connectData conn_data = MQTTPacket_connectData_initializer; |
fabio_gatti | 2:983b7f5dde1e | 246 | conn_data.username.cstring = assess_token; |
fabio_gatti | 0:705a0a684de2 | 247 | |
fabio_gatti | 2:983b7f5dde1e | 248 | if (network.connect(MQTT_HOST, MQTT_PORT) < 0) { |
fabio_gatti | 2:983b7f5dde1e | 249 | printf("failed to connect to " MQTT_HOST "\n"); |
fabio_gatti | 2:983b7f5dde1e | 250 | return -1; |
fabio_gatti | 2:983b7f5dde1e | 251 | } |
fabio_gatti | 0:705a0a684de2 | 252 | |
fabio_gatti | 2:983b7f5dde1e | 253 | if (client.connect(conn_data) < 0) { |
fabio_gatti | 2:983b7f5dde1e | 254 | printf("failed to send MQTT connect message\n"); |
fabio_gatti | 2:983b7f5dde1e | 255 | return -1; |
fabio_gatti | 2:983b7f5dde1e | 256 | } |
fabio_gatti | 0:705a0a684de2 | 257 | |
fabio_gatti | 2:983b7f5dde1e | 258 | printf("successfully connect to MQTT server!\n"); |
fabio_gatti | 3:647e37ef0eec | 259 | |
fabio_gatti | 3:647e37ef0eec | 260 | #elif COMM_PROTO == 1 //HTTP |
fabio_gatti | 3:647e37ef0eec | 261 | |
fabio_gatti | 3:647e37ef0eec | 262 | printf("Collegamento HTTP server: " HTTP_HOST "\n"); |
fabio_gatti | 3:647e37ef0eec | 263 | TCPSocket socket; |
fabio_gatti | 3:647e37ef0eec | 264 | nsapi_error_t response; |
fabio_gatti | 3:647e37ef0eec | 265 | |
fabio_gatti | 3:647e37ef0eec | 266 | // Open a socket on the network interface, and create a TCP connection |
fabio_gatti | 3:647e37ef0eec | 267 | socket.open(&wifi); |
fabio_gatti | 3:647e37ef0eec | 268 | response = socket.connect(HTTP_HOST, HTTP_PORT); |
fabio_gatti | 3:647e37ef0eec | 269 | if(0 != response) { |
fabio_gatti | 3:647e37ef0eec | 270 | printf("Error connecting: %d\n", response); |
fabio_gatti | 3:647e37ef0eec | 271 | socket.close(); |
fabio_gatti | 3:647e37ef0eec | 272 | return -1; |
fabio_gatti | 3:647e37ef0eec | 273 | } |
fabio_gatti | 3:647e37ef0eec | 274 | socket.close(); |
fabio_gatti | 3:647e37ef0eec | 275 | #elif COMM_PROTO == 2 // COAP |
fabio_gatti | 3:647e37ef0eec | 276 | |
fabio_gatti | 3:647e37ef0eec | 277 | //inserire un test di invio dati al server coap |
fabio_gatti | 3:647e37ef0eec | 278 | |
fabio_gatti | 3:647e37ef0eec | 279 | |
fabio_gatti | 2:983b7f5dde1e | 280 | #endif |
fabio_gatti | 0:705a0a684de2 | 281 | |
fabio_gatti | 0:705a0a684de2 | 282 | // Initialize sensors -------------------------------------------------- |
fabio_gatti | 5:69b70c8fc368 | 283 | /* |
fabio_gatti | 3:647e37ef0eec | 284 | DevI2C i2c_2(PB_11, PB_10); |
fabio_gatti | 3:647e37ef0eec | 285 | HTS221Sensor hum_temp(&i2c_2); |
fabio_gatti | 3:647e37ef0eec | 286 | |
fabio_gatti | 3:647e37ef0eec | 287 | hum_temp.init(NULL); |
fabio_gatti | 3:647e37ef0eec | 288 | hum_temp.enable(); |
fabio_gatti | 3:647e37ef0eec | 289 | hum_temp.read_id(&id); |
fabio_gatti | 3:647e37ef0eec | 290 | printf("HTS221 humidity & temperature sensor = 0x%X\r\n", id); |
fabio_gatti | 5:69b70c8fc368 | 291 | */ |
fabio_gatti | 3:647e37ef0eec | 292 | // Variabili di appoggio ----------------------------------------------- |
fabio_gatti | 3:647e37ef0eec | 293 | #if COMM_PROTO == 1 |
fabio_gatti | 3:647e37ef0eec | 294 | uint8_t http_request[1024]; |
fabio_gatti | 3:647e37ef0eec | 295 | char request_body[256]; |
fabio_gatti | 3:647e37ef0eec | 296 | static uint8_t http_resp[512]; |
fabio_gatti | 3:647e37ef0eec | 297 | uint16_t reqLen; |
fabio_gatti | 3:647e37ef0eec | 298 | uint16_t respLen; |
fabio_gatti | 0:705a0a684de2 | 299 | |
fabio_gatti | 3:647e37ef0eec | 300 | #elif COMM_PROTO == 2 // COAP |
fabio_gatti | 3:647e37ef0eec | 301 | char coap_body[256]; |
fabio_gatti | 3:647e37ef0eec | 302 | char coap_uri_path[256]; |
fabio_gatti | 3:647e37ef0eec | 303 | uint16_t coap_message_id; |
fabio_gatti | 3:647e37ef0eec | 304 | coap_message_id=0; |
fabio_gatti | 3:647e37ef0eec | 305 | |
fabio_gatti | 0:705a0a684de2 | 306 | |
fabio_gatti | 3:647e37ef0eec | 307 | #endif |
fabio_gatti | 3:647e37ef0eec | 308 | |
fabio_gatti | 3:647e37ef0eec | 309 | |
fabio_gatti | 3:647e37ef0eec | 310 | |
fabio_gatti | 3:647e37ef0eec | 311 | |
fabio_gatti | 3:647e37ef0eec | 312 | // ciclo di lettura sensori e caricamento su cloud |
fabio_gatti | 0:705a0a684de2 | 313 | for (;;) { |
fabio_gatti | 5:69b70c8fc368 | 314 | /* |
fabio_gatti | 5:69b70c8fc368 | 315 | |
fabio_gatti | 0:705a0a684de2 | 316 | float temp, humid; |
fabio_gatti | 0:705a0a684de2 | 317 | |
fabio_gatti | 0:705a0a684de2 | 318 | hum_temp.get_temperature(&temp); |
fabio_gatti | 0:705a0a684de2 | 319 | hum_temp.get_humidity(&humid); |
fabio_gatti | 3:647e37ef0eec | 320 | |
fabio_gatti | 3:647e37ef0eec | 321 | printf("ID: %d HTS221: [temp] %.2f C, [hum] %.2f%%\r\n", DEVICE_ID,temp, humid); |
fabio_gatti | 5:69b70c8fc368 | 322 | */ |
fabio_gatti | 0:705a0a684de2 | 323 | |
fabio_gatti | 5:69b70c8fc368 | 324 | sensor_temperature_value = BSP_TSENSOR_ReadTemp(); |
fabio_gatti | 5:69b70c8fc368 | 325 | printf("TEMPERATURE: %.2f degC\n", sensor_temperature_value); |
fabio_gatti | 5:69b70c8fc368 | 326 | |
fabio_gatti | 5:69b70c8fc368 | 327 | sensor_humidity_value = BSP_HSENSOR_ReadHumidity(); |
fabio_gatti | 5:69b70c8fc368 | 328 | printf("HUMIDITY : %.2f %%\n", sensor_humidity_value); |
fabio_gatti | 5:69b70c8fc368 | 329 | |
fabio_gatti | 5:69b70c8fc368 | 330 | sensor_pressure_value = BSP_PSENSOR_ReadPressure(); |
fabio_gatti | 5:69b70c8fc368 | 331 | printf("PRESSURE : %.2f mBar\n", sensor_pressure_value); |
fabio_gatti | 5:69b70c8fc368 | 332 | |
fabio_gatti | 5:69b70c8fc368 | 333 | BSP_ACCELERO_AccGetXYZ(pDataXYZ); |
fabio_gatti | 5:69b70c8fc368 | 334 | printf("ACCELERO_X : %d\n", pDataXYZ[0]); |
fabio_gatti | 5:69b70c8fc368 | 335 | //printf("ACCELERO_Y = %d\n", pDataXYZ[1]); |
fabio_gatti | 5:69b70c8fc368 | 336 | //printf("ACCELERO_Z = %d\n", pDataXYZ[2]); |
fabio_gatti | 5:69b70c8fc368 | 337 | |
fabio_gatti | 5:69b70c8fc368 | 338 | uint32_t distance; |
fabio_gatti | 5:69b70c8fc368 | 339 | |
fabio_gatti | 5:69b70c8fc368 | 340 | int status = range.get_distance(&distance); |
fabio_gatti | 5:69b70c8fc368 | 341 | if (status == VL53L0X_ERROR_NONE) { |
fabio_gatti | 5:69b70c8fc368 | 342 | printf("VL53L0X [mm]: %6ld\n", distance); |
fabio_gatti | 5:69b70c8fc368 | 343 | } else { |
fabio_gatti | 5:69b70c8fc368 | 344 | printf("VL53L0X [mm]: --\n"); |
fabio_gatti | 5:69b70c8fc368 | 345 | } |
fabio_gatti | 5:69b70c8fc368 | 346 | if (abs(pDataXYZ[0]) < TILT_THRESHOLD) { |
fabio_gatti | 5:69b70c8fc368 | 347 | blnTilted = true; |
fabio_gatti | 5:69b70c8fc368 | 348 | } else { |
fabio_gatti | 5:69b70c8fc368 | 349 | blnTilted = false; |
fabio_gatti | 5:69b70c8fc368 | 350 | } //controllo inclinazione |
fabio_gatti | 5:69b70c8fc368 | 351 | |
fabio_gatti | 5:69b70c8fc368 | 352 | if (status == VL53L0X_ERROR_NONE && distance<=MIN_DISTANCE_OBSTACLE && LaserCountObstacle < NUMBER_OF_OBSTACLE_FULL){ |
fabio_gatti | 5:69b70c8fc368 | 353 | LaserCountObstacle++; |
fabio_gatti | 5:69b70c8fc368 | 354 | } else if (status == VL53L0X_ERROR_NONE && distance>MIN_DISTANCE_OBSTACLE && LaserCountObstacle>0){ |
fabio_gatti | 5:69b70c8fc368 | 355 | LaserCountObstacle--; |
fabio_gatti | 5:69b70c8fc368 | 356 | } //controllo distanza ostacolo |
fabio_gatti | 5:69b70c8fc368 | 357 | |
fabio_gatti | 5:69b70c8fc368 | 358 | if (LaserCountObstacle>=NUMBER_OF_OBSTACLE_FULL){ |
fabio_gatti | 5:69b70c8fc368 | 359 | blnIsFull = true; |
fabio_gatti | 5:69b70c8fc368 | 360 | } else if (LaserCountObstacle == 0) { |
fabio_gatti | 5:69b70c8fc368 | 361 | blnIsFull = false; |
fabio_gatti | 5:69b70c8fc368 | 362 | } |
fabio_gatti | 5:69b70c8fc368 | 363 | printf("laser counter: %d\n",LaserCountObstacle); |
fabio_gatti | 5:69b70c8fc368 | 364 | |
fabio_gatti | 5:69b70c8fc368 | 365 | |
fabio_gatti | 3:647e37ef0eec | 366 | #if COMM_PROTO == 0 |
fabio_gatti | 5:69b70c8fc368 | 367 | char msg[256]; |
fabio_gatti | 5:69b70c8fc368 | 368 | |
fabio_gatti | 5:69b70c8fc368 | 369 | |
fabio_gatti | 0:705a0a684de2 | 370 | int n = snprintf(msg, sizeof(msg), |
fabio_gatti | 5:69b70c8fc368 | 371 | "{\"ID\":%d,\"temperature\":%f, \"humidity\":%f, \"pressure\":%f, \"IsTilted\":%d, \"IsFull\":%d}", |
fabio_gatti | 5:69b70c8fc368 | 372 | DEVICE_ID,sensor_temperature_value, sensor_humidity_value,sensor_pressure_value,blnTilted,blnIsFull); |
fabio_gatti | 5:69b70c8fc368 | 373 | printf("messaggio: %s\n",msg); |
fabio_gatti | 0:705a0a684de2 | 374 | |
fabio_gatti | 0:705a0a684de2 | 375 | void *payload = reinterpret_cast<void*>(msg); |
fabio_gatti | 0:705a0a684de2 | 376 | size_t payload_len = n; |
fabio_gatti | 3:647e37ef0eec | 377 | printf("Message payload lenght: %d\r\n",payload_len); |
fabio_gatti | 0:705a0a684de2 | 378 | printf("publish to: %s %d %s\r\n", MQTT_HOST, MQTT_PORT, MQTT_TOPIC); |
fabio_gatti | 0:705a0a684de2 | 379 | if (client.publish(MQTT_TOPIC, payload, n) < 0) { |
fabio_gatti | 5:69b70c8fc368 | 380 | printf("-- ERROR -- :failed to publish MQTT message\n"); |
fabio_gatti | 0:705a0a684de2 | 381 | } |
fabio_gatti | 5:69b70c8fc368 | 382 | |
fabio_gatti | 3:647e37ef0eec | 383 | #elif COMM_PROTO == 1 |
fabio_gatti | 3:647e37ef0eec | 384 | // ciclo di scrittura su socket |
fabio_gatti | 3:647e37ef0eec | 385 | // - open |
fabio_gatti | 3:647e37ef0eec | 386 | // - connect |
fabio_gatti | 3:647e37ef0eec | 387 | // - send |
fabio_gatti | 3:647e37ef0eec | 388 | // - close |
fabio_gatti | 3:647e37ef0eec | 389 | |
fabio_gatti | 3:647e37ef0eec | 390 | socket.open(&wifi); |
fabio_gatti | 3:647e37ef0eec | 391 | response = socket.connect(HTTP_HOST, HTTP_PORT); |
fabio_gatti | 3:647e37ef0eec | 392 | if(0 != response) { |
fabio_gatti | 3:647e37ef0eec | 393 | printf("Error connecting: %d\n", response); |
fabio_gatti | 3:647e37ef0eec | 394 | socket.close(); |
fabio_gatti | 3:647e37ef0eec | 395 | return -1; |
fabio_gatti | 3:647e37ef0eec | 396 | } |
fabio_gatti | 3:647e37ef0eec | 397 | |
fabio_gatti | 3:647e37ef0eec | 398 | |
fabio_gatti | 3:647e37ef0eec | 399 | // body of the request |
fabio_gatti | 3:647e37ef0eec | 400 | |
fabio_gatti | 5:69b70c8fc368 | 401 | sprintf(request_body, "{\"ID\":%d,\"temperature\":%f, \"humidity\":%f, \"pressure\":%f, \"IsTilted\":%d, \"IsFull\":%d}", |
fabio_gatti | 5:69b70c8fc368 | 402 | DEVICE_ID,sensor_temperature_value, sensor_humidity_value,sensor_pressure_value,blnTilted,blnIsFull); |
fabio_gatti | 5:69b70c8fc368 | 403 | |
fabio_gatti | 5:69b70c8fc368 | 404 | printf("messaggio: %s\r\n",request_body); |
fabio_gatti | 5:69b70c8fc368 | 405 | |
fabio_gatti | 3:647e37ef0eec | 406 | |
fabio_gatti | 3:647e37ef0eec | 407 | // build header of the request |
fabio_gatti | 3:647e37ef0eec | 408 | sprintf((char *)http_request, "POST %s%s%s HTTP/1.1\r\nHost: %s \r\n", HTTP_STR_1,DEVICE_ACCESS_TOKEN,HTTP_STR_2, HTTP_HOST); |
fabio_gatti | 3:647e37ef0eec | 409 | strcat((char *)http_request, "Accept: */*\r\n"); |
fabio_gatti | 3:647e37ef0eec | 410 | strcat((char *)http_request, "User-agent: ST-475-IOT\r\n"); |
fabio_gatti | 3:647e37ef0eec | 411 | strcat((char *)http_request, "Connection: Close\r\n"); |
fabio_gatti | 3:647e37ef0eec | 412 | char buffer[64]; |
fabio_gatti | 3:647e37ef0eec | 413 | strcat((char *)http_request, "Content-Type: application/json\r\n"); |
fabio_gatti | 5:69b70c8fc368 | 414 | sprintf(buffer, "Content-Length: %d \r\n", strlen(request_body)); |
fabio_gatti | 3:647e37ef0eec | 415 | strcat((char *)http_request, buffer); |
fabio_gatti | 3:647e37ef0eec | 416 | |
fabio_gatti | 3:647e37ef0eec | 417 | // append body to the header of the request |
fabio_gatti | 3:647e37ef0eec | 418 | strcat((char *)http_request, request_body); |
fabio_gatti | 3:647e37ef0eec | 419 | reqLen = strlen((char *)http_request); |
fabio_gatti | 3:647e37ef0eec | 420 | printf((char *)http_request); |
fabio_gatti | 3:647e37ef0eec | 421 | |
fabio_gatti | 3:647e37ef0eec | 422 | // Send a simple http request |
fabio_gatti | 3:647e37ef0eec | 423 | |
fabio_gatti | 3:647e37ef0eec | 424 | nsapi_size_t size = strlen((char *)http_request); |
fabio_gatti | 3:647e37ef0eec | 425 | response = 0; |
fabio_gatti | 3:647e37ef0eec | 426 | |
fabio_gatti | 3:647e37ef0eec | 427 | while(size) |
fabio_gatti | 3:647e37ef0eec | 428 | { |
fabio_gatti | 3:647e37ef0eec | 429 | response = socket.send(((char *)http_request)+response, size); |
fabio_gatti | 3:647e37ef0eec | 430 | |
fabio_gatti | 3:647e37ef0eec | 431 | if (response < 0) { |
fabio_gatti | 3:647e37ef0eec | 432 | printf("Error sending data: %d\n", response); |
fabio_gatti | 3:647e37ef0eec | 433 | socket.close(); |
fabio_gatti | 3:647e37ef0eec | 434 | return -1; |
fabio_gatti | 3:647e37ef0eec | 435 | } else { |
fabio_gatti | 3:647e37ef0eec | 436 | size -= response; |
fabio_gatti | 3:647e37ef0eec | 437 | // Check if entire message was sent or not |
fabio_gatti | 3:647e37ef0eec | 438 | printf("sent %d [%.*s]\n", response, strstr((char *)http_request, "\r\n")-(char *)http_request, (char *)http_request); |
fabio_gatti | 3:647e37ef0eec | 439 | } |
fabio_gatti | 3:647e37ef0eec | 440 | } |
fabio_gatti | 3:647e37ef0eec | 441 | // pulizia risorse della socket |
fabio_gatti | 3:647e37ef0eec | 442 | socket.close(); |
fabio_gatti | 3:647e37ef0eec | 443 | |
fabio_gatti | 3:647e37ef0eec | 444 | #elif COMM_PROTO == 2 //COAP |
fabio_gatti | 3:647e37ef0eec | 445 | |
fabio_gatti | 3:647e37ef0eec | 446 | |
fabio_gatti | 3:647e37ef0eec | 447 | // Open a socket on the network interface |
fabio_gatti | 3:647e37ef0eec | 448 | socket.open(&wifi); |
fabio_gatti | 3:647e37ef0eec | 449 | |
fabio_gatti | 3:647e37ef0eec | 450 | // Initialize the CoAP protocol handle, pointing to local implementations on malloc/free/tx/rx functions |
fabio_gatti | 3:647e37ef0eec | 451 | coapHandle = sn_coap_protocol_init(&coap_malloc, &coap_free, &coap_tx_cb, &coap_rx_cb); |
fabio_gatti | 3:647e37ef0eec | 452 | |
fabio_gatti | 3:647e37ef0eec | 453 | |
fabio_gatti | 3:647e37ef0eec | 454 | // Path to the resource we want to retrieve |
fabio_gatti | 3:647e37ef0eec | 455 | sprintf(coap_uri_path, "%s%s%s", COAP_STR_1,DEVICE_ACCESS_TOKEN,COAP_STR_2); |
fabio_gatti | 5:69b70c8fc368 | 456 | sprintf(coap_body, "{\"ID\":%d,\"temperature\":%f, \"humidity\":%f, \"pressure\":%f, \"IsTilted\":%d, \"IsFull\":%d}", |
fabio_gatti | 5:69b70c8fc368 | 457 | DEVICE_ID,sensor_temperature_value, sensor_humidity_value,sensor_pressure_value,blnTilted,blnIsFull); |
fabio_gatti | 3:647e37ef0eec | 458 | |
fabio_gatti | 3:647e37ef0eec | 459 | printf ("URI PATH: %s\n",coap_uri_path); |
fabio_gatti | 3:647e37ef0eec | 460 | printf ("BODY: %s\n",coap_body); |
fabio_gatti | 3:647e37ef0eec | 461 | printf ("id: %d\n",coap_message_id); |
fabio_gatti | 3:647e37ef0eec | 462 | |
fabio_gatti | 3:647e37ef0eec | 463 | // See ns_coap_header.h |
fabio_gatti | 3:647e37ef0eec | 464 | sn_coap_hdr_s *coap_res_ptr = (sn_coap_hdr_s*)calloc(sizeof(sn_coap_hdr_s), 1); |
fabio_gatti | 3:647e37ef0eec | 465 | coap_res_ptr->uri_path_ptr = (uint8_t*)coap_uri_path; // Path |
fabio_gatti | 3:647e37ef0eec | 466 | coap_res_ptr->uri_path_len = strlen(coap_uri_path); |
fabio_gatti | 3:647e37ef0eec | 467 | coap_res_ptr->msg_type = COAP_MSG_TYPE_NON_CONFIRMABLE; |
fabio_gatti | 3:647e37ef0eec | 468 | coap_res_ptr->msg_code = COAP_MSG_CODE_REQUEST_POST; // CoAP method |
fabio_gatti | 3:647e37ef0eec | 469 | coap_res_ptr->content_format = COAP_CT_JSON; // CoAP content type |
fabio_gatti | 3:647e37ef0eec | 470 | coap_res_ptr->payload_len = strlen(coap_body); // Body length |
fabio_gatti | 3:647e37ef0eec | 471 | coap_res_ptr->payload_ptr = (uint8_t*)coap_body; // Body pointer |
fabio_gatti | 3:647e37ef0eec | 472 | coap_res_ptr->options_list_ptr = 0; // Optional: options list |
fabio_gatti | 3:647e37ef0eec | 473 | coap_res_ptr->msg_id = coap_message_id; //msg ID, don't forget to increase it |
fabio_gatti | 3:647e37ef0eec | 474 | coap_message_id++; |
fabio_gatti | 3:647e37ef0eec | 475 | |
fabio_gatti | 3:647e37ef0eec | 476 | // Calculate the CoAP message size, allocate the memory and build the message |
fabio_gatti | 3:647e37ef0eec | 477 | uint16_t message_len = sn_coap_builder_calc_needed_packet_data_size(coap_res_ptr); |
fabio_gatti | 3:647e37ef0eec | 478 | printf("Calculated message length: %d bytes\n", message_len); |
fabio_gatti | 3:647e37ef0eec | 479 | |
fabio_gatti | 3:647e37ef0eec | 480 | uint8_t* message_ptr = (uint8_t*)malloc(message_len); |
fabio_gatti | 3:647e37ef0eec | 481 | sn_coap_builder(message_ptr, coap_res_ptr); |
fabio_gatti | 3:647e37ef0eec | 482 | |
fabio_gatti | 3:647e37ef0eec | 483 | // Uncomment to see the raw buffer that will be sent... |
fabio_gatti | 3:647e37ef0eec | 484 | printf("Message is: "); |
fabio_gatti | 3:647e37ef0eec | 485 | for (size_t ix = 0; ix < message_len; ix++) { |
fabio_gatti | 3:647e37ef0eec | 486 | printf("%02x ", message_ptr[ix]); |
fabio_gatti | 3:647e37ef0eec | 487 | } |
fabio_gatti | 3:647e37ef0eec | 488 | printf("\n"); |
fabio_gatti | 3:647e37ef0eec | 489 | |
fabio_gatti | 3:647e37ef0eec | 490 | int scount = socket.sendto(COAP_HOST, COAP_PORT, message_ptr, message_len); |
fabio_gatti | 3:647e37ef0eec | 491 | printf("Sent %d bytes to coap://%s:%d\n", scount,COAP_HOST, COAP_PORT); |
fabio_gatti | 3:647e37ef0eec | 492 | |
fabio_gatti | 3:647e37ef0eec | 493 | // routine di ricezione |
fabio_gatti | 3:647e37ef0eec | 494 | #if SHOW_COAP_RESPONSE == true |
fabio_gatti | 3:647e37ef0eec | 495 | SocketAddress addr; |
fabio_gatti | 3:647e37ef0eec | 496 | uint8_t* recv_buffer = (uint8_t*)malloc(1280); // Suggested is to keep packet size under 1280 bytes |
fabio_gatti | 3:647e37ef0eec | 497 | |
fabio_gatti | 3:647e37ef0eec | 498 | if ((ret = socket.recvfrom(&addr, recv_buffer, 1280)) >= 0) { |
fabio_gatti | 3:647e37ef0eec | 499 | // to see where the message came from, inspect addr.get_addr() and addr.get_port() |
fabio_gatti | 3:647e37ef0eec | 500 | |
fabio_gatti | 3:647e37ef0eec | 501 | printf("Received packets from (%s,%d)\n", addr.get_ip_address(),addr.get_port()); |
fabio_gatti | 3:647e37ef0eec | 502 | |
fabio_gatti | 3:647e37ef0eec | 503 | sn_coap_hdr_s* parsed = sn_coap_parser(coapHandle, ret, recv_buffer, &coapVersion); |
fabio_gatti | 3:647e37ef0eec | 504 | |
fabio_gatti | 3:647e37ef0eec | 505 | // We know the payload is going to be a string |
fabio_gatti | 3:647e37ef0eec | 506 | std::string payload((const char*)parsed->payload_ptr, parsed->payload_len); |
fabio_gatti | 3:647e37ef0eec | 507 | |
fabio_gatti | 3:647e37ef0eec | 508 | printf("\tmsg_id: %d\n", parsed->msg_id); |
fabio_gatti | 3:647e37ef0eec | 509 | printf("\tmsg_code: %d\n", parsed->msg_code); |
fabio_gatti | 3:647e37ef0eec | 510 | printf("\tcontent_format: %d\n", parsed->content_format); |
fabio_gatti | 3:647e37ef0eec | 511 | printf("\tpayload_len: %d\n", parsed->payload_len); |
fabio_gatti | 3:647e37ef0eec | 512 | printf("\tpayload: %s\n", payload.c_str()); |
fabio_gatti | 3:647e37ef0eec | 513 | printf("\toptions_list_ptr: %p\n", parsed->options_list_ptr); |
fabio_gatti | 3:647e37ef0eec | 514 | } |
fabio_gatti | 3:647e37ef0eec | 515 | |
fabio_gatti | 3:647e37ef0eec | 516 | free(recv_buffer); |
fabio_gatti | 3:647e37ef0eec | 517 | |
fabio_gatti | 3:647e37ef0eec | 518 | |
fabio_gatti | 3:647e37ef0eec | 519 | #endif //end SHOW_COAP_RESPONSE |
fabio_gatti | 3:647e37ef0eec | 520 | socket.close(); |
fabio_gatti | 3:647e37ef0eec | 521 | sn_coap_protocol_destroy(coapHandle); |
fabio_gatti | 3:647e37ef0eec | 522 | free(coap_res_ptr); |
fabio_gatti | 3:647e37ef0eec | 523 | free(message_ptr); |
fabio_gatti | 3:647e37ef0eec | 524 | |
fabio_gatti | 3:647e37ef0eec | 525 | |
fabio_gatti | 3:647e37ef0eec | 526 | #endif //end protocol selection |
fabio_gatti | 5:69b70c8fc368 | 527 | |
fabio_gatti | 5:69b70c8fc368 | 528 | |
fabio_gatti | 3:647e37ef0eec | 529 | wait_ms(SENSOR_READING_PERIOD); |
fabio_gatti | 0:705a0a684de2 | 530 | } |
fabio_gatti | 0:705a0a684de2 | 531 | |
fabio_gatti | 3:647e37ef0eec | 532 | //le disconnect non vengono raggiunte perche' rimaniamo all'interno del ciclo |
fabio_gatti | 2:983b7f5dde1e | 533 | //client.disconnect(); |
fabio_gatti | 2:983b7f5dde1e | 534 | //wifi.disconnect(); |
fabio_gatti | 0:705a0a684de2 | 535 | |
fabio_gatti | 2:983b7f5dde1e | 536 | //printf("\ndone\n"); |
fabio_gatti | 2:983b7f5dde1e | 537 | //return 0; |
fabio_gatti | 0:705a0a684de2 | 538 | |
fabio_gatti | 0:705a0a684de2 | 539 | } |