Smartage application

Dependencies:   BufferedSerial SX1276GenericLib USBDeviceHT mbed Crypto X_NUCLEO_IKS01A2

Fork of STM32L0_LoRa by Helmut Tschemernjak

Committer:
marcozecchini
Date:
Mon Sep 17 20:37:51 2018 +0000
Revision:
29:04e1489f8fe2
Parent:
26:d93f1206909c
Child:
32:c22ce5fdacbf
Bug fixed

Who changed what in which revision?

UserRevisionLine numberNew contents of line
marcozecchini 29:04e1489f8fe2 1 #include "main.h"
Helmut64 0:c43b6919ae15 2
Helmut64 17:98f2528e8399 3 DigitalOut myled(LED);
marcozecchini 19:7763501775e5 4 //D12 TRIGGER D11 ECHO
marcozecchini 19:7763501775e5 5 HCSR04 sensor(D12, D11);
marcozecchini 24:bb733d746bda 6 /* Instantiate the expansion board */
marcozecchini 24:bb733d746bda 7 XNucleoIKS01A2 *mems_expansion_board = XNucleoIKS01A2::instance(D14, D15, D4, D5);
marcozecchini 24:bb733d746bda 8 volatile int mems_event = 0;
marcozecchini 24:bb733d746bda 9 volatile int notification = 0;
marcozecchini 24:bb733d746bda 10
marcozecchini 24:bb733d746bda 11 /* Sensor initialization */
marcozecchini 24:bb733d746bda 12 HTS221Sensor *hum_temp = mems_expansion_board->ht_sensor;
marcozecchini 24:bb733d746bda 13 LSM6DSLSensor *acc_gyro = mems_expansion_board->acc_gyro;
marcozecchini 24:bb733d746bda 14
Helmut64 17:98f2528e8399 15 int main() {
Helmut64 17:98f2528e8399 16 #ifdef HELTEC_STM32L4
Helmut64 17:98f2528e8399 17 DigitalOut vext(POWER_VEXT);
Helmut64 17:98f2528e8399 18 vext = POWER_VEXT_ON;
Helmut64 17:98f2528e8399 19 #endif
Helmut64 17:98f2528e8399 20 /*
Helmut64 17:98f2528e8399 21 * inits the Serial or USBSerial when available (230400 baud).
Helmut64 17:98f2528e8399 22 * If the serial uart is not is not connected it swiches to USB Serial
Helmut64 17:98f2528e8399 23 * blinking LED means USBSerial detected, waiting for a connect.
Helmut64 17:98f2528e8399 24 * It waits up to 30 seconds for a USB terminal connections
Helmut64 17:98f2528e8399 25 */
marcozecchini 19:7763501775e5 26 InitSerial(30*1000, &myled);
marcozecchini 24:bb733d746bda 27 hum_temp->enable();
marcozecchini 24:bb733d746bda 28
marcozecchini 24:bb733d746bda 29 /* Enable LSM6DSL accelerometer */
marcozecchini 24:bb733d746bda 30 acc_gyro->enable_x();
marcozecchini 25:18a57be7bb17 31 /* Enable 6D Orientation. */
marcozecchini 25:18a57be7bb17 32 acc_gyro->enable_6d_orientation();
marcozecchini 24:bb733d746bda 33
marcozecchini 20:1557c9d9c183 34 print_stuff();
marcozecchini 24:bb733d746bda 35 bool empty = true;
marcozecchini 26:d93f1206909c 36 //WAIT
marcozecchini 29:04e1489f8fe2 37 wait(30.0f);
marcozecchini 19:7763501775e5 38 while(1){
marcozecchini 19:7763501775e5 39
marcozecchini 24:bb733d746bda 40 char distance[4], empty_distance[4], temperature[4];//Message to be sent
marcozecchini 24:bb733d746bda 41 get_distance(distance);
marcozecchini 24:bb733d746bda 42 get_temperature(temperature);
marcozecchini 25:18a57be7bb17 43 notification = send_orientation();
marcozecchini 24:bb733d746bda 44
marcozecchini 24:bb733d746bda 45 if(empty) {
marcozecchini 24:bb733d746bda 46 memcpy(empty_distance, distance, 4);
marcozecchini 24:bb733d746bda 47 SendAndBack((uint8_t*)distance, (uint8_t*)empty_distance, (uint8_t*)temperature, notification); //invia due volte
marcozecchini 24:bb733d746bda 48 empty = false;
marcozecchini 24:bb733d746bda 49 }
marcozecchini 24:bb733d746bda 50 else{
marcozecchini 24:bb733d746bda 51 SendAndBack((uint8_t*)distance, (uint8_t*)empty_distance, (uint8_t*)temperature, notification);
marcozecchini 24:bb733d746bda 52 }
marcozecchini 24:bb733d746bda 53
marcozecchini 19:7763501775e5 54 }
marcozecchini 22:2c1359292de1 55 }
marcozecchini 22:2c1359292de1 56
marcozecchini 22:2c1359292de1 57 void get_distance(char message[]){
marcozecchini 22:2c1359292de1 58 //Ultrasonic measurement
marcozecchini 24:bb733d746bda 59 long distance = 401;
marcozecchini 24:bb733d746bda 60 while (distance >= 400) distance = sensor.distance();
marcozecchini 22:2c1359292de1 61 dprintf("distance %d \n",distance);
marcozecchini 22:2c1359292de1 62
marcozecchini 22:2c1359292de1 63 sprintf(message, "%d", distance);
marcozecchini 24:bb733d746bda 64 }
marcozecchini 24:bb733d746bda 65
marcozecchini 24:bb733d746bda 66 void get_temperature(char message[]){
marcozecchini 24:bb733d746bda 67 float value;
marcozecchini 24:bb733d746bda 68 hum_temp->get_temperature(&value);
marcozecchini 24:bb733d746bda 69 dprintf("temperature %f", value);
marcozecchini 24:bb733d746bda 70
marcozecchini 24:bb733d746bda 71 sprintf(message, "%f", value);
marcozecchini 24:bb733d746bda 72 dprintf(message);
marcozecchini 24:bb733d746bda 73
marcozecchini 25:18a57be7bb17 74 }
marcozecchini 25:18a57be7bb17 75
marcozecchini 25:18a57be7bb17 76 /* Print the orientation. */
marcozecchini 25:18a57be7bb17 77 bool send_orientation() {
marcozecchini 25:18a57be7bb17 78 uint8_t xl = 0;
marcozecchini 25:18a57be7bb17 79 uint8_t xh = 0;
marcozecchini 25:18a57be7bb17 80 uint8_t yl = 0;
marcozecchini 25:18a57be7bb17 81 uint8_t yh = 0;
marcozecchini 25:18a57be7bb17 82 uint8_t zl = 0;
marcozecchini 25:18a57be7bb17 83 uint8_t zh = 0;
marcozecchini 25:18a57be7bb17 84
marcozecchini 25:18a57be7bb17 85 acc_gyro->get_6d_orientation_xl(&xl);
marcozecchini 25:18a57be7bb17 86 acc_gyro->get_6d_orientation_xh(&xh);
marcozecchini 25:18a57be7bb17 87 acc_gyro->get_6d_orientation_yl(&yl);
marcozecchini 25:18a57be7bb17 88 acc_gyro->get_6d_orientation_yh(&yh);
marcozecchini 25:18a57be7bb17 89 acc_gyro->get_6d_orientation_zl(&zl);
marcozecchini 25:18a57be7bb17 90 acc_gyro->get_6d_orientation_zh(&zh);
marcozecchini 25:18a57be7bb17 91
marcozecchini 25:18a57be7bb17 92
marcozecchini 25:18a57be7bb17 93 if ( xl == 0 && yl == 0 && zl == 0 && xh == 0 && yh == 0 && zh == 1 ) {
marcozecchini 25:18a57be7bb17 94 return false;
marcozecchini 25:18a57be7bb17 95 }
marcozecchini 25:18a57be7bb17 96
marcozecchini 25:18a57be7bb17 97 else {
marcozecchini 25:18a57be7bb17 98 return true;
marcozecchini 25:18a57be7bb17 99 }
marcozecchini 19:7763501775e5 100 }