-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
112 lines (93 loc) · 2.49 KB
/
main.c
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "fmt.h"
#include "pm_layered.h"
#include "board.h"
#include "ztimer.h"
#include "periph/gpio.h"
#include "lorawan.h"
#include "cayenne_lpp.h"
#include "periph/adc.h"
#include "sensors.h"
#define ENABLE_DEBUG 0
#include "debug.h"
/* Messages are sent every SLEEP_DURATION to respect the duty cycle on each channel */
#define SLEEP_DURATION (600U)
#define SENSOR_SWITCH GPIO_PIN(PB, 22)
static bool initializedADC = false;
void initSensorSwitch(void)
{
gpio_init(SENSOR_SWITCH, GPIO_OUT);
gpio_clear(SENSOR_SWITCH);
}
void prepareSleep(void)
{
gpio_clear(TX_OUTPUT_SEL_PIN);
gpio_clear(TX_SWITCH_PWR_PIN);
gpio_clear(TCXO_PWR_PIN);
gpio_clear(SENSOR_SWITCH);
}
void postSleep(void)
{
gpio_set(TX_OUTPUT_SEL_PIN);
gpio_set(TX_SWITCH_PWR_PIN);
gpio_set(TCXO_PWR_PIN);
gpio_set(SENSOR_SWITCH);
ztimer_spin(ZTIMER_MSEC, 300);
}
void rtc_cb(void *arg)
{
(void)arg;
DEBUG_PUTS("Wakeup from sleep...");
}
void sleepUtilNextAlarm(void)
{
ztimer_t timeout = {.callback = rtc_cb, .arg = "Hello ztimer!"};
ztimer_set(ZTIMER_MSEC, &timeout, SLEEP_DURATION * 1000);
DEBUG("Go to sleep for %u Seconds\n", SLEEP_DURATION);
pm_set(1);
}
void gatherSensorData(cayenne_lpp_t *lpp)
{
if (!initializedADC)
{
for (u_int8_t line = 0; line < ADC_NUMOF; line++)
{
if (adc_init(ADC_LINE(line)) < 0)
{
DEBUG("Initialization of ADC_LINE(%u) failed\n", line);
return;
}
}
initializedADC = true;
}
float vBatt = measureVbatt(ADC_LINE(0));
float soilHumidity = measureSoilHumidity(ADC_LINE(2));
float soilTemperature = measureSoilTemperature(ADC_LINE(1));
DEBUG("Vbatt: %f\nHum: %f\nTemp: %f\n", vBatt, soilHumidity, soilTemperature);
cayenne_lpp_add_analog_input(lpp, 1, vBatt);
cayenne_lpp_add_relative_humidity(lpp, 3, soilHumidity);
cayenne_lpp_add_temperature(lpp, 5, soilTemperature);
}
semtech_loramac_t loramac;
int main(void)
{
DEBUG_PUTS("=====================================");
DEBUG_PUTS(CONFIG_LORAMAC_DEV_EUI_DEFAULT);
initSensorSwitch();
ztimer_init();
ztimer_spin(ZTIMER_MSEC, 300);
pm_unblock(0);
joinNetwork(&loramac, 3);
while (true)
{
cayenne_lpp_t lpp = {0};
postSleep();
gatherSensorData(&lpp);
sendData(&loramac, &lpp);
prepareSleep();
sleepUtilNextAlarm();
}
return 0;
}