-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.cpp
96 lines (76 loc) · 2 KB
/
main.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
#include "mbed.h"
#include "mbed-trace/mbed_trace.h"
#include "mbedtls/debug.h"
#include "stm32l475e_iot01_tsensor.h"
#include "stm32l475e_iot01_hsensor.h"
#include "stm32l475e_iot01_psensor.h"
#include "core/cloud.h"
#define AWS_IOT_UPDATE_FREQUENCY 60
DigitalOut led(LED1);
/*
Blinks or not
val 1 = blink, 0 = don't
*/
void blink(int val){
if(val == -1){
for(int x=0; x<4; x++){
led = 1;
wait(0.8);
led = 0;
wait(0.8);
}
}
if(val == 0){
led = 0;
wait(0.5);
}
if(val == 1){
led = 1;
wait(0.5);
}
}
/* connect services */
int connect(){
int result = 0;
int cloud_status = cloud_connect();
if(cloud_status == 1){
result = 1;
}
return result;
}
/* just the funky start */
int main(){
mbed_trace_init();
printf("IoT device starting\r\n");
/* define sensor vars */
float temperature = 0;
float humidity = 0;
float pressure = 0;
/* initalise sensors */
BSP_TSENSOR_Init();
BSP_HSENSOR_Init();
BSP_PSENSOR_Init();
/* init cloud (connect wifi) */
cloud_init();
/* connect to network */
int connected = connect();
while(1) {
if(connected == 1){
/* all good here */
blink(1);
/* get current sensor data */
temperature = BSP_TSENSOR_ReadTemp();
humidity = BSP_HSENSOR_ReadHumidity();
pressure = BSP_PSENSOR_ReadPressure();
/* send message to cloud */
cloud_send(temperature,humidity,pressure);
/* wait secs as defined */
wait(AWS_IOT_UPDATE_FREQUENCY);
}else{
/* give it time */
wait(2);
/* network is off */
blink(-1);
}
}
}