Skip to content

Commit

Permalink
use new packet format in temp sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
breaker27 committed Dec 5, 2013
1 parent f14fbd2 commit 7952f30
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 27 deletions.
77 changes: 52 additions & 25 deletions firmware/shc_tempsensor/shc_tempsensor.c
Expand Up @@ -27,6 +27,9 @@
#include "rfm12.h"
#include "uart.h"

#include "../src_common/msggrp_generic.h"
#include "../src_common/msggrp_tempsensor.h"

// switch on debugging by UART
//#define UART_DEBUG

Expand All @@ -41,6 +44,7 @@
#endif

#define AVERAGE_COUNT 4 // Average over how many values before sending over RFM12?
#define SEND_BATT_STATUS_CYCLE 30 // send battery status x times less than temp status

// How often should the packetcounter_base be increased and written to EEPROM?
// This should be 2^32 (which is the maximum transmitted packet counter) /
Expand All @@ -51,6 +55,7 @@
uint32_t packetcounter = 0;
uint8_t temperature_sensor_type = 0;
uint8_t brightness_sensor_type = 0;
uint8_t batt_status_cycle = SEND_BATT_STATUS_CYCLE - 1; // send promptly after startup

void printbytearray(uint8_t * b, uint8_t len)
{
Expand Down Expand Up @@ -85,10 +90,11 @@ int main ( void )
{
uint16_t vbat = 0;
uint16_t vlight = 0;
int16_t temp = 0;
int32_t temp = 0;
uint16_t hum = 0;
uint8_t device_id = 0;
uint8_t avg = 0;
uint8_t bat_p_val = 0;

// delay 1s to avoid further communication with uart or RFM12 when my programmer resets the MC after 500ms...
_delay_ms(1000);
Expand Down Expand Up @@ -121,7 +127,7 @@ int main ( void )
UART_PUTS ("\r\n");
UART_PUTS ("smarthomatic Tempsensor V1.0 (c) 2013 Uwe Freese, www.smarthomatic.org\r\n");
UART_PUTF ("Device ID: %u\r\n", device_id);
UART_PUTF ("Packet counter: %u\r\n", packetcounter);
UART_PUTF ("Packet counter: %lu\r\n", packetcounter);
UART_PUTF ("Temperature Sensor Type: %u\r\n", temperature_sensor_type);
UART_PUTF ("Brightness Sensor Type: %u\r\n", brightness_sensor_type);
#endif
Expand Down Expand Up @@ -180,9 +186,6 @@ int main ( void )
temp /= AVERAGE_COUNT;
hum /= AVERAGE_COUNT;

// set device ID
bufx[0] = device_id;

// update packet counter
packetcounter++;

Expand All @@ -191,50 +194,74 @@ int main ( void )
eeprom_write_UIntValue(EEPROM_PACKETCOUNTER_BYTE, EEPROM_PACKETCOUNTER_BIT, EEPROM_PACKETCOUNTER_LENGTH_BITS, packetcounter);
}

setBuf32(1, packetcounter);

// set command ID 10 (Temperature Sensor Status)
bufx[5] = 10;
// TODO: Send battery status from time to time.

// update battery percentage
bufx[6] = bat_percentage(vbat);
// Set packet content
temp = -5000;
pkg_header_init_tempsensor_tempstatus();
pkg_header_set_senderid(device_id);
pkg_header_set_packetcounter(packetcounter);
msg_tempsensor_tempstatus_set_temperature(temp);
msg_tempsensor_tempstatus_set_humidity(hum);

// update temperature and humidity
setBuf16(7, (uint16_t)temp);
setBuf16(9, hum);

// update brightness
if (brightness_sensor_type == BRIGHTNESSSENSORTYPE_PHOTOCELL)
{
bufx[11] = 100 - (int)((long)vlight * 100 / 1024);
msg_tempsensor_tempstatus_set_brightness(100 - (int)((long)vlight * 100 / 1024));
}
uint32_t crc = crc32(bufx, 12);
#ifdef UART_DEBUG
UART_PUTF("CRC32 is %lx (added as last 4 bytes)\r\n", crc);
#endif
setBuf32(12, crc);


pkg_header_crc32_tempsensor_tempstatus();

bat_p_val = bat_percentage(vbat);

#ifdef UART_DEBUG
UART_PUTF("Battery: %u%%, Temperature: ", bat_percentage(vbat));
UART_PUTF("CRC32 is %lx (added as first 4 bytes)\r\n", getBuf32(0));
UART_PUTF("Battery: %u%%, Temperature: ", bat_p_val);
printSigned(temp);
UART_PUTS(" deg.C, Humidity: ");
printSigned(hum);
UART_PUTS("%\r\n");
#endif

rfm12_sendbuf();

rfm12_tick(); // send packet, and then WAIT SOME TIME BEFORE GOING TO SLEEP (otherwise packet would not be sent)

switch_led(1);
_delay_ms(200);
switch_led(0);

vbat = temp = hum = vlight = avg = 0;
batt_status_cycle++;
}
else
{
if (batt_status_cycle >= SEND_BATT_STATUS_CYCLE)
{
batt_status_cycle = 0;

// Set packet content
pkg_header_init_generic_batterystatus();
pkg_header_set_senderid(device_id);
pkg_header_set_packetcounter(packetcounter);
msg_generic_batterystatus_set_percentage(bat_p_val);
pkg_header_crc32_generic_batterystatus();

#ifdef UART_DEBUG
UART_PUTF("Battery: %u%%\r\n", bat_p_val);
#endif
rfm12_sendbuf();
rfm12_tick(); // send packet, and then WAIT SOME TIME BEFORE GOING TO SLEEP (otherwise packet would not be sent)

switch_led(1);
_delay_ms(200);
switch_led(0);
}
}

// go to sleep. Wakeup by RFM12 wakeup-interrupt
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_mode();
}
}

// Ursprünglich 10260 Bytes ("text")

11 changes: 9 additions & 2 deletions firmware/src_common/e2p_access.h
Expand Up @@ -43,7 +43,7 @@ static inline void eeprom_write_UIntValue(uint16_t byte, uint8_t bit, uint16_t l
static inline void eeprom_write_IntValue(uint16_t byte, uint8_t bit, uint16_t length_bits, int32_t val)
{
_eeprom_write_UIntValue(byte, bit, length_bits,
(uint32_t)(((((uint32_t)val) >> 31) << (length_bits - 1)) | (val & ((1 << length_bits) - 1))),
(uint32_t)(((((uint32_t)val) >> 31) << (length_bits - 1)) | ((uint32_t)val & (uint32_t)((1 << (length_bits - 1)) - 1))),
NULL);
}

Expand All @@ -70,8 +70,15 @@ static inline void array_write_UIntValue(uint16_t byte, uint8_t bit, uint16_t le

static inline void array_write_IntValue(uint16_t byte, uint8_t bit, uint16_t length_bits, int32_t val, uint8_t * array)
{
//UART_PUTF ("IntVal: %d\r\n", val);
//UART_PUTF ("as UIntVal: %lu\r\n", (uint32_t)val);
//UART_PUTF ("Shifted: %u\r\n", ((uint32_t)val) >> 31);
//UART_PUTF ("Shifted2: %u\r\n", ((((uint32_t)val) >> 31) << (length_bits - 1)));
//UART_PUTF ("MASK: %u\r\n", ((1 << (length_bits - 1)) - 1));
//UART_PUTF ("OR: %lu\r\n", ((uint32_t)val & (uint32_t)((1 << (length_bits - 1)) - 1)));

_eeprom_write_UIntValue(byte, bit, length_bits,
(uint32_t)(((((uint32_t)val) >> 31) << (length_bits - 1)) | (val & ((1 << length_bits) - 1))),
(uint32_t)(((((uint32_t)val) >> 31) << (length_bits - 1)) | ((uint32_t)val & (uint32_t)((1 << (length_bits - 1)) - 1))),
array);
}

Expand Down

0 comments on commit 7952f30

Please sign in to comment.