une 30aine passés sans pb entre les trames

Dependencies:   ADS1015 ARNSRS_SENSORS DmTftLibrary MS5803_14BA SDFileSystem mbed

Fork of ARNSRS_SERVOS_USB_TFT by POTLESS

Committer:
POTLESS_2
Date:
Mon May 08 16:01:53 2017 +0000
Revision:
14:e6686a44db84
Parent:
13:aa620707126d
Child:
15:c9205e490740
Avec BLE fonctionnel....Reste ? optimiser, faire le m?nage...; Utilisation de ma m?thode pour envoyer (car au moins je la comprend...).; Le tout est assez lent je trouve....m?me en abaissant le wait ? 500 ms...

Who changed what in which revision?

UserRevisionLine numberNew contents of line
potless 1:bef7856b5c0a 1 /* procédure lecture capteur COZIR ou MINIR - CO2 seul
potless 1:bef7856b5c0a 2 sur broches F401RE SERIAL PA_11 et PA_12
potless 1:bef7856b5c0a 3 renvoie sur moniteur (IDE arduino par exemple) de la valeur lue
potless 2:4a8bf1d53439 4
potless 2:4a8bf1d53439 5 procédures possibles sont :
potless 2:4a8bf1d53439 6 request (char commande) - renvoie la valeur lue si la commande ne représente qu'un seul caractère (entre '')
potless 2:4a8bf1d53439 7 cozirSend (string commande) - permet d'envoyer des commandes multicaractères (entre "") - exemple A 32 - donne la réponse obtenue
potless 2:4a8bf1d53439 8
potless 2:4a8bf1d53439 9
potless 2:4a8bf1d53439 10 la fonction cozirSend est davantage "sécurisée"
POTLESS_2 4:d84250f67dec 11 */
potless 0:69d4b21d58e0 12
potless 0:69d4b21d58e0 13
potless 1:bef7856b5c0a 14 #include "mbed.h"
POTLESS_2 5:e1d0dc7940a6 15 #include "SDFileSystem.h"
POTLESS_2 5:e1d0dc7940a6 16 #include "ARNSRS_SENSORS.h"
POTLESS_2 13:aa620707126d 17 #include "ble/BLE.h"
POTLESS_2 14:e6686a44db84 18
POTLESS_2 13:aa620707126d 19 #include "ble/services/UARTService.h"
POTLESS_2 13:aa620707126d 20 #include <string>
POTLESS_2 13:aa620707126d 21
POTLESS_2 13:aa620707126d 22 //Ecrit dans le moniteur série de l'ordi à 9600 bds
POTLESS_2 14:e6686a44db84 23 #define DEBUG(...) { serialMonit.printf(__VA_ARGS__); }
POTLESS_2 13:aa620707126d 24
POTLESS_2 13:aa620707126d 25 //Init BLE
POTLESS_2 13:aa620707126d 26 BLEDevice ble;
POTLESS_2 13:aa620707126d 27 UARTService *uartServicePtr;
POTLESS_2 5:e1d0dc7940a6 28
POTLESS_2 5:e1d0dc7940a6 29 //Init de la lib ARNSRS;
POTLESS_2 5:e1d0dc7940a6 30 ARNSRS arnsrs;
POTLESS_2 5:e1d0dc7940a6 31
POTLESS_2 5:e1d0dc7940a6 32 //Quelque FLAG...
POTLESS_2 5:e1d0dc7940a6 33 bool FLAG_SIMPLE_TIME = false;
potless 1:bef7856b5c0a 34
POTLESS_2 9:d945fa4be3a5 35 //COM Série vers l'ordi, Serial 2 par défaut
POTLESS_2 14:e6686a44db84 36 Serial serialMonit (USBTX,USBRX);
potless 2:4a8bf1d53439 37
POTLESS_2 5:e1d0dc7940a6 38 //Variable des capteurs
POTLESS_2 12:9ac5be447764 39 int co2 = 0;
POTLESS_2 12:9ac5be447764 40 int ppO2 = 0;
POTLESS_2 12:9ac5be447764 41 float pression = 0;
POTLESS_2 12:9ac5be447764 42 float Temp1 = 0;
POTLESS_2 12:9ac5be447764 43 float Temp2 = 0;
POTLESS_2 12:9ac5be447764 44 float Humi = 0;
POTLESS_2 12:9ac5be447764 45
POTLESS_2 5:e1d0dc7940a6 46 string DateHeure;
potless 2:4a8bf1d53439 47
POTLESS_2 6:1c2212891714 48 //SD card
POTLESS_2 6:1c2212891714 49 SDFileSystem sd(D11, D12, D13, D10, "sd"); // MOSI, MISO, SCK, CS
POTLESS_2 6:1c2212891714 50 FILE *fp;
POTLESS_2 7:c5a5d8f678ff 51 char fileName[32];
POTLESS_2 7:c5a5d8f678ff 52 int points = 1;
POTLESS_2 4:d84250f67dec 53
POTLESS_2 13:aa620707126d 54 //Fonction pour créer et envoyer la chaine d'in en BLE
POTLESS_2 14:e6686a44db84 55 //On balance tout en float, même pour des int...Qu peut le plus peut le moins...
POTLESS_2 14:e6686a44db84 56 //3 par 3 ça passe juste avec un décimal....Sinon faire deux par deux ???
POTLESS_2 14:e6686a44db84 57 void build_send_Message(string com, float A, float B, float C)
POTLESS_2 14:e6686a44db84 58 {
POTLESS_2 14:e6686a44db84 59 char buf[50];
POTLESS_2 14:e6686a44db84 60 sprintf(buf,"%s %.1f %.1f %.1f\r\n", com, A, B, C);
POTLESS_2 14:e6686a44db84 61 uartServicePtr->writeString(buf);
POTLESS_2 14:e6686a44db84 62 }
POTLESS_2 13:aa620707126d 63
POTLESS_2 13:aa620707126d 64 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
POTLESS_2 13:aa620707126d 65 {
POTLESS_2 14:e6686a44db84 66 //DEBUG("Disconnected!\n\r");
POTLESS_2 14:e6686a44db84 67 //DEBUG("Restarting the advertising process\n\r");
POTLESS_2 13:aa620707126d 68 ble.startAdvertising();
POTLESS_2 13:aa620707126d 69 }
POTLESS_2 13:aa620707126d 70
POTLESS_2 13:aa620707126d 71 void onDataWritten(const GattWriteCallbackParams *params)
POTLESS_2 13:aa620707126d 72 {
POTLESS_2 13:aa620707126d 73 if ((uartServicePtr != NULL) && (params->handle == uartServicePtr->getTXCharacteristicHandle())) {
POTLESS_2 13:aa620707126d 74 uint16_t bytesRead = params->len;
POTLESS_2 14:e6686a44db84 75 /*
POTLESS_2 14:e6686a44db84 76 // les chaines de réponse
POTLESS_2 14:e6686a44db84 77 /uint8_t press_mb_ToSend[14];
POTLESS_2 14:e6686a44db84 78 uint8_t co2_ppm_ToSend[14];
POTLESS_2 14:e6686a44db84 79 uint8_t o2_mb_ToSend[14];
POTLESS_2 14:e6686a44db84 80
POTLESS_2 14:e6686a44db84 81 // fabrication des chaines de réponse
POTLESS_2 14:e6686a44db84 82 // on suppose que chaque donnee peut être représentée par 5 digits
POTLESS_2 14:e6686a44db84 83 char s_press_mb [50];
POTLESS_2 14:e6686a44db84 84 sprintf(s_press_mb,"PRESS %i mb ",press_mb); // mef ! laisser les espaces de la fin
POTLESS_2 14:e6686a44db84 85
POTLESS_2 14:e6686a44db84 86 char s_co2_ppm [50];
POTLESS_2 14:e6686a44db84 87 sprintf(s_co2_ppm,"CO2 %i ppm ",co2_ppm);
POTLESS_2 14:e6686a44db84 88
POTLESS_2 14:e6686a44db84 89 char s_o2_mb [50];
POTLESS_2 14:e6686a44db84 90 sprintf(s_o2_mb,"O2 %i mb ",o2_mb);
POTLESS_2 14:e6686a44db84 91
POTLESS_2 14:e6686a44db84 92 // convertir les chaines en uint8_t
POTLESS_2 14:e6686a44db84 93 for(int j=0;j<sizeof(s_press_mb);j++){ // hypothèse : les chaines press, co2, o2 ont la même longeur
POTLESS_2 14:e6686a44db84 94 press_mb_ToSend[j]=s_press_mb[j];
POTLESS_2 14:e6686a44db84 95 co2_ppm_ToSend[j]=s_co2_ppm[j];
POTLESS_2 14:e6686a44db84 96 o2_mb_ToSend[j]=s_o2_mb[j];
POTLESS_2 14:e6686a44db84 97 }
POTLESS_2 14:e6686a44db84 98 */
POTLESS_2 14:e6686a44db84 99 // chaine reçue de l'app android (à dimensionner selon besoin)
POTLESS_2 14:e6686a44db84 100 char commande [bytesRead] ;
POTLESS_2 14:e6686a44db84 101
POTLESS_2 13:aa620707126d 102 // conversion de la commande uint8_t en chaine pour permettre comparaison avec commandes connues
POTLESS_2 14:e6686a44db84 103 for (int j=0; j<bytesRead; j++) {
POTLESS_2 14:e6686a44db84 104 commande [j] = (*((params->data)+j));
POTLESS_2 13:aa620707126d 105 }
POTLESS_2 14:e6686a44db84 106
POTLESS_2 14:e6686a44db84 107 /*
POTLESS_2 13:aa620707126d 108 DEBUG("received %u bytes => ", bytesRead);
POTLESS_2 13:aa620707126d 109 DEBUG("commande recue -%s-\r\n",commande);
POTLESS_2 13:aa620707126d 110 DEBUG ( "comparaison a commande 'p' vaut %d\r\n",strcmp(commande, "p"));
POTLESS_2 13:aa620707126d 111 DEBUG ( "comparaison a commande 'o' vaut %d\r\n",strcmp(commande, "o"));
POTLESS_2 13:aa620707126d 112 DEBUG ( "comparaison a commande 'c' vaut %d\r\n",strcmp(commande, "c"));
POTLESS_2 14:e6686a44db84 113 DEBUG ( "comparaison a commande 't' vaut %d\r\n",strcmp(commande, "t"));
POTLESS_2 14:e6686a44db84 114 DEBUG ( "comparaison a commande 'u' vaut %d\r\n",strcmp(commande, "t"));
POTLESS_2 14:e6686a44db84 115
POTLESS_2 14:e6686a44db84 116 // envoi des réponses en fonction de la commande recue
POTLESS_2 14:e6686a44db84 117 if (strcmp(commande, "p") == 0) {
POTLESS_2 14:e6686a44db84 118 DEBUG("s_press_mb %s +++> data to send %s \r\n",s_press_mb,press_mb_ToSend);
POTLESS_2 14:e6686a44db84 119 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), press_mb_ToSend, sizeof(press_mb_ToSend));
POTLESS_2 14:e6686a44db84 120 }
POTLESS_2 14:e6686a44db84 121 if (strcmp(commande, "c") == 0) {
POTLESS_2 14:e6686a44db84 122 DEBUG("s_co2_ppm %s +++> data to send %s \r\n",s_co2_ppm,co2_ppm_ToSend);
POTLESS_2 14:e6686a44db84 123 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), co2_ppm_ToSend, sizeof(co2_ppm_ToSend));
POTLESS_2 14:e6686a44db84 124 }
POTLESS_2 14:e6686a44db84 125 if (strcmp(commande, "o") == 0) {
POTLESS_2 14:e6686a44db84 126 DEBUG("s_o2_mb %s +++> data to send %s \r\n",s_o2_mb,o2_mb_ToSend);
POTLESS_2 14:e6686a44db84 127 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), o2_mb_ToSend, sizeof(o2_mb_ToSend));
POTLESS_2 14:e6686a44db84 128 }
POTLESS_2 14:e6686a44db84 129 else */
POTLESS_2 14:e6686a44db84 130 if (strcmp(commande, "t") == 0) {
POTLESS_2 14:e6686a44db84 131 build_send_Message("t", ppO2, co2, pression);
POTLESS_2 14:e6686a44db84 132 }else
POTLESS_2 14:e6686a44db84 133 if (strcmp(commande, "u") == 0) {
POTLESS_2 14:e6686a44db84 134 build_send_Message("u", Temp1, Temp2, Humi);
POTLESS_2 14:e6686a44db84 135 }
POTLESS_2 14:e6686a44db84 136 }
POTLESS_2 13:aa620707126d 137 }
POTLESS_2 14:e6686a44db84 138
POTLESS_2 7:c5a5d8f678ff 139 //Fonction qui change le nom du fichier ouvert pour le LOG
POTLESS_2 7:c5a5d8f678ff 140 FILE *nextLogFile(void)
POTLESS_2 7:c5a5d8f678ff 141 {
POTLESS_2 7:c5a5d8f678ff 142 static unsigned int fileNumber = 0;
POTLESS_2 7:c5a5d8f678ff 143 FILE *filePtr = NULL;
POTLESS_2 7:c5a5d8f678ff 144 do {
POTLESS_2 7:c5a5d8f678ff 145 if (filePtr != NULL)
POTLESS_2 7:c5a5d8f678ff 146 fclose(filePtr);
POTLESS_2 7:c5a5d8f678ff 147 sprintf(fileName,"/sd/LOG_Capteurs_%04u.txt",fileNumber++);
POTLESS_2 7:c5a5d8f678ff 148 filePtr = fopen(fileName,"r");
POTLESS_2 7:c5a5d8f678ff 149 } while (filePtr != NULL);
POTLESS_2 7:c5a5d8f678ff 150 return fopen( fileName,"w");
POTLESS_2 7:c5a5d8f678ff 151 }
POTLESS_2 14:e6686a44db84 152 void SetTime()
POTLESS_2 14:e6686a44db84 153 {
POTLESS_2 14:e6686a44db84 154 //Reglage date / heure depuis le terminal
POTLESS_2 7:c5a5d8f678ff 155 struct tm t;
POTLESS_2 14:e6686a44db84 156 DEBUG("Entrer la date et l'heure :\n");
POTLESS_2 14:e6686a44db84 157 DEBUG("YYYY MM DD HH MM SS [enter]\n");
POTLESS_2 7:c5a5d8f678ff 158 scanf("%d %d %d %d %d %d", &t.tm_year, &t.tm_mon, &t.tm_mday
POTLESS_2 14:e6686a44db84 159 , &t.tm_hour, &t.tm_min, &t.tm_sec);
POTLESS_2 7:c5a5d8f678ff 160 t.tm_year = t.tm_year - 1900;
POTLESS_2 7:c5a5d8f678ff 161 t.tm_mon = t.tm_mon - 1;
POTLESS_2 14:e6686a44db84 162
POTLESS_2 7:c5a5d8f678ff 163 // set the time
POTLESS_2 14:e6686a44db84 164 set_time(mktime(&t));
POTLESS_2 12:9ac5be447764 165 }
POTLESS_2 12:9ac5be447764 166
POTLESS_2 14:e6686a44db84 167 void init_DATALOG()
POTLESS_2 14:e6686a44db84 168 {
POTLESS_2 12:9ac5be447764 169 //Initialisation DATA LOG
POTLESS_2 14:e6686a44db84 170 DEBUG("Initialisation SD card\r\n");
POTLESS_2 12:9ac5be447764 171 fp = nextLogFile();
POTLESS_2 14:e6686a44db84 172
POTLESS_2 14:e6686a44db84 173 if (!fp) {
POTLESS_2 14:e6686a44db84 174 DEBUG("Probleme SD card...Fin du programme...\r\n");
POTLESS_2 14:e6686a44db84 175 //exit(0);
POTLESS_2 14:e6686a44db84 176 } else {
POTLESS_2 14:e6686a44db84 177 DEBUG("Nouveau fichier LOG cree = %s\r\n", fileName);
POTLESS_2 14:e6686a44db84 178 DEBUG("\r\n", "");
POTLESS_2 12:9ac5be447764 179 }
POTLESS_2 14:e6686a44db84 180 }
POTLESS_2 14:e6686a44db84 181
POTLESS_2 14:e6686a44db84 182 void DATA_LOG()
POTLESS_2 14:e6686a44db84 183 {
POTLESS_2 12:9ac5be447764 184 time_t seconds = time(NULL);
POTLESS_2 12:9ac5be447764 185 if (fp) {
POTLESS_2 14:e6686a44db84 186 fprintf(fp, "%s,%d,%d,%f,%f,%f,%f\r\n", ctime(&seconds), co2 , ppO2, pression, Temp1, Temp2, Humi);
POTLESS_2 14:e6686a44db84 187 DEBUG(" Enregistrement d'un point sur la carte SD\r\n");
POTLESS_2 14:e6686a44db84 188 DEBUG(" Nombre de points = %d\r\n", points);
POTLESS_2 14:e6686a44db84 189 DEBUG("\r\n", "");
POTLESS_2 14:e6686a44db84 190 points++;
POTLESS_2 14:e6686a44db84 191 } else {
POTLESS_2 14:e6686a44db84 192 DEBUG(" Probleme carte SD\r\n");
POTLESS_2 14:e6686a44db84 193 }
POTLESS_2 14:e6686a44db84 194 }
potless 2:4a8bf1d53439 195 ///////////////////////////////////
potless 2:4a8bf1d53439 196 // fonction initialisation /////
potless 2:4a8bf1d53439 197 ///////////////////////////////////
POTLESS_2 4:d84250f67dec 198 void setup()
POTLESS_2 4:d84250f67dec 199 {
POTLESS_2 7:c5a5d8f678ff 200 //Réglage de l'heure
POTLESS_2 11:278bdb497ba3 201 SetTime();
POTLESS_2 14:e6686a44db84 202
POTLESS_2 12:9ac5be447764 203 init_DATALOG();
POTLESS_2 14:e6686a44db84 204
POTLESS_2 7:c5a5d8f678ff 205 //Initialisation capteurs
POTLESS_2 8:e864edfe656e 206 arnsrs.Sensors_INIT(false, 5, SPOOLING, DIGI_FILTER32, CALIB_AIR);
potless 0:69d4b21d58e0 207 }
potless 0:69d4b21d58e0 208
potless 1:bef7856b5c0a 209 /////////////////////////////////////
potless 1:bef7856b5c0a 210 /// procédure principale /////////
potless 1:bef7856b5c0a 211 /////////////////////////////////////
potless 0:69d4b21d58e0 212
POTLESS_2 4:d84250f67dec 213 int main()
POTLESS_2 4:d84250f67dec 214 {
POTLESS_2 7:c5a5d8f678ff 215 setup();
potless 2:4a8bf1d53439 216
POTLESS_2 14:e6686a44db84 217 //DEBUG("Initialising....\n\r");
POTLESS_2 13:aa620707126d 218 ble.init();
POTLESS_2 13:aa620707126d 219 ble.onDisconnection(disconnectionCallback);
POTLESS_2 13:aa620707126d 220 ble.onDataWritten(onDataWritten);
POTLESS_2 14:e6686a44db84 221
POTLESS_2 13:aa620707126d 222 /* setup advertising */
POTLESS_2 13:aa620707126d 223 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
POTLESS_2 13:aa620707126d 224 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
POTLESS_2 13:aa620707126d 225 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
POTLESS_2 13:aa620707126d 226 (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1);
POTLESS_2 13:aa620707126d 227 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
POTLESS_2 13:aa620707126d 228 (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
POTLESS_2 14:e6686a44db84 229
POTLESS_2 13:aa620707126d 230 ble.setAdvertisingInterval(1000); /* 1000ms; in multiples of 0.625ms. */
POTLESS_2 13:aa620707126d 231 ble.startAdvertising();
POTLESS_2 14:e6686a44db84 232
POTLESS_2 13:aa620707126d 233 UARTService uartService(ble);
POTLESS_2 13:aa620707126d 234 uartServicePtr = &uartService;
POTLESS_2 14:e6686a44db84 235
POTLESS_2 14:e6686a44db84 236
POTLESS_2 4:d84250f67dec 237 while (1) {
POTLESS_2 5:e1d0dc7940a6 238
POTLESS_2 14:e6686a44db84 239 ble.waitForEvent();
POTLESS_2 14:e6686a44db84 240
POTLESS_2 14:e6686a44db84 241 wait_ms(500);
POTLESS_2 14:e6686a44db84 242
POTLESS_2 5:e1d0dc7940a6 243 //RTC
POTLESS_2 7:c5a5d8f678ff 244 time_t seconds = time(NULL);
POTLESS_2 14:e6686a44db84 245
POTLESS_2 14:e6686a44db84 246 DEBUG(" Date / Heure = %s\r\n", ctime(&seconds));
POTLESS_2 14:e6686a44db84 247
POTLESS_2 4:d84250f67dec 248 //CO2 / H / T sur Cozir
POTLESS_2 6:1c2212891714 249 co2 = arnsrs.requestCO2();
POTLESS_2 6:1c2212891714 250 Humi = arnsrs.requestHUMI();
POTLESS_2 14:e6686a44db84 251 Temp1 = arnsrs.requestTEMP();
POTLESS_2 14:e6686a44db84 252
POTLESS_2 6:1c2212891714 253 //P / T sur MS5803_14BA
POTLESS_2 5:e1d0dc7940a6 254 pression = arnsrs.requestPress();
POTLESS_2 5:e1d0dc7940a6 255 Temp2 = arnsrs.requestTemp();
POTLESS_2 5:e1d0dc7940a6 256
POTLESS_2 5:e1d0dc7940a6 257 //PPO2 sur ADS1015
POTLESS_2 8:e864edfe656e 258 ppO2 = arnsrs.requestPpO2(false);
POTLESS_2 5:e1d0dc7940a6 259
POTLESS_2 5:e1d0dc7940a6 260 //Affichage sur moniteur série
POTLESS_2 14:e6686a44db84 261 DEBUG(" CO2 = %d\r\n" , co2);
POTLESS_2 14:e6686a44db84 262 DEBUG(" Humidité = %f\r\n" , Humi);
POTLESS_2 14:e6686a44db84 263 DEBUG(" Température = %f\r\n" ,Temp1);
POTLESS_2 4:d84250f67dec 264 //P / T sur MS5803
POTLESS_2 14:e6686a44db84 265 DEBUG(" Pression = %f\r\n", pression);
POTLESS_2 14:e6686a44db84 266 DEBUG(" Température = %f\r\n", Temp2);
POTLESS_2 4:d84250f67dec 267 //PPO2 sur ADS1015
POTLESS_2 14:e6686a44db84 268 DEBUG(" PPO2 = %d\r\n", ppO2);
POTLESS_2 14:e6686a44db84 269 DEBUG("\r\n", "");
POTLESS_2 5:e1d0dc7940a6 270
POTLESS_2 7:c5a5d8f678ff 271 //Data LOG
POTLESS_2 12:9ac5be447764 272 DATA_LOG();
POTLESS_2 14:e6686a44db84 273
POTLESS_2 13:aa620707126d 274 }
POTLESS_2 4:d84250f67dec 275 }