Smartage application

Dependencies:   BufferedSerial SX1276GenericLib USBDeviceHT mbed Crypto X_NUCLEO_IKS01A2

Fork of STM32L0_LoRa by Helmut Tschemernjak

Committer:
marcozecchini
Date:
Mon Sep 17 22:16:48 2018 +0000
Revision:
34:8393ded26b4f
Parent:
32:c22ce5fdacbf
Bug fixed 4

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