Funk wetterstations Analyse Programm

Aus Ethersex_Wiki
Wechseln zu: Navigation, Suche










dnl ################################################################################
dnl # This program is free software; you can redistribute it and/or modify it      #
dnl # under the terms of the GNU General Public License (either version 2 or       #
dnl # version 3) as published by the Free Software Foundation.                     #
dnl #                                                                              #
dnl # This program is distributed in the hope that it will be useful,              #
dnl # but WITHOUT ANY WARRANTY; without even the implied warranty of               #
dnl # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                #
dnl # GNU General Public License for more details.                                 #
dnl #                                                                              #
dnl # You should have received a copy of the GNU General Public License            #
dnl # along with this program; if not, write to the Free Software                  #
dnl # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                    #
dnl #                                                                              #
dnl # For more information on the GPL, please go to:                               #
dnl # http://www.gnu.org/copyleft/gpl.html                                         #
dnl #                                                                              #
dnl #                                                                              #
dnl # Wetter-Funk-Daten Analyse Programm für Wetter Station der Firma              #
dnl #     Krippl Watches GmbH   --   http://www.produktservice.info                #
dnl # Protokoll analyse siehe:                                                     #
dnl #     http://www.ethersex.de/index.php/Funk-wetterstations-protokoll           #
dnl #                                                                              #
dnl # Daten Analyse und Programm Erstellung:                                       #
dnl #   Copyright (c) 08/2009 - Michael Schultz <ethersex@keyb.de>                 #
dnl #      Mit viel unterstuetzung/Geduld von: stesie, veyron und DanielW          #
dnl #                                                                              #
dnl ################################################################################
dnl
dnl     Timing definitions @16 MHz - Pollin AVR-Net-IO with ATMega644 (Not ATMega32)

#define PRESCALER 1024
#define NS_PER_TICK     (PRESCALER * 1000000UL / F_CPU * 1000)
#define US_TO_TICKS(n)  ((n) * 1000UL / NS_PER_TICK)

                                               dnl                     Timing          Timer Count     Timer C. Hex    Decode
uint8_t Time0 = US_TO_TICKS(1920);             dnl     L       space 1920 bis 1999us   30-31,2         0x1E    0x1F    0
uint8_t Time1 = US_TO_TICKS(3990);             dnl     H       space 3990 bis 4049us   62,3-63,2       0x3E    0x3F    1
uint8_t Time2 = US_TO_TICKS(2060);             dnl     End-l   space 2060 bis 2089us   32,1-32,6       0x20    0x21    2
uint8_t Time3 = US_TO_TICKS(4130);             dnl     End-h   space 4130 bis 4169us   64,5-65,1       0x40    0x41    3
uint8_t Time4 = US_TO_TICKS(8890);             dnl     Start   space 8890 bis 8939us   138,9-139,6     0x8A    0x8B    4
uint8_t Time5 = US_TO_TICKS(360);              dnl     End-    pulse  360 bis 369us    5,6-5,7         0x05            5
uint8_t Time6 = US_TO_TICKS(440);              dnl     _       pulse  440 bis 519us    6,8-8,1         0x06    0x08    6
                                               dnl     Pause
                                dnl
uint8_t BitPos = 0;             dnl     Bit Position innerhalb eines 36 bit worts (1 bis 37)
uint8_t WordCound = 0;          dnl     Daten Wort Count (1 bis 9)
uint8_t WordData[40];           dnl     1 Data Word = 5 Byte    /       Max 8 Data Words
uint8_t WordPos = 0;            dnl     Byte Pos in WordData
                                dnl
uint16_t Temp = 0;              dnl     Temperatur Wert (-)76,8 bis (+)127,9 °C, Vorzeichen wird erst bei der ausgabe gesetzt.
uint16_t Feuchte = 0;           dnl     Luftfeuchte Wert 20-99 %
uint8_t timer_0 = 0;            dnl     Timer Wert für zeitbestimming zwichen 2 Pegel änderungen (Interrupts)
uint8_t Temp1 = 0;              dnl     Temperatur wert, einer stelle --> 0,n °C
uint8_t Temp10 = 0;             dnl     Temperatur wert, zener stelle --> n,0 °C
                                dnl
uint8_t Analysis[10] ;          dnl     Daten Analyse array
                                dnl     Analysis[0 bis 7]               Treffer der Daten Packets Übereinstimmung.
                                dnl     Analysis[8] - bit(0) = 1        Daten Liegen an und können vearbeitete werden
                                dnl     Analysis[8] - bit(1) = 1        Datenanalyse erfolgreich, Mindestens 3 Übereinstimmungen
                                dnl     Analysis[8] - bit(2) = 1        Daten sind Verarbeitet und können gesendet werden
                                dnl     Analysis[9]                     Array Positions Counter

