bugfixes battery read

This commit is contained in:
Klaus K Wilting 2018-06-03 21:02:16 +02:00
parent ede1cd77a5
commit 34cdbdd5b1
3 changed files with 19 additions and 32 deletions

View File

@ -218,19 +218,19 @@ device answers with it's current configuration. The configuration is a C structu
byte 13: Wifi antenna switch (0=internal, 1=external) byte 13: Wifi antenna switch (0=internal, 1=external)
byte 14: Vendorfilter mode (0=disabled, 1=enabled) byte 14: Vendorfilter mode (0=disabled, 1=enabled)
byte 15: RGB LED luminosity (0..100 %) byte 15: RGB LED luminosity (0..100 %)
bytes 16-25: Software version (ASCII format) bytes 16-26: Software version (ASCII format, terminating with zero)
0x81 get device uptime 0x81 get device uptime
bytes 1-7: uptime in seconds (little endian format) bytes 1-8: uptime in seconds (little endian format)
0x82 get device cpu temperature 0x82 get device cpu temperature
bytes 1-3: chip temperature in degrees celsius (little endian format) bytes 1-4: chip temperature in degrees celsius (little endian format)
0x83 get device battery voltage 0x83 get device battery voltage
bytes 1-3: battery voltage in millivolt, 0 if unreadable (little endian format) bytes 1-2: battery voltage in millivolt, 0 if unreadable (little endian format)
# License # License

View File

@ -28,6 +28,7 @@ uint16_t read_voltage(void)
{ {
static const adc1_channel_t channel = HAS_BATTERY_PROBE; static const adc1_channel_t channel = HAS_BATTERY_PROBE;
static const adc_atten_t atten = ADC_ATTEN_DB_11; static const adc_atten_t atten = ADC_ATTEN_DB_11;
static const adc_unit_t unit = ADC_UNIT_1;
//configure ADC1 //configure ADC1
ESP_ERROR_CHECK(adc1_config_width(ADC_WIDTH_BIT_12)); ESP_ERROR_CHECK(adc1_config_width(ADC_WIDTH_BIT_12));
@ -35,9 +36,8 @@ uint16_t read_voltage(void)
//calibrate ADC1 //calibrate ADC1
esp_adc_cal_characteristics_t *adc_chars = (esp_adc_cal_characteristics_t *) calloc(1, sizeof(esp_adc_cal_characteristics_t)); esp_adc_cal_characteristics_t *adc_chars = (esp_adc_cal_characteristics_t *) calloc(1, sizeof(esp_adc_cal_characteristics_t));
#ifdef VERBOSE esp_adc_cal_value_t val_type = esp_adc_cal_characterize(unit, atten, ADC_WIDTH_BIT_12, DEFAULT_VREF, adc_chars);
print_char_val_type(esp_adc_cal_characterize(ADC_UNIT_1, atten, ADC_WIDTH_BIT_12, DEFAULT_VREF, adc_chars)); print_char_val_type(val_type);
#endif
//multisample ADC1 //multisample ADC1
uint32_t adc_reading = 0; uint32_t adc_reading = 0;
@ -48,7 +48,7 @@ uint16_t read_voltage(void)
adc_reading /= NO_OF_SAMPLES; adc_reading /= NO_OF_SAMPLES;
//Convert adc_reading to voltage in mV //Convert adc_reading to voltage in mV
uint32_t voltage = esp_adc_cal_raw_to_voltage(adc_reading, adc_chars); uint16_t voltage = (uint16_t) esp_adc_cal_raw_to_voltage(adc_reading, adc_chars);
#ifdef BATT_FACTOR #ifdef BATT_FACTOR
voltage *= BATT_FACTOR; voltage *= BATT_FACTOR;
#endif #endif

View File

@ -211,51 +211,38 @@ void set_noop (uint8_t val) {
void get_config (uint8_t val) { void get_config (uint8_t val) {
ESP_LOGI(TAG, "Remote command: get configuration"); ESP_LOGI(TAG, "Remote command: get configuration");
int size = sizeof(configData_t); int size = sizeof(configData_t);
// declare send buffer (char byte array) LMIC_setTxData2(RCMDPORT, (byte*)&cfg, size, 0); // send data unconfirmed on RCMD Port
unsigned char *sendData = new unsigned char[size]; ESP_LOGI(TAG, "%d bytes queued in send queue", size);
// copy current configuration (struct) to send buffer
memcpy(sendData, &cfg, size);
LMIC_setTxData2(RCMDPORT, sendData, size-1, 0); // send data unconfirmed on RCMD Port
delete sendData; // free memory
ESP_LOGI(TAG, "%d bytes queued in send queue", size-1);
}; };
void get_uptime (uint8_t val) { void get_uptime (uint8_t val) {
ESP_LOGI(TAG, "Remote command: get uptime"); ESP_LOGI(TAG, "Remote command: get uptime");
int size = sizeof(uptimecounter); int size = sizeof(uptimecounter);
unsigned char *sendData = new unsigned char[size]; LMIC_setTxData2(RCMDPORT, (byte*)&uptimecounter, size, 0); // send data unconfirmed on RCMD Port
memcpy(sendData, (unsigned char*)&uptimecounter , size); ESP_LOGI(TAG, "%d bytes queued in send queue", size);
LMIC_setTxData2(RCMDPORT, sendData, size-1, 0); // send data unconfirmed on RCMD Port
delete sendData; // free memory
ESP_LOGI(TAG, "%d bytes queued in send queue", size-1);
}; };
void get_cputemp (uint8_t val) { void get_cputemp (uint8_t val) {
ESP_LOGI(TAG, "Remote command: get cpu temperature"); ESP_LOGI(TAG, "Remote command: get cpu temperature");
float temp = temperatureRead(); float temp = temperatureRead();
int size = sizeof(temp); int size = sizeof(temp);
unsigned char *sendData = new unsigned char[size]; LMIC_setTxData2(RCMDPORT, (byte*)&temp, size, 0); // send data unconfirmed on RCMD Port
memcpy(sendData, (unsigned char*)&temp, size); ESP_LOGI(TAG, "%d bytes queued in send queue", size);
LMIC_setTxData2(RCMDPORT, sendData, size-1, 0); // send data unconfirmed on RCMD Port
delete sendData; // free memory
ESP_LOGI(TAG, "%d bytes queued in send queue", size-1);
}; };
void get_voltage (uint8_t val) { void get_voltage (uint8_t val) {
ESP_LOGI(TAG, "Remote command: get battery voltage"); ESP_LOGI(TAG, "Remote command: get battery voltage");
#ifdef HAS_BATTERY_PROBE #ifdef HAS_BATTERY_PROBE
uint32_t voltage = read_voltage(); uint16_t voltage = read_voltage();
#else #else
uint32_t voltage = 0; uint16_t voltage = 0;
#endif #endif
int size = sizeof(voltage); int size = sizeof(voltage);
unsigned char *sendData = new unsigned char[size]; LMIC_setTxData2(RCMDPORT, (byte*)&voltage, size, 0); // send data unconfirmed on RCMD Port
memcpy(sendData, (unsigned char*)&voltage, size); ESP_LOGI(TAG, "%d bytes queued in send queue", size);
LMIC_setTxData2(RCMDPORT, sendData, size-1, 0); // send data unconfirmed on RCMD Port
delete sendData; // free memory
ESP_LOGI(TAG, "%d bytes queued in send queue", size-1);
}; };
// assign previously defined functions to set of numeric remote commands // assign previously defined functions to set of numeric remote commands
// format: opcode, function, flag (1 = do make settings persistent / 0 = don't) // format: opcode, function, flag (1 = do make settings persistent / 0 = don't)
// //