initial release
Dependencies: BSP_B-L475E-IOT01 MQTT
Fork of Multiprotocol by
Revision 5:69b70c8fc368, committed 2018-08-22
- Comitter:
- fabio_gatti
- Date:
- Wed Aug 22 21:43:31 2018 +0000
- Parent:
- 4:591b0f33f8b0
- Commit message:
- initial release
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/BSP_B-L475E-IOT01.lib Wed Aug 22 21:43:31 2018 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/teams/Farnell24-IOT-Team/code/BSP_B-L475E-IOT01/#8cb54b28bc45
--- a/HTS221.lib Fri Aug 17 18:09:07 2018 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://os.mbed.com/teams/ST/code/HTS221/#312ee2694a77
--- a/main.cpp Fri Aug 17 18:09:07 2018 +0000 +++ b/main.cpp Wed Aug 22 21:43:31 2018 +0000 @@ -4,10 +4,20 @@ #include "mbed.h" #include "ISM43362Interface.h" #include "TCPSocket.h" -#include "HTS221Sensor.h" +//#include "HTS221Sensor.h" #include "thingsboard_account.h" +// Sensors drivers present in the BSP library +#include "stm32l475e_iot01_tsensor.h" +#include "stm32l475e_iot01_hsensor.h" +#include "stm32l475e_iot01_psensor.h" +#include "stm32l475e_iot01_magneto.h" +#include "stm32l475e_iot01_gyro.h" +#include "stm32l475e_iot01_accelero.h" +// laser driver +//#include "lis3mdl_class.h" +#include "VL53L0X.h" // H file per MQTT #include "MQTTmbed.h" @@ -22,14 +32,14 @@ // Change it with your WiFi network name //#define WIFI_NETWORK_NAME "farnell_iot_lab" -#define WIFI_NETWORK_NAME "rucola" +#define WIFI_NETWORK_NAME "rucola" // Change it with your WiFi password name //#define WIFI_NETWORK_PASSWORD "smartlab" -#define WIFI_NETWORK_PASSWORD "Rosmarino_10" +#define WIFI_NETWORK_PASSWORD "Rosmarino_10" -#define WIFI_SECURITY NSAPI_SECURITY_WPA_WPA2 +#define WIFI_SECURITY NSAPI_SECURITY_WPA_WPA2 -#define COMM_PROTO 1 +#define COMM_PROTO 2 // scegliere il protocollo di trasporto dati // COMM_PROTO = 0 -> accesso MQTT (default); @@ -41,7 +51,12 @@ // scegliere il token al device #define DEVICE_ID 1 -#define DEVICE_ACCESS_TOKEN TEAM_1_DEVICE_ACCESS_TOKEN +#define DEVICE_ACCESS_TOKEN SMART_BIN_1_DEVICE_ACCESS_TOKEN + +//parametri rilevamento laser +#define MIN_DISTANCE_OBSTACLE 80 //distanza di rilevamento in mm +#define NUMBER_OF_OBSTACLE_FULL 3 //numero di letture per dichiarare il cestino pieno +#define TILT_THRESHOLD 700 //valore di soglia per ribaltamento pari a sin(angolo)*1000 // ciclo di frequenza lettura #define SENSOR_READING_PERIOD 5000 //in ms @@ -51,6 +66,7 @@ #define MQTT_HOST "demo.thingsboard.io" #define MQTT_PORT 1883 #define MQTT_TOPIC "v1/devices/me/telemetry" + #define MQTT_MAX_MSG_LENGHT 500 //max mqtt lenght in byte #elif COMM_PROTO==1 //HTTP protocol #define HTTP_HOST "demo.thingsboard.io" @@ -133,12 +149,47 @@ #endif +#ifdef TARGET_DISCO_L475VG_IOT01A + +static DevI2C devI2c(PB_11,PB_10); +static DigitalOut shutdown_pin(PC_6); +static VL53L0X range(&devI2c, &shutdown_pin, PC_7); + +#endif + +DigitalOut led(LED1); Serial pc(USBTX, USBRX); //use these pins for serial coms. int main() { - + + bool blnTilted = false; // variabile per l'assetto del cestino + bool blnIsFull=false; // variabile per lo stato (pieno/vuoto) del cestino + int LaserCountObstacle=0; //contatore di ostacoli rilevati dal laser + int count = 0; pc.baud(115200); + + +//initing sensori + float sensor_pressure_value = 0; + float sensor_temperature_value = 0; + float sensor_humidity_value = 0; + + int16_t pDataXYZ[3] = {0}; + //float pGyroDataXYZ[3] = {0}; + + range.init_sensor(VL53L0X_DEFAULT_ADDRESS); + + BSP_TSENSOR_Init(); + BSP_HSENSOR_Init(); + BSP_PSENSOR_Init(); + + //BSP_MAGNETO_Init(); + //BSP_GYRO_Init(); + BSP_ACCELERO_Init(); + +//initing sensori + printf(" --- START SESSION ---\n"); ISM43362Interface wifi(MBED_CONF_APP_WIFI_SPI_MOSI, @@ -187,7 +238,7 @@ printf("Collegamento MQTT server: " MQTT_HOST "\n"); MQTTNetwork network(&wifi); - MQTT::Client<MQTTNetwork, Countdown> client(network); + MQTT::Client<MQTTNetwork, Countdown,MQTT_MAX_MSG_LENGHT> client(network); // attenzione alla MAX DIM MSG MQTT char assess_token[] = DEVICE_ACCESS_TOKEN; @@ -229,8 +280,7 @@ #endif // Initialize sensors -------------------------------------------------- - -uint8_t id; +/* DevI2C i2c_2(PB_11, PB_10); HTS221Sensor hum_temp(&i2c_2); @@ -238,7 +288,7 @@ hum_temp.enable(); hum_temp.read_id(&id); printf("HTS221 humidity & temperature sensor = 0x%X\r\n", id); - +*/ // Variabili di appoggio ----------------------------------------------- #if COMM_PROTO == 1 uint8_t http_request[1024]; @@ -261,27 +311,75 @@ // ciclo di lettura sensori e caricamento su cloud for (;;) { +/* + float temp, humid; hum_temp.get_temperature(&temp); hum_temp.get_humidity(&humid); printf("ID: %d HTS221: [temp] %.2f C, [hum] %.2f%%\r\n", DEVICE_ID,temp, humid); +*/ + sensor_temperature_value = BSP_TSENSOR_ReadTemp(); + printf("TEMPERATURE: %.2f degC\n", sensor_temperature_value); + + sensor_humidity_value = BSP_HSENSOR_ReadHumidity(); + printf("HUMIDITY : %.2f %%\n", sensor_humidity_value); + + sensor_pressure_value = BSP_PSENSOR_ReadPressure(); + printf("PRESSURE : %.2f mBar\n", sensor_pressure_value); + + BSP_ACCELERO_AccGetXYZ(pDataXYZ); + printf("ACCELERO_X : %d\n", pDataXYZ[0]); + //printf("ACCELERO_Y = %d\n", pDataXYZ[1]); + //printf("ACCELERO_Z = %d\n", pDataXYZ[2]); + + uint32_t distance; + + int status = range.get_distance(&distance); + if (status == VL53L0X_ERROR_NONE) { + printf("VL53L0X [mm]: %6ld\n", distance); + } else { + printf("VL53L0X [mm]: --\n"); + } + if (abs(pDataXYZ[0]) < TILT_THRESHOLD) { + blnTilted = true; + } else { + blnTilted = false; + } //controllo inclinazione + + if (status == VL53L0X_ERROR_NONE && distance<=MIN_DISTANCE_OBSTACLE && LaserCountObstacle < NUMBER_OF_OBSTACLE_FULL){ + LaserCountObstacle++; + } else if (status == VL53L0X_ERROR_NONE && distance>MIN_DISTANCE_OBSTACLE && LaserCountObstacle>0){ + LaserCountObstacle--; + } //controllo distanza ostacolo + + if (LaserCountObstacle>=NUMBER_OF_OBSTACLE_FULL){ + blnIsFull = true; + } else if (LaserCountObstacle == 0) { + blnIsFull = false; + } + printf("laser counter: %d\n",LaserCountObstacle); + + #if COMM_PROTO == 0 - char msg[256]; + char msg[256]; + + int n = snprintf(msg, sizeof(msg), - "{\"ID\":%d,\"temperature\":%f, \"humidity\":%f}", - DEVICE_ID,temp, humid); + "{\"ID\":%d,\"temperature\":%f, \"humidity\":%f, \"pressure\":%f, \"IsTilted\":%d, \"IsFull\":%d}", + DEVICE_ID,sensor_temperature_value, sensor_humidity_value,sensor_pressure_value,blnTilted,blnIsFull); + printf("messaggio: %s\n",msg); void *payload = reinterpret_cast<void*>(msg); size_t payload_len = n; printf("Message payload lenght: %d\r\n",payload_len); printf("publish to: %s %d %s\r\n", MQTT_HOST, MQTT_PORT, MQTT_TOPIC); if (client.publish(MQTT_TOPIC, payload, n) < 0) { - printf("failed to publish MQTT message"); + printf("-- ERROR -- :failed to publish MQTT message\n"); } - + #elif COMM_PROTO == 1 // ciclo di scrittura su socket // - open @@ -300,7 +398,11 @@ // body of the request - sprintf(request_body, "{\"ID\":%d,\"temperature\": %f,\"humidity\": %f}\r\n",DEVICE_ID, temp, humid); + sprintf(request_body, "{\"ID\":%d,\"temperature\":%f, \"humidity\":%f, \"pressure\":%f, \"IsTilted\":%d, \"IsFull\":%d}", + DEVICE_ID,sensor_temperature_value, sensor_humidity_value,sensor_pressure_value,blnTilted,blnIsFull); + + printf("messaggio: %s\r\n",request_body); + // build header of the request 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); @@ -309,7 +411,7 @@ strcat((char *)http_request, "Connection: Close\r\n"); char buffer[64]; strcat((char *)http_request, "Content-Type: application/json\r\n"); - sprintf(buffer, "Content-Length: %d \r\n\r\n", strlen(request_body)); + sprintf(buffer, "Content-Length: %d \r\n", strlen(request_body)); strcat((char *)http_request, buffer); // append body to the header of the request @@ -351,7 +453,8 @@ // Path to the resource we want to retrieve sprintf(coap_uri_path, "%s%s%s", COAP_STR_1,DEVICE_ACCESS_TOKEN,COAP_STR_2); - sprintf(coap_body, "{\"ID\":%d,\"temperature\": %f,\"humidity\": %f}\r\n", DEVICE_ID,temp, humid); + sprintf(coap_body, "{\"ID\":%d,\"temperature\":%f, \"humidity\":%f, \"pressure\":%f, \"IsTilted\":%d, \"IsFull\":%d}", + DEVICE_ID,sensor_temperature_value, sensor_humidity_value,sensor_pressure_value,blnTilted,blnIsFull); printf ("URI PATH: %s\n",coap_uri_path); printf ("BODY: %s\n",coap_body); @@ -421,6 +524,8 @@ #endif //end protocol selection + + wait_ms(SENSOR_READING_PERIOD); }
--- a/thingsboard_account.h Fri Aug 17 18:09:07 2018 +0000 +++ b/thingsboard_account.h Wed Aug 22 21:43:31 2018 +0000 @@ -13,6 +13,7 @@ #if !defined(THINGSBOARD_ACCOUNT_H) #define THINGSBOARD_ACCOUNT_H +#define SMART_BIN_1_DEVICE_ACCESS_TOKEN "GMmiya1zi1nLOdQXldIv" #define TEAM_1_DEVICE_ACCESS_TOKEN "yzxOKW9ECPRIcSYPT6FL" #define TEAM_2_DEVICE_ACCESS_TOKEN "dLt8RY6rk17QWwHgwWRS"