-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsenseBox-bike-mcus2.ino
178 lines (151 loc) · 4.61 KB
/
senseBox-bike-mcus2.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// First sketch for the senseBox:bike working with the second generation for the MCU
// SPS is disabled
//
#include <SenseBoxBLE.h>
#include <Wire.h>
#include <SDConfig.h>
#include <SD.h>
#include <Adafruit_HDC1000.h>
#include <sps30.h>
#include <NMEAGPS.h>
#include <Adafruit_MPU6050.h>
#include <NewPing.h> // http://librarymanager/All#NewPing
#include <vl53l8cx_class.h>
#define TRIGGER_LEFT 1
#define ECHO_LEFT 2
#define MAX_DISTANCE_A 400
NewPing sonarA(TRIGGER_LEFT, ECHO_LEFT, MAX_DISTANCE_A);
VL53L8CX sensor_vl53l8cx_top(&Wire, -1, -1);
static NMEAGPS gps;
// https://github.com/SlashDevin/NeoGPS/blob/master/extras/doc/Data%20Model.md
static gps_fix fix;
// Accelerometer and Gyroscope
Adafruit_MPU6050 mpu;
// Temperatur and humidity
Adafruit_HDC1000 HDC = Adafruit_HDC1000();
int period = 1000;
unsigned long time_now = 0;
bool recording = false;
long last;
long time_start = 0;
float temp = 0;
float humi = 0;
float pm1 = 0;
float pm25 = 0;
float pm4 = 0;
float pm10 = 0;
float accX = 0;
float accY = 0;
float accZ = 0;
float gps_lat = 0;
float gps_lng = 0;
float gps_spd = 0;
float speed;
float latitude;
float longitude;
float dist_l = 0;
struct sps30_measurement m;
float sumAccZ;
float sumAccY;
float sumAccX;
int temperatureCharacteristic = 0;
int humidityCharacteristic = 0;
int PMCharacteristic = 0;
int accelerationCharacteristic = 0;
int GPSCharacteristic = 0;
int distanceCharacteristic = 0;
int GPSFixCharacteristisc = 0;
float status;
String name;
// init all sensors
// todo: give feedback through LED if all is working?
void initsSensors() {
Serial.print("Ultrasonic...");
// initUltrasonic();
initVL53L8CX();
// ATTENTION! SPS Disabled for essen-auf-raedern
// Serial.print("SPS30...");
// initSPS(); Serial.println("done!");
Serial.print("MPU6050...");
initBMX();
Serial.println("done!");
Serial.print("GPS...");
Serial.println("done!");
Serial.print("HDC1080...");
if (!HDC.begin()) {
Serial.println("Couldn't find HDC1080!");
}
Serial.println("done!");
Serial.print("Propeller...");
pinMode(3, OUTPUT);
delay(100);
digitalWrite(3, HIGH);
Serial.println("done!");
}
// set measurements for acceleration, distance, humidity and temperature
void setMeasurements() {
getAccAmplitudes(&sumAccX, &sumAccY, &sumAccZ);
handleDistanceVL53L8CX();
temp = HDC.readTemperature();
humi = HDC.readHumidity();
}
// starts bluetooth and sets the name according to the Bluetooth Bee
// TODO: can the ID be seen on the hardware?
void startBluetooth() {
SenseBoxBLE::start("senseBox-BLE");
delay(1000);
name = "senseBox:bike [" + SenseBoxBLE::getMCUId() + "]";
SenseBoxBLE::setName(name);
Serial.println(name);
delay(1000);
Serial.print("Adding BLE characteristics...");
SenseBoxBLE::addService("CF06A218F68EE0BEAD048EBC1EB0BC84");
temperatureCharacteristic = SenseBoxBLE::addCharacteristic("2CDF217435BEFDC44CA26FD173F8B3A8");
humidityCharacteristic = SenseBoxBLE::addCharacteristic("772DF7EC8CDC4EA986AF410ABE0BA257");
PMCharacteristic = SenseBoxBLE::addCharacteristic("7E14E07084EA489FB45AE1317364B979");
accelerationCharacteristic = SenseBoxBLE::addCharacteristic("B944AF10F4954560968F2F0D18CAB522");
GPSCharacteristic = SenseBoxBLE::addCharacteristic("8EDF8EBB12464329928DEE0C91DB2389");
distanceCharacteristic = SenseBoxBLE::addCharacteristic("B3491B60C0F34306A30D49C91F37A62B");
GPSFixCharacteristisc = SenseBoxBLE::addCharacteristic("PPFSSPSPXGIPIIPFFXXPSXSFSSXSPXFF");
Serial.println("done!");
}
// sends phenomenas to the given BT characteristics
void writeToBluetooth() {
bool connected = SenseBoxBLE::write(temperatureCharacteristic, temp);
SenseBoxBLE::write(humidityCharacteristic, humi);
SenseBoxBLE::write(PMCharacteristic, pm1, pm25, pm4, pm10);
SenseBoxBLE::write(accelerationCharacteristic, sumAccX, sumAccY, sumAccZ);
SenseBoxBLE::write(GPSCharacteristic, latitude, longitude, speed);
SenseBoxBLE::write(distanceCharacteristic, dist_l);
SenseBoxBLE::write(GPSFixCharacteristisc, status);
}
void resetVariables(){
sumAccX = 0;
sumAccY = 0;
sumAccZ = 0;
}
void setup() {
Serial.begin(9600);
Serial.println("Starting Sketch!");
Serial.println("Initializing sensors..");
initsSensors();
Serial.println("Sensor init done! ");
startBluetooth();
Serial.println("Bluetooth done!");
delay(500);
}
void loop() {
SenseBoxBLE::poll();
time_start = millis();
// set global variables for measurements
setMeasurements();
// write measurements to bluetooth
writeToBluetooth();
// reset acceleration values
resetVariables();
time_now = millis();
while (millis() < time_start + period) {
SenseBoxBLE::poll();
delay(5);
}
}