HAHcentral no config source?

5 replies [Last post]
garrydwilms
Offline
United Kingdom
Joined: 31 Mar 2011

Hi All,

is the source code for the no config version of HAHcentral available anywhere?  Am struggling to find it.

reason being, when transmitting data (Ir control etc) using basenode with HAHCentral all is ok until the documented issues with RF band being changed by accident appears.

When I try to stop this happening by using the no config version I cannot transmit data? Receiving data from my temp devices on the HAH nodes is fine but I cannot transmit to them, moving back to HAHCentral, all is Ok again.

I would like to study the code to understand why?

thanks

Garry 

BoxingOrange
Offline
United Kingdom
Joined: 11 Jun 2010
Source Code

I didn't realise that this was in use so much.  I just modified RF12demo.pde to ignore any input from the serial port, the HAH seems to send something down the line which was causing the JeeNode's to lose it's configuration on a HAH reboot.

I think I just commented out lines 687, 691 and 692.  

 

Karl

AttachmentSize
RF12nocfg.txt 22.2 KB
garrydwilms
Offline
United Kingdom
Joined: 31 Mar 2011
Thanks for this Karl, will

Thanks for this Karl,

 

will probably mod further  so that only the config data is ignored in that case.

will post when I get round to it

 

cheers

 

Garry

BoxingOrange
Offline
United Kingdom
Joined: 11 Jun 2010
Quick and Dirty

My solution was a bit of a sledgehammer, but at the time that was all I could do, my coding has improved a little since then.

Karl

 

PS - I'm currently writing a Corona interface similiar to the Lighware one, not sure how far I'll get with it but it's certainly fun.

garrydwilms
Offline
United Kingdom
Joined: 31 Mar 2011
Modified HAHCentral with no

Modified HAHCentral with no config as promised.

this version still allows the sending of data but blocks config changes.

Posting on ipad as laptop kaput so cannot attach file, therefore have posted text long hand!

 

 

 

 

