BLE sample kinda works. Pending calibration
This commit is contained in:
parent
339e5bebe9
commit
5510fd963c
4 changed files with 35 additions and 14 deletions
|
|
@ -14,10 +14,18 @@ config PRST_NETWORK_BLE
|
|||
|
||||
endchoice # PRST_NETWORK
|
||||
|
||||
config PRST_SLEEP_DURATION_SEC
|
||||
int "Sleep duration in seconds"
|
||||
default 1
|
||||
|
||||
###
|
||||
### BLE configs
|
||||
###
|
||||
|
||||
config PRST_BLE_ADV_DURATION_SEC
|
||||
int "Advertising duration in seconds"
|
||||
default 1
|
||||
|
||||
choice PRST_BLE_ENCODING
|
||||
prompt "b-parasite BLE encoding"
|
||||
depends on PRST_NETWORK_BLE
|
||||
|
|
|
|||
|
|
@ -35,8 +35,8 @@
|
|||
|
||||
soil_pwm: soil_pwm {
|
||||
compatible = "pwm-fixed";
|
||||
pwms = <&pwm0 0 PWM_MSEC(100) PWM_POLARITY_NORMAL>;
|
||||
pulse = <PWM_MSEC(50)>;
|
||||
pwms = <&pwm0 0 PWM_USEC(2) PWM_POLARITY_NORMAL>;
|
||||
pulse = <PWM_USEC(1)>;
|
||||
};
|
||||
|
||||
ctrl {
|
||||
|
|
@ -95,10 +95,12 @@
|
|||
#address-cells = <1>;
|
||||
#size-cells = <0>;
|
||||
status = "ok";
|
||||
|
||||
// Soil.
|
||||
channel@0 {
|
||||
reg = <0>;
|
||||
zephyr,gain = "ADC_GAIN_1_6";
|
||||
zephyr,reference = "ADC_REF_INTERNAL";
|
||||
zephyr,reference = "ADC_REF_VDD_1_4";
|
||||
zephyr,acquisition-time = <ADC_ACQ_TIME_DEFAULT>;
|
||||
// P0.03.
|
||||
zephyr,input-positive = <NRF_SAADC_AIN1>;
|
||||
|
|
@ -106,6 +108,7 @@
|
|||
|
||||
};
|
||||
|
||||
// Photo.
|
||||
channel@1 {
|
||||
reg = <1>;
|
||||
zephyr,gain = "ADC_GAIN_1_6";
|
||||
|
|
@ -116,6 +119,7 @@
|
|||
zephyr,resolution = <10>;
|
||||
};
|
||||
|
||||
// Battery.
|
||||
channel@2 {
|
||||
reg = <2>;
|
||||
zephyr,gain = "ADC_GAIN_1_6";
|
||||
|
|
|
|||
|
|
@ -41,16 +41,12 @@ int main(void) {
|
|||
RET_IF_ERR(prst_ble_adv_set_data(&sensors));
|
||||
RET_IF_ERR(prst_ble_adv_start());
|
||||
|
||||
k_sleep(K_SECONDS(2));
|
||||
k_sleep(K_SECONDS(CONFIG_PRST_BLE_ADV_DURATION_SEC));
|
||||
k_msleep(200);
|
||||
k_busy_wait(200 * 1000);
|
||||
|
||||
RET_IF_ERR(prst_ble_adv_stop());
|
||||
|
||||
k_sleep(K_SECONDS(2));
|
||||
|
||||
prst_led_flash(1);
|
||||
|
||||
// Example: go to deep sleep.
|
||||
pm_state_force(0u, &(struct pm_state_info){PM_STATE_SOFT_OFF, 0, 0});
|
||||
k_sleep(K_SECONDS(2));
|
||||
k_sleep(K_SECONDS(CONFIG_PRST_SLEEP_DURATION_SEC));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,19 +1,23 @@
|
|||
#include "adc.h"
|
||||
|
||||
#include <drivers/adc.h>
|
||||
#include <drivers/gpio.h>
|
||||
#include <drivers/pwm.h>
|
||||
#include <logging/log.h>
|
||||
#include <zephyr/zephyr.h>
|
||||
|
||||
#include "macros.h"
|
||||
|
||||
LOG_MODULE_REGISTER(adc, LOG_LEVEL_WRN);
|
||||
LOG_MODULE_REGISTER(adc, LOG_LEVEL_DBG);
|
||||
|
||||
// PWM spec for square wave. Input to the soil sensing circuit.
|
||||
static const struct pwm_dt_spec soil_pwm_dt =
|
||||
PWM_DT_SPEC_GET(DT_NODELABEL(soil_pwm));
|
||||
static const uint32_t pulse = DT_PROP(DT_NODELABEL(soil_pwm), pulse);
|
||||
|
||||
struct gpio_dt_spec fast_disch_dt =
|
||||
GPIO_DT_SPEC_GET(DT_NODELABEL(fast_disch), gpios);
|
||||
|
||||
// Shared buffer and adc_sequennce.
|
||||
static int16_t buf;
|
||||
static struct adc_sequence sequence = {
|
||||
|
|
@ -36,8 +40,8 @@ static inline float get_soil_moisture_percent(float battery_voltage,
|
|||
const double x = battery_voltage;
|
||||
const double dry = -12.9 * x * x + 111 * x + 228;
|
||||
const double wet = -5.71 * x * x + 60.2 * x + 126;
|
||||
LOG_DBG("Batt: %.2f", x);
|
||||
LOG_DBG("Dry: %.2f | wet: %.2f", dry, wet);
|
||||
LOG_DBG("Raw %u | Batt: %.2f | Dry: %.2f | Wet: %.2f", raw_adc_output, x, dry,
|
||||
wet);
|
||||
return (raw_adc_output - dry) / (wet - dry);
|
||||
}
|
||||
|
||||
|
|
@ -64,6 +68,9 @@ int prst_adc_init() {
|
|||
for (int i = 0; i < sizeof(all_specs) / sizeof(all_specs[0]); i++) {
|
||||
RET_IF_ERR(adc_channel_setup_dt(all_specs[i]));
|
||||
}
|
||||
|
||||
RET_IF_ERR(!device_is_ready(fast_disch_dt.port));
|
||||
return gpio_pin_configure_dt(&fast_disch_dt, GPIO_OUTPUT);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
@ -73,6 +80,9 @@ int prst_adc_batt_read(prst_adc_read_t* out) {
|
|||
}
|
||||
|
||||
int prst_adc_soil_read(float battery_voltage, prst_adc_soil_moisture_t* out) {
|
||||
// Enable fast discharge circuit.
|
||||
RET_IF_ERR(gpio_pin_set_dt(&fast_disch_dt, 1));
|
||||
|
||||
// Start PWM.
|
||||
RET_IF_ERR(pwm_set_dt(&soil_pwm_dt, soil_pwm_dt.period, pulse));
|
||||
|
||||
|
|
@ -83,6 +93,9 @@ int prst_adc_soil_read(float battery_voltage, prst_adc_soil_moisture_t* out) {
|
|||
// Stop PWM.
|
||||
RET_IF_ERR(pwm_set_dt(&soil_pwm_dt, 0, 0));
|
||||
|
||||
// Turn off fast discharge circuit.
|
||||
RET_IF_ERR(gpio_pin_set_dt(&fast_disch_dt, 0));
|
||||
|
||||
out->percentage = get_soil_moisture_percent(battery_voltage, buf);
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue