Program for decoding radio-signals sent by a ETH-Window-Shutter-Contact, received with a RFM12B-module. The messages are sent to KNX via a freebus rs-interface. Details see http://mbed.org/users/charly/notebook/connecting-a-radio-window-shutter-contact-to-knx/
Dependencies: TextLCD mbed ConfigFile
main.cpp@0:b79cb3278583, 2011-04-27 (annotated)
- Committer:
- charly
- Date:
- Wed Apr 27 19:02:00 2011 +0000
- Revision:
- 0:b79cb3278583
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
charly | 0:b79cb3278583 | 1 | #include "mbed.h" |
charly | 0:b79cb3278583 | 2 | |
charly | 0:b79cb3278583 | 3 | # include "string.h" |
charly | 0:b79cb3278583 | 4 | |
charly | 0:b79cb3278583 | 5 | #include "TextLCD.h" |
charly | 0:b79cb3278583 | 6 | |
charly | 0:b79cb3278583 | 7 | #include "ConfigFile.h" |
charly | 0:b79cb3278583 | 8 | |
charly | 0:b79cb3278583 | 9 | #include "eth_comfort.h" |
charly | 0:b79cb3278583 | 10 | #include "rfm.h" |
charly | 0:b79cb3278583 | 11 | #include "rfm12b.h" |
charly | 0:b79cb3278583 | 12 | |
charly | 0:b79cb3278583 | 13 | /*! |
charly | 0:b79cb3278583 | 14 | * \file main.cpp |
charly | 0:b79cb3278583 | 15 | * \brief Read messages from ETH-Radio-Shutters and send On/Off-Commands to KNX (via freebus-rs-interface) |
charly | 0:b79cb3278583 | 16 | * \author Karl Zweimüller |
charly | 0:b79cb3278583 | 17 | */ |
charly | 0:b79cb3278583 | 18 | |
charly | 0:b79cb3278583 | 19 | TextLCD lcd(p30, p29, p28, p27, p26, p25, TextLCD::LCD16x2); // rs, e, d0-d3 |
charly | 0:b79cb3278583 | 20 | |
charly | 0:b79cb3278583 | 21 | eth_comfort eth_comf(p11, p12, p13, p14, p18, LED4); // mosi, miso, sclk, cs, rxdata, rxled |
charly | 0:b79cb3278583 | 22 | |
charly | 0:b79cb3278583 | 23 | Serial pc(USBTX, USBRX); // tx, rx |
charly | 0:b79cb3278583 | 24 | |
charly | 0:b79cb3278583 | 25 | //Freebus-RS-Interface connected to serial |
charly | 0:b79cb3278583 | 26 | Serial knxrs(p9, p10); // tx, rx |
charly | 0:b79cb3278583 | 27 | |
charly | 0:b79cb3278583 | 28 | // Filesystem for Config-File |
charly | 0:b79cb3278583 | 29 | LocalFileSystem local("local"); |
charly | 0:b79cb3278583 | 30 | |
charly | 0:b79cb3278583 | 31 | ConfigFile cfg; |
charly | 0:b79cb3278583 | 32 | |
charly | 0:b79cb3278583 | 33 | // mbed LEDs |
charly | 0:b79cb3278583 | 34 | /* |
charly | 0:b79cb3278583 | 35 | DigitalOut led1(LED1); |
charly | 0:b79cb3278583 | 36 | DigitalOut led2(LED2); |
charly | 0:b79cb3278583 | 37 | DigitalOut led3(LED3); |
charly | 0:b79cb3278583 | 38 | DigitalOut led4(LED4); |
charly | 0:b79cb3278583 | 39 | */ |
charly | 0:b79cb3278583 | 40 | |
charly | 0:b79cb3278583 | 41 | //--------------------------------------------------------------------- |
charly | 0:b79cb3278583 | 42 | // |
charly | 0:b79cb3278583 | 43 | //--------------------------------------------------------------------- |
charly | 0:b79cb3278583 | 44 | |
charly | 0:b79cb3278583 | 45 | int main() { |
charly | 0:b79cb3278583 | 46 | |
charly | 0:b79cb3278583 | 47 | char group[255]; // the KNX-Group-address |
charly | 0:b79cb3278583 | 48 | char ethid[255]; //the ETH-ID as string |
charly | 0:b79cb3278583 | 49 | char command[255]; // a command to send to the freebus rs-interface |
charly | 0:b79cb3278583 | 50 | |
charly | 0:b79cb3278583 | 51 | eth_message message; // holds a message from the ETH-window-shutter |
charly | 0:b79cb3278583 | 52 | |
charly | 0:b79cb3278583 | 53 | pc.baud(115200); |
charly | 0:b79cb3278583 | 54 | |
charly | 0:b79cb3278583 | 55 | pc.printf("\n\rConnected to mbed\n\r"); |
charly | 0:b79cb3278583 | 56 | lcd.printf("ETH-Freebus: OK\n"); |
charly | 0:b79cb3278583 | 57 | |
charly | 0:b79cb3278583 | 58 | /* |
charly | 0:b79cb3278583 | 59 | * Read the configuration file from mbed. |
charly | 0:b79cb3278583 | 60 | */ |
charly | 0:b79cb3278583 | 61 | if (!cfg.read("/local/knx.cfg")) { |
charly | 0:b79cb3278583 | 62 | pc.printf("Failure to read configuration file knx.cfg.\n\r"); |
charly | 0:b79cb3278583 | 63 | } |
charly | 0:b79cb3278583 | 64 | |
charly | 0:b79cb3278583 | 65 | |
charly | 0:b79cb3278583 | 66 | // initialize freebus-rs-interface |
charly | 0:b79cb3278583 | 67 | // 115.200 Baud,n,8,1 |
charly | 0:b79cb3278583 | 68 | knxrs.baud(115200); |
charly | 0:b79cb3278583 | 69 | |
charly | 0:b79cb3278583 | 70 | knxrs.printf("fbecho=0\r"); //switch off echo |
charly | 0:b79cb3278583 | 71 | |
charly | 0:b79cb3278583 | 72 | |
charly | 0:b79cb3278583 | 73 | do { |
charly | 0:b79cb3278583 | 74 | // anything new? |
charly | 0:b79cb3278583 | 75 | if (eth_comf.readable()) { |
charly | 0:b79cb3278583 | 76 | // read the new message and display |
charly | 0:b79cb3278583 | 77 | message = eth_comf.getMessage(); |
charly | 0:b79cb3278583 | 78 | pc.printf("\n\rCounter: %02X\n\r",message.cnt); |
charly | 0:b79cb3278583 | 79 | pc.printf( " Dev-ID: %06X\n\r",message.adr); |
charly | 0:b79cb3278583 | 80 | pc.printf( " cmd: %0X\n\r",message.cmd); |
charly | 0:b79cb3278583 | 81 | //pc.printf( "cmd&0x80: %0X\n\r",message.cmd&0x80); |
charly | 0:b79cb3278583 | 82 | // why doesn't work the following?????????????? |
charly | 0:b79cb3278583 | 83 | //pc.printf( "Battery: "); |
charly | 0:b79cb3278583 | 84 | //if (message.cmd&0x80 == 0x00) pc.printf("GOOD\n\r"); else pc.printf("WEAK\n\r"); |
charly | 0:b79cb3278583 | 85 | |
charly | 0:b79cb3278583 | 86 | pc.printf( "Window : %s\n\r\n\r", (message.cmd&0x01 != 0x00) ? "OPEN" : "CLOSE"); |
charly | 0:b79cb3278583 | 87 | lcd.cls(); |
charly | 0:b79cb3278583 | 88 | lcd.printf("#:%02X ID: %06X\n",message.cnt,message.adr); |
charly | 0:b79cb3278583 | 89 | lcd.printf("Window : %s\n", (message.cmd&0x01 != 0x00) ? "OPEN" : "CLOSE"); |
charly | 0:b79cb3278583 | 90 | pc.printf("\n\r"); |
charly | 0:b79cb3278583 | 91 | |
charly | 0:b79cb3278583 | 92 | |
charly | 0:b79cb3278583 | 93 | // convert id to string |
charly | 0:b79cb3278583 | 94 | sprintf(ethid,"%06X",message.adr); |
charly | 0:b79cb3278583 | 95 | /* |
charly | 0:b79cb3278583 | 96 | * Get the group address from configuration value. |
charly | 0:b79cb3278583 | 97 | */ |
charly | 0:b79cb3278583 | 98 | if (cfg.getValue(ethid, &group[0], sizeof(group))) { |
charly | 0:b79cb3278583 | 99 | pc.printf("Config-File: '%s'='%s'\n\r", ethid,group ); |
charly | 0:b79cb3278583 | 100 | } else { |
charly | 0:b79cb3278583 | 101 | strcpy(group ,""); |
charly | 0:b79cb3278583 | 102 | } |
charly | 0:b79cb3278583 | 103 | |
charly | 0:b79cb3278583 | 104 | |
charly | 0:b79cb3278583 | 105 | if (strlen(group) > 0) { |
charly | 0:b79cb3278583 | 106 | sprintf(command, "fbs01/%s=%s\r",group,(message.cmd&0x01 != 0x00) ? "1" : "0"); // EIS01 on Group-address group : Open=1 Close=0 |
charly | 0:b79cb3278583 | 107 | // Send a KNX-Telegramm |
charly | 0:b79cb3278583 | 108 | knxrs.printf("%s",command); |
charly | 0:b79cb3278583 | 109 | pc.printf("%s\n\r",command); |
charly | 0:b79cb3278583 | 110 | } else { |
charly | 0:b79cb3278583 | 111 | // device not in config file, so add it |
charly | 0:b79cb3278583 | 112 | // add the new device to the config and write file |
charly | 0:b79cb3278583 | 113 | pc.printf("new unknown device!\n\r"); |
charly | 0:b79cb3278583 | 114 | /* we don't write the file, as all comments are lost |
charly | 0:b79cb3278583 | 115 | if (!cfg.setValue(ethid, "")) { |
charly | 0:b79cb3278583 | 116 | error("Failure to set a value.\n\r"); |
charly | 0:b79cb3278583 | 117 | } |
charly | 0:b79cb3278583 | 118 | if (!cfg.write("/local/knx.cfg")) { |
charly | 0:b79cb3278583 | 119 | error("Failure to write the configuration file.\n\r"); |
charly | 0:b79cb3278583 | 120 | } |
charly | 0:b79cb3278583 | 121 | */ |
charly | 0:b79cb3278583 | 122 | } |
charly | 0:b79cb3278583 | 123 | } |
charly | 0:b79cb3278583 | 124 | } while (1==1); |
charly | 0:b79cb3278583 | 125 | |
charly | 0:b79cb3278583 | 126 | } |
charly | 0:b79cb3278583 | 127 | |
charly | 0:b79cb3278583 | 128 |