File: rfm69_test_listen.pl

package info (click to toggle)
libhipi-perl 0.93-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 20,048 kB
  • sloc: perl: 471,917; ansic: 22; makefile: 10
file content (350 lines) | stat: -rwxr-xr-x 12,879 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#!/usr/bin/perl
use strict;
use warnings;

use HiPi 0.89;
use HiPi qw( :energenie :openthings :rpi :hrf69 :mcp23017 );
use Time::HiRes qw( usleep );
use HiPi::RF::OpenThings::Message;
use HiPi::RF::OpenThings;
use HiPi::Interface::HopeRF69;
use HiPi::Interface::MCP23017;
use Getopt::Long;

use constant {
    RFM69_TEST_OUTPUT_POWER => 0x1,
    RFM69_TEST_SPI_DEVICE   => 0x2,
    RFM69_TEST_SPI_CSS      => 0x3,
    RFM69_TEST_ITERATION    => 0x4,
    RFM69_TEST_RSSI         => 0x5,
    RFM69_TEST_PRODUCT      => 0x81,
};

HiPi::RF::OpenThings->register_parameter(RFM69_TEST_OUTPUT_POWER, 'Test Programmed Power', 'dBm');
HiPi::RF::OpenThings->register_parameter(RFM69_TEST_SPI_DEVICE, 'Test SPI Device', '');
HiPi::RF::OpenThings->register_parameter(RFM69_TEST_SPI_CSS, 'Test SPI CSS', '');
HiPi::RF::OpenThings->register_parameter(RFM69_TEST_ITERATION,  'Test Iteration', '');
HiPi::RF::OpenThings->register_parameter(RFM69_TEST_RSSI,  'Test RSSI', 'dBm');
HiPi::RF::OpenThings->register_product(
    OPENTHINGS_MANUFACTURER_RASPBERRY, RFM69_TEST_PRODUCT, 'RFM69 Test Messager', 0
);

my $options = {
    high_power_module => 0,
    reset_gpio        => RPI_PIN_22,
    device            => '/dev/spidev0.1',
    sensor_key        => undef,
    autojoin          => 0,
    power             => 10,
};

GetOptions( $options,
    'high_power_module|h!',
    'reset_gpio|r:s',
    'device|d:s',
    'sensor_key|s:s',
    'autojoin|a!',
    'power|p:i',
);

my $mcp;

if ($options->{reset_gpio} eq 'code') {
    $mcp = HiPi::Interface::MCP23017->new(
        devicename => '/dev/i2c-1',
        address    => 0x20,
    );
    $mcp->pin_mode( MCP_PIN_A1, MCP23017_OUTPUT);
    $mcp->pin_value( MCP_PIN_A1, 0 );
    
    $options->{reset_gpio} = sub {
        my $value = shift;
        $mcp->pin_value( MCP_PIN_A1, $value );
    };
}

##############################################################
## settings
##############################################################

# do you have an RF69MHW / RF69MHCW high power module ?
my $high_power_module = $options->{high_power_module};

# You need to define which GPIO pin is connected to the module
# reset pin.
# For the Energenie ENER314_RT board and the Adafruit RFM69HCW
# Radio Bonnet, this is GPIO 25 / RPI_PIN_22
my $reset_gpio = $options->{reset_gpio};

# You need to define the SPI device you will be using and the
# cable select pin.
# The Energenie ENER314_RT board and the Adafruit RFM69HCW
# Radio Bonnet use SPI0, CE1
my $spidevice = $options->{device};

# automatically respond to join commands
# and toggle on / off any devices capable of Switch commands
# that join 
my $autojoin_enabled = $options->{autojoin};

# set output power ( for autojoin messages)

my $dbm = $options->{power};

# listen only for this sensor
my $limitsensor = $options->{sensor_key};

if ( $limitsensor && $limitsensor eq 'test' ) {
    $limitsensor = HiPi::RF::OpenThings->format_sensor_key(
        OPENTHINGS_MANUFACTURER_RASPBERRY, RFM69_TEST_PRODUCT, 1
    );
    
}

#############################################################

# This demo script will listen for OpenThings messages

our $VERSION ='0.89';

# keep a hash of switches we have 'registered'
# for use if autojoin is on
my $switch_states = {};

# registered devices we are waiting for confirmation - autojoin
my $pending_reg = {};

# autojoin toggle
my $switchtime = time() + 7;

my $rfmodule = HiPi::Interface::HopeRF69->new(
    high_power_module => $high_power_module,
    reset_gpio        => $reset_gpio,
    devicename        => $spidevice,
    transmit_dbm      => $dbm
);

print qq(\n\tPress Ctrl + C to stop\n);

while (1) {
    my $msg = $rfmodule->receive_hipi_message(
        'HiPi::RF::OpenThings::Message',
        {
            cryptseed => 1,
        }
    );
    my $messagefromswitch = 0;
    my $switchstate = 0;
    my $sensorkey = 'NOT KNOWN';
    if ( $msg ) {
        my $rssival = $rfmodule->read_register( RF69_REG_RSSIVALUE );
        ## Need to re-init the module to remove automatic adjustment controls
        ## and allow initial RSSI measurement. Only use for testing.
        $rfmodule->init_radio_module;
        
        my $manuid = $msg->mid || 0;
    
        if ( $manuid == OPENTHINGS_MANUFACTURER_ENERGENIE ) {
            $msg->cryptseed( ENERGENIE_DEFAULT_CRYPTSEED );
        } elsif( $manuid == OPENTHINGS_MANUFACTURER_RASPBERRY ) {
            $msg->cryptseed( OPENTHINGS_RASPBERRY_CRYPTSEED );
        } else {
            $msg->cryptseed( OPENTHINGS_DEFAULT_CRYPTSEED );
        }
        
        $msg->decode_buffer unless $msg->is_decoded;
        
        if( $msg->ok ) {
            $sensorkey = $msg->sensor_key;
            next if ( $limitsensor &&  $sensorkey ne $limitsensor);
            
            my $received = scalar(localtime($msg->epoch));

            print qq(\nMessage received $received\n);
            
            printf(qq(\tManufacturer ID: 0x%04x, Product ID: 0x%04x, Sensor ID: 0x%06x, Encrypt PIP: 0x%04x\n), $msg->manufacturer_id, $msg->product_id, $msg->sensor_id, $msg->encrypt_pip);
            printf(qq(\tSensor Key: %s\n), $msg->sensor_key);
            
            printf(qq(\tSignal: %.1f dBm\n), $rssival / - 2);
            
            my $productname = HiPi::RF::OpenThings->product_name($msg->manufacturer_id, $msg->product_id );
            my $manufacname = HiPi::RF::OpenThings->manufacturer_name($msg->manufacturer_id);
            print qq(\t$manufacname : $productname\n);
            
            my @joinrecords = ();
            
            for my $record ( @{ $msg->records } ) {
                if ($record->typeid == OPENTHINGS_ENUMERATION ) {
                    printf(qq(\tEnumeration for %s ( command bit %s) : \n), $record->name, $record->command || '0' );
                    for my $enum ( $record->enumerated_values ) {
                        printf(qq(\t    %s : %s %s ( %s ) ( bytes %s )\n), $enum->enumeration_id, $enum->value, $record->units, $enum->typename, $enum->length );
                    }
                    
                } else {
                    my $typename = $record->typename;
                    
                    printf(qq(\t%s = %s ( command bit %s ) ( bytes %s )\n), $record->name, $record->value . ' ' . $record->units . qq( ( $typename )) , $record->command || '0', $record->length || 0);
                    if ($record->selector) {
                        printf(qq(\t    Source selector : 0b%b\n), $record->selector);
                    }
                    
                    ## newly joined devices
                    if ( $autojoin_enabled && $record->id == OPENTHINGS_PARAM_JOIN && $record->command ) {
                        push @joinrecords, $record;
                    } elsif( $autojoin_enabled && $record->id == OPENTHINGS_PARAM_SWITCH_STATE ) {
                        $messagefromswitch = 1;
                        $switchstate = $record->value;
                    }
                }
            }
            
            # toggling just joined switches when
            # autojoin is enabled
            if(exists($pending_reg->{$sensorkey})) {
                delete($pending_reg->{$sensorkey});
                # is this a switch we just registered
                if( $messagefromswitch ) {
                    $switch_states->{$sensorkey} = {
                        mid => $msg->manufacturer_id,
                        pid => $msg->product_id,
                        sid => $msg->sensor_id,
                        cryptseed => $msg->cryptseed,
                        state => $switchstate,
                    }
                }
            }
            
            # handle this record autojoin     
            for my $jreq( @joinrecords ) {
                
                delete($switch_states->{$sensorkey}) if exists ($switch_states->{$sensorkey});
                
                my $outmsg = HiPi::RF::OpenThings::Message->new(
                    cryptseed => $msg->cryptseed,
                    mid => $msg->manufacturer_id,
                    pid => $msg->product_id,
                    sid => $msg->sensor_id,
                );
                
                $outmsg->add_record(
                    id      => OPENTHINGS_PARAM_JOIN,
                    command => 0,
                    typeid  => OPENTHINGS_UINT,
                    value   => undef,
                );
                
                $outmsg->encode_buffer;
                $rfmodule->send_hipi_message( $outmsg );
                $outmsg->decode_buffer;
                
                if ( $outmsg->ok ) {
                    print qq(\n\n\tAccept join request sent decoded OK\n);
                    for my $record ( @{ $outmsg->records } ) {
                        printf(qq(\t%s = %s ( command bit %s)\n), $record->name, $record->value . ' ' . $record->units, $record->command || '0');
                    }
                    
                    $pending_reg->{$sensorkey} = 1;
                                        
                } else {
                    print qq(\n\n\tMessage Decoded With Errors\n);
                    while ( my $error = $outmsg->shift_error ) {
                        print qq(\tERROR: $error\n);
                    }
                }   
            }
            
            if ( $msg->product_id == RFM69_TEST_PRODUCT  && $msg->has_command ) {
                my $outmsg = HiPi::RF::OpenThings::Message->new(
                    cryptseed => $msg->cryptseed,
                    mid => $msg->manufacturer_id,
                    pid => $msg->product_id,
                    sid => 2,
                );
                
                for my $echo_record ( @{ $msg->records } ) {
                    $echo_record->command(0);
                    $outmsg->add_record($echo_record);
                }
                             
                $outmsg->add_record(
                    id      => RFM69_TEST_RSSI,
                    command => 0,
                    typeid  => OPENTHINGS_SINT_BP8,
                    value   => $rssival / - 2,
                );
                
                $outmsg->encode_buffer;
                $rfmodule->send_hipi_message( $outmsg );
                $outmsg->decode_buffer;
                
                if ( $outmsg->ok ) {
                    print qq(\n\n\tTest Response Sent Decoded OK\n);
                    for my $record ( @{ $outmsg->records } ) {
                        printf(qq(\t%s = %s ( command bit %s)\n), $record->name, $record->value . ' ' . $record->units, $record->command || '0');
                    }
                                        
                } else {
                    print qq(\n\n\tTest Response Decoded With Errors\n);
                    while ( my $error = $outmsg->shift_error ) {
                        print qq(\tERROR: $error\n);
                    }
                }   
            }
            
        } else {
            print qq(\n);
            while ( my $error = $msg->shift_error ) {
                print qq(INFORMATION : $error\n);
            }
        }
        print qq(\n\tPress Ctrl + C to stop\n);
    }
    
    # process any switch states (autojoin enabled)
    if( $messagefromswitch && exists($switch_states->{$sensorkey}) ) {
        $switch_states->{$sensorkey}->{state} = $switchstate;
    }
    
    # send switch toggle messages every 7 seconds ( autojoin enabled )
    if( time > $switchtime) {
        for my $skey ( keys %$switch_states ) {
            my $switchmsg = HiPi::RF::OpenThings::Message->new(
                cryptseed => $switch_states->{$skey}->{cryptseed},
                mid => $switch_states->{$skey}->{mid},
                pid => $switch_states->{$skey}->{pid},
                sid => $switch_states->{$skey}->{sid},
            );
        
            my $newstate = ( $switch_states->{$skey}->{state} ) ? 0 : 1;
            
            $switchmsg->add_record(
                id      => OPENTHINGS_PARAM_SWITCH_STATE,
                command => 1,
                typeid  => OPENTHINGS_UINT,
                value   => $newstate,
            );
            
            
            $rfmodule->send_hipi_message( $switchmsg );
            
            print qq(\nSent switch message to sensor $skey to set state $newstate\n);
            
            my @powerdump = $rfmodule->dump_transmit_power_info;
            print qq(\t$_\n) for ( @powerdump );
            
            $switchmsg->decode_buffer;
                
            if ( $switchmsg->ok ) {
                for my $record ( @{ $switchmsg->records } ) {
                    printf(qq(\t%s = %s ( command bit %s)\n), $record->name, $record->value . $record->units, $record->command || '0');
                }
            }
        }
        
        $switchtime = time() + 7;
    }
    
    usleep( 10000 );
}

1;