diff --git a/libraries/includes/defines.h b/libraries/includes/defines.h index 961af31..39c930d 100644 --- a/libraries/includes/defines.h +++ b/libraries/includes/defines.h @@ -4,8 +4,7 @@ #define ONE_WIRE 2 -#define LED_RED 3 -#define LED_GREEN 4 +#define U_ADC_IN 4 #define LED_BLUE 45 #define SEND_PERIOD 15000 // ms diff --git a/readme.md b/readme.md index 33f993a..f793f13 100644 --- a/readme.md +++ b/readme.md @@ -8,7 +8,7 @@ * inside the container go into the directory `~/project` * set the env variable `ARDUINO_SKETCHBOOK_DIR` to `$PWD` * build application in container in directory `~/project` using `arduino-cli compile --fqbn=Heltec-esp32:esp32:WIFI_LoRa_32_V3 --export-binaries /home/arduino/project/sketch/` -* flash device using `esptool.py --port /dev/tty.usbserial-0001 --baud 921600 --chip esp32s3 write_flash --flash_mode dio --flash_size 8MB 0x0 sketch.ino.bootloader.bin 0x8000 sketch.ino.partitions.bin 0x10000 sketch.ino.bin` +* on the host go to `sketch/build/Heltec-esp32.esp32.WIFI_LoRa_32_V3` and flash device using `esptool.py --port /dev/tty.usbserial-0001 --baud 921600 --chip esp32s3 write_flash --flash_mode dio --flash_size 8MB 0x0 sketch.ino.bootloader.bin 0x8000 sketch.ino.partitions.bin 0x10000 sketch.ino.bin` From Arduino IDE: diff --git a/sketch/production.cpp b/sketch/production.cpp index bbbda4d..074c96f 100644 --- a/sketch/production.cpp +++ b/sketch/production.cpp @@ -6,9 +6,6 @@ #include #include -#include "HT_SSD1306Wire.h" -extern SSD1306Wire display; - // from config.cpp extern config_t myConfig; @@ -18,10 +15,10 @@ 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_C; +DeviceClass_t loraWanClass = CLASS_A; /*the application data transmission duty cycle. value in [ms].*/ -uint32_t appTxDutyCycle = 15000; +uint32_t appTxDutyCycle = SEND_PERIOD; /*ADR enable*/ bool loraWanAdr = true; @@ -57,156 +54,46 @@ uint8_t confirmedNbTrials = 4; OneWire oneWire(ONE_WIRE); DallasTemperature DS18B20(&oneWire); -bool firstrun = true; - - -typedef struct { - uint64_t addr; - uint8_t index; - char label[LABEL_LENGTH+1]; -} __attribute__((packed)) sensor_t; - -sensor_t sensors[NUM_OF_SENSORS]; - -typedef enum { - ERR_OK = 0x0000, - ERR_SENSOR_LOST = 0x0001, - ERR_ILLEGAL_DOWNLINK_MESSAGE_RECEIVED = 0x0002, - ERR_START_UP = 0x0004, - ERR_INVALID_NUM_OF_SENSORS = 0x8008, -} errorCode_t; -#define FATAL_MASK 0x8000 -uint16_t errorCode = ERR_OK; /* Prepares the payload of the frame */ static void prepareTxFrame( uint8_t port ) { - if ((errorCode & FATAL_MASK) == 0) { - display.clear(); + 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)); - if (firstrun) { - struct { - uint16_t status; - uint64_t addrs[NUM_OF_SENSORS]; - } __attribute__((packed)) msg; - msg.status = ERR_START_UP; - for (uint8_t i = 0; i < NUM_OF_SENSORS; i++) { - msg.addrs[i] = sensors[i].addr; - } - appDataSize = sizeof(msg); - Serial.printf("appDataSize: %ld\n\r", appDataSize); - memcpy(&appData, (void*)&msg, appDataSize); - display.drawString(1, 0, "Start up"); - Serial.println("Start up message send"); - firstrun = false; - } else { - DS18B20.begin(); - struct { - uint16_t status; - struct { - uint64_t addr; - int32_t value; - } __attribute__((packed)) sensors[NUM_OF_SENSORS]; - } __attribute__((packed)) msg; - msg.status = (DS18B20.getDS18Count() == NUM_OF_SENSORS) ? errorCode : errorCode | ERR_SENSOR_LOST; - DS18B20.requestTemperatures(); - for (uint8_t i = 0; i < NUM_OF_SENSORS; i++) { - for (uint8_t j = 0; j < NUM_OF_SENSORS; j++) { - if (sensors[j].index == i) { - msg.sensors[i].addr = sensors[j].addr; - Serial.printf("%d, %d: %s: %016llx\n\r", i, j, sensors[j].label, sensors[j].addr); - msg.sensors[i].value = DS18B20.getTemp(((const uint8_t*)(&(sensors[j].addr)))); - Serial.printf("v: %08x\n\r", msg.sensors[i].value); - - float tempC = ((float)msg.sensors[i].value) / 128; - Serial.printf("f: %.2f\n\r", tempC); - char dispbuf[128]; - sprintf(dispbuf, "%s: %.2f °C", sensors[j].label, tempC); - display.drawString(1, i * 16, dispbuf); - Serial.printf("%d, %016llx, %s, %.2f\n\r", sensors[j].index, sensors[j].addr, sensors[j].label, tempC); - } - } - } - appDataSize = sizeof(msg); - memcpy(&appData, (void*)&msg, appDataSize); - Serial.println("Send"); - } - - display.display(); - - errorCode = ERR_OK; - } else { - appDataSize = 2; - appData[0] = errorCode; + 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 downLinkDataHandle(McpsIndication_t *mcpsIndication) -{ - sensor_t downlinkSensors[NUM_OF_SENSORS]; - - if (mcpsIndication->BufferSize != sizeof(downlinkSensors)) { - Serial.println("illegal number of octets in downlink message"); - } else { - for(uint8_t i=0;iBufferSize;i++) { - Serial.printf("%02X",mcpsIndication->Buffer[i]); - } - memcpy(&downlinkSensors, mcpsIndication->Buffer, sizeof(downlinkSensors)); - for (uint8_t i = 0; i < NUM_OF_SENSORS; i++) { - bool found = false; - for (uint8_t j = 0; j < NUM_OF_SENSORS; j++) { - if (sensors[i].addr == downlinkSensors[j].addr) { - found = true; - sensors[i].index = downlinkSensors[j].index; - memcpy(sensors[i].label, downlinkSensors[j].label, LABEL_LENGTH); - sensors[i].label[LABEL_LENGTH] = 0; - } - } - if (! found) { - Serial.printf("Illegal label received for not existing %016llx\n\r", downlinkSensors[i].addr); - errorCode |= ERR_ILLEGAL_DOWNLINK_MESSAGE_RECEIVED; - } - } - } - Serial.println(); -} - - - - - void productionSetup() { Serial.println("Starting"); Mcu.begin(); - digitalWrite(Vext,LOW); - display.init(); - display.setFont(ArialMT_Plain_16); - display.screenRotate(ANGLE_180_DEGREE); - display.clear(); - display.display(); - deviceState = DEVICE_STATE_INIT; - - DS18B20.begin(); - uint8_t cnt = DS18B20.getDS18Count(); - if (cnt != 4) { - display.drawString(1, 0, "invalid number of sensors"); - Serial.println("invalid number of sensors"); - errorCode = ERR_INVALID_NUM_OF_SENSORS; - } else { - for (uint8_t i = 0; i < NUM_OF_SENSORS; i++) { - sprintf(sensors[i].label, "Sens%d", i); - DS18B20.getAddress(((uint8_t*)(&(sensors[i].addr))), i); - char buf[128]; - sprintf(buf, "%d: %s: %016llx", i, sensors[i].label, sensors[i].addr); - Serial.println(buf); - //display.drawString(1, i*16, buf); - sensors[i].index = i; - } - } } void productionLoop() @@ -215,14 +102,12 @@ void productionLoop() 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; diff --git a/sketch/sketch.ino b/sketch/sketch.ino index 714691d..e06db14 100644 --- a/sketch/sketch.ino +++ b/sketch/sketch.ino @@ -13,10 +13,7 @@ bool productionMode = false; void setup() { pinMode(LED_BLUE, OUTPUT); digitalWrite(LED_BLUE, LOW); - pinMode(LED_RED, OUTPUT); - digitalWrite(LED_RED, LOW); - pinMode(LED_GREEN, OUTPUT); - digitalWrite(LED_GREEN, LOW); + pinMode(U_ADC_IN, INPUT); Serial.begin(115200);