#include "LoRaWan_APP.h" #include "defines.h" #include "configuration.h" #include #include #include #include "HT_SSD1306Wire.h" extern SSD1306Wire display; // 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_C; /*the application data transmission duty cycle. value in [ms].*/ uint32_t appTxDutyCycle = 15000; /*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); bool firstrun = true; char *labels; uint8_t cnt; /* Prepares the payload of the frame */ static void prepareTxFrame( uint8_t port ) { display.clear(); DS18B20.begin(); cnt = DS18B20.getDS18Count(); Serial.printf("cnt: %d\n\r", cnt); const uint8_t SLOT_WIDTH = 1 + 8 + 4; // 1 byte statue, 8 bytes address, 4 bytes value // status: 00: regular sending // 01: very first sending, please send label mapping appDataSize = 1 + (cnt * SLOT_WIDTH); uint8_t *buf = appData; char *tmpLabels; if (firstrun) { *buf = 0x01; firstrun = false; labels = (char*) malloc(cnt * (LABEL_LENGTH + 1)); memset(labels, 0, cnt * (LABEL_LENGTH + 1)); tmpLabels = labels; for (uint8_t i = 0; i < cnt; i++) { snprintf(tmpLabels, LABEL_LENGTH + 1, "Sens%d", i); tmpLabels += (LABEL_LENGTH + 1); } } else { *buf = 0x00; } buf++; tmpLabels = labels; uint8_t yline = 0; for (uint8_t i = 0; i < cnt; i++) { uint8_t *addr = (buf + (i * SLOT_WIDTH)); uint32_t *value = (uint32_t*) (buf + (i * SLOT_WIDTH) + 8); DS18B20.getAddress(addr, 0); Serial.printf("%02x %02x %02x %02x %02x %02x %02x %02x\n\r", addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], addr[6], addr[7]); DS18B20.requestTemperatures(); // send the command to get temperatures *value = DS18B20.getTemp(addr); Serial.printf("tempC: %08x\n\r", *value); float tempC = ((float)*value) / 128; char dispbuf[128]; sprintf(dispbuf, "%s: %.2f °C", tmpLabels, tempC); tmpLabels += (LABEL_LENGTH + 1); display.drawString(1, yline, dispbuf); yline += 17; } display.display(); } void downLinkDataHandle(McpsIndication_t *mcpsIndication) { Serial.printf("+REV DATA:%s,RXSIZE %d,PORT %d\r\n",mcpsIndication->RxSlot?"RXWIN2":"RXWIN1",mcpsIndication->BufferSize,mcpsIndication->Port); Serial.print("+REV DATA:"); if (mcpsIndication->BufferSize != cnt * (LABEL_LENGTH + 1)) { Serial.println("illegal number of octets in downlink message"); } else { for(uint8_t i=0;iBufferSize;i++) { Serial.printf("%02X",mcpsIndication->Buffer[i]); } memcpy(labels, mcpsIndication->Buffer, mcpsIndication->BufferSize); } Serial.println(); uint32_t color=mcpsIndication->Buffer[0]<<16|mcpsIndication->Buffer[1]<<8|mcpsIndication->Buffer[2]; } void productionSetup() { Serial.println("Starting"); Mcu.begin(); digitalWrite(Vext,LOW); display.init(); display.setFont(ArialMT_Plain_16); display.clear(); display.display(); deviceState = DEVICE_STATE_INIT; } void productionLoop() { static uint32_t goingToSleepTime = 0; static uint8_t subStateSleep = 0; static uint32_t myCycleTime = 0; digitalWrite(LED_GREEN, HIGH); if (deviceState != DEVICE_STATE_SLEEP) { Serial.printf("State: %d\n\r", deviceState); } switch( deviceState ) { case DEVICE_STATE_INIT: digitalWrite(LED_GREEN, LOW); 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; } }