SysBee v1

Dependencies:   C027_Support mbed

Fork of App_Pese_Ruche_SYSBEE by Sysbee

Committer:
thomaspotier
Date:
Thu Sep 06 11:16:03 2018 +0000
Revision:
4:bee94a4094b6
Parent:
3:8b90a40df4db
Child:
5:d2702cbe5afe
Test

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>
bennettmatt1977 0:15a06b50e14e 3
bennettmatt1977 0:15a06b50e14e 4 #include "GPS.h"
bennettmatt1977 0:15a06b50e14e 5 #include "MDM.h"
thomaspotier 4:bee94a4094b6 6
thomaspotier 4:bee94a4094b6 7 // sim pin code
maxelior 1:5639bc1208d5 8 #define SIMPIN "1234"
bennettmatt1977 0:15a06b50e14e 9 #define APN NULL
bennettmatt1977 0:15a06b50e14e 10 #define USERNAME NULL
maxelior 1:5639bc1208d5 11 #define PASSWORD NULL
maxelior 2:57d040afa937 12
maxelior 2:57d040afa937 13
thomaspotier 4:bee94a4094b6 14 // size of the average buffer (more means more averaged)
thomaspotier 4:bee94a4094b6 15 #define AVERAGING_SIZE 2000
thomaspotier 4:bee94a4094b6 16 // time between each capture in us
thomaspotier 4:bee94a4094b6 17 #define AVERAGING_TIME 100
maxelior 2:57d040afa937 18
thomaspotier 4:bee94a4094b6 19 // time between each mesures in s
thomaspotier 4:bee94a4094b6 20 #define MEASURE_INTERVAL 5
thomaspotier 4:bee94a4094b6 21
thomaspotier 4:bee94a4094b6 22 #define SENSOR_OFFSET 6.1f
thomaspotier 4:bee94a4094b6 23 #define SENSOR_FACTOR 39.0f
thomaspotier 4:bee94a4094b6 24
thomaspotier 4:bee94a4094b6 25 #define VALUE_SAVE_COUNT 100
maxelior 1:5639bc1208d5 26
thomaspotier 4:bee94a4094b6 27 // weight difference needed to send sms
thomaspotier 4:bee94a4094b6 28 #define DIFF_THRESHOLD 0.4f
maxelior 3:8b90a40df4db 29
thomaspotier 4:bee94a4094b6 30 // states of the hive
thomaspotier 4:bee94a4094b6 31 typedef enum E_HIVE_STATE {
thomaspotier 4:bee94a4094b6 32 FIRST = 0,
thomaspotier 4:bee94a4094b6 33 HIVE_STATE_COUNT
thomaspotier 4:bee94a4094b6 34 } t_hive_state;
maxelior 3:8b90a40df4db 35
thomaspotier 4:bee94a4094b6 36 t_hive_state current_state = FIRST;
thomaspotier 4:bee94a4094b6 37
thomaspotier 4:bee94a4094b6 38 // buffer used to create message content
thomaspotier 4:bee94a4094b6 39 char buffer[1024];
maxelior 3:8b90a40df4db 40
thomaspotier 4:bee94a4094b6 41 float values[VALUE_SAVE_COUNT];
thomaspotier 4:bee94a4094b6 42 int current_value = 0;
thomaspotier 4:bee94a4094b6 43 int last_value = 0;
thomaspotier 4:bee94a4094b6 44
thomaspotier 4:bee94a4094b6 45 // analog read of sensor board output
thomaspotier 4:bee94a4094b6 46 AnalogIn sensor(PC_5);
thomaspotier 4:bee94a4094b6 47
thomaspotier 4:bee94a4094b6 48 // phone number for sms (Cosima DORLAND)
thomaspotier 4:bee94a4094b6 49 const char phone_number[] = "0783099416";
thomaspotier 4:bee94a4094b6 50
thomaspotier 4:bee94a4094b6 51 // Serial object for pc communications
thomaspotier 4:bee94a4094b6 52 Serial pc(USBTX, USBRX);
maxelior 1:5639bc1208d5 53
thomaspotier 4:bee94a4094b6 54 // modem object for sms communications
thomaspotier 4:bee94a4094b6 55 MDMSerial modem;
thomaspotier 4:bee94a4094b6 56
thomaspotier 4:bee94a4094b6 57 // led for test blink
thomaspotier 4:bee94a4094b6 58 DigitalOut led(LED1);
thomaspotier 4:bee94a4094b6 59
thomaspotier 4:bee94a4094b6 60
thomaspotier 4:bee94a4094b6 61
thomaspotier 4:bee94a4094b6 62 void blink()
thomaspotier 4:bee94a4094b6 63 {
thomaspotier 4:bee94a4094b6 64 led.write(1);
thomaspotier 4:bee94a4094b6 65 wait(0.2);
thomaspotier 4:bee94a4094b6 66 led.write(0);
maxelior 2:57d040afa937 67 }
maxelior 1:5639bc1208d5 68
thomaspotier 4:bee94a4094b6 69 // send a sms to the phone number
thomaspotier 4:bee94a4094b6 70 void send_message(char msg[])
thomaspotier 4:bee94a4094b6 71 {
thomaspotier 4:bee94a4094b6 72 MDMParser::DevStatus devStatus = {};
thomaspotier 4:bee94a4094b6 73 MDMParser::NetStatus netStatus = {};
maxelior 2:57d040afa937 74
thomaspotier 4:bee94a4094b6 75 // log sms sending to pc
thomaspotier 4:bee94a4094b6 76 pc.printf("\x1b[34m");
thomaspotier 4:bee94a4094b6 77 pc.printf("SMS: ");
thomaspotier 4:bee94a4094b6 78 pc.printf(msg);
thomaspotier 4:bee94a4094b6 79 pc.printf("\n\r");
thomaspotier 4:bee94a4094b6 80 pc.printf("\x1b[0m");
thomaspotier 4:bee94a4094b6 81
thomaspotier 4:bee94a4094b6 82 while (!modem.checkNetStatus(&netStatus)) {
thomaspotier 4:bee94a4094b6 83 pc.printf("\x1b[34m");
thomaspotier 4:bee94a4094b6 84 pc.printf("MODEM net_status failed...\n\r");
thomaspotier 4:bee94a4094b6 85 pc.printf("\x1b[0m");
thomaspotier 4:bee94a4094b6 86 wait(1);
thomaspotier 4:bee94a4094b6 87 }
thomaspotier 4:bee94a4094b6 88 modem.dumpDevStatus(&devStatus);
thomaspotier 4:bee94a4094b6 89 while (!modem.registerNet(&netStatus)) {
thomaspotier 4:bee94a4094b6 90 pc.printf("\x1b[34m");
thomaspotier 4:bee94a4094b6 91 pc.printf("MODEM register_net failed...\n\r");
thomaspotier 4:bee94a4094b6 92 pc.printf("\x1b[0m");
thomaspotier 4:bee94a4094b6 93 wait(1);
thomaspotier 4:bee94a4094b6 94 }
thomaspotier 4:bee94a4094b6 95 modem.dumpNetStatus(&netStatus);
thomaspotier 4:bee94a4094b6 96 modem.smsSend(phone_number ,msg);
maxelior 2:57d040afa937 97 }
maxelior 1:5639bc1208d5 98
thomaspotier 4:bee94a4094b6 99 // return the current calibrated weight
thomaspotier 4:bee94a4094b6 100 float get_weight()
thomaspotier 4:bee94a4094b6 101 {
thomaspotier 4:bee94a4094b6 102 float weight;
thomaspotier 4:bee94a4094b6 103
thomaspotier 4:bee94a4094b6 104 // weight is the sensor output in volts
thomaspotier 4:bee94a4094b6 105 weight = sensor.read() * 3.3f;
thomaspotier 4:bee94a4094b6 106 // return calibrated result
thomaspotier 4:bee94a4094b6 107 return (weight * SENSOR_FACTOR - SENSOR_OFFSET);
thomaspotier 4:bee94a4094b6 108 }
thomaspotier 4:bee94a4094b6 109
thomaspotier 4:bee94a4094b6 110 // return an averaged weight (time consuming function)
thomaspotier 4:bee94a4094b6 111 float get_averaged_weight()
thomaspotier 4:bee94a4094b6 112 {
thomaspotier 4:bee94a4094b6 113 float average = 0;
thomaspotier 4:bee94a4094b6 114 int i;
thomaspotier 4:bee94a4094b6 115
thomaspotier 4:bee94a4094b6 116 for(i = 0; i < AVERAGING_SIZE; i++) {
thomaspotier 4:bee94a4094b6 117 average += get_weight();
thomaspotier 4:bee94a4094b6 118 wait_us(AVERAGING_TIME);
thomaspotier 4:bee94a4094b6 119 }
thomaspotier 4:bee94a4094b6 120 return (average / AVERAGING_SIZE);
thomaspotier 4:bee94a4094b6 121 }
thomaspotier 4:bee94a4094b6 122
thomaspotier 4:bee94a4094b6 123 // automata calculations
maxelior 1:5639bc1208d5 124 void automate()
maxelior 1:5639bc1208d5 125 {
thomaspotier 4:bee94a4094b6 126 switch(current_state) {
thomaspotier 4:bee94a4094b6 127 case FIRST:
thomaspotier 4:bee94a4094b6 128 values[current_value] = get_averaged_weight();
thomaspotier 4:bee94a4094b6 129 pc.printf("Weight is %02.2fkg", values[current_value]);
thomaspotier 4:bee94a4094b6 130 pc.printf("\n\r");
thomaspotier 4:bee94a4094b6 131 if (abs(values[current_value] - values[last_value]) > DIFF_THRESHOLD)
thomaspotier 4:bee94a4094b6 132 {
thomaspotier 4:bee94a4094b6 133 sprintf(buffer, "Weight is %02.2fkg, change of %02.2fkg", values[current_value], values[current_value] - values[last_value]);
thomaspotier 4:bee94a4094b6 134 send_message(buffer);
thomaspotier 4:bee94a4094b6 135 last_value = current_value;
maxelior 1:5639bc1208d5 136 }
thomaspotier 4:bee94a4094b6 137 current_value = (current_value + 1) % VALUE_SAVE_COUNT;
maxelior 1:5639bc1208d5 138 break;
thomaspotier 4:bee94a4094b6 139 default:
thomaspotier 4:bee94a4094b6 140 sprintf(buffer, "Error...");
thomaspotier 4:bee94a4094b6 141 pc.printf(buffer);
maxelior 2:57d040afa937 142 break;
maxelior 1:5639bc1208d5 143 }
maxelior 1:5639bc1208d5 144 }
bennettmatt1977 0:15a06b50e14e 145
thomaspotier 4:bee94a4094b6 146 void init_modem(void)
thomaspotier 4:bee94a4094b6 147 {
thomaspotier 4:bee94a4094b6 148 MDMParser::DevStatus devStatus = {};
thomaspotier 4:bee94a4094b6 149 MDMParser::NetStatus netStatus = {};
thomaspotier 4:bee94a4094b6 150
thomaspotier 4:bee94a4094b6 151 // retry from begining if something fails
thomaspotier 4:bee94a4094b6 152 while (!modem.init(SIMPIN, &devStatus, D4)) {
thomaspotier 4:bee94a4094b6 153 pc.printf("\x1b[34m");
thomaspotier 4:bee94a4094b6 154 pc.printf("MODEM init failed...\n\r");
thomaspotier 4:bee94a4094b6 155 pc.printf("\x1b[0m");
thomaspotier 4:bee94a4094b6 156 wait(1);
thomaspotier 4:bee94a4094b6 157 }
thomaspotier 4:bee94a4094b6 158 while (!modem.checkNetStatus(&netStatus)) {
thomaspotier 4:bee94a4094b6 159 pc.printf("\x1b[34m");
thomaspotier 4:bee94a4094b6 160 pc.printf("MODEM net_status failed...\n\r");
thomaspotier 4:bee94a4094b6 161 pc.printf("\x1b[0m");
thomaspotier 4:bee94a4094b6 162 wait(1);
thomaspotier 4:bee94a4094b6 163 }
thomaspotier 4:bee94a4094b6 164 modem.dumpDevStatus(&devStatus);
thomaspotier 4:bee94a4094b6 165 }
thomaspotier 4:bee94a4094b6 166
bennettmatt1977 0:15a06b50e14e 167 int main(void)
bennettmatt1977 0:15a06b50e14e 168 {
thomaspotier 4:bee94a4094b6 169 // setting baud rate for serial communication with the pc
maxelior 1:5639bc1208d5 170 pc.baud(115200);
thomaspotier 4:bee94a4094b6 171 pc.printf("TEST\n\r");
thomaspotier 4:bee94a4094b6 172 init_modem();
thomaspotier 4:bee94a4094b6 173
thomaspotier 4:bee94a4094b6 174 // main loop
maxelior 1:5639bc1208d5 175 while(1) {
thomaspotier 4:bee94a4094b6 176 // blink once for each measure
thomaspotier 4:bee94a4094b6 177 blink();
thomaspotier 4:bee94a4094b6 178 // getting sensor values
thomaspotier 4:bee94a4094b6 179 // acquisition();
thomaspotier 4:bee94a4094b6 180 // changing state of automata according to read values
maxelior 1:5639bc1208d5 181 automate();
thomaspotier 4:bee94a4094b6 182 wait(MEASURE_INTERVAL);
bennettmatt1977 0:15a06b50e14e 183 }
bennettmatt1977 0:15a06b50e14e 184 }
maxelior 1:5639bc1208d5 185
maxelior 1:5639bc1208d5 186
maxelior 1:5639bc1208d5 187
maxelior 1:5639bc1208d5 188
maxelior 1:5639bc1208d5 189