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
main.cpp@13:aa620707126d, 2017-05-08 (annotated)
- Committer:
- POTLESS_2
- Date:
- Mon May 08 13:15:35 2017 +0000
- Revision:
- 13:aa620707126d
- Parent:
- 12:9ac5be447764
- Child:
- 14:e6686a44db84
Version STM32L476RG fonctionnelle...
Who changed what in which revision?
User | Revision | Line number | New 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 | 13:aa620707126d | 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 | 13:aa620707126d | 23 | #define DEBUG(...) { 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 | 9:d945fa4be3a5 | 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 | 13:aa620707126d | 55 | void build_send_Message(int co2, int ppo2, float press){ |
POTLESS_2 | 13:aa620707126d | 56 | char buf[50]; |
POTLESS_2 | 13:aa620707126d | 57 | sprintf(buf,"%d %d %f\r\n", ppo2, co2, press); |
POTLESS_2 | 13:aa620707126d | 58 | uartServicePtr->writeString(buf); |
POTLESS_2 | 13:aa620707126d | 59 | } |
POTLESS_2 | 13:aa620707126d | 60 | |
POTLESS_2 | 13:aa620707126d | 61 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
POTLESS_2 | 13:aa620707126d | 62 | { |
POTLESS_2 | 13:aa620707126d | 63 | DEBUG("Disconnected!\n\r"); |
POTLESS_2 | 13:aa620707126d | 64 | DEBUG("Restarting the advertising process\n\r"); |
POTLESS_2 | 13:aa620707126d | 65 | ble.startAdvertising(); |
POTLESS_2 | 13:aa620707126d | 66 | } |
POTLESS_2 | 13:aa620707126d | 67 | |
POTLESS_2 | 13:aa620707126d | 68 | void onDataWritten(const GattWriteCallbackParams *params) |
POTLESS_2 | 13:aa620707126d | 69 | { |
POTLESS_2 | 13:aa620707126d | 70 | if ((uartServicePtr != NULL) && (params->handle == uartServicePtr->getTXCharacteristicHandle())) { |
POTLESS_2 | 13:aa620707126d | 71 | uint16_t bytesRead = params->len; |
POTLESS_2 | 13:aa620707126d | 72 | |
POTLESS_2 | 13:aa620707126d | 73 | // chaine reçue de l'app android (à dimensionner selon besoin) |
POTLESS_2 | 13:aa620707126d | 74 | char commande [bytesRead] ; |
POTLESS_2 | 13:aa620707126d | 75 | |
POTLESS_2 | 13:aa620707126d | 76 | // conversion de la commande uint8_t en chaine pour permettre comparaison avec commandes connues |
POTLESS_2 | 13:aa620707126d | 77 | for (int j=0;j<bytesRead;j++) { |
POTLESS_2 | 13:aa620707126d | 78 | commande [j] = (*((params->data)+j)); |
POTLESS_2 | 13:aa620707126d | 79 | } |
POTLESS_2 | 13:aa620707126d | 80 | |
POTLESS_2 | 13:aa620707126d | 81 | DEBUG("received %u bytes => ", bytesRead); |
POTLESS_2 | 13:aa620707126d | 82 | DEBUG("commande recue -%s-\r\n",commande); |
POTLESS_2 | 13:aa620707126d | 83 | DEBUG ( "comparaison a commande 'p' vaut %d\r\n",strcmp(commande, "p")); |
POTLESS_2 | 13:aa620707126d | 84 | DEBUG ( "comparaison a commande 'o' vaut %d\r\n",strcmp(commande, "o")); |
POTLESS_2 | 13:aa620707126d | 85 | DEBUG ( "comparaison a commande 'c' vaut %d\r\n",strcmp(commande, "c")); |
POTLESS_2 | 13:aa620707126d | 86 | |
POTLESS_2 | 13:aa620707126d | 87 | |
POTLESS_2 | 13:aa620707126d | 88 | DEBUG("Tous les capteurs = %d %d %d\r\n", ppO2, co2, pression); |
POTLESS_2 | 13:aa620707126d | 89 | build_send_Message(ppO2, co2, pression); |
POTLESS_2 | 13:aa620707126d | 90 | |
POTLESS_2 | 13:aa620707126d | 91 | } |
POTLESS_2 | 13:aa620707126d | 92 | |
POTLESS_2 | 13:aa620707126d | 93 | } |
POTLESS_2 | 13:aa620707126d | 94 | |
POTLESS_2 | 7:c5a5d8f678ff | 95 | //Fonction qui change le nom du fichier ouvert pour le LOG |
POTLESS_2 | 7:c5a5d8f678ff | 96 | FILE *nextLogFile(void) |
POTLESS_2 | 7:c5a5d8f678ff | 97 | { |
POTLESS_2 | 7:c5a5d8f678ff | 98 | static unsigned int fileNumber = 0; |
POTLESS_2 | 7:c5a5d8f678ff | 99 | FILE *filePtr = NULL; |
POTLESS_2 | 7:c5a5d8f678ff | 100 | do { |
POTLESS_2 | 7:c5a5d8f678ff | 101 | if (filePtr != NULL) |
POTLESS_2 | 7:c5a5d8f678ff | 102 | fclose(filePtr); |
POTLESS_2 | 7:c5a5d8f678ff | 103 | sprintf(fileName,"/sd/LOG_Capteurs_%04u.txt",fileNumber++); |
POTLESS_2 | 7:c5a5d8f678ff | 104 | filePtr = fopen(fileName,"r"); |
POTLESS_2 | 7:c5a5d8f678ff | 105 | } while (filePtr != NULL); |
POTLESS_2 | 7:c5a5d8f678ff | 106 | return fopen( fileName,"w"); |
POTLESS_2 | 7:c5a5d8f678ff | 107 | } |
POTLESS_2 | 7:c5a5d8f678ff | 108 | void SetTime() { |
POTLESS_2 | 7:c5a5d8f678ff | 109 | //Reglage date / heure depuis le terminal |
POTLESS_2 | 7:c5a5d8f678ff | 110 | struct tm t; |
POTLESS_2 | 7:c5a5d8f678ff | 111 | printf("Entrer la date et l'heure :\n"); |
POTLESS_2 | 7:c5a5d8f678ff | 112 | printf("YYYY MM DD HH MM SS [enter]\n"); |
POTLESS_2 | 7:c5a5d8f678ff | 113 | scanf("%d %d %d %d %d %d", &t.tm_year, &t.tm_mon, &t.tm_mday |
POTLESS_2 | 7:c5a5d8f678ff | 114 | , &t.tm_hour, &t.tm_min, &t.tm_sec); |
POTLESS_2 | 7:c5a5d8f678ff | 115 | t.tm_year = t.tm_year - 1900; |
POTLESS_2 | 7:c5a5d8f678ff | 116 | t.tm_mon = t.tm_mon - 1; |
POTLESS_2 | 7:c5a5d8f678ff | 117 | |
POTLESS_2 | 7:c5a5d8f678ff | 118 | // set the time |
POTLESS_2 | 7:c5a5d8f678ff | 119 | set_time(mktime(&t)); |
POTLESS_2 | 12:9ac5be447764 | 120 | } |
POTLESS_2 | 12:9ac5be447764 | 121 | |
POTLESS_2 | 12:9ac5be447764 | 122 | void init_DATALOG(){ |
POTLESS_2 | 12:9ac5be447764 | 123 | //Initialisation DATA LOG |
POTLESS_2 | 12:9ac5be447764 | 124 | serialMonit.printf("Initialisation SD card\r\n"); |
POTLESS_2 | 12:9ac5be447764 | 125 | fp = nextLogFile(); |
POTLESS_2 | 12:9ac5be447764 | 126 | |
POTLESS_2 | 12:9ac5be447764 | 127 | if (!fp) { |
POTLESS_2 | 12:9ac5be447764 | 128 | serialMonit.printf("Probleme SD card...Fin du programme...\r\n"); |
POTLESS_2 | 12:9ac5be447764 | 129 | //exit(0); |
POTLESS_2 | 12:9ac5be447764 | 130 | }else{ |
POTLESS_2 | 12:9ac5be447764 | 131 | serialMonit.printf("Nouveau fichier LOG cree = %s\r\n", fileName); |
POTLESS_2 | 12:9ac5be447764 | 132 | serialMonit.printf("\r\n", ""); |
POTLESS_2 | 12:9ac5be447764 | 133 | } |
POTLESS_2 | 12:9ac5be447764 | 134 | } |
POTLESS_2 | 12:9ac5be447764 | 135 | |
POTLESS_2 | 12:9ac5be447764 | 136 | void DATA_LOG(){ |
POTLESS_2 | 12:9ac5be447764 | 137 | time_t seconds = time(NULL); |
POTLESS_2 | 12:9ac5be447764 | 138 | if (fp) { |
POTLESS_2 | 12:9ac5be447764 | 139 | fprintf(fp, "%s,%d,%d,%f,%f,%f,%f\r\n", ctime(&seconds), co2 , ppO2, pression, Temp1, Temp2, Humi); |
POTLESS_2 | 12:9ac5be447764 | 140 | serialMonit.printf(" Enregistrement d'un point sur la carte SD\r\n"); |
POTLESS_2 | 12:9ac5be447764 | 141 | serialMonit.printf(" Nombre de points = %d\r\n", points); |
POTLESS_2 | 12:9ac5be447764 | 142 | serialMonit.printf("\r\n", ""); |
POTLESS_2 | 12:9ac5be447764 | 143 | points++; |
POTLESS_2 | 12:9ac5be447764 | 144 | }else{ |
POTLESS_2 | 12:9ac5be447764 | 145 | serialMonit.printf(" Probleme carte SD\r\n"); |
POTLESS_2 | 12:9ac5be447764 | 146 | } |
POTLESS_2 | 12:9ac5be447764 | 147 | } |
potless | 2:4a8bf1d53439 | 148 | /////////////////////////////////// |
potless | 2:4a8bf1d53439 | 149 | // fonction initialisation ///// |
potless | 2:4a8bf1d53439 | 150 | /////////////////////////////////// |
POTLESS_2 | 4:d84250f67dec | 151 | void setup() |
POTLESS_2 | 4:d84250f67dec | 152 | { |
POTLESS_2 | 7:c5a5d8f678ff | 153 | //Réglage de l'heure |
POTLESS_2 | 11:278bdb497ba3 | 154 | SetTime(); |
POTLESS_2 | 7:c5a5d8f678ff | 155 | |
POTLESS_2 | 12:9ac5be447764 | 156 | init_DATALOG(); |
POTLESS_2 | 7:c5a5d8f678ff | 157 | |
POTLESS_2 | 7:c5a5d8f678ff | 158 | //Initialisation capteurs |
POTLESS_2 | 8:e864edfe656e | 159 | arnsrs.Sensors_INIT(false, 5, SPOOLING, DIGI_FILTER32, CALIB_AIR); |
potless | 0:69d4b21d58e0 | 160 | } |
potless | 0:69d4b21d58e0 | 161 | |
potless | 1:bef7856b5c0a | 162 | ///////////////////////////////////// |
potless | 1:bef7856b5c0a | 163 | /// procédure principale ///////// |
potless | 1:bef7856b5c0a | 164 | ///////////////////////////////////// |
potless | 0:69d4b21d58e0 | 165 | |
POTLESS_2 | 4:d84250f67dec | 166 | int main() |
POTLESS_2 | 4:d84250f67dec | 167 | { |
POTLESS_2 | 7:c5a5d8f678ff | 168 | setup(); |
potless | 2:4a8bf1d53439 | 169 | |
POTLESS_2 | 13:aa620707126d | 170 | DEBUG("Initialising....\n\r"); |
POTLESS_2 | 13:aa620707126d | 171 | ble.init(); |
POTLESS_2 | 13:aa620707126d | 172 | ble.onDisconnection(disconnectionCallback); |
POTLESS_2 | 13:aa620707126d | 173 | ble.onDataWritten(onDataWritten); |
POTLESS_2 | 13:aa620707126d | 174 | |
POTLESS_2 | 13:aa620707126d | 175 | /* setup advertising */ |
POTLESS_2 | 13:aa620707126d | 176 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); |
POTLESS_2 | 13:aa620707126d | 177 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
POTLESS_2 | 13:aa620707126d | 178 | ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, |
POTLESS_2 | 13:aa620707126d | 179 | (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1); |
POTLESS_2 | 13:aa620707126d | 180 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, |
POTLESS_2 | 13:aa620707126d | 181 | (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed)); |
POTLESS_2 | 13:aa620707126d | 182 | |
POTLESS_2 | 13:aa620707126d | 183 | ble.setAdvertisingInterval(1000); /* 1000ms; in multiples of 0.625ms. */ |
POTLESS_2 | 13:aa620707126d | 184 | ble.startAdvertising(); |
POTLESS_2 | 13:aa620707126d | 185 | |
POTLESS_2 | 13:aa620707126d | 186 | UARTService uartService(ble); |
POTLESS_2 | 13:aa620707126d | 187 | uartServicePtr = &uartService; |
POTLESS_2 | 13:aa620707126d | 188 | |
POTLESS_2 | 13:aa620707126d | 189 | while (true) { |
POTLESS_2 | 13:aa620707126d | 190 | |
POTLESS_2 | 13:aa620707126d | 191 | ble.waitForEvent(); |
POTLESS_2 | 13:aa620707126d | 192 | |
POTLESS_2 | 13:aa620707126d | 193 | |
POTLESS_2 | 4:d84250f67dec | 194 | while (1) { |
POTLESS_2 | 5:e1d0dc7940a6 | 195 | |
POTLESS_2 | 6:1c2212891714 | 196 | wait(2); |
POTLESS_2 | 6:1c2212891714 | 197 | |
POTLESS_2 | 5:e1d0dc7940a6 | 198 | //RTC |
POTLESS_2 | 7:c5a5d8f678ff | 199 | time_t seconds = time(NULL); |
POTLESS_2 | 5:e1d0dc7940a6 | 200 | |
POTLESS_2 | 7:c5a5d8f678ff | 201 | printf(" Date / Heure = %s\r\n", ctime(&seconds)); |
POTLESS_2 | 5:e1d0dc7940a6 | 202 | |
POTLESS_2 | 4:d84250f67dec | 203 | //CO2 / H / T sur Cozir |
POTLESS_2 | 6:1c2212891714 | 204 | co2 = arnsrs.requestCO2(); |
POTLESS_2 | 6:1c2212891714 | 205 | Humi = arnsrs.requestHUMI(); |
POTLESS_2 | 6:1c2212891714 | 206 | Temp1 = arnsrs.requestTEMP(); |
POTLESS_2 | 5:e1d0dc7940a6 | 207 | |
POTLESS_2 | 6:1c2212891714 | 208 | //P / T sur MS5803_14BA |
POTLESS_2 | 5:e1d0dc7940a6 | 209 | pression = arnsrs.requestPress(); |
POTLESS_2 | 5:e1d0dc7940a6 | 210 | Temp2 = arnsrs.requestTemp(); |
POTLESS_2 | 5:e1d0dc7940a6 | 211 | |
POTLESS_2 | 5:e1d0dc7940a6 | 212 | //PPO2 sur ADS1015 |
POTLESS_2 | 8:e864edfe656e | 213 | ppO2 = arnsrs.requestPpO2(false); |
POTLESS_2 | 5:e1d0dc7940a6 | 214 | |
POTLESS_2 | 5:e1d0dc7940a6 | 215 | //Affichage sur moniteur série |
POTLESS_2 | 7:c5a5d8f678ff | 216 | serialMonit.printf(" CO2 = %d\r\n" , co2); |
POTLESS_2 | 6:1c2212891714 | 217 | serialMonit.printf(" Humidité = %f\r\n" , Humi); |
POTLESS_2 | 7:c5a5d8f678ff | 218 | serialMonit.printf(" Température = %f\r\n" ,Temp1); |
POTLESS_2 | 4:d84250f67dec | 219 | //P / T sur MS5803 |
POTLESS_2 | 6:1c2212891714 | 220 | serialMonit.printf(" Pression = %f\r\n", pression); |
POTLESS_2 | 7:c5a5d8f678ff | 221 | serialMonit.printf(" Température = %f\r\n", Temp2); |
POTLESS_2 | 4:d84250f67dec | 222 | //PPO2 sur ADS1015 |
POTLESS_2 | 6:1c2212891714 | 223 | serialMonit.printf(" PPO2 = %d\r\n", ppO2); |
POTLESS_2 | 5:e1d0dc7940a6 | 224 | serialMonit.printf("\r\n", ""); |
POTLESS_2 | 5:e1d0dc7940a6 | 225 | |
POTLESS_2 | 7:c5a5d8f678ff | 226 | //Data LOG |
POTLESS_2 | 12:9ac5be447764 | 227 | DATA_LOG(); |
POTLESS_2 | 7:c5a5d8f678ff | 228 | |
POTLESS_2 | 4:d84250f67dec | 229 | } |
POTLESS_2 | 13:aa620707126d | 230 | |
POTLESS_2 | 13:aa620707126d | 231 | } |
POTLESS_2 | 4:d84250f67dec | 232 | } |