Creating an IoT platform for smart agriculture. Collecting data and sending them to thingspeak for analysis

Dependencies:   DHT11 LPS22HB LSM6DSL VL53L0X

Committer:
kaoriw
Date:
Fri Jun 08 12:16:19 2018 +0000
Revision:
1:469ea8167b80
Parent:
0:07ff689741d2
Uncomplete

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kaoriw 0:07ff689741d2 1 #include "Sensors.h"
kaoriw 0:07ff689741d2 2
kaoriw 0:07ff689741d2 3
kaoriw 0:07ff689741d2 4 using namespace std;
kaoriw 0:07ff689741d2 5
kaoriw 0:07ff689741d2 6 void enable_sensors()
kaoriw 0:07ff689741d2 7 {
kaoriw 0:07ff689741d2 8 /* Init all sensors with default params */
kaoriw 0:07ff689741d2 9 press_s.init(NULL);
kaoriw 0:07ff689741d2 10 acc_gyro.init(NULL);
kaoriw 0:07ff689741d2 11 range.init_sensor(VL53L0X_DEFAULT_ADDRESS);
kaoriw 0:07ff689741d2 12
kaoriw 0:07ff689741d2 13 /* Enable all sensors */
kaoriw 0:07ff689741d2 14 press_s.enable();
kaoriw 0:07ff689741d2 15 acc_gyro.enable_x();
kaoriw 0:07ff689741d2 16 acc_gyro.enable_g();
kaoriw 0:07ff689741d2 17 }
kaoriw 0:07ff689741d2 18
kaoriw 0:07ff689741d2 19 void get_values()
kaoriw 0:07ff689741d2 20 {
kaoriw 0:07ff689741d2 21 press_status = 0;
kaoriw 0:07ff689741d2 22
kaoriw 0:07ff689741d2 23 //Temperature sensor
kaoriw 0:07ff689741d2 24 temp = hum_temp_s.getCelsius();
kaoriw 0:07ff689741d2 25
kaoriw 0:07ff689741d2 26 //Humidity sensor
kaoriw 0:07ff689741d2 27 hum = hum_temp_s.getHumidity();
kaoriw 0:07ff689741d2 28
kaoriw 0:07ff689741d2 29
kaoriw 0:07ff689741d2 30
kaoriw 0:07ff689741d2 31 //Light sensor
kaoriw 0:07ff689741d2 32 light = light_s.read();
kaoriw 0:07ff689741d2 33
kaoriw 0:07ff689741d2 34 //Pressure sensor
kaoriw 0:07ff689741d2 35 if(press_s.get_pressure(&press) != 0)
kaoriw 0:07ff689741d2 36 {
kaoriw 0:07ff689741d2 37 printf("Error getting pressure\r\n");
kaoriw 0:07ff689741d2 38 press_status = VALUE_ERROR;
kaoriw 0:07ff689741d2 39 }
kaoriw 0:07ff689741d2 40
kaoriw 0:07ff689741d2 41 }
kaoriw 0:07ff689741d2 42
kaoriw 0:07ff689741d2 43 void get_motion_data(int dt, int* speed, int* prev_speed, int* prev_pos, int* prev_angle)
kaoriw 0:07ff689741d2 44 {
kaoriw 0:07ff689741d2 45
kaoriw 0:07ff689741d2 46 //Accelerometer
kaoriw 0:07ff689741d2 47 if(acc_gyro.get_x_axes(acc_axes) != 0)
kaoriw 0:07ff689741d2 48 {
kaoriw 0:07ff689741d2 49 printf("Error getting acceleration\n");
kaoriw 0:07ff689741d2 50 }
kaoriw 0:07ff689741d2 51
kaoriw 0:07ff689741d2 52 //Gyroscope
kaoriw 0:07ff689741d2 53 if(acc_gyro.get_g_axes(gyro_axes) != 0)
kaoriw 0:07ff689741d2 54 {
kaoriw 0:07ff689741d2 55 printf("Error getting data from gyroscope\r\n");
kaoriw 0:07ff689741d2 56 }
kaoriw 0:07ff689741d2 57
kaoriw 0:07ff689741d2 58 prev_speed[0] += acc_axes[0]*dt;
kaoriw 0:07ff689741d2 59 prev_speed[1] += acc_axes[1]*dt;
kaoriw 0:07ff689741d2 60 //prev_speed[2] += acc_axes[2]*dt;
kaoriw 0:07ff689741d2 61
kaoriw 0:07ff689741d2 62 *speed = (int)sqrt(pow(prev_speed[0],2.0)+pow(prev_speed[1],2.0)); //+pow(prev_speed[2],2.0)); //restrain to 2D
kaoriw 0:07ff689741d2 63
kaoriw 0:07ff689741d2 64 prev_pos[0] += prev_speed[0]*dt; //x
kaoriw 0:07ff689741d2 65 prev_pos[1] += prev_speed[1]*dt; //y
kaoriw 0:07ff689741d2 66
kaoriw 0:07ff689741d2 67 *prev_angle += gyro_axes[2]*dt; //rotation around z axis
kaoriw 0:07ff689741d2 68
kaoriw 0:07ff689741d2 69 }