diff --git a/libraries/includes/defines.h b/libraries/includes/defines.h index 2aca326..b51601c 100644 --- a/libraries/includes/defines.h +++ b/libraries/includes/defines.h @@ -8,6 +8,7 @@ #define LED_GREEN 4 #define LED_BLUE 45 - +#define SEND_PERIOD 15000 // ms +#define LABEL_LENGTH 5 #endif /* _DEFINES_H_ */ \ No newline at end of file diff --git a/sketch/production.cpp b/sketch/production.cpp index 9bddc1e..9ccca32 100644 --- a/sketch/production.cpp +++ b/sketch/production.cpp @@ -6,6 +6,9 @@ #include #include +#include "HT_SSD1306Wire.h" +extern SSD1306Wire display; + // from config.cpp extern config_t myConfig; @@ -15,7 +18,7 @@ extern config_t myConfig; uint16_t userChannelsMask[6]={ 0x00FF,0x0000,0x0000,0x0000,0x0000,0x0000 }; /*LoraWan Class, Class A and Class C are supported*/ -DeviceClass_t loraWanClass = CLASS_A; +DeviceClass_t loraWanClass = CLASS_C; /*the application data transmission duty cycle. value in [ms].*/ uint32_t appTxDutyCycle = 15000; @@ -54,15 +57,42 @@ 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(); - uint8_t cnt = DS18B20.getDS18Count(); + cnt = DS18B20.getDS18Count(); Serial.printf("cnt: %d\n\r", cnt); - const uint8_t SLOT_WIDTH = 8 + 4; // 8 bytes address, 4 bytes value - appDataSize = cnt * SLOT_WIDTH; + 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); @@ -72,16 +102,29 @@ static void prepareTxFrame( uint8_t port ) 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:"); - for(uint8_t i=0;iBufferSize;i++) - { - Serial.printf("%02X",mcpsIndication->Buffer[i]); + + 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]; @@ -90,62 +133,61 @@ void downLinkDataHandle(McpsIndication_t *mcpsIndication) -RTC_DATA_ATTR bool firstrun = true; 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); - switch( deviceState ) - { + if (deviceState != DEVICE_STATE_SLEEP) { + Serial.printf("State: %d\n\r", deviceState); + } + switch( deviceState ) { case DEVICE_STATE_INIT: - digitalWrite(LED_GREEN, LOW); - { + 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); - { + digitalWrite(LED_BLUE, HIGH); Serial.println("sending"); prepareTxFrame( appPort ); LoRaWAN.send(); deviceState = DEVICE_STATE_CYCLE; break; - } case DEVICE_STATE_CYCLE: - digitalWrite(LED_BLUE, LOW); - { + 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; - } } }