SysBee v1

Dependencies:   C027_Support mbed

Fork of App_Pese_Ruche_SYSBEE by Sysbee

Committer:
Cosi
Date:
Thu Sep 13 09:03:46 2018 +0000
Revision:
9:4c09643fbfb9
Parent:
8:3af661bcfcec
Version with get_season function

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bennettmatt1977 0:15a06b50e14e 1 #include "mbed.h"
maxelior 1:5639bc1208d5 2 #include <string.h>
thomaspotier 5:d2702cbe5afe 3 //#include <LowPowerTimeout.h>
bennettmatt1977 0:15a06b50e14e 4
bennettmatt1977 0:15a06b50e14e 5 #include "GPS.h"
bennettmatt1977 0:15a06b50e14e 6 #include "MDM.h"
thomaspotier 4:bee94a4094b6 7
thomaspotier 4:bee94a4094b6 8 // sim pin code
maxelior 1:5639bc1208d5 9 #define SIMPIN "1234"
bennettmatt1977 0:15a06b50e14e 10 #define APN NULL
bennettmatt1977 0:15a06b50e14e 11 #define USERNAME NULL
maxelior 1:5639bc1208d5 12 #define PASSWORD NULL
maxelior 2:57d040afa937 13
thomaspotier 4:bee94a4094b6 14 // size of the average buffer (more means more averaged)
thomaspotier 5:d2702cbe5afe 15 #define AVERAGING_SIZE 500
thomaspotier 4:bee94a4094b6 16 // time between each capture in us
thomaspotier 5:d2702cbe5afe 17 #define AVERAGING_TIME 500
maxelior 2:57d040afa937 18
Cosi 7:95ddf8fa6260 19 /*
Cosi 7:95ddf8fa6260 20 // time between each measure in s
Cosi 6:b69c4870db2c 21 #define MEASURE_INTERVAL (10 * 60)
thomaspotier 5:d2702cbe5afe 22 // time between each averaging in s
Cosi 6:b69c4870db2c 23 #define AVERAGE_INTERVAL (24 * 60 * 60)
Cosi 6:b69c4870db2c 24 //WEEK measure time interval
Cosi 6:b69c4870db2c 25 #define WEEK_INTERVALL (7 * 24 * 60 * 60)
Cosi 7:95ddf8fa6260 26 */
Cosi 7:95ddf8fa6260 27
Cosi 7:95ddf8fa6260 28 //Test intervals :
Cosi 7:95ddf8fa6260 29 #define MEASURE_INTERVAL 5
Cosi 7:95ddf8fa6260 30 #define AVERAGE_INTERVAL 20
Cosi 7:95ddf8fa6260 31 #define WEEK_INTERVAL (2 * 60)
Cosi 7:95ddf8fa6260 32
thomaspotier 5:d2702cbe5afe 33 // number of measure before average
thomaspotier 5:d2702cbe5afe 34 #define AVERAGE_COUNT ((int)(AVERAGE_INTERVAL / MEASURE_INTERVAL))
thomaspotier 4:bee94a4094b6 35
Cosi 6:b69c4870db2c 36 #define WEEK_COUNT ((int)(WEEK_INTERVAL / AVERAGE_INTERVAL))
Cosi 6:b69c4870db2c 37
tlegros 8:3af661bcfcec 38 #define SEUIL 0.5
tlegros 8:3af661bcfcec 39
Cosi 6:b69c4870db2c 40 //#define SENSOR_OFFSET 6.1f
Cosi 6:b69c4870db2c 41 //#define SENSOR_FACTOR 39.0f
Cosi 6:b69c4870db2c 42 #define SENSOR_OFFSET 9.4f
Cosi 6:b69c4870db2c 43 #define SENSOR_FACTOR 26.3f
thomaspotier 4:bee94a4094b6 44
thomaspotier 4:bee94a4094b6 45 // weight difference needed to send sms
thomaspotier 4:bee94a4094b6 46 #define DIFF_THRESHOLD 0.4f
maxelior 3:8b90a40df4db 47
Cosi 9:4c09643fbfb9 48 // Season constants - used in the get_season function
Cosi 9:4c09643fbfb9 49 #define SPRING 1
Cosi 9:4c09643fbfb9 50 #define SUMMER 2
Cosi 9:4c09643fbfb9 51 #define AUTUMN 3
Cosi 9:4c09643fbfb9 52 #define WINTER 4
Cosi 9:4c09643fbfb9 53
thomaspotier 4:bee94a4094b6 54 // states of the hive
thomaspotier 4:bee94a4094b6 55 typedef enum E_HIVE_STATE {
thomaspotier 5:d2702cbe5afe 56 HIVE_STATE_INIT = 0,
Cosi 6:b69c4870db2c 57 HIVE_STATE_STANDBY,
Cosi 6:b69c4870db2c 58 HIVE_STATE_WEEK,
Cosi 7:95ddf8fa6260 59 HIVE_STATE_DAILY,
thomaspotier 5:d2702cbe5afe 60 HIVE_STATE_MEASURE,
thomaspotier 5:d2702cbe5afe 61 HIVE_STATE_NOTHING_NEW,
thomaspotier 5:d2702cbe5afe 62 HIVE_STATE_HONEY,
thomaspotier 5:d2702cbe5afe 63 HIVE_STATE_PROBLEM_SPRING,
thomaspotier 4:bee94a4094b6 64 } t_hive_state;
maxelior 3:8b90a40df4db 65
thomaspotier 5:d2702cbe5afe 66 t_hive_state current_state = HIVE_STATE_INIT;
thomaspotier 4:bee94a4094b6 67
thomaspotier 4:bee94a4094b6 68 // buffer used to create message content
thomaspotier 4:bee94a4094b6 69 char buffer[1024];
maxelior 3:8b90a40df4db 70
thomaspotier 5:d2702cbe5afe 71 float average_values[AVERAGE_COUNT] = {0.0f};
tlegros 8:3af661bcfcec 72 float week_values[WEEK_COUNT] = {0.0f};
Cosi 7:95ddf8fa6260 73 //float diff_values[AVERAGE_COUNT] = {0.0f};
thomaspotier 5:d2702cbe5afe 74 float last_average = 0;
Cosi 6:b69c4870db2c 75 float week_last_average = 0;
thomaspotier 5:d2702cbe5afe 76 float current_average = 0;
Cosi 7:95ddf8fa6260 77 float week_average = 0;
thomaspotier 5:d2702cbe5afe 78 float current_average_diff = 0;
Cosi 6:b69c4870db2c 79 float week_average_diff = 0;
thomaspotier 5:d2702cbe5afe 80 float current_measure_diff = 0;
thomaspotier 5:d2702cbe5afe 81 unsigned int average_ticks_passed = 0;
Cosi 6:b69c4870db2c 82 unsigned int week_ticks_passed = 0;
Cosi 7:95ddf8fa6260 83 int size_tab = 0;
Cosi 7:95ddf8fa6260 84 int time_diff = 0;
Cosi 7:95ddf8fa6260 85
Cosi 7:95ddf8fa6260 86 //void init_modem(void);
thomaspotier 4:bee94a4094b6 87
thomaspotier 4:bee94a4094b6 88 // analog read of sensor board output
thomaspotier 4:bee94a4094b6 89 AnalogIn sensor(PC_5);
thomaspotier 4:bee94a4094b6 90
thomaspotier 4:bee94a4094b6 91 // phone number for sms (Cosima DORLAND)
Cosi 6:b69c4870db2c 92 //const char phone_number[] = "0680182150";
thomaspotier 5:d2702cbe5afe 93 const char phone_number[] = "0786074814";
thomaspotier 4:bee94a4094b6 94
thomaspotier 4:bee94a4094b6 95 // Serial object for pc communications
thomaspotier 4:bee94a4094b6 96 Serial pc(USBTX, USBRX);
maxelior 1:5639bc1208d5 97
thomaspotier 4:bee94a4094b6 98 // modem object for sms communications
thomaspotier 4:bee94a4094b6 99 MDMSerial modem;
thomaspotier 4:bee94a4094b6 100
thomaspotier 5:d2702cbe5afe 101 // sleep utils
thomaspotier 5:d2702cbe5afe 102 Timeout sleep_timout;
thomaspotier 5:d2702cbe5afe 103
thomaspotier 5:d2702cbe5afe 104 bool awake = true;
thomaspotier 5:d2702cbe5afe 105
thomaspotier 4:bee94a4094b6 106 // led for test blink
thomaspotier 4:bee94a4094b6 107 DigitalOut led(LED1);
thomaspotier 4:bee94a4094b6 108
thomaspotier 4:bee94a4094b6 109
thomaspotier 5:d2702cbe5afe 110 void blink(void)
thomaspotier 4:bee94a4094b6 111 {
thomaspotier 4:bee94a4094b6 112 led.write(1);
thomaspotier 4:bee94a4094b6 113 wait(0.2);
thomaspotier 4:bee94a4094b6 114 led.write(0);
maxelior 2:57d040afa937 115 }
maxelior 1:5639bc1208d5 116
thomaspotier 5:d2702cbe5afe 117 void wake_up(void)
thomaspotier 5:d2702cbe5afe 118 {
thomaspotier 5:d2702cbe5afe 119 awake = true;
thomaspotier 5:d2702cbe5afe 120 }
thomaspotier 5:d2702cbe5afe 121
thomaspotier 5:d2702cbe5afe 122 void power_sleep(float time)
thomaspotier 5:d2702cbe5afe 123 {
thomaspotier 5:d2702cbe5afe 124 awake = false;
thomaspotier 5:d2702cbe5afe 125 sleep_timout.attach(wake_up, time);
thomaspotier 5:d2702cbe5afe 126 pc.printf("Going to sleep !\n\r");
thomaspotier 5:d2702cbe5afe 127 while (!awake) sleep();
thomaspotier 5:d2702cbe5afe 128 pc.printf("Woke up !\n\r");
thomaspotier 5:d2702cbe5afe 129 }
thomaspotier 5:d2702cbe5afe 130
Cosi 9:4c09643fbfb9 131 // Returns integer representing the season : Spring 1, Summer 2, Autumn 3, Winter 4
Cosi 9:4c09643fbfb9 132 int get_season (char month[4])
Cosi 9:4c09643fbfb9 133 {
Cosi 9:4c09643fbfb9 134 time_t seconds = time(NULL);
Cosi 9:4c09643fbfb9 135 sscanf(ctime(&seconds), "%*s %s %*s", month);
Cosi 9:4c09643fbfb9 136
Cosi 9:4c09643fbfb9 137 if (!strcmp(month, "Mar") || !strcmp(month, "Apr") || !strcmp(month, "May"))
Cosi 9:4c09643fbfb9 138 return SPRING;
Cosi 9:4c09643fbfb9 139 else if (!strcmp(month, "Jun") || !strcmp(month, "Jul") || !strcmp(month, "Aug"))
Cosi 9:4c09643fbfb9 140 return SUMMER;
Cosi 9:4c09643fbfb9 141 else if (!strcmp(month, "Sep") || !strcmp(month, "Oct") || !strcmp(month, "Nov"))
Cosi 9:4c09643fbfb9 142 return AUTUMN;
Cosi 9:4c09643fbfb9 143 else if (!strcmp(month, "Dec") || !strcmp(month, "Jan") || !strcmp(month, "Feb"))
Cosi 9:4c09643fbfb9 144 return WINTER;
Cosi 9:4c09643fbfb9 145 }
Cosi 9:4c09643fbfb9 146
thomaspotier 4:bee94a4094b6 147 // send a sms to the phone number
thomaspotier 4:bee94a4094b6 148 void send_message(char msg[])
thomaspotier 4:bee94a4094b6 149 {
thomaspotier 4:bee94a4094b6 150 MDMParser::DevStatus devStatus = {};
thomaspotier 4:bee94a4094b6 151 MDMParser::NetStatus netStatus = {};
maxelior 2:57d040afa937 152
thomaspotier 4:bee94a4094b6 153 // log sms sending to pc
thomaspotier 5:d2702cbe5afe 154 pc.printf("\x1b[34mSMS: %s\n\r\x1b[0m", msg);
thomaspotier 4:bee94a4094b6 155
thomaspotier 4:bee94a4094b6 156 while (!modem.checkNetStatus(&netStatus)) {
thomaspotier 5:d2702cbe5afe 157 pc.printf("\x1b[34mMODEM checkNetStatus failed...\n\r\x1b[0m");
thomaspotier 4:bee94a4094b6 158 wait(1);
thomaspotier 4:bee94a4094b6 159 }
thomaspotier 4:bee94a4094b6 160 modem.dumpDevStatus(&devStatus);
thomaspotier 4:bee94a4094b6 161 while (!modem.registerNet(&netStatus)) {
thomaspotier 5:d2702cbe5afe 162 pc.printf("\x1b[34mMODEM registerNet failed...\n\r\x1b[0m");
thomaspotier 4:bee94a4094b6 163 wait(1);
thomaspotier 4:bee94a4094b6 164 }
thomaspotier 4:bee94a4094b6 165 modem.dumpNetStatus(&netStatus);
thomaspotier 4:bee94a4094b6 166 modem.smsSend(phone_number ,msg);
maxelior 2:57d040afa937 167 }
maxelior 1:5639bc1208d5 168
thomaspotier 4:bee94a4094b6 169 // return the current calibrated weight
thomaspotier 4:bee94a4094b6 170 float get_weight()
thomaspotier 4:bee94a4094b6 171 {
thomaspotier 4:bee94a4094b6 172 float weight;
thomaspotier 4:bee94a4094b6 173
thomaspotier 4:bee94a4094b6 174 // weight is the sensor output in volts
thomaspotier 4:bee94a4094b6 175 weight = sensor.read() * 3.3f;
thomaspotier 4:bee94a4094b6 176 // return calibrated result
thomaspotier 4:bee94a4094b6 177 return (weight * SENSOR_FACTOR - SENSOR_OFFSET);
thomaspotier 4:bee94a4094b6 178 }
thomaspotier 4:bee94a4094b6 179
thomaspotier 4:bee94a4094b6 180 // return an averaged weight (time consuming function)
thomaspotier 4:bee94a4094b6 181 float get_averaged_weight()
thomaspotier 4:bee94a4094b6 182 {
thomaspotier 4:bee94a4094b6 183 float average = 0;
thomaspotier 4:bee94a4094b6 184 int i;
thomaspotier 4:bee94a4094b6 185
thomaspotier 4:bee94a4094b6 186 for(i = 0; i < AVERAGING_SIZE; i++) {
thomaspotier 4:bee94a4094b6 187 average += get_weight();
thomaspotier 4:bee94a4094b6 188 wait_us(AVERAGING_TIME);
thomaspotier 4:bee94a4094b6 189 }
thomaspotier 4:bee94a4094b6 190 return (average / AVERAGING_SIZE);
thomaspotier 4:bee94a4094b6 191 }
thomaspotier 4:bee94a4094b6 192
Cosi 7:95ddf8fa6260 193 // return average of differential
Cosi 7:95ddf8fa6260 194 float get_differential_average (float tab[])
Cosi 7:95ddf8fa6260 195 {
Cosi 7:95ddf8fa6260 196 int i = 1;
Cosi 7:95ddf8fa6260 197 float differential = 0;
Cosi 7:95ddf8fa6260 198
Cosi 7:95ddf8fa6260 199 for ( i = 1; i < size_tab; i++)
Cosi 7:95ddf8fa6260 200 differential += (tab[i] - tab[i-1])/time_diff;
Cosi 7:95ddf8fa6260 201
tlegros 8:3af661bcfcec 202 return differential/(size_tab -1);
tlegros 8:3af661bcfcec 203 }
tlegros 8:3af661bcfcec 204 /*
tlegros 8:3af661bcfcec 205 //return the real daily average
tlegros 8:3af661bcfcec 206 float true_average(float tab[], float differential)
tlegros 8:3af661bcfcec 207 {
tlegros 8:3af661bcfcec 208 int i;
tlegros 8:3af661bcfcec 209 float dpoint, avg = 0, error = 0;
tlegros 8:3af661bcfcec 210 for(i = 1; i < size_tab; i++)
tlegros 8:3af661bcfcec 211 {
tlegros 8:3af661bcfcec 212 dpoint = (tab[i] - tab[i-1])/time_diff;
tlegros 8:3af661bcfcec 213 if ((differential + SEUIL > dpoint) && (differential - SEUIL < dpoint))
tlegros 8:3af661bcfcec 214 avg += tab[i];
tlegros 8:3af661bcfcec 215 else
tlegros 8:3af661bcfcec 216 error ++;
tlegros 8:3af661bcfcec 217 }
tlegros 8:3af661bcfcec 218 avg = avg/(AVERAGE_COUNT - (error +1));
tlegros 8:3af661bcfcec 219 return avg;
tlegros 8:3af661bcfcec 220 }
tlegros 8:3af661bcfcec 221 */
Cosi 7:95ddf8fa6260 222
Cosi 7:95ddf8fa6260 223 void init_modem(void)
Cosi 7:95ddf8fa6260 224 {
Cosi 7:95ddf8fa6260 225 MDMParser::DevStatus devStatus = {};
Cosi 7:95ddf8fa6260 226 MDMParser::NetStatus netStatus = {};
Cosi 7:95ddf8fa6260 227
Cosi 7:95ddf8fa6260 228 // retry from begining if something fails
Cosi 7:95ddf8fa6260 229 while (!modem.init(SIMPIN, &devStatus, D4)) {
Cosi 7:95ddf8fa6260 230 pc.printf("\x1b[34m");
Cosi 7:95ddf8fa6260 231 pc.printf("MODEM init failed...\n\r");
Cosi 7:95ddf8fa6260 232 pc.printf("\x1b[0m");
Cosi 7:95ddf8fa6260 233 wait(1);
Cosi 7:95ddf8fa6260 234 }
Cosi 7:95ddf8fa6260 235 while (!modem.checkNetStatus(&netStatus)) {
Cosi 7:95ddf8fa6260 236 pc.printf("\x1b[34m");
Cosi 7:95ddf8fa6260 237 pc.printf("MODEM net_status failed...\n\r");
Cosi 7:95ddf8fa6260 238 pc.printf("\x1b[0m");
Cosi 7:95ddf8fa6260 239 wait(1);
tlegros 8:3af661bcfcec 240 }
Cosi 7:95ddf8fa6260 241 modem.dumpDevStatus(&devStatus);
Cosi 7:95ddf8fa6260 242 }
Cosi 7:95ddf8fa6260 243
Cosi 7:95ddf8fa6260 244
thomaspotier 4:bee94a4094b6 245 // automata calculations
maxelior 1:5639bc1208d5 246 void automate()
maxelior 1:5639bc1208d5 247 {
thomaspotier 4:bee94a4094b6 248 switch(current_state) {
thomaspotier 5:d2702cbe5afe 249 case HIVE_STATE_INIT:
Cosi 6:b69c4870db2c 250 // setting baud rate for serial communication with the pc
Cosi 6:b69c4870db2c 251 pc.baud(115200);
Cosi 6:b69c4870db2c 252 pc.printf("TEST\n\r");
Cosi 6:b69c4870db2c 253 init_modem();
Cosi 6:b69c4870db2c 254 current_state = HIVE_STATE_STANDBY;
Cosi 6:b69c4870db2c 255 break;
Cosi 6:b69c4870db2c 256
Cosi 6:b69c4870db2c 257 case HIVE_STATE_STANDBY:
thomaspotier 5:d2702cbe5afe 258 if (average_ticks_passed == AVERAGE_COUNT)
Cosi 7:95ddf8fa6260 259 current_state = HIVE_STATE_DAILY;
thomaspotier 5:d2702cbe5afe 260 else
thomaspotier 5:d2702cbe5afe 261 current_state = HIVE_STATE_MEASURE;
Cosi 7:95ddf8fa6260 262 power_sleep(MEASURE_INTERVAL);
Cosi 7:95ddf8fa6260 263 break;
Cosi 7:95ddf8fa6260 264
thomaspotier 5:d2702cbe5afe 265 case HIVE_STATE_MEASURE:
thomaspotier 5:d2702cbe5afe 266 average_values[average_ticks_passed] = get_averaged_weight();
tlegros 8:3af661bcfcec 267 week_values[week_ticks_passed] = get_averaged_weight();
thomaspotier 5:d2702cbe5afe 268 pc.printf("Weight is %02.2fkg\n\r", average_values[average_ticks_passed]);
thomaspotier 5:d2702cbe5afe 269 // if we are of the first measure use the one at the end of the list (last one)
thomaspotier 5:d2702cbe5afe 270 current_measure_diff = average_ticks_passed == 0 ? average_values[average_ticks_passed] - average_values[AVERAGE_COUNT - 1] : average_values[average_ticks_passed] - average_values[average_ticks_passed - 1];
thomaspotier 5:d2702cbe5afe 271 if (abs(current_measure_diff) > DIFF_THRESHOLD)
Cosi 6:b69c4870db2c 272 {
Cosi 6:b69c4870db2c 273 sprintf(buffer, "3 There has been a problem : %0.1fkg of change in %d minutes !", current_measure_diff, MEASURE_INTERVAL / 60);
Cosi 6:b69c4870db2c 274 send_message(buffer);
Cosi 6:b69c4870db2c 275 }
thomaspotier 5:d2702cbe5afe 276 else
Cosi 6:b69c4870db2c 277 current_state = HIVE_STATE_STANDBY;
Cosi 7:95ddf8fa6260 278 average_ticks_passed++;
Cosi 7:95ddf8fa6260 279 break;
Cosi 7:95ddf8fa6260 280
Cosi 7:95ddf8fa6260 281 case HIVE_STATE_DAILY:
Cosi 7:95ddf8fa6260 282 float dif_av = 0;
Cosi 7:95ddf8fa6260 283 size_tab = AVERAGE_COUNT;
Cosi 7:95ddf8fa6260 284 time_diff = MEASURE_INTERVAL;
thomaspotier 5:d2702cbe5afe 285 current_average = 0;
thomaspotier 5:d2702cbe5afe 286 for(int i = 0; i < average_ticks_passed; i++) {
thomaspotier 5:d2702cbe5afe 287 current_average += average_values[i];
maxelior 1:5639bc1208d5 288 }
thomaspotier 5:d2702cbe5afe 289 current_average /= average_ticks_passed;
tlegros 8:3af661bcfcec 290 dif_av = get_differential_average(average_values);
tlegros 8:3af661bcfcec 291 current_average = average_values[0] + dif_av * (average_ticks_passed -1);
thomaspotier 5:d2702cbe5afe 292 current_average_diff = current_average - last_average;
thomaspotier 5:d2702cbe5afe 293 last_average = current_average;
Cosi 7:95ddf8fa6260 294
tlegros 8:3af661bcfcec 295
Cosi 6:b69c4870db2c 296 if (current_average_diff > DIFF_THRESHOLD)
Cosi 6:b69c4870db2c 297 {
Cosi 6:b69c4870db2c 298 sprintf(buffer, "1 There is more and more honey in the hive ! %0.1fkg of change in %d hours !", current_average_diff, AVERAGE_INTERVAL / 60);
Cosi 6:b69c4870db2c 299 send_message(buffer);
Cosi 6:b69c4870db2c 300 }
Cosi 6:b69c4870db2c 301 else if (current_average_diff < -DIFF_THRESHOLD)
Cosi 6:b69c4870db2c 302 {
Cosi 6:b69c4870db2c 303 sprintf(buffer, "2 There is less honey ! %0.1fkg of change in %d hours !", current_average_diff, AVERAGE_INTERVAL / 60);
Cosi 6:b69c4870db2c 304 send_message(buffer);
Cosi 6:b69c4870db2c 305 }
thomaspotier 5:d2702cbe5afe 306 else
Cosi 6:b69c4870db2c 307 {
Cosi 6:b69c4870db2c 308 sprintf(buffer, "0 Everything is fine ! %0.1fkg of change in %d hours !", current_average_diff, AVERAGE_INTERVAL / 60);
Cosi 6:b69c4870db2c 309 send_message(buffer);
Cosi 6:b69c4870db2c 310 }
thomaspotier 5:d2702cbe5afe 311 average_ticks_passed = 0;
Cosi 6:b69c4870db2c 312 current_state = HIVE_STATE_STANDBY;
thomaspotier 5:d2702cbe5afe 313 break;
Cosi 6:b69c4870db2c 314
Cosi 6:b69c4870db2c 315 case HIVE_STATE_WEEK:
Cosi 7:95ddf8fa6260 316 size_tab = 7;
Cosi 7:95ddf8fa6260 317 time_diff = WEEK_INTERVAL;
Cosi 6:b69c4870db2c 318 week_average = 0;
Cosi 6:b69c4870db2c 319 for(int i = 0; i < week_ticks_passed; i++) {
Cosi 6:b69c4870db2c 320 week_average += average_values[i];
Cosi 6:b69c4870db2c 321
Cosi 6:b69c4870db2c 322 }
Cosi 6:b69c4870db2c 323 week_average /= week_ticks_passed;
Cosi 6:b69c4870db2c 324 week_average_diff = week_average - week_last_average;
tlegros 8:3af661bcfcec 325 dif_av = get_differential_average(average_values);
tlegros 8:3af661bcfcec 326 current_average = week_values[0] + dif_av * (week_ticks_passed -1);
Cosi 6:b69c4870db2c 327 week_last_average = week_average;
Cosi 6:b69c4870db2c 328 if (week_average_diff > DIFF_THRESHOLD)
Cosi 6:b69c4870db2c 329 {
Cosi 6:b69c4870db2c 330 sprintf(buffer, "4 There is more and more honey in the hive over the week! %0.1fkg of change in %d hours !", week_average_diff, WEEK_INTERVAL / 60);
Cosi 6:b69c4870db2c 331 send_message(buffer);
Cosi 6:b69c4870db2c 332 }
Cosi 6:b69c4870db2c 333 else if (week_average_diff < -DIFF_THRESHOLD)
Cosi 6:b69c4870db2c 334 {
Cosi 7:95ddf8fa6260 335 sprintf(buffer, "5 There is less honey over the last week ! %0.1fkg of change in %d hours !", week_average_diff, WEEK_INTERVAL / 60);
Cosi 6:b69c4870db2c 336 send_message(buffer);
Cosi 6:b69c4870db2c 337 }
Cosi 6:b69c4870db2c 338 else
Cosi 6:b69c4870db2c 339 {
Cosi 6:b69c4870db2c 340 sprintf(buffer, "6 Everything was fine over the last week ! %0.1fkg of change in %d hours !", week_average_diff, WEEK_INTERVAL / 60);
Cosi 6:b69c4870db2c 341 send_message(buffer);
Cosi 6:b69c4870db2c 342 }
Cosi 6:b69c4870db2c 343 week_ticks_passed = 0;
Cosi 6:b69c4870db2c 344 current_state = HIVE_STATE_STANDBY;
thomaspotier 5:d2702cbe5afe 345 break;
Cosi 6:b69c4870db2c 346
Cosi 6:b69c4870db2c 347
Cosi 6:b69c4870db2c 348
thomaspotier 4:bee94a4094b6 349 default:
thomaspotier 5:d2702cbe5afe 350 pc.printf("Error...");
maxelior 2:57d040afa937 351 break;
maxelior 1:5639bc1208d5 352 }
maxelior 1:5639bc1208d5 353 }
bennettmatt1977 0:15a06b50e14e 354
bennettmatt1977 0:15a06b50e14e 355 int main(void)
bennettmatt1977 0:15a06b50e14e 356 {
Cosi 6:b69c4870db2c 357
Cosi 6:b69c4870db2c 358
thomaspotier 4:bee94a4094b6 359
thomaspotier 4:bee94a4094b6 360 // main loop
maxelior 1:5639bc1208d5 361 while(1) {
thomaspotier 4:bee94a4094b6 362 // blink once for each measure
thomaspotier 5:d2702cbe5afe 363 //blink();
thomaspotier 4:bee94a4094b6 364 // getting sensor values
thomaspotier 4:bee94a4094b6 365 // acquisition();
thomaspotier 4:bee94a4094b6 366 // changing state of automata according to read values
maxelior 1:5639bc1208d5 367 automate();
bennettmatt1977 0:15a06b50e14e 368 }
bennettmatt1977 0:15a06b50e14e 369 }
maxelior 1:5639bc1208d5 370
maxelior 1:5639bc1208d5 371
maxelior 1:5639bc1208d5 372
maxelior 1:5639bc1208d5 373
maxelior 1:5639bc1208d5 374