diff --git a/src/app/app.ino b/src/app/app.ino new file mode 100644 index 0000000..df8522d --- /dev/null +++ b/src/app/app.ino @@ -0,0 +1,199 @@ +/** +* Code source de l'application (qui a servi de démo pour la présentation finale) +* Anatole SCHRAMM-HENRY et Tim THUREL +* +* Récupère 16 mesures de température (une barrette) et les envoies en LoRa à interval de temps régulier +**/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define RADIO_ENABLED +#define ONE_CHANNEL_GW +//INTERVAL_ENVOI en secondes (15 minutes) +#define INTERVAL_ENVOI 15*60 + +//16 CS, 12 MISO +LTC2439 adc(16,12); + +//Beta de 3380, Résistance à 25 °C de 10K Ohm +ThermistorSettings thermistorSettings(3380, 10000); + +//16 thermistances sur 1 rangée, résistance de précision de 1k Ohm +MeasureUnit measureUnit(16, 1000, thermistorSettings, adc); + +//Objet de création des trames LoRa +PayloadFormatter payloadFormatter(2,8); + +RTC_DS1307 rtc; +DateTime payloadDate; +STS21 sts21; + +boolean data(false); +uint8_t *payload(NULL), size(0), _channel(0); +double *tempArray = NULL; + +boolean sendTriggered(false); +uint16_t counterSave(1); +unsigned long ts(millis()); + +/** +* Partie d'initialisation de la radio +* et définition des identifiants TTN +**/ + +void os_getArtEui (u1_t* buf) { } +void os_getDevEui (u1_t* buf) { } +void os_getDevKey (u1_t* buf) { } + +static u1_t NWKSKEY[16] = { 0x1F, 0x9E, 0xE2, 0x7A, 0xC8, 0xBA, 0xE8, 0xEA, 0xF5, 0xC2, 0x5E, 0x47, 0x5D, 0xE0, 0x77, 0x55 }; +static u1_t APPSKEY[16] = { 0x3B, 0x89, 0x86, 0x96, 0xBB, 0xAA, 0x38, 0x1E, 0x1F, 0xC4, 0xAD, 0x03, 0xEF, 0x3F, 0x56, 0x12 }; +static u4_t DEVADDR = 0x260113D3; + +u1_t dio[3] = {15,3,LMIC_UNUSED_PIN}; +PinMap pinMap(2, LMIC_UNUSED_PIN, 0, dio); +LoRaRadio radio(pinMap, DR_SF9); + +void downlinkHandler(u1_t length, u1_t dataBeg, u1_t *data) +{ + Serial.println("Downlink received : "); + for(uint8_t i(0); i < length; i++) + { + Serial.printf("%u -> %d\n",i,data[dataBeg + i]); + } + Serial.println(); + + //Action en fonction de l'octet de commande + switch(data[dataBeg+0]) + { + case 0x01://Mise à jour de l'heure + //Octets suivants: + //2 jour 3 mois 4 année 5 heures 6 minutes + if(length == 6) + { + Serial.printf("dd: %u, m: %u, yyyy: %d, hh: %u, mm: %u\n", data[dataBeg+1], data[dataBeg+2], data[dataBeg+3]+2000, data[dataBeg+4], data[dataBeg+5]); + } + else + Serial.println("Action réglage RTC : paramètres insuffisants"); + break; + case 0x02: + break; + default: + Serial.println("Action inconnue"); + } +} + +void sendCompleteHandler() +{ + Serial.printf("Send is done !\n"); +} + +void setup() +{ + Serial.begin(115200, SERIAL_8N1, SERIAL_TX_ONLY); + //Met le pin 3 RX en GPIO + pinMode(3, FUNCTION_3); + delay(1000); + Serial.println("Start setup"); + WiFi.mode(WIFI_OFF); + + //Initialisation de la radio + #ifdef RADIO_ENABLED + radio.init(); + radio.setTTNSession(0x1, DEVADDR, NWKSKEY, APPSKEY); + radio.setRadioEUChannels(); + #ifdef ONE_CHANNEL_GW + radio.disableAllEUChannelsBut(0); + #endif + /** + * La directive setMCUClockError() permet de laisser une fenêtre plus grande pour le slot de + * réception (Downlink). En effet ce slot doit durer 2 secondes et il peut durer moins en raison + * d'imprécisions d'horloge. + **/ + radio.setMCUClockError(); + #endif + //Enregistrement des callbacks d'evenements + radio.setDownlinkHandler(&(downlinkHandler)); + radio.setSendCompleteHandler(&(sendCompleteHandler)); + + if(rtc.begin()) + Serial.println("RTC Ok!"); + else + Serial.println("RTC Fail!"); + + if(sts21.begin()) + { + Serial.println("Sensor present !"); + sts21.setResolution(STS21::RES_14); + } + else + Serial.println("Sensor missing !"); + + Serial.println("End setup"); + Serial.println("| T1 | T2 | T3 | T4 | T5 | T6 | T7 | T8 | T9 | T10 | T11 | T12 | T13 | T14 | T15 | T16 |"); + +} + +void loop() { + //Version asynchrone : + measureUnit.startTemperatureMeasurement(); + + //On peut tester si la conversion est terminée avec : + //if(measureUnit.isMeasurementReady()) + + //measureUnit.getAsyncTemperatures() renvoie NULL si la recupération de la température n'est pas terminée + tempArray = measureUnit.getAsyncTemperatures(); + + double externalTemp = sts21.getTemperature(); + + if(tempArray != NULL) + { + Serial.print("|"); + for(int i(0); i < 16; i++) + { + Serial.print(" ");Serial.print(tempArray[i],2);Serial.print(" |"); + } + + //On affiche la trame associée: + payloadFormatter.startSession(1); + payloadDate = rtc.now(); + size = payloadFormatter.buildPayload(&payload, &payloadDate, externalTemp,tempArray); + if(size != 0) + { + //Serial.print("LoRa packet --> ");Serial.print("size : ");Serial.print(size);Serial.println(" bytes"); + for(int i(0); i < size; i++) + { + payload[i] <= 0x0F ? Serial.print("0") : Serial.print(""); Serial.print(payload[i], HEX); Serial.print(" "); + } + Serial.printf("|%u-%u-%u %u:%u ext temp : %.2f \n", payloadDate.day(),payloadDate.month(),payloadDate.year(),payloadDate.hour(),payloadDate.minute(), externalTemp); + } + else + Serial.print("Failed to build LoRa packet"); + + payloadFormatter.endSession(); + + #ifdef RADIO_ENABLED + + //Envoie des trames à interval de temps régulier + if(millis() - ts > INTERVAL_ENVOI*1000 && size != 0) + { + Serial.println("Sending data"); + radio.send(1, payload, size); + + ts = millis(); + } + #endif + } + + #ifdef RADIO_ENABLED + radio.run(); + #endif + measureUnit.run(); +}