class a, no display, no labels, u bat measurement

This commit is contained in:
2023-05-17 21:39:13 +02:00
parent 589b1990ce
commit 18cabad2a7
4 changed files with 30 additions and 149 deletions

View File

@ -6,9 +6,6 @@
#include <OneWire.h>
#include <DallasTemperature.h>
#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;i<mcpsIndication->BufferSize;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;