CONTROL_START
        ON STARTUP DO
                TCCR0B |=  _BV(CS02);           dnl     TCCR0B(CS02)=1
                TCCR0B &= ~_BV(CS01);           dnl     TCCR0B(CS01)=0
                TCCR0B |=  _BV(CS00);           dnl     TCCR0B(CS00)=1
                EIMSK  |=  _BV(INT0);           dnl     EIMSK(INT0) = 1
                EICRA  &= ~_BV(ISC01);          dnl     EICRA(ISC01) = 0
                EICRA  |=  _BV(ISC00);          dnl     EICRA(ISC00) = 1
                TIMSK0 |=  _BV(TOIE0);          dnl     TIMSK0(TOIE0) = 1       Interrupt on Timer Overflow aktivieren
                TCP_CONNECT(10.0.0.35, 4444, message_handler);
        END
        TCP_HANDLER_PERSIST(message_handler)
        for (;;) {
                                Temp = 0 ;
                                if ( WordData[1] & ( 1 << 3 ) ) { Temp = Temp + 1 ; }
                                if ( WordData[1] & ( 1 << 2 ) ) { Temp = Temp + 2 ; }
                                if ( WordData[1] & ( 1 << 1 ) ) { Temp = Temp + 4 ; }
                                if ( WordData[1] & ( 1 << 0 ) ) { Temp = Temp + 8 ; }
                                if ( WordData[2] & ( 1 << 7 ) ) { Temp = Temp + 16 ; }
                                if ( WordData[2] & ( 1 << 6 ) ) { Temp = Temp + 32 ; }
                                if ( WordData[2] & ( 1 << 5 ) ) { Temp = Temp + 64 ; }
                                if ( WordData[2] & ( 1 << 4 ) ) { Temp = Temp + 128 ; }
                                if ( WordData[2] & ( 1 << 3 ) ) { Temp = Temp + 256 ; }
                                if ( WordData[2] & ( 1 << 2 ) ) { Temp = Temp + 512 ; }
                                if ( WordData[2] & ( 1 << 1 ) ) { Temp = Temp + 1024 ; }
                                Feuchte = 0 ;
                                if ( WordData[3] & ( 1 << 7 ) ) { Feuchte = Feuchte + 1 ; }
                                if ( WordData[3] & ( 1 << 6 ) ) { Feuchte = Feuchte + 2 ; }
                                if ( WordData[3] & ( 1 << 5 ) ) { Feuchte = Feuchte + 4 ; }
                                if ( WordData[3] & ( 1 << 4 ) ) { Feuchte = Feuchte + 8 ; }
                                if ( WordData[3] & ( 1 << 3 ) ) { Feuchte = Feuchte + 10 ; }
                                if ( WordData[3] & ( 1 << 2 ) ) { Feuchte = Feuchte + 20 ; }
                                if ( WordData[3] & ( 1 << 1 ) ) { Feuchte = Feuchte + 40 ; }
                                if ( WordData[3] & ( 1 << 0 ) ) { Feuchte = Feuchte + 80 ; }

                                Temp10=( Temp / 10 );
                                Temp1=( Temp - ( ( Temp / 10 ) * 10 ) );

                                TCP_SEND("%04d-%02d-%02d %02d:%02d:%02d %s -- Sender=", CLOCK_YEAR(), CLOCK_MONTH(), CLOCK_DAY(), CLOCK_HOUR(), CLOCK_MIN(), CLOCK_SEC(), CONF_HOSTNAME);
                                if ( WordData[0] & ( 1 << 3 ) ) { TCP_SEND("1"); } else { TCP_SEND("0"); }
                                if ( WordData[0] & ( 1 << 2 ) ) { TCP_SEND("1-"); } else { TCP_SEND("0-"); }
                                if ( WordData[0] & ( 1 << 7 ) ) { TCP_SEND("1"); } else { TCP_SEND("0"); }
                                if ( WordData[0] & ( 1 << 6 ) ) { TCP_SEND("1"); } else { TCP_SEND("0"); }
                                if ( WordData[0] & ( 1 << 5 ) ) { TCP_SEND("1"); } else { TCP_SEND("0"); }
                                if ( WordData[0] & ( 1 << 4 ) ) { TCP_SEND("1-"); } else { TCP_SEND("0-"); }
                                if ( WordData[0] & ( 1 << 1 ) ) { TCP_SEND("1"); } else { TCP_SEND("0"); }
                                if ( WordData[0] & ( 1 << 0 ) ) { TCP_SEND("1-"); } else { TCP_SEND("0-"); }
                                if ( WordData[1] & ( 1 << 7 ) ) { TCP_SEND("1"); } else { TCP_SEND("0"); }
                                if ( WordData[1] & ( 1 << 6 ) ) { TCP_SEND("1"); } else { TCP_SEND("0"); }
                                if ( WordData[1] & ( 1 << 5 ) ) { TCP_SEND("1"); } else { TCP_SEND("0"); }
                                TCP_SEND(" Temp=", Temp10, Temp1, Feuchte);
                                if ( WordData[2] & ( 1 << 0 ) ) { TCP_SEND("-"); } else { TCP_SEND("+"); }
                                TCP_SEND("%d.%d Feuchte=%d%% SonderSendung=", Temp10, Temp1, Feuchte);
                                if ( WordData[1] & ( 1 << 4 ) ) { TCP_SEND("1"); } else { TCP_SEND("0"); }
                                TCP_SEND(" \n");

                                Analysis[9] = 0;

dnl                             if ( WordData[Analysis[9]] == WordData[Analysis[9] + 5] ) {
dnl                             for{ init; match; do } {anweisung;}
dnl                             for(zahl=0; zahl<20; zahl+=5)
dnl                                 Array[zahl] = 1;
dnl                             for (int i=0;i<5;i++) Arrayzahl[i*5]=1;

                                TCP_SEND("00--> %02x%02x%02x%02x%02x  Match:%d\n", WordData[0],   WordData[1],   WordData[2],   WordData[3],   WordData[4],  Analysis[0]);
                                TCP_SEND("01--> %02x%02x%02x%02x%02x  Match:%d\n", WordData[5],   WordData[6],   WordData[7],   WordData[8],   WordData[9],  Analysis[1]);
                                TCP_SEND("02--> %02x%02x%02x%02x%02x  Match:%d\n", WordData[10],  WordData[11],  WordData[12],  WordData[13],  WordData[14], Analysis[2]);
                                TCP_SEND("03--> %02x%02x%02x%02x%02x  Match:%d\n", WordData[15],  WordData[16],  WordData[17],  WordData[18],  WordData[19], Analysis[3]);
                                TCP_SEND("04--> %02x%02x%02x%02x%02x  Match:%d\n", WordData[20],  WordData[21],  WordData[22],  WordData[23],  WordData[24], Analysis[4]);
                                TCP_SEND("05--> %02x%02x%02x%02x%02x  Match:%d\n", WordData[25],  WordData[26],  WordData[27],  WordData[28],  WordData[29], Analysis[5]);
                                TCP_SEND("06--> %02x%02x%02x%02x%02x  Match:%d\n", WordData[30],  WordData[31],  WordData[32],  WordData[33],  WordData[34], Analysis[6]);
                                TCP_SEND("07--> %02x%02x%02x%02x%02x  Match:%d\n", WordData[35],  WordData[36],  WordData[37],  WordData[38],  WordData[39], Analysis[7]);
                                TCP_SEND(" \n");

                                dnl     Analysis[0 bis 7]               Treffer der Daten Packets Übereinstimmung.
                                dnl     Analysis[8] - bit(0) = 1        Daten Liegen an und können vearbeitete werden
                                dnl     Analysis[8] - bit(1) = 1        Datenanalyse erfolgreich, Mindestens 3 Übereinstimmungen
                                dnl     Analysis[8] - bit(2) = 1        Daten sind Verarbeitet und können gesendet werden


dnl                             PT_YIELD(pt);
                                WAIT(3);
        }
        TCP_HANDLER_END();
CONTROL_END

ISR(INT0_vect)
{
        timer_0 = TCNT0;
        WordPos=(( BitPos / 8 ) + ( WordCound * 5 ));
        if ( WordPos >= 40 ) { BitPos = 0 ; WordCound = 0 ; }
        if ( timer_0 >= Time6 && timer_0 <= ( Time6 + 2 ) ) {
        } else if ( timer_0 == Time0 || timer_0 == ( Time0 + 1 ) ) {
                dnl //  L       0       (L - Bit - Normal)
                WordData[WordPos] &= ~(1 << ( 7 - (BitPos % 8)));               dnl WordData[n] = 0;
                BitPos ++ ;
        } else if ( timer_0 == Time1 || timer_0 == ( Time1 + 1 ) ) {
                dnl //  H       1       (H - Bit - Normal)
                WordData[WordPos] |= 1 << ( 7 - (BitPos % 8));          dnl WordData[n] = 1;
                BitPos ++ ;
        } else if ( timer_0 == Time2 || timer_0 == ( Time2 + 1 ) ) {
                dnl //  End-l   2       (Ende Datenwort L)
                WordData[WordPos] &= ~(1 << ( 7 - (BitPos % 8)));               dnl WordData[n] = 0;
                BitPos ++ ;
        } else if ( timer_0 == Time3 || timer_0 == ( Time3 + 1 ) ) {
                dnl //  End-h   3       (Ende Datenwort H)
                WordData[WordPos] |= 1 << ( 7 - (BitPos % 8));          dnl WordData[n] = 1;
                BitPos ++ ;
        } else if ( timer_0 >= ( Time4 -2 ) && timer_0 <= ( Time4 + 3 ) ) {
                dnl //  Start   4       (Start Datenwort)
                if ( BitPos != 1 ) {
                        WordCound ++ ;
                        WordData[ WordPos ] = 0 ;
                        WordData[ WordPos + 1 ] = 0 ;
                        WordData[ WordPos + 2 ] = 0 ;
                        WordData[ WordPos + 3 ] = 0 ;
                        WordData[ WordPos + 4 ] = 0 ;
                }
                BitPos = 0;
        } else if ( timer_0 == Time5 || timer_0 == ( Time5 + 1 ) ) {
                dnl //  End-    5       (Daten Sendung Ende)
        } else {
                dnl //  ?       8       (Nicht Erkanntes Timing)
                BitPos ++ ;
        }
        TCNT0 = 0;
dnl
dnl     L       0       (L - Bit)                       30
dnl     H       1       (H - Bit)                       62
dnl     End-l   2       (Ende Datenwort L)              32
dnl     End-h   3       (Ende Datenwort H)              64
dnl     Start   4       (Start Datenwort)               138
dnl     End-    5       (Daten Sendung Ende)            5
dnl     _       6       (puls)                          6
dnl     Pause   7       (Timer Overflow)
dnl     ?       8       (Nicht Erkanntes Timing)
dnl
dnl     uint8_t BitPos = 0;             dnl     Bit Position innerhalb eines 36 bit worts (0 bis 36)
dnl     uint8_t WordCound = 0;          dnl     Daten Wort Count (0 bis 7)
dnl     uint8_t WordData[40];           dnl     1 Data Word = 5 Byte    /       Max 8 Data Words
dnl

dnl     WordData[0]     WordData[1]     WordData[2]     WordData[3]     WordData[4]
dnl
dnl     0    0          1    1          2    2          3    3          4    4
dnl     5    D          0    0          4    8          E    4          7    0
dnl     0101 1101       0000 0000       0100 1000       1110 0100       0111 0000
dnl     RRRR CCBB       ???S TTTT       TTTT TTTT       LLLL LLLL       cccc ----
dnl

}

ISR(TIMER0_OVF_vect)
{
                TIFR0  &= ~_BV(TOV0);           dnl     TIFR0(TOV0) = 0
                BitPos = 0;
                WordCound = 0;
}


dnl     gummi:~#
dnl     gummi:~# while true ; do socat stdio tcp4-listen:4444 ; echo "##########" ; sleep 1 ; done
dnl
dnl     gummi:/usr/src/ethersex/090730_wichtel_ac:de:48:d3:99:5f/ethersex/control6#
dnl             cd .. ; make clean ; make ; scp ethersex.bin 10.33.0.10:/srv/tftp/ACDE48D3995F ; cd control6/ ;
dnl             echo "bootloader" | socat stdio tcp4:10.0.0.40:2701
dnl
dnl     gummi:/usr/src/ethersex/090730_wichtel_ac:de:48:d3:99:5f/ethersex/control6#
dnl             cat funk_temp_und_feuchte_daten.src | sed 's/dnl[\ \t]*.*//g' | sed 's/^\t\+$//'  | grep -v '^$'
dnl
dnl     gummi:~#
dnl             while true ; do socat stdio tcp4-listen:4444 | while read Line ; do
dnl             if [ "$(echo $Line | grep -c '^--')" = "1" ] ; then echo ; echo ; else
dnl             echo -en "$Line" | sed 's/^_//' ; fi ; done | sed 's/\(8[bc]0[78]\)/\n\1/g' ;
dnl             echo "##########" ; sleep 1 ; done
dnl
dnl                     Timing          Timer Count     Timer C. Hex    Decode
dnl     L       space 1920 bis 1999us   30-31,2         0x1E    0x1F    0
dnl     H       space 3990 bis 4049us   62,3-63,2       0x3E    0x3F    1
dnl     End-l   space 2060 bis 2089us   32,1-32,6       0x20    0x21    2
dnl     End-h   space 4130 bis 4169us   64,5-65,1       0x40    0x41    3
dnl     Start   space 8890 bis 8939us   138,9-139,6     0x8A    0x8B    4
dnl     End-    pulse  360 bis 369us    5,6-5,7         0x05            5
dnl     _       pulse  440 bis 519us    6,8-8,1         0x06    0x08    6
dnl     Pause                                                           7
dnl
dnl
dnl
dnl