// $Id $
#include <Ports.h>
#include <RF12.h>
#include <util/crc16.h>
#include <util/parity.h>
#include <avr/eeprom.h>
#include <avr/pgmspace.h>
//#define LED_PIN     9   // activity LED, comment out to disable
static unsigned long now () {
    // FIXME 49-day overflow
    return millis() / 1000;
}
static void activityLed (byte on) {
#ifdef LED_PIN
    pinMode(LED_PIN, OUTPUT);
    digitalWrite(LED_PIN, !on);
#endif
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// RF12 configuration setup code
typedef struct {
    byte nodeId;
    byte group;
    char msg[RF12_EEPROM_SIZE-4];
    word crc;
} RF12Config;
static RF12Config config;
static char cmd;
static byte value, stack[RF12_MAXDATA], top, sendLen, dest, quiet=1;
static byte testbuf[RF12_MAXDATA], testCounter;
static void addCh (char* msg, char c) {
    byte n = strlen(msg);
    msg[n] = c;
}
static void addInt (char* msg, word v) {
    if (v >= 10)
        addInt(msg, v / 10);
    addCh(msg, '0' + v % 10);
}
static void saveConfig () {
    // set up a nice config string to be shown on startup
    memset(config.msg, 0, sizeof config.msg);
    strcpy(config.msg, " ");
    
    byte id = config.nodeId & 0x1F;
    addCh(config.msg, '@' + id);
    strcat(config.msg, " i");
    addInt(config.msg, id);
    
    strcat(config.msg, " g");
    addInt(config.msg, config.group);
    
    strcat(config.msg, " @ ");
    static word bands[4] = { 315, 433, 868, 915 };
    word band = config.nodeId >> 6;
    addInt(config.msg, bands[band]);
    strcat(config.msg, " MHz ");
    
    config.crc = ~0;
    for (byte i = 0; i < sizeof config - 2; ++i)
        config.crc = _crc16_update(config.crc, ((byte*) &config)[i]);
    // save to EEPROM
    for (byte i = 0; i < sizeof config; ++i) {
        byte b = ((byte*) &config)[i];
        eeprom_write_byte(RF12_EEPROM_ADDR + i, b);
    }
    
    if (!rf12_config())
        Serial.println("config save failed");
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
char helpText1[] PROGMEM = 
    "\n"
    "Available commands:" "\n"
    "  <nn> i     - set node ID (standard node ids are 1..26)" "\n"
    "  <n> b      - set MHz band (4 = 433, 8 = 868, 9 = 915)" "\n"
    "  <nnn> g    - set network group (RFM12 only allows 212, 0 = any)" "\n"
    "  ...,<nn> a - send data packet to node <nn>, with ack" "\n"
    "  ...,<nn> s - send data packet to node <nn>, no ack" "\n"
    "  <n> q      - set quiet mode (1 = don't report bad packets)" "\n"
;
static void showString (PGM_P s) {
    for (;;) {
        char c = pgm_read_byte(s++);
        if (c == 0)
            break;
        if (c == '\n')
            Serial.print('\r');
        Serial.print(c);
    }
}
static void showHelp () {
    showString(helpText1);
    Serial.println("Current configuration:");
    rf12_config();
}
static void handleInput (char c) {
    if ('0' <= c && c <= '9')
        value = 10 * value + c - '0';
    else if (c == ',') {
        if (top < sizeof stack)
            stack[top++] = value;
        value = 0;
    } else if ('a' <= c && c <='z') {
        Serial.print("> ");
        Serial.print((int) value);
        Serial.println(c);
        switch (c) {
            default:
                showHelp();
                break;
    //        case 'i': // set node id
    //            config.nodeId = (config.nodeId & 0xE0) + (value & 0x1F);
    //            saveConfig();
    //            break;
    //        case 'b': // set band: 4 = 433, 8 = 868, 9 = 915
    //            value = value == 8 ? RF12_868MHZ :
    //                    value == 9 ? RF12_915MHZ : RF12_433MHZ;
    //            config.nodeId = (value << 6) + (config.nodeId & 0x3F);
    //            saveConfig();
    //            break;
    //        case 'g': // set network group
    //            config.group = value;
    //            saveConfig();
    //           break;
            case 'a': // send packet to node ID N, request an ack
            case 's': // send packet to node ID N, no ack
                cmd = c;
                sendLen = top;
                dest = value;
                memcpy(testbuf, stack, top);
                break;
            case 'q': // turn quiet mode on or off (don't report bad packets)
                quiet = value;
                break;
        }
        value = top = 0;
        memset(stack, 0, sizeof stack);   
    } else if (c > ' ')
        showHelp();
}
void setup() {
    Serial.begin(57600);
    Serial.print("\n[HAHCentral.1]");
    if (rf12_config()) {
        config.nodeId = eeprom_read_byte(RF12_EEPROM_ADDR);
        config.group = eeprom_read_byte(RF12_EEPROM_ADDR + 1);
    } else {
        config.nodeId = 0x41; // node A1 @ 433 MHz
        config.group = 0xD4;
        saveConfig();
    }    
}
void loop() {
    if (Serial.available())
        handleInput(Serial.read());
    if (rf12_recvDone()) {
        byte n = rf12_len;
        if (rf12_crc == 0) {
            Serial.print("OK");
        } else {
            if (quiet)
                return;
            Serial.print(" ?");
            if (n > 20) // print at most 20 bytes if crc is wrong
                n = 20;
        }
        if (config.group == 0) {
            Serial.print("G ");
            Serial.print((int) rf12_grp);
        }
        Serial.print(' ');
        Serial.print((int) rf12_hdr);
        for (byte i = 0; i < n; ++i) {
            Serial.print(' ');
            Serial.print((int) rf12_data[i]);
        }
        Serial.println();
        
        if (rf12_crc == 0) {
            activityLed(1);
            
            if (RF12_WANTS_ACK) {
                Serial.println(" -> ack");
                rf12_sendStart(RF12_ACK_REPLY, 0, 0);
            }
            
            activityLed(0);
        }
    }
    if (cmd && rf12_canSend()) {
        activityLed(1);
        Serial.print(" -> ");
        Serial.print((int) sendLen);
        Serial.println(" b");
        byte header = cmd == 'a' ? RF12_HDR_ACK : 0;
        if (dest)
            header |= RF12_HDR_DST | dest;
        rf12_sendStart(header, testbuf, sendLen);
        cmd = 0;
        activityLed(0);
    }
}
Cheers, Garry 
out of interest, how would I convert this to a hex file using the arduino environment or is that not possible? Would make it easier for others to flash onto chips that's all.

 

brett
Offline
Providence, United States
Joined: 9 Jan 2010
I see what you are doing here
I see what you are doing here but there is a better way as you are still poking stuff into the EEPROM.
void setup() {
    Serial.begin(57600);
    Serial.print("\n[HAHCentral.1]");
    if (rf12_config()) {
        config.nodeId = eeprom_read_byte(RF12_EEPROM_ADDR);
        config.group = eeprom_read_byte(RF12_EEPROM_ADDR + 1);
    } else {
        config.nodeId = 0x41; // node A1 @ 433 MHz
        config.group = 0xD4;
        saveConfig();
    }    
}
Just hardcode everything and avoid the EEPROM altogether.
void setup() {
    Serial.begin(57600);
    Serial.println("\n[HAHCentral.1]");
    rf12_initialize(1, RF12_868MHZ, 212);   // Node 1, Group 212
}
As you are not calling rf12_config() you won't get output of how its setup, but as its hardcoded I'm pretty sure you'll remember.
Brett
Hardware Info