0
Dependencies: Cayenne-MQTT-mbed nfc X_NUCLEO_IDW01M1v2 NetworkSocketAPI 13
Diff: main.cpp
- Revision:
- 9:c206c33736fe
- Parent:
- 8:3bcc1a412fe3
diff -r 3bcc1a412fe3 -r c206c33736fe main.cpp --- a/main.cpp Mon Oct 30 07:19:47 2017 +0000 +++ b/main.cpp Wed Aug 28 18:52:43 2019 +0000 @@ -8,14 +8,88 @@ #include "MQTTNetworkIDW01M1.h" #include "SpwfInterface.h" + + +#include "mbed.h" +#include "XNucleoIKS01A2.h" + +/* Instantiate the expansion board */ +static XNucleoIKS01A2 *mems_expansion_board = XNucleoIKS01A2::instance(D14, D15, D4, D5); + +/* Retrieve the composing elements of the expansion board */ +static LSM303AGRMagSensor *magnetometer = mems_expansion_board->magnetometer; +static HTS221Sensor *hum_temp = mems_expansion_board->ht_sensor; +static LPS22HBSensor *press_temp = mems_expansion_board->pt_sensor; +static LSM6DSLSensor *acc_gyro = mems_expansion_board->acc_gyro; +static LSM303AGRAccSensor *accelerometer = mems_expansion_board->accelerometer; + +/* Helper function for printing floats & doubles */ +static char *print_double(char* str, double v, int decimalDigits=2) +{ + int i = 1; + int intPart, fractPart; + int len; + char *ptr; + + /* prepare decimal digits multiplicator */ + for (;decimalDigits!=0; i*=10, decimalDigits--); + + /* calculate integer & fractinal parts */ + intPart = (int)v; + fractPart = (int)((v-(double)(int)v)*i); + + /* fill in integer part */ + sprintf(str, "%i.", intPart); + + /* prepare fill in of fractional part */ + len = strlen(str); + ptr = &str[len]; + + /* fill in leading fractional zeros */ + for (i/=10;i>1; i/=10, ptr++) { + if (fractPart >= i) { + break; + } + *ptr = '0'; + } + + /* fill in (rest of) fractional part */ + sprintf(ptr, "%i", fractPart); + + return str; +} + + + + +#include "XNucleoNFC01A1.h" +#include "NDefLib/NDefNfcTag.h" +#include "NDefLib/RecordType/RecordURI.h" + +/** + * Write a Ndef URI message linking to st.com site. + * Write an NDef message with a Uri record linking the st.com site + * @param nfcNucleo expansion board where write the NDef message + */ + + + + + + + + + + + // WiFi network info. -char* ssid = "JeffiPhone7"; -char* wifiPassword = "iots2480"; +char* ssid = "Interneto"; +char* wifiPassword = "matu1234"; // Cayenne authentication info. This should be obtained from the Cayenne Dashboard. -char* username = "1f764db0-122b-11e7-b235-9359079e690e"; -char* password = "0d4e9d94e7897df9af861007853ed3f906b0627d"; -char* clientID = "536277d0-8cb9-11e7-9727-55550d1a07e7"; +char* username = "68880f30-7425-11e9-beb3-736c9e4bf7d0"; +char* password = "19f07b4d8806fe42bdda724980634f39d8e639ba"; +char* clientID = "bb8e7cc0-74b9-11e9-94e9-493d67fd755e"; SpwfSAInterface interface(D8, D2); // TX, RX MQTTNetwork<SpwfSAInterface> network(interface); @@ -154,28 +228,83 @@ // Publish some example data every few seconds. This should be changed to send your actual data to Cayenne. if (timer.expired()) { int error = 0; - if ((error = mqttClient.publishData(DATA_TOPIC, 1, TYPE_TEMPERATURE, UNIT_CELSIUS, 30.5)) != CAYENNE_SUCCESS) { + + uint8_t id; + float value1, value2; + char buffer1[32], buffer2[32]; + int32_t axes[3]; + + /* Enable all sensors */ + hum_temp->enable(); + press_temp->enable(); + magnetometer->enable(); + accelerometer->enable(); + acc_gyro->enable_x(); + acc_gyro->enable_g(); + + printf("\r\n--- Starting new run ---\r\n"); + + hum_temp->read_id(&id); + printf("HTS221 humidity & temperature = 0x%X\r\n", id); + press_temp->read_id(&id); + printf("LPS22HB pressure & temperature = 0x%X\r\n", id); + magnetometer->read_id(&id); + printf("LSM303AGR magnetometer = 0x%X\r\n", id); + accelerometer->read_id(&id); + printf("LSM303AGR accelerometer = 0x%X\r\n", id); + acc_gyro->read_id(&id); + printf("LSM6DSL accelerometer & gyroscope = 0x%X\r\n", id); + + printf("\r\n"); + + hum_temp->get_temperature(&value1); + hum_temp->get_humidity(&value2); + printf("HTS221: [temp] %7s C, [hum] %s%%\r\n", print_double(buffer1, value1), print_double(buffer2, value2)); + + press_temp->get_temperature(&value1); + press_temp->get_pressure(&value2); + printf("LPS22HB: [temp] %7s C, [press] %s mbar\r\n", print_double(buffer1, value1), print_double(buffer2, value2)); + + printf("---\r\n"); + + magnetometer->get_m_axes(axes); + printf("LSM303AGR [mag/mgauss]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]); + + accelerometer->get_x_axes(axes); + printf("LSM303AGR [acc/mg]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]); + + acc_gyro->get_x_axes(axes); + printf("LSM6DSL [acc/mg]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]); + + acc_gyro->get_g_axes(axes); + printf("LSM6DSL [gyro/mdps]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]); + + + + if ((error = mqttClient.publishData(DATA_TOPIC, 1, TYPE_TEMPERATURE, UNIT_CELSIUS, value1)) != CAYENNE_SUCCESS) { printf("Publish temperature failed, error: %d\n", error); } - if ((error = mqttClient.publishData(DATA_TOPIC, 2, TYPE_LUMINOSITY, UNIT_LUX, 1000)) != CAYENNE_SUCCESS) { - printf("Publish luminosity failed, error: %d\n", error); - } - if ((error = mqttClient.publishData(DATA_TOPIC, 3, TYPE_BAROMETRIC_PRESSURE, UNIT_HECTOPASCAL, 800)) != CAYENNE_SUCCESS) { + + + if ((error = mqttClient.publishData(DATA_TOPIC, 2, TYPE_BAROMETRIC_PRESSURE, UNIT_HECTOPASCAL, value2)) != CAYENNE_SUCCESS) { printf("Publish barometric pressure failed, error: %d\n", error); } - // Restart the countdown timer for publishing data every 5 seconds. Change the timeout parameter to publish at a different interval. - timer.countdown_ms(5000); + // Restart the countdown timer for publishing data every 2 seconds. Change the timeout parameter to publish at a different interval. + timer.countdown_ms(2000); } } } + + + + /** * Main function. */ int main() { - // Initialize the network interface. - printf("Initializing interface\n"); + printf("Initializing interface\n"); interface.connect(ssid, wifiPassword, NSAPI_SECURITY_WPA2); // Set the default function that receives Cayenne messages. @@ -194,7 +323,9 @@ mqttClient.disconnect(); if (network.connected()) network.disconnect(); +return 0; - return 0; } + +