-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain2.cpp
197 lines (164 loc) · 5.56 KB
/
main2.cpp
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include "DigitalOut.h"
#include "InterruptIn.h"
#include "PinNames.h"
#include "mbed.h"
#include "ISM43362Interface.h"
#include "TCPSocket.h"
#include "stm32l475e_iot01_tsensor.h"
#include "stm32l475e_iot01_accelero.h"
#include <cstdio>
#include <string>
const char* ssid = "Hyperbola";
const char* password = "Rjak232177*";
ISM43362Interface wifi;
TCPSocket socket;
SocketAddress addr;
// Set up flag and interrupt handler for user input
volatile int send_sig = 0;
InterruptIn button(BUTTON1);
// Set up flag and ticker for LED
volatile int acknowledge = 0;
DigitalOut led(LED1);
LowPowerTicker led_toggle;
// Set up flag for which state the board is in depending on its orientation
volatile int position_state = 0;
// Set up flag for whether or not the last orientation of board was vertical
volatile bool vertical_last = 0;
// Set up flag for whether or not the last trigger was high temperature
volatile bool high_temp_last = 0;
int connect_to_help(ISM43362Interface *wifi, SocketAddress * addr, TCPSocket * socket, int temp_high)
{
// Open a socket on the network interface, and create a TCP connection to localhost
if (socket->open(wifi) != NSAPI_ERROR_OK){
printf("Socket opening failed\n");
return -1;
}
// localhost port number is 5000
addr->set_port(5000);
if (socket->connect(*addr) != NSAPI_ERROR_OK) {
printf("Failed to connect to server\n");
return -1;
}
printf("Successfully connected. Begininning help packet transfer.\n");
const char* message;
// Send a sos message that contains information of what triggered the SOS
if (temp_high == 0) {
message = "SOS: User Triggered";
}
else {
message = "SOS: Temperature Triggered";
}
int sentNum = socket->send(message, strlen(message));
if (sentNum < 0) {
printf("Failed to send\n");
} else {
printf("Message sent successfully\n");
}
char buffer[1024] = {0};
nsapi_size_or_error_t receivedNum = socket->recv(buffer, sizeof(buffer));
if (receivedNum < 0) {
printf("Did not receive acknowledgement\n");
} else {
printf("Received acknowledgement!\n");
acknowledge = 1;
}
socket->close();
return 0;
}
// Set signal flag to 1
void signal_interrupt_handler() {
if (position_state == 2 || position_state == 3){
send_sig = 1;
}
}
// Toggle the LED
void toggle_led() {
led = !led;
}
bool check_device_vertical(){
int16_t xyz_counts[3] = {0};
BSP_ACCELERO_AccGetXYZ(xyz_counts);
int x_threshold = 850;
return abs(xyz_counts[0]) > x_threshold;
}
void update_position_state(){
bool curr_vertical = check_device_vertical();
if (curr_vertical != vertical_last){
if (curr_vertical && (position_state == 0 || position_state == 2)){
position_state += 1;
}
else if (!curr_vertical && (position_state == 1 || position_state == 3)){
position_state += 1;
}
}
vertical_last = curr_vertical;
}
int main() {
// Attach interrupt handler for button
button.fall(&signal_interrupt_handler);
// Initialize temperature sensor
uint32_t temp_init = BSP_TSENSOR_Init();
// Initialize accelerometer sensor
uint32_t acc_init = BSP_ACCELERO_Init();
// Print if temperature sensor was successfully initialized
if (temp_init != TSENSOR_OK) {
printf("Error initializing temperature sensor\n");
}
else {
printf("Temperature sensor successfully initialized\n");
}
// Print if accelerometer sensor was successfully initialized
if (acc_init != ACCELERO_OK) {
printf("Error initializing accelerometer sensor\n");
}
else {
printf("Accelerometer sensor successfully initialized\n");
}
printf("Beginning connection\n");
printf("Connecting to %s...\n", ssid);
if (wifi.connect(ssid, password, NSAPI_SECURITY_WPA_WPA2) != 0) {
printf("Failed to connect to WiFi\n");
return -1;
}
printf("Connected to WiFi\n");
// Use IP address of server host instead of 10.88.111.9
if (static_cast<NetworkInterface*>(&wifi)->gethostbyname("10.88.111.9", &addr) != NSAPI_ERROR_OK) {
printf("DNS failed\n");
return -1;
}
while(true){
// We sleep for 2 minutes and enter low power mode after which we will recheck the temperature -- allows us to operate in low-power mode
while (send_sig == 0 && (BSP_TSENSOR_ReadTemp() < 60.0 || high_temp_last == 1)) {
printf("Sleeping until either user triggers SOS or auto-detected based on temperature\n");
if (position_state == 4){
position_state = 0;
}
update_position_state();
ThisThread::sleep_for(2s);
}
int temp_high = 0;
// Print what caused an SOS to be triggered
if (send_sig != 0) {
printf("User triggered SOS\n");
high_temp_last = 0;
}
else {
printf("Temperature reaching unsafe values (> 59°C)\n");
high_temp_last = 1;
temp_high = 1;
}
int num = connect_to_help(&wifi, &addr, &socket, temp_high);
if (num != 0){
printf("Connection Failed\n");
return num;
}
// Once we receive acknowledgement we toggle the LED. We use a low power ticker so that we can go to sleep
if (acknowledge == 1) {
printf("Toggling LED and going to sleep\n");
led_toggle.attach(&toggle_led, 1s);
}
send_sig = 0;
// Going to sleep now
sleep();
}
}