From 67b1ba7ea0a86eac679eafc8c724fcb6bb4f8c4f Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sun, 21 Jun 2015 19:15:37 +0200 Subject: o Letting each sensor use the configured update interval. o Changing the ID of the "get value" command. It has to include the sensor index when used as a notification message. Dunno if this is the best strategy. I guess messages are cheap, but complexity is expensive. --- app.cpp | 92 +++++++++++++++++++++------------------------ app.h | 11 +++++- config.h | 6 +-- services.h | 1 - services_lock.h | 1 - trygvisio_soil_moisture.ino | 1 - 6 files changed, 53 insertions(+), 59 deletions(-) diff --git a/app.cpp b/app.cpp index cd38d66..81a9622 100644 --- a/app.cpp +++ b/app.cpp @@ -1,8 +1,6 @@ #include "app.h" -#include #include -#include #if SM_BOARD_VERSION == 1 #define SENSOR_COUNT 2 @@ -18,7 +16,7 @@ #define LED_PIN 13 -#define EEPROM_MAGIC 0x5a +#define EEPROM_MAGIC 0xa5 // See http://redbearlab.com/blendmicro/ for pins. struct sm_sensor { @@ -35,7 +33,7 @@ struct sm_sensor { #if SM_BOARD_VERSION == 1 { 8, 3, 5, 0, 200, 3000, 9, "Sensor #1"}, - { 10, 11, 12, 0, 200, 3000, 9, "Sensor #2"}, + { 10, 11, 12, 0, 200, 5000, 9, "Sensor #2"}, #elif SM_BOARD_VERSION == 2 { A0, 1, 2, 0, 200, 3000, 3, "wat"}, { A1, 3, 5, 0, 200, 3000, 4, "woot"}, @@ -48,8 +46,8 @@ static unsigned long next_update[SENSOR_COUNT]; // How often the connected light should toggle, in ms #define BLINK_WAIT 500 -static boolean connected; -static boolean blink; +static bool connected; +static bool blink; static unsigned long last_blink; static void setup_settings(); @@ -71,9 +69,11 @@ void sm_setup() { } void sm_on_connect() { + unsigned long now = millis(); + // Disable all updates until enabled. for (int i = 0; i < SENSOR_COUNT; i++) { - next_update[i] = 0; + next_update[i] = now + sensors[i].update_interval; } connected = true; @@ -88,13 +88,11 @@ void sm_on_disconnect() { } void sm_loop() { - static unsigned long last = 0, now; - - static uint8_t x = 10; + unsigned long now = millis(); - now = millis(); + // TODO: make the blink configurable. It's nice for debugging, bad for power usage when continuously connected - if (now - last_blink > BLINK_WAIT) { + if (now - last_blink >= BLINK_WAIT) { last_blink = now; if (connected) { blink = !blink; @@ -102,48 +100,41 @@ void sm_loop() { } } - if (now - last > 3000) { - last = now; - for (int i = 0; i < SENSOR_COUNT; i++) { - struct sm_sensor& s = sensors[i]; - - digitalWrite(s.d1_pin, HIGH); - digitalWrite(s.d2_pin, LOW); + // TODO: there is no reason to read the values unless someone are connected *and* have asked for notifications. - s.value = analogRead(s.a_pin); + int count = 0; + for (uint8_t i = 0; i < SENSOR_COUNT; i++) { + struct sm_sensor &s = sensors[i]; - if (i > 0) { - Serial.print(", "); - } - Serial.print("#"); - Serial.print(i, DEC); - Serial.print(" = "); - Serial.print(s.value, DEC); + if (now < next_update[i]) { + continue; } - Serial.println(); - } + next_update[i] += s.update_interval; - // TODO: implement proper notification - if (false && now - last > 3000) { - last = now; + digitalWrite(s.d1_pin, HIGH); + digitalWrite(s.d2_pin, LOW); - struct sm_res res; - res.code = SM_CMD_GET_VALUE; - res.get_value.value = x; + s.value = (uint16_t) analogRead(s.a_pin); - x++; - if(x == 0) { - x = 10; + if (count++ > 0) { + Serial.print(", "); } + Serial.print("#"); + Serial.print(i, DEC); + Serial.print(" = "); + Serial.print(s.value, DEC); -// if(x % 2 == 0) { -// notify_soil_moisture(res); -// } else { -// notify_battery_level(x); -// } + struct sm_res res; + res.code = SM_CMD_GET_VALUE; + res.get_value.sensor = i; + res.get_value.value = s.value; - notify_soil_moisture(res, sizeof(sm_get_value_res)); + notify_soil_moisture(res, sizeof(sm_get_value_res)); + } + + if (count > 0) { + Serial.println(); } } @@ -220,7 +211,9 @@ void write_res(struct sm_res const& res) { case SM_CMD_GET_VALUE: Serial.print("SM_CMD_GET_VALUE"); - Serial.print(": value="); + Serial.print(": sensor="); + Serial.print(res.get_value.sensor, DEC); + Serial.print(", value="); Serial.print(res.get_value.value, DEC); break; @@ -254,7 +247,7 @@ void write_res(struct sm_res const& res) { } Serial.println(); } -#endif +#endif // SM_DEBUG void on_soil_moisture_ctrl(uint8_t *data, uint8_t len) { struct sm_req *req = (struct sm_req *) data; @@ -356,13 +349,12 @@ void on_soil_moisture_ctrl(uint8_t *data, uint8_t len) { notify_soil_moisture(res, body_len); } - #if PERSISTENT_CONFIGURATION_SUPPORT == 1 static int store_string(int index, uint8_t len, char *chars) { EEPROM.write(index++, len); for (int i = 0; i < len; i++) { - EEPROM.write(index++, chars[i]); + EEPROM.write(index++, (uint8_t) chars[i]); } return 1 + len; @@ -373,7 +365,7 @@ static int load_string(int index, const uint8_t max_length, uint8_t& length, cha uint8_t len = min(length, max_length); for (int i = 0; i < len; i++) { - chars[i] = EEPROM.read(index++); + chars[i] = (char) EEPROM.read(index++); } return 1 + len; @@ -417,7 +409,6 @@ static void store_settings() { Serial.println("Settings saved"); } - static void setup_settings() { if (!load_settings()) { Serial.println("Could not load settings, storing defaults"); @@ -454,3 +445,4 @@ static void setup_settings() {} // vim: set ft=arduino: + diff --git a/app.h b/app.h index 6c48aa4..26e52cb 100644 --- a/app.h +++ b/app.h @@ -7,14 +7,18 @@ #define SENSOR_NAME_LEN 10 +// TODO: Randomize these numbers. There are no reason why they should be in order. + enum sm_cmd_code { SM_CMD_GET_SENSOR_COUNT = 1, - SM_CMD_GET_VALUE = 2, + // Used to be 2 + SM_CMD_GET_VALUE = 8, 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, + // 8 is used SM_CMD_FAIL = 255, }; @@ -29,7 +33,11 @@ struct sm_get_value_req { uint8_t sensor; }; +/** + * This includes the sensor id because it can be used as a notification too + */ struct sm_get_value_res { + uint8_t sensor; uint16_t value; }; @@ -126,4 +134,3 @@ void write_res(struct sm_res const& res); #endif // vim: set ft=arduino: - diff --git a/config.h b/config.h index e18a655..e8b9ef1 100644 --- a/config.h +++ b/config.h @@ -53,7 +53,7 @@ values are commented out, but some have default values // Default: 1 // #define WAIT_FOR_SERIAL_BEFORE_STARING ... -// If enabled the sensor's name will be persisted in EEPROM +// If enabled the settings for each sensor will be persisted in EEPROM // // Values: // 0: disabled @@ -74,8 +74,7 @@ Do not touch stuff in this section, it only sets default values. #endif #ifndef PERSISTENT_CONFIGURATION_SUPPORT -// Default to off, the code is not fully functional yet -#define PERSISTENT_CONFIGURATION_SUPPORT 0 +#define PERSISTENT_CONFIGURATION_SUPPORT 1 #endif #ifndef SM_DEBUG @@ -83,4 +82,3 @@ Do not touch stuff in this section, it only sets default values. #endif #endif - diff --git a/services.h b/services.h index bace5e1..f8f1ff4 100644 --- a/services.h +++ b/services.h @@ -184,4 +184,3 @@ } #endif - diff --git a/services_lock.h b/services_lock.h index a0e8801..57af1da 100644 --- a/services_lock.h +++ b/services_lock.h @@ -188,4 +188,3 @@ } #endif - diff --git a/trygvisio_soil_moisture.ino b/trygvisio_soil_moisture.ino index f824106..621a64d 100644 --- a/trygvisio_soil_moisture.ino +++ b/trygvisio_soil_moisture.ino @@ -418,4 +418,3 @@ void notify_soil_moisture(const struct sm_res& res, uint8_t body_len) { } } } - -- cgit v1.2.3