#include "LoRaWan_APP.h" #include "defines.h" #include "configuration.h" #include #include #include // from config.cpp extern config_t myConfig; /*LoraWan channelsmask, default channels 0-7*/ uint16_t userChannelsMask[6]={ 0x00FF,0x0000,0x0000,0x0000,0x0000,0x0000 }; /*LoraWan Class, Class A and Class C are supported*/ DeviceClass_t loraWanClass = CLASS_A; /*the application data transmission duty cycle. value in [ms].*/ uint32_t appTxDutyCycle = SEND_PERIOD; /*ADR enable*/ bool loraWanAdr = true; /* Indicates if the node is sending confirmed or unconfirmed messages */ bool isTxConfirmed = true; /* Application port */ uint8_t appPort = 2; /*! * Number of trials to transmit the frame, if the LoRaMAC layer did not * receive an acknowledgment. The MAC performs a datarate adaptation, * according to the LoRaWAN Specification V1.0.2, chapter 18.4, according * to the following table: * * Transmission nb | Data Rate * ----------------|----------- * 1 (first) | DR * 2 | DR * 3 | max(DR-1,0) * 4 | max(DR-1,0) * 5 | max(DR-2,0) * 6 | max(DR-2,0) * 7 | max(DR-3,0) * 8 | max(DR-3,0) * * Note, that if NbTrials is set to 1 or 2, the MAC will not decrease * the datarate, in case the LoRaMAC layer did not receive an acknowledgment */ uint8_t confirmedNbTrials = 4; OneWire oneWire(ONE_WIRE); DallasTemperature DS18B20(&oneWire); /* Prepares the payload of the frame */ static void prepareTxFrame( uint8_t port ) { struct { uint16_t status; uint16_t u_bat; struct { uint64_t addr; int32_t value; } __attribute__((packed)) sensors[NUM_OF_SENSORS]; } __attribute__((packed)) msg; memset(&msg, 0, sizeof(msg)); DS18B20.begin(); DS18B20.requestTemperatures(); uint8_t cnt = DS18B20.getDS18Count(); cnt = (cnt > NUM_OF_SENSORS) ? NUM_OF_SENSORS : cnt; for (uint8_t i = 0; i < cnt; i++) { DS18B20.getAddress(((uint8_t*)(&(msg.sensors[i].addr))), i); Serial.printf("%d: %016llx\n\r", i, msg.sensors[i].addr); msg.sensors[i].value = DS18B20.getTemp(((const uint8_t*)(&(msg.sensors[i].addr)))); Serial.printf("v: %08x\n\r", msg.sensors[i].value); } msg.u_bat = analogRead(U_ADC_IN); Serial.printf("u_bat: %d\n\r", msg.u_bat); appDataSize = sizeof(msg); memcpy(&appData, (void*)&msg, appDataSize); Serial.println("Send"); } void productionSetup() { Serial.println("Starting"); Mcu.begin(); deviceState = DEVICE_STATE_INIT; } void productionLoop() { static uint32_t goingToSleepTime = 0; static uint8_t subStateSleep = 0; static uint32_t myCycleTime = 0; if (deviceState != DEVICE_STATE_SLEEP) { Serial.printf("State: %d\n\r", deviceState); } switch( deviceState ) { case DEVICE_STATE_INIT: LoRaWAN.generateDeveuiByChipID(); LoRaWAN.init(loraWanClass,loraWanRegion); break; case DEVICE_STATE_JOIN: Serial.println("Joining"); LoRaWAN.join(); break; case DEVICE_STATE_SEND: digitalWrite(LED_BLUE, HIGH); Serial.println("sending"); prepareTxFrame( appPort ); LoRaWAN.send(); deviceState = DEVICE_STATE_CYCLE; break; case DEVICE_STATE_CYCLE: digitalWrite(LED_BLUE, LOW); // Schedule next packet transmission txDutyCycleTime = appTxDutyCycle + randr( -APP_TX_DUTYCYCLE_RND, APP_TX_DUTYCYCLE_RND ); LoRaWAN.cycle(txDutyCycleTime); deviceState = DEVICE_STATE_SLEEP; break; case DEVICE_STATE_SLEEP: LoRaWAN.sleep(loraWanClass); break; default: deviceState = DEVICE_STATE_INIT; break; } }