From 08f269afd4926edca9cdfd00c3d15d419e6c87a4 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sun, 4 Jan 2015 23:43:14 +0100 Subject: o Adding an operation to set the wanted update interval. Useful for debugging to stream values and later devices where this can control how often the values are broadcasted. --- README.md | 4 +++- app.cpp | 46 +++++++++++++++++++++++++++++++++------ app.h | 52 ++++++++++++++++++++++++++++----------------- trygvisio_soil_moisture.ino | 20 ++++++++++------- 4 files changed, 86 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index 7e3bfc0..12ff308 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,9 @@ Included characteristics: ## Gauge Control: trygvis.io + 0x0004 -## Soil Moisture Level: trygvis.io + 0x0011 +## Soil Moisture Control: trygvis.io + 0x0011 + + Format: two bytes of data. Byte #1: sensor id, byte #2: sensor level (unsigned int, 0-255). diff --git a/app.cpp b/app.cpp index b72b07d..19d7285 100644 --- a/app.cpp +++ b/app.cpp @@ -9,24 +9,37 @@ #define SENSOR_COUNT 1 -// See http://redbearlab.com/blendmicro/ +// See http://redbearlab.com/blendmicro/ for pins. struct sm_sensor { uint8_t pin; uint8_t value; uint16_t warning_value; + uint16_t update_interval; uint8_t name_length; uint8_t name[SENSOR_NAME_LEN]; } sensors[SENSOR_COUNT] = { - {9, 0, 10, 9, 'sensor #1'}, + // Pin, Value, Warning Value, Update Interval, Length of Name, Name + { 9, 0, 10, 0, 9, 'sensor #1'}, }; -void on_loop() { +static unsigned long next_update[SENSOR_COUNT]; + +void sm_on_connect() { + // Disable all updates until enabled. + for (int i = 0; i < SENSOR_COUNT; i++) { + next_update[i] = 0; + } +} + +void sm_on_loop() { static unsigned long last = 0, now; static uint8_t x = 10; now = millis(); - if (now - last > 3000) { + + // TODO: implement proper notification + if (false || now - last > 3000) { last = now; struct sm_res res; @@ -43,6 +56,7 @@ void on_loop() { // } else { // notify_battery_level(x); // } + notify_soil_moisture(res, sizeof(sm_get_value_res)); } } @@ -97,6 +111,12 @@ void write_req(struct sm_req const& req) { Serial.print("SM_CMD_GET_SENSOR_NAME"); break; + case SM_CMD_SET_UPDATE_INTERVAL: + Serial.print("SM_CMD_SET_UPDATE_INTERVAL"); + Serial.print(": interval_in_seconds="); + Serial.print(req.set_update_interval.interval_in_seconds, DEC); + break; + default: Serial.print("Unknown command!"); } @@ -141,6 +161,10 @@ void write_res(struct sm_res const& res) { write_name(res.get_sensor_name.name, res.get_sensor_name.length); break; + case SM_CMD_SET_UPDATE_INTERVAL: + Serial.print("SM_CMD_SET_UPDATE_INTERVAL"); + break; + default: Serial.print("Unknown command!"); } @@ -215,7 +239,6 @@ void on_soil_moisture_ctrl(uint8_t *data, uint8_t len) { case SM_CMD_GET_SENSOR_NAME: body_len = sizeof(sm_get_sensor_name_res); sensor = req->get_sensor_name.sensor; - Serial.print("sensor="); Serial.print(sensor, DEC); if (sensor < SENSOR_COUNT) { res.get_sensor_name.length = sensors[sensor].name_length; @@ -225,13 +248,22 @@ void on_soil_moisture_ctrl(uint8_t *data, uint8_t len) { } break; + case SM_CMD_SET_UPDATE_INTERVAL: + body_len = sizeof(sm_set_update_interval_res); + sensor = req->set_update_interval.sensor; + + if (sensor < SENSOR_COUNT) { + sensors[sensor].update_interval = req->set_update_interval.interval_in_seconds; + } else { + res.code = SM_CMD_FAIL; + } + break; + default: res.code = SM_CMD_FAIL; break; } - Serial.println(); - if (res.code == SM_CMD_FAIL) { body_len = 0; } diff --git a/app.h b/app.h index e13478e..ee9c1cd 100644 --- a/app.h +++ b/app.h @@ -7,13 +7,14 @@ #define SENSOR_NAME_LEN 10 enum sm_cmd_code { - SM_CMD_GET_SENSOR_COUNT = 1, - SM_CMD_GET_VALUE = 2, - SM_CMD_SET_WARNING_VALUE = 3, - SM_CMD_GET_WARNING_VALUE = 4, - SM_CMD_SET_SENSOR_NAME = 5, - SM_CMD_GET_SENSOR_NAME = 6, - SM_CMD_FAIL = 255, + SM_CMD_GET_SENSOR_COUNT = 1, + SM_CMD_GET_VALUE = 2, + SM_CMD_SET_WARNING_VALUE = 3, + SM_CMD_GET_WARNING_VALUE = 4, + SM_CMD_SET_SENSOR_NAME = 5, + SM_CMD_GET_SENSOR_NAME = 6, + SM_CMD_SET_UPDATE_INTERVAL = 7, + SM_CMD_FAIL = 255, }; struct sm_get_sensor_count_req { @@ -65,15 +66,24 @@ struct sm_get_sensor_name_res { uint8_t name[SENSOR_NAME_LEN]; }; +struct sm_set_update_interval_req { + uint8_t sensor; + uint8_t interval_in_seconds; +}; + +struct sm_set_update_interval_res { +}; + struct sm_req { uint8_t code; union { - struct sm_get_sensor_count_req get_sensor_count; - struct sm_get_value_req get_value; - struct sm_set_warning_value_req set_warning_value; - struct sm_get_warning_value_req get_warning_value; - struct sm_set_sensor_name_req set_sensor_name; - struct sm_get_sensor_name_req get_sensor_name; + struct sm_get_sensor_count_req get_sensor_count; + struct sm_get_value_req get_value; + struct sm_set_warning_value_req set_warning_value; + struct sm_get_warning_value_req get_warning_value; + struct sm_set_sensor_name_req set_sensor_name; + struct sm_get_sensor_name_req get_sensor_name; + struct sm_set_update_interval_req set_update_interval; }; }; @@ -86,16 +96,18 @@ struct sm_res { // body union { - struct sm_get_sensor_count_res get_sensor_count; - struct sm_get_value_res get_value; - struct sm_set_warning_value_res set_warning_value; - struct sm_get_warning_value_res get_warning_value; - struct sm_set_sensor_name_res set_sensor_name; - struct sm_get_sensor_name_res get_sensor_name; + struct sm_get_sensor_count_res get_sensor_count; + struct sm_get_value_res get_value; + struct sm_set_warning_value_res set_warning_value; + struct sm_get_warning_value_res get_warning_value; + struct sm_set_sensor_name_res set_sensor_name; + struct sm_get_sensor_name_res get_sensor_name; + struct sm_set_update_interval_res set_update_interval; }; }; -void on_loop(); +void sm_on_connect(); +void sm_on_loop(); //boolean tx_soil_moisture(struct sm_res& res); void notify_soil_moisture(const struct sm_res& res, uint8_t body_len); diff --git a/trygvisio_soil_moisture.ino b/trygvisio_soil_moisture.ino index 9d9f9f9..ecadbdf 100644 --- a/trygvisio_soil_moisture.ino +++ b/trygvisio_soil_moisture.ino @@ -182,6 +182,8 @@ static void aci_loop() { // Get the device version of the nRF8001 and store it in the Hardware Revision String. // This will trigger a ACI_CMD_GET_DEVICE_VERSION. lib_aci_device_version(); + + sm_on_connect(); break; case ACI_EVT_PIPE_STATUS: @@ -329,7 +331,7 @@ void loop() { } } - on_loop(); + sm_on_loop(); } static void show_pipes() { @@ -378,17 +380,15 @@ void notify_battery_level(uint8_t value) { } void notify_soil_moisture(const struct sm_res& res, uint8_t body_len) { -#if SM_DEBUG == 1 - write_res(res); -#endif uint8_t *data = (uint8_t *)&res; uint8_t len = SM_RES_HEADER_SIZE + body_len; - Serial.print(F("notify_soil_moisture, code=")); - Serial.print(res.code, DEC); - Serial.print(F(", body_len=")); - Serial.println(body_len, DEC); +// Serial.print(F("notify_soil_moisture, code=")); +// Serial.print(res.code, DEC); +// Serial.print(F(", body_len=")); +// Serial.println(body_len, DEC); + // Serial.print(F("aci_state.data_credit_available=")); // Serial.println(aci_state.data_credit_available, DEC); @@ -400,6 +400,10 @@ void notify_soil_moisture(const struct sm_res& res, uint8_t body_len) { boolean sent = lib_aci_send_data(pipe_tx, data, len); if (sent) { aci_state.data_credit_available--; + +#if SM_DEBUG == 1 + write_res(res); +#endif } } } -- cgit v1.2